forked from maxdotio/node-qdrant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.ts
48 lines (37 loc) · 1.08 KB
/
request.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
import fetch from "node-fetch";
type HTTPMethod = "PUT" | "GET" | "DELETE" | "PATCH" | "POST";
export async function body_request(url: string, body: any, method: HTTPMethod, api_key?: string) {
method = method || "POST";
let fetch_spec:any = {
method: method,
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
};
if (api_key) {
fetch_spec.headers['api-key'] = api_key;
}
if (body) fetch_spec.body = JSON.stringify(body);
let response = await fetch(url, fetch_spec);
try {
const output = await response.json();
return [null, output];
} catch (ex) {
const output = null;
return [ex, output];
}
}
export async function url_request(url, params) {
if (params) {
url += "?" + new URLSearchParams(params).toString();
}
let response = await fetch(url);
try {
const output = await response.json();
return [null, output];
} catch (ex) {
const output = null;
return [ex, output];
}
}