-
Notifications
You must be signed in to change notification settings - Fork 80
/
profile.ts
44 lines (35 loc) 路 1.22 KB
/
profile.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
import { env } from 'decentraland-commons'
import { User, RemoteUser } from 'modules/auth/types'
import { authorize } from './auth'
export const PROFILE_API_URL = env.get('REACT_APP_PROFILE_API_URL', '')
export class ProfileAPI {
constructor(public url: string) { }
async request(method: string, path: string, params: any | null = null, options: RequestInit) {
const res = await fetch(this.getUrl(path), {
method,
...options,
body: params
})
if (!res.ok) {
throw new Error(`Status: ${res.status} - ${res.statusText}`)
}
return res.json()
}
getUrl(path: string) {
return `${this.url}${path}`
}
fetchUser = async (userId: string, accessToken?: string): Promise<User> => {
const user: RemoteUser = await this.request('get', `/profile/${userId}`, null, authorize(accessToken))
const createdAt = new Date(Date.parse(user.createdAt))
const updatedAt = new Date(Date.parse(user.updatedAt))
return { ...user, id: userId, createdAt, updatedAt }
}
fetchProfileById = async (userId: string): Promise<User | null> => {
try {
return this.fetchUser(userId)
} catch (e) {
return null
}
}
}
export const profile = new ProfileAPI(PROFILE_API_URL)