kubernetesjs 0.5.0 → 0.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +32 -41
- package/{main/client.js → client.js} +10 -17
- package/{module → esm}/client.js +10 -17
- package/package.json +33 -42
- package/src/client.ts +0 -259
- package/src/index.ts +0 -16358
- /package/{types/client.d.ts → client.d.ts} +0 -0
- /package/{module → esm}/index.js +0 -0
- /package/{types/index.d.ts → index.d.ts} +0 -0
- /package/{main/index.js → index.js} +0 -0
package/LICENSE
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
The MIT License (MIT)
|
|
2
2
|
|
|
3
|
-
Copyright (c)
|
|
3
|
+
Copyright (c) 2024 Dan Lynch <pyramation@gmail.com>
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
6
|
of this software and associated documentation files (the "Software"), to deal
|
package/README.md
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
# KubernetesJS
|
|
2
2
|
|
|
3
3
|
<p align="center" width="100%">
|
|
4
|
-
<img src="https://github.com/
|
|
4
|
+
<img src="https://github.com/hyperweb-io/interweb-utils/assets/545047/89c743c4-be88-409f-9a77-4b02cd7fe9a4" width="80">
|
|
5
5
|
<br/>
|
|
6
6
|
TypeScript Client for Kubernetes
|
|
7
7
|
<br />
|
|
8
|
-
<a href="https://github.com/
|
|
8
|
+
<a href="https://github.com/hyperweb-io/kubernetesjs/blob/main/LICENSE">
|
|
9
9
|
<img height="20" src="https://img.shields.io/badge/license-MIT-blue.svg"/>
|
|
10
10
|
</a>
|
|
11
11
|
</p>
|
|
@@ -40,58 +40,49 @@ const client = new KubernetesClient({
|
|
|
40
40
|
restEndpoint: 'http://127.0.0.1:8001'
|
|
41
41
|
});
|
|
42
42
|
|
|
43
|
-
client.listCoreV1NamespacedPod({
|
|
44
|
-
path: {
|
|
45
|
-
namespace: 'default'
|
|
46
|
-
},
|
|
47
|
-
query: {
|
|
48
|
-
// Add any necessary query parameters here
|
|
49
|
-
}
|
|
50
|
-
}).then(result => {
|
|
51
|
-
if (result.items && result.items.length) {
|
|
52
|
-
result.items.forEach(item => {
|
|
53
|
-
console.log('NODE:', item.spec.nodeName);
|
|
54
|
-
|
|
55
|
-
const initContainers = item.status.initContainerStatuses?.map(ic => ({
|
|
56
|
-
image: ic.image,
|
|
57
|
-
name: ic.name,
|
|
58
|
-
ready: ic.ready,
|
|
59
|
-
state: ic.state
|
|
60
|
-
}));
|
|
61
|
-
|
|
62
|
-
const containers = item.status.containerStatuses?.map(c => ({
|
|
63
|
-
image: c.image,
|
|
64
|
-
name: c.name,
|
|
65
|
-
ready: c.ready,
|
|
66
|
-
state: c.state
|
|
67
|
-
}));
|
|
68
|
-
|
|
69
|
-
console.log({ containers });
|
|
70
|
-
console.log({ initContainers });
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
}).catch(reason => {
|
|
74
|
-
console.error('Failed to fetch pods:', reason);
|
|
43
|
+
const result = await client.listCoreV1NamespacedPod({
|
|
44
|
+
path: { namespace: 'default' }
|
|
75
45
|
});
|
|
76
|
-
```
|
|
77
46
|
|
|
47
|
+
if (result.items && result.items.length) {
|
|
48
|
+
result.items.forEach(item => {
|
|
49
|
+
console.log('NODE:', item.spec.nodeName);
|
|
50
|
+
|
|
51
|
+
const initContainers = item.status.initContainerStatuses?.map(ic => ({
|
|
52
|
+
image: ic.image,
|
|
53
|
+
name: ic.name,
|
|
54
|
+
ready: ic.ready,
|
|
55
|
+
state: ic.state
|
|
56
|
+
}));
|
|
57
|
+
|
|
58
|
+
const containers = item.status.containerStatuses?.map(c => ({
|
|
59
|
+
image: c.image,
|
|
60
|
+
name: c.name,
|
|
61
|
+
ready: c.ready,
|
|
62
|
+
state: c.state
|
|
63
|
+
}));
|
|
64
|
+
|
|
65
|
+
console.log({ containers });
|
|
66
|
+
console.log({ initContainers });
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
```
|
|
78
70
|
|
|
79
71
|
## Related
|
|
80
72
|
|
|
81
73
|
Checkout these related projects:
|
|
82
74
|
|
|
83
|
-
* [`schema-typescript`](https://github.com/
|
|
75
|
+
* [`schema-typescript`](https://github.com/hyperweb-io/schema-typescript/tree/main/packages/schema-typescript)
|
|
84
76
|
Provides robust tools for handling JSON schemas and converting them to TypeScript interfaces with ease and efficiency.
|
|
85
|
-
* [`@schema-typescript/cli`](https://github.com/
|
|
77
|
+
* [`@schema-typescript/cli`](https://github.com/hyperweb-io/schema-typescript/tree/main/packages/cli)
|
|
86
78
|
CLI is the command line utility for `schema-typescript`.
|
|
87
|
-
* [`schema-sdk`](https://github.com/
|
|
79
|
+
* [`schema-sdk`](https://github.com/hyperweb-io/schema-typescript/tree/main/packages/schema-sdk)
|
|
88
80
|
Provides robust tools for handling OpenAPI schemas and converting them to TypeScript clients with ease and efficiency.
|
|
89
|
-
* [`starship`](https://github.com/
|
|
81
|
+
* [`starship`](https://github.com/hyperweb-io/starship) Unified Testing and Development for the Interchain.
|
|
90
82
|
|
|
91
83
|
## Credits
|
|
92
84
|
|
|
93
|
-
🛠 Built by
|
|
94
|
-
|
|
85
|
+
🛠 Built by Hyperweb — if you like our tools, please checkout and contribute to [our github ⚛️](https://github.com/hyperweb-io)
|
|
95
86
|
|
|
96
87
|
## Disclaimer
|
|
97
88
|
|
|
@@ -16,7 +16,7 @@ class APIClient {
|
|
|
16
16
|
return url.toString();
|
|
17
17
|
}
|
|
18
18
|
async request(options) {
|
|
19
|
-
const { path, headers, method, params, timeout } = options;
|
|
19
|
+
const { path, headers, method, params, body, timeout } = options;
|
|
20
20
|
const url = this.buildFullPath(path, method === 'GET' ? params : undefined);
|
|
21
21
|
const controller = new AbortController();
|
|
22
22
|
const id = setTimeout(() => controller.abort(), timeout || this.defaultTimeout);
|
|
@@ -24,13 +24,14 @@ class APIClient {
|
|
|
24
24
|
method,
|
|
25
25
|
headers,
|
|
26
26
|
signal: controller.signal,
|
|
27
|
-
body: method !== 'GET' && method !== 'DELETE' ? JSON.stringify(
|
|
27
|
+
body: method !== 'GET' && method !== 'DELETE' ? JSON.stringify(body) : null,
|
|
28
28
|
};
|
|
29
29
|
try {
|
|
30
30
|
const response = await fetch(url, fetchOptions);
|
|
31
31
|
clearTimeout(id);
|
|
32
32
|
if (!response.ok) {
|
|
33
|
-
|
|
33
|
+
const errorBody = await response.text();
|
|
34
|
+
throw new Error(`HTTP error! status: ${response.status}\nResponse body: ${errorBody}`);
|
|
34
35
|
}
|
|
35
36
|
return response.json();
|
|
36
37
|
}
|
|
@@ -50,25 +51,17 @@ class APIClient {
|
|
|
50
51
|
});
|
|
51
52
|
}
|
|
52
53
|
post(endpoint, query, body, opts = {}) {
|
|
53
|
-
const headers =
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
}
|
|
58
|
-
: {
|
|
59
|
-
'Content-Type': 'application/json',
|
|
60
|
-
...opts.headers,
|
|
61
|
-
};
|
|
62
|
-
const bodyContent = opts.isFormData
|
|
63
|
-
? new url_1.URLSearchParams(body).toString()
|
|
64
|
-
: JSON.stringify(body);
|
|
54
|
+
const headers = {
|
|
55
|
+
'Content-Type': 'application/json',
|
|
56
|
+
...(opts.headers || {})
|
|
57
|
+
};
|
|
65
58
|
return this.request({
|
|
66
59
|
path: endpoint,
|
|
67
60
|
method: 'POST',
|
|
68
|
-
// @ts-ignore
|
|
69
61
|
headers,
|
|
70
62
|
timeout: opts.timeout || this.defaultTimeout,
|
|
71
|
-
params:
|
|
63
|
+
params: query,
|
|
64
|
+
body: body
|
|
72
65
|
});
|
|
73
66
|
}
|
|
74
67
|
patch(endpoint, query, body, opts = {}) {
|
package/{module → esm}/client.js
RENAMED
|
@@ -13,7 +13,7 @@ export class APIClient {
|
|
|
13
13
|
return url.toString();
|
|
14
14
|
}
|
|
15
15
|
async request(options) {
|
|
16
|
-
const { path, headers, method, params, timeout } = options;
|
|
16
|
+
const { path, headers, method, params, body, timeout } = options;
|
|
17
17
|
const url = this.buildFullPath(path, method === 'GET' ? params : undefined);
|
|
18
18
|
const controller = new AbortController();
|
|
19
19
|
const id = setTimeout(() => controller.abort(), timeout || this.defaultTimeout);
|
|
@@ -21,13 +21,14 @@ export class APIClient {
|
|
|
21
21
|
method,
|
|
22
22
|
headers,
|
|
23
23
|
signal: controller.signal,
|
|
24
|
-
body: method !== 'GET' && method !== 'DELETE' ? JSON.stringify(
|
|
24
|
+
body: method !== 'GET' && method !== 'DELETE' ? JSON.stringify(body) : null,
|
|
25
25
|
};
|
|
26
26
|
try {
|
|
27
27
|
const response = await fetch(url, fetchOptions);
|
|
28
28
|
clearTimeout(id);
|
|
29
29
|
if (!response.ok) {
|
|
30
|
-
|
|
30
|
+
const errorBody = await response.text();
|
|
31
|
+
throw new Error(`HTTP error! status: ${response.status}\nResponse body: ${errorBody}`);
|
|
31
32
|
}
|
|
32
33
|
return response.json();
|
|
33
34
|
}
|
|
@@ -47,25 +48,17 @@ export class APIClient {
|
|
|
47
48
|
});
|
|
48
49
|
}
|
|
49
50
|
post(endpoint, query, body, opts = {}) {
|
|
50
|
-
const headers =
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
}
|
|
55
|
-
: {
|
|
56
|
-
'Content-Type': 'application/json',
|
|
57
|
-
...opts.headers,
|
|
58
|
-
};
|
|
59
|
-
const bodyContent = opts.isFormData
|
|
60
|
-
? new URLSearchParams(body).toString()
|
|
61
|
-
: JSON.stringify(body);
|
|
51
|
+
const headers = {
|
|
52
|
+
'Content-Type': 'application/json',
|
|
53
|
+
...(opts.headers || {})
|
|
54
|
+
};
|
|
62
55
|
return this.request({
|
|
63
56
|
path: endpoint,
|
|
64
57
|
method: 'POST',
|
|
65
|
-
// @ts-ignore
|
|
66
58
|
headers,
|
|
67
59
|
timeout: opts.timeout || this.defaultTimeout,
|
|
68
|
-
params:
|
|
60
|
+
params: query,
|
|
61
|
+
body: body
|
|
69
62
|
});
|
|
70
63
|
}
|
|
71
64
|
patch(endpoint, query, body, opts = {}) {
|
package/package.json
CHANGED
|
@@ -1,55 +1,46 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "kubernetesjs",
|
|
3
|
-
"version": "0.5.
|
|
4
|
-
"description": "Fully Typed Kubernetes",
|
|
3
|
+
"version": "0.5.1",
|
|
5
4
|
"author": "Dan Lynch <pyramation@gmail.com>",
|
|
6
|
-
"
|
|
5
|
+
"description": "Fully Typed Kubernetes",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"module": "esm/index.js",
|
|
8
|
+
"types": "index.d.ts",
|
|
9
|
+
"homepage": "https://github.com/hyperweb-io/kubernetesjs",
|
|
7
10
|
"license": "SEE LICENSE IN LICENSE",
|
|
8
|
-
"main": "main/index.js",
|
|
9
|
-
"module": "module/index.js",
|
|
10
|
-
"typings": "types/index.d.ts",
|
|
11
|
-
"directories": {
|
|
12
|
-
"lib": "src",
|
|
13
|
-
"test": "__tests__"
|
|
14
|
-
},
|
|
15
|
-
"files": [
|
|
16
|
-
"types",
|
|
17
|
-
"module",
|
|
18
|
-
"src",
|
|
19
|
-
"main"
|
|
20
|
-
],
|
|
21
|
-
"scripts": {
|
|
22
|
-
"build:main": "yarn tsc -p tsconfig.json --outDir main --module commonjs",
|
|
23
|
-
"build:module": "yarn tsc -p tsconfig.json --outDir module --module es2022",
|
|
24
|
-
"build": "npm run build:module && npm run build:main",
|
|
25
|
-
"clean": "rimraf ./types && rimraf ./main && rimraf ./module",
|
|
26
|
-
"prepare": "npm run clean && npm run build",
|
|
27
|
-
"dev": "ts-node ./test/test.ts",
|
|
28
|
-
"codegen": "ts-node ./scripts/codegen.ts",
|
|
29
|
-
"lint": "eslint .",
|
|
30
|
-
"format": "eslint . --fix"
|
|
31
|
-
},
|
|
32
11
|
"publishConfig": {
|
|
33
|
-
"access": "public"
|
|
12
|
+
"access": "public",
|
|
13
|
+
"directory": "dist"
|
|
34
14
|
},
|
|
35
15
|
"repository": {
|
|
36
16
|
"type": "git",
|
|
37
|
-
"url": "https://github.com/
|
|
17
|
+
"url": "https://github.com/hyperweb-io/kubernetesjs"
|
|
38
18
|
},
|
|
39
|
-
"keywords": [],
|
|
40
19
|
"bugs": {
|
|
41
|
-
"url": "https://github.com/
|
|
20
|
+
"url": "https://github.com/hyperweb-io/kubernetesjs/issues"
|
|
42
21
|
},
|
|
22
|
+
"scripts": {
|
|
23
|
+
"copy": "copyfiles -f ../../LICENSE README.md package.json dist",
|
|
24
|
+
"clean": "rimraf dist/**",
|
|
25
|
+
"prepare": "npm run build",
|
|
26
|
+
"build": "npm run clean; tsc; tsc -p tsconfig.esm.json; npm run copy",
|
|
27
|
+
"build:dev": "npm run clean; tsc --declarationMap; tsc -p tsconfig.esm.json; npm run copy",
|
|
28
|
+
"lint": "eslint . --fix",
|
|
29
|
+
"dev": "ts-node ./test/test.ts",
|
|
30
|
+
"deploy": "ts-node ./test/deployment.ts",
|
|
31
|
+
"codegen": "ts-node ./scripts/codegen.ts",
|
|
32
|
+
"test": "jest",
|
|
33
|
+
"test:watch": "jest --watch",
|
|
34
|
+
"test:teardown": "ts-node scripts/teardown.ts",
|
|
35
|
+
"test:list": "ts-node scripts/list.ts",
|
|
36
|
+
"test:deploy": "ts-node scripts/deploy.ts"
|
|
37
|
+
},
|
|
38
|
+
"keywords": [
|
|
39
|
+
"kubernetes",
|
|
40
|
+
"k8s"
|
|
41
|
+
],
|
|
43
42
|
"devDependencies": {
|
|
44
|
-
"
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
"eslint-plugin-prettier": "^4.0.0",
|
|
48
|
-
"esprima": "4.0.1",
|
|
49
|
-
"prettier": "^2.8.7",
|
|
50
|
-
"rimraf": "5.0.0",
|
|
51
|
-
"schema-sdk": "^0.7.0",
|
|
52
|
-
"ts-node": "10.9.2",
|
|
53
|
-
"typescript": "^5.0.4"
|
|
54
|
-
}
|
|
43
|
+
"schema-sdk": "^0.7.0"
|
|
44
|
+
},
|
|
45
|
+
"gitHead": "559b940ad9838ef22e59b983def6e3db4659a359"
|
|
55
46
|
}
|
package/src/client.ts
DELETED
|
@@ -1,259 +0,0 @@
|
|
|
1
|
-
import { URLSearchParams } from 'url';
|
|
2
|
-
|
|
3
|
-
interface RequestOptions<Params> {
|
|
4
|
-
hostname: string;
|
|
5
|
-
path: string;
|
|
6
|
-
headers?: { [key: string]: string };
|
|
7
|
-
method?: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
|
|
8
|
-
params?: Params;
|
|
9
|
-
timeout?: number;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export interface APIClientOptions {
|
|
13
|
-
restEndpoint: string;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export interface APIClientRequestHeaders {
|
|
17
|
-
accept?: string | string[] | undefined;
|
|
18
|
-
'accept-charset'?: string | string[] | undefined;
|
|
19
|
-
'accept-encoding'?: string | string[] | undefined;
|
|
20
|
-
'accept-language'?: string | string[] | undefined;
|
|
21
|
-
'accept-ranges'?: string | undefined;
|
|
22
|
-
'access-control-allow-credentials'?: string | undefined;
|
|
23
|
-
'access-control-allow-headers'?: string | undefined;
|
|
24
|
-
'access-control-allow-methods'?: string | undefined;
|
|
25
|
-
'access-control-allow-origin'?: string | undefined;
|
|
26
|
-
'access-control-expose-headers'?: string | undefined;
|
|
27
|
-
'access-control-max-age'?: string | undefined;
|
|
28
|
-
'access-control-request-headers'?: string | undefined;
|
|
29
|
-
'access-control-request-method'?: string | undefined;
|
|
30
|
-
age?: string | undefined;
|
|
31
|
-
allow?: string | undefined;
|
|
32
|
-
authorization?: string | undefined;
|
|
33
|
-
'cache-control'?: string | undefined;
|
|
34
|
-
'cdn-cache-control'?: string | undefined;
|
|
35
|
-
connection?: string | string[] | undefined;
|
|
36
|
-
'content-disposition'?: string | undefined;
|
|
37
|
-
'content-encoding'?: string | undefined;
|
|
38
|
-
'content-language'?: string | undefined;
|
|
39
|
-
'content-length'?: string | number | undefined;
|
|
40
|
-
'content-location'?: string | undefined;
|
|
41
|
-
'content-range'?: string | undefined;
|
|
42
|
-
'content-security-policy'?: string | undefined;
|
|
43
|
-
'content-security-policy-report-only'?: string | undefined;
|
|
44
|
-
cookie?: string | string[] | undefined;
|
|
45
|
-
dav?: string | string[] | undefined;
|
|
46
|
-
dnt?: string | undefined;
|
|
47
|
-
date?: string | undefined;
|
|
48
|
-
etag?: string | undefined;
|
|
49
|
-
expect?: string | undefined;
|
|
50
|
-
expires?: string | undefined;
|
|
51
|
-
forwarded?: string | undefined;
|
|
52
|
-
from?: string | undefined;
|
|
53
|
-
host?: string | undefined;
|
|
54
|
-
'if-match'?: string | undefined;
|
|
55
|
-
'if-modified-since'?: string | undefined;
|
|
56
|
-
'if-none-match'?: string | undefined;
|
|
57
|
-
'if-range'?: string | undefined;
|
|
58
|
-
'if-unmodified-since'?: string | undefined;
|
|
59
|
-
'last-modified'?: string | undefined;
|
|
60
|
-
link?: string | string[] | undefined;
|
|
61
|
-
location?: string | undefined;
|
|
62
|
-
'max-forwards'?: string | undefined;
|
|
63
|
-
origin?: string | undefined;
|
|
64
|
-
prgama?: string | string[] | undefined;
|
|
65
|
-
'proxy-authenticate'?: string | string[] | undefined;
|
|
66
|
-
'proxy-authorization'?: string | undefined;
|
|
67
|
-
'public-key-pins'?: string | undefined;
|
|
68
|
-
'public-key-pins-report-only'?: string | undefined;
|
|
69
|
-
range?: string | undefined;
|
|
70
|
-
referer?: string | undefined;
|
|
71
|
-
'referrer-policy'?: string | undefined;
|
|
72
|
-
refresh?: string | undefined;
|
|
73
|
-
'retry-after'?: string | undefined;
|
|
74
|
-
'sec-websocket-accept'?: string | undefined;
|
|
75
|
-
'sec-websocket-extensions'?: string | string[] | undefined;
|
|
76
|
-
'sec-websocket-key'?: string | undefined;
|
|
77
|
-
'sec-websocket-protocol'?: string | string[] | undefined;
|
|
78
|
-
'sec-websocket-version'?: string | undefined;
|
|
79
|
-
server?: string | undefined;
|
|
80
|
-
'set-cookie'?: string | string[] | undefined;
|
|
81
|
-
'strict-transport-security'?: string | undefined;
|
|
82
|
-
te?: string | undefined;
|
|
83
|
-
trailer?: string | undefined;
|
|
84
|
-
'transfer-encoding'?: string | undefined;
|
|
85
|
-
'user-agent'?: string | undefined;
|
|
86
|
-
upgrade?: string | undefined;
|
|
87
|
-
'upgrade-insecure-requests'?: string | undefined;
|
|
88
|
-
vary?: string | undefined;
|
|
89
|
-
via?: string | string[] | undefined;
|
|
90
|
-
warning?: string | undefined;
|
|
91
|
-
'www-authenticate'?: string | string[] | undefined;
|
|
92
|
-
'x-content-type-options'?: string | undefined;
|
|
93
|
-
'x-dns-prefetch-control'?: string | undefined;
|
|
94
|
-
'x-frame-options'?: string | undefined;
|
|
95
|
-
'x-xss-protection'?: string | undefined;
|
|
96
|
-
}
|
|
97
|
-
export type APIClientRequestOpts = {
|
|
98
|
-
headers?: APIClientRequestHeaders;
|
|
99
|
-
timeout?: number;
|
|
100
|
-
isFormData?: boolean;
|
|
101
|
-
};
|
|
102
|
-
|
|
103
|
-
export class APIClient {
|
|
104
|
-
private baseUrl: string;
|
|
105
|
-
private defaultTimeout: number = 10000; // 10 seconds
|
|
106
|
-
|
|
107
|
-
constructor(options: APIClientOptions) {
|
|
108
|
-
this.baseUrl = options.restEndpoint;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
private buildFullPath(endpoint: string, query?: { [key: string]: any }): string {
|
|
112
|
-
const url = new URL(endpoint, this.baseUrl);
|
|
113
|
-
if (query) {
|
|
114
|
-
Object.keys(query).forEach(key => url.searchParams.append(key, query[key]));
|
|
115
|
-
}
|
|
116
|
-
return url.toString();
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
private async request<Resp>(options: RequestOptions<any>): Promise<Resp> {
|
|
120
|
-
const { path, headers, method, params, timeout } = options;
|
|
121
|
-
const url = this.buildFullPath(path, method === 'GET' ? params : undefined);
|
|
122
|
-
|
|
123
|
-
const controller = new AbortController();
|
|
124
|
-
const id = setTimeout(() => controller.abort(), timeout || this.defaultTimeout);
|
|
125
|
-
|
|
126
|
-
const fetchOptions: RequestInit = {
|
|
127
|
-
method,
|
|
128
|
-
headers,
|
|
129
|
-
signal: controller.signal,
|
|
130
|
-
body: method !== 'GET' && method !== 'DELETE' ? JSON.stringify(params) : null,
|
|
131
|
-
};
|
|
132
|
-
|
|
133
|
-
try {
|
|
134
|
-
const response = await fetch(url, fetchOptions);
|
|
135
|
-
clearTimeout(id);
|
|
136
|
-
|
|
137
|
-
if (!response.ok) {
|
|
138
|
-
throw new Error(`HTTP error! status: ${response.status}`);
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
return response.json() as Promise<Resp>;
|
|
142
|
-
} catch (error) {
|
|
143
|
-
clearTimeout(id);
|
|
144
|
-
throw error;
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
get<Resp = unknown>(
|
|
149
|
-
endpoint: string,
|
|
150
|
-
query?: { [key: string]: any },
|
|
151
|
-
body?: void,
|
|
152
|
-
opts: APIClientRequestOpts = {}
|
|
153
|
-
): Promise<Resp> {
|
|
154
|
-
return this.request<Resp>({
|
|
155
|
-
path: endpoint,
|
|
156
|
-
method: 'GET',
|
|
157
|
-
// @ts-ignore
|
|
158
|
-
headers: opts.headers,
|
|
159
|
-
timeout: opts.timeout || this.defaultTimeout,
|
|
160
|
-
params: query,
|
|
161
|
-
});
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
post<Resp = unknown, Params = any>(
|
|
165
|
-
endpoint: string,
|
|
166
|
-
query?: { [key: string]: any },
|
|
167
|
-
body?: Params,
|
|
168
|
-
opts: APIClientRequestOpts = {}
|
|
169
|
-
): Promise<Resp> {
|
|
170
|
-
const headers = opts.isFormData
|
|
171
|
-
? {
|
|
172
|
-
'Content-Type': 'application/x-www-form-urlencoded',
|
|
173
|
-
...opts.headers,
|
|
174
|
-
}
|
|
175
|
-
: {
|
|
176
|
-
'Content-Type': 'application/json',
|
|
177
|
-
...opts.headers,
|
|
178
|
-
};
|
|
179
|
-
const bodyContent = opts.isFormData
|
|
180
|
-
? new URLSearchParams(body as any).toString()
|
|
181
|
-
: JSON.stringify(body);
|
|
182
|
-
|
|
183
|
-
return this.request<Resp>({
|
|
184
|
-
path: endpoint,
|
|
185
|
-
method: 'POST',
|
|
186
|
-
// @ts-ignore
|
|
187
|
-
headers,
|
|
188
|
-
timeout: opts.timeout || this.defaultTimeout,
|
|
189
|
-
params: bodyContent,
|
|
190
|
-
});
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
patch<Resp = unknown, Params = any>(
|
|
194
|
-
endpoint: string,
|
|
195
|
-
query?: { [key: string]: any },
|
|
196
|
-
body?: Params,
|
|
197
|
-
opts: APIClientRequestOpts = {}
|
|
198
|
-
): Promise<Resp> {
|
|
199
|
-
const headers = opts.isFormData
|
|
200
|
-
? {
|
|
201
|
-
'Content-Type': 'application/x-www-form-urlencoded',
|
|
202
|
-
...opts.headers,
|
|
203
|
-
}
|
|
204
|
-
: {
|
|
205
|
-
'Content-Type': 'application/json',
|
|
206
|
-
...opts.headers,
|
|
207
|
-
};
|
|
208
|
-
const bodyContent = opts.isFormData
|
|
209
|
-
? new URLSearchParams(body as any).toString()
|
|
210
|
-
: JSON.stringify(body);
|
|
211
|
-
|
|
212
|
-
return this.request<Resp>({
|
|
213
|
-
path: endpoint,
|
|
214
|
-
method: 'PATCH',
|
|
215
|
-
// @ts-ignore
|
|
216
|
-
headers,
|
|
217
|
-
timeout: opts.timeout || this.defaultTimeout,
|
|
218
|
-
params: bodyContent,
|
|
219
|
-
});
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
put<Resp = unknown, Params = any>(
|
|
223
|
-
endpoint: string,
|
|
224
|
-
query?: { [key: string]: any },
|
|
225
|
-
body?: Params,
|
|
226
|
-
opts: APIClientRequestOpts = {}
|
|
227
|
-
): Promise<Resp> {
|
|
228
|
-
const headers = {
|
|
229
|
-
'Content-Type': 'application/json',
|
|
230
|
-
...opts.headers,
|
|
231
|
-
};
|
|
232
|
-
const bodyContent = JSON.stringify(body);
|
|
233
|
-
|
|
234
|
-
return this.request<Resp>({
|
|
235
|
-
path: endpoint,
|
|
236
|
-
method: 'PUT',
|
|
237
|
-
// @ts-ignore
|
|
238
|
-
headers,
|
|
239
|
-
timeout: opts.timeout || this.defaultTimeout,
|
|
240
|
-
params: bodyContent,
|
|
241
|
-
});
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
delete<Resp = unknown>(
|
|
245
|
-
endpoint: string,
|
|
246
|
-
query?: { [key: string]: any },
|
|
247
|
-
body?: void,
|
|
248
|
-
opts: APIClientRequestOpts = {}
|
|
249
|
-
): Promise<Resp> {
|
|
250
|
-
return this.request<Resp>({
|
|
251
|
-
path: endpoint,
|
|
252
|
-
method: 'DELETE',
|
|
253
|
-
// @ts-ignore
|
|
254
|
-
headers: opts.headers,
|
|
255
|
-
timeout: opts.timeout || this.defaultTimeout,
|
|
256
|
-
params: query,
|
|
257
|
-
});
|
|
258
|
-
}
|
|
259
|
-
}
|