-
-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathfetcher.ts
More file actions
53 lines (45 loc) · 1.08 KB
/
fetcher.ts
File metadata and controls
53 lines (45 loc) · 1.08 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
50
51
52
53
import { requestUrl } from "obsidian";
export interface WebFetcher {
fetch(params: RequestParams): Promise<WebResponse>;
}
export type StatusCode = number;
export const StatusCode = {
isError(code: StatusCode): boolean {
return code >= StatusCodes.BadRequest;
},
isServerError(code: StatusCode): boolean {
return code >= StatusCodes.InternalServerError;
},
} as const;
export const StatusCodes = {
OK: 200,
BadRequest: 400,
Unauthorized: 401,
Forbidden: 403,
InternalServerError: 500,
} as const;
export type RequestParams = {
url: string;
method: string;
headers: Record<string, string>;
body?: string;
};
export type WebResponse = {
statusCode: StatusCode;
body: string;
};
export class ObsidianFetcher implements WebFetcher {
public async fetch(params: RequestParams): Promise<WebResponse> {
const response = await requestUrl({
url: params.url,
method: params.method,
body: params.body,
headers: params.headers,
throw: false,
});
return {
statusCode: response.status,
body: response.text,
};
}
}