-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathsignup.ts
51 lines (45 loc) · 928 Bytes
/
signup.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
import {
type Endpoint,
fetchJSON,
type FetchOptions,
hasStringKey
} from './utils.ts'
export interface SignUpRequest {
id: string
password: string
}
export interface SignUpResponse {
id: string
session: string
}
const METHOD = 'PUT'
export async function signUp(
params: SignUpRequest,
opts?: FetchOptions
): Promise<SignUpResponse> {
return fetchJSON(METHOD, `/users/${params.id}`, params, opts)
}
const URL_PATTERN = /^\/users\/([^/]+)$/
export const signUpEndpoint: Endpoint<
SignUpResponse,
SignUpRequest,
{ id: string }
> = {
checkBody(body, urlParams) {
if (hasStringKey(body, 'id') && hasStringKey(body, 'password')) {
if (body.id === urlParams.id) {
return body
}
}
return false
},
method: METHOD,
parseUrl(url) {
let match = url.match(URL_PATTERN)
if (match) {
return { id: match[1]! }
} else {
return false
}
}
}