forked from utily/cloudly-http
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.ts
More file actions
49 lines (47 loc) · 2.09 KB
/
Client.ts
File metadata and controls
49 lines (47 loc) · 2.09 KB
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
import { fetch } from "./fetch"
import { Method } from "./Method"
import { Request } from "./Request"
import { Response } from "./Response"
export class Client<Error = void> {
onError?: (request: Request, response: Response) => Promise<boolean>
onUnauthorized?: (connection: Client<Error>) => Promise<boolean>
constructor(public url?: string, public key?: string) {}
private async fetch<R>(path: string, method: Method, body?: any, header?: Request.Header): Promise<R | Error> {
const request = await this.preProcess(Request.create({ url: `${this.url ?? ""}/${path}`, method, header, body }))
const response = await this.postProcess(
await fetch(request).catch(error => Response.create({ status: 601, body: error }))
)
return (response.status == 401 && this.onUnauthorized && (await this.onUnauthorized(this))) ||
(response.status >= 300 && this.onError && (await this.onError(request, response)))
? await this.fetch<R>(path, method, body, header)
: ((await response.body) as R | Error)
}
protected async preProcess(request: Request): Promise<Request> {
return {
...request,
header: {
contentType: request.body ? "application/json; charset=utf-8" : undefined,
authorization: this.key ? `Bearer ${this.key}` : undefined,
...request.header,
},
}
}
protected async postProcess(response: Response): Promise<Response> {
return response
}
async get<R>(path: string, header?: Request.Header): Promise<R | Error> {
return await this.fetch<R>(path, "GET", undefined, header)
}
async post<R>(path: string, request: any, header?: Request.Header): Promise<R | Error> {
return await this.fetch<R>(path, "POST", request, header)
}
async delete<R>(path: string, header?: Request.Header): Promise<R | Error> {
return await this.fetch<R>(path, "DELETE", undefined, header)
}
async patch<R>(path: string, request: any, header?: Request.Header): Promise<R | Error> {
return await this.fetch<R>(path, "PATCH", request, header)
}
async put<R>(path: string, request: any, header?: Request.Header): Promise<R | Error> {
return await this.fetch<R>(path, "PUT", request, header)
}
}