-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.ts
68 lines (59 loc) · 1.54 KB
/
user.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import ValidationError from "src/layers/domain/errors/validation-error";
import { z } from "zod";
type Presenter = {
name: string;
avatar_url: string;
bio: string;
}
type Model = z.infer<typeof User.schema>
export class User {
static presenter: Presenter;
static model: Model;
static schema = z.object({
login: z.string(),
id: z.number(),
node_id: z.string(),
avatar_url: z.string(),
gravatar_id: z.string(),
url: z.string(),
html_url: z.string(),
followers_url: z.string(),
following_url: z.string(),
gists_url: z.string(),
starred_url: z.string(),
subscriptions_url: z.string(),
organizations_url: z.string(),
repos_url: z.string(),
events_url: z.string(),
received_events_url: z.string(),
type: z.string(),
user_view_type: z.string(),
site_admin: z.boolean(),
name: z.string(),
company: z.null(),
blog: z.string(),
location: z.string(),
email: z.null(),
hireable: z.null(),
bio: z.string(),
twitter_username: z.null(),
public_repos: z.number(),
public_gists: z.number(),
followers: z.number(),
following: z.number(),
created_at: z.string(),
updated_at: z.string(),
});
static toPresenter(v: Model): Presenter {
return {
name: v.name,
avatar_url: v.avatar_url,
bio: v.bio,
}
}
static toModel(value: any): Model {
const v = this.schema.safeParse(value);
if (v.success) return value;
throw new ValidationError({ message: 'Error parsing to model', errors: [v.error?.name] })
}
}