-
Notifications
You must be signed in to change notification settings - Fork 20
/
users.ts
42 lines (39 loc) · 986 Bytes
/
users.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
export class User {
constructor(
public gender: "male" | "female",
public name: {
title: "Mr" | "Mrs";
first: string;
last: string;
},
public location: {
street: string;
city: string;
state: string;
country: string;
postcode: number;
},
public login: {
username: string;
password: string;
},
public email: string,
public picture: {
large: string;
medium: string;
thumbnail: string;
}
) {}
get fullName() {
return `${this.name.first} ${this.name.last}`;
}
}
export const loadUsers = async (n: number) => {
const response = await fetch(`https://randomuser.me/api?results=${n}`);
const { results } = (await response.json()) as { results: any[] };
const users: Array<User> = [];
for (const { gender, name, location, login, email, picture } of results) {
users.push(new User(gender, name, location, login, email, picture));
}
return users;
};