@gitbeaker/requester-utils 35.8.0 → 35.8.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/dist/index.d.ts +74 -0
- package/dist/index.mjs +111 -0
- package/package.json +2 -3
- package/LICENSE.md +0 -22
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
type ResponseBodyTypes = Record<string, unknown> | Record<string, unknown>[] | ReadableStream | Blob | string | string[] | number | void;
|
|
2
|
+
interface FormattedResponse<T extends ResponseBodyTypes = ResponseBodyTypes> {
|
|
3
|
+
body: T;
|
|
4
|
+
headers: Record<string, string>;
|
|
5
|
+
status: number;
|
|
6
|
+
}
|
|
7
|
+
interface RequesterType {
|
|
8
|
+
get<T extends ResponseBodyTypes>(endpoint: string, options?: Record<string, unknown>): Promise<FormattedResponse<T>>;
|
|
9
|
+
post<T extends ResponseBodyTypes>(endpoint: string, options?: Record<string, unknown>): Promise<FormattedResponse<T>>;
|
|
10
|
+
put<T extends ResponseBodyTypes>(endpoint: string, options?: Record<string, unknown>): Promise<FormattedResponse<T>>;
|
|
11
|
+
patch<T extends ResponseBodyTypes>(endpoint: string, options?: Record<string, unknown>): Promise<FormattedResponse<T>>;
|
|
12
|
+
delete<T extends ResponseBodyTypes>(endpoint: string, options?: Record<string, unknown>): Promise<FormattedResponse<T>>;
|
|
13
|
+
}
|
|
14
|
+
interface Constructable<T = any> {
|
|
15
|
+
new (...args: any[]): T;
|
|
16
|
+
}
|
|
17
|
+
type DefaultResourceOptions = {
|
|
18
|
+
headers: {
|
|
19
|
+
[header: string]: string;
|
|
20
|
+
};
|
|
21
|
+
requestTimeout: number;
|
|
22
|
+
url: string;
|
|
23
|
+
rejectUnauthorized: boolean;
|
|
24
|
+
};
|
|
25
|
+
type DefaultRequestOptions = {
|
|
26
|
+
body?: FormData | Record<string, unknown>;
|
|
27
|
+
searchParams?: Record<string, unknown>;
|
|
28
|
+
sudo?: string;
|
|
29
|
+
method?: string;
|
|
30
|
+
asStream?: boolean;
|
|
31
|
+
};
|
|
32
|
+
type RequestOptions = {
|
|
33
|
+
headers: Record<string, string>;
|
|
34
|
+
timeout?: number;
|
|
35
|
+
method: string;
|
|
36
|
+
searchParams?: string;
|
|
37
|
+
prefixUrl: string;
|
|
38
|
+
body?: string | FormData;
|
|
39
|
+
asStream?: boolean;
|
|
40
|
+
};
|
|
41
|
+
declare function formatQuery(params?: Record<string, unknown>): string;
|
|
42
|
+
type OptionsHandlerFn = (serviceOptions: DefaultResourceOptions, requestOptions: DefaultRequestOptions) => Promise<RequestOptions>;
|
|
43
|
+
declare function defaultOptionsHandler(resourceOptions: DefaultResourceOptions, { body, searchParams, sudo, asStream, method }?: DefaultRequestOptions): Promise<RequestOptions>;
|
|
44
|
+
type RequestHandlerFn<T extends ResponseBodyTypes = ResponseBodyTypes> = (endpoint: string, options?: Record<string, unknown>) => Promise<FormattedResponse<T>>;
|
|
45
|
+
declare function createRequesterFn(optionsHandler: OptionsHandlerFn, requestHandler: RequestHandlerFn): (serviceOptions: DefaultResourceOptions) => RequesterType;
|
|
46
|
+
declare function presetResourceArguments<T extends Record<string, Constructable>>(resources: T, customConfig?: Record<string, unknown>): T;
|
|
47
|
+
|
|
48
|
+
interface BaseResourceOptions<C> {
|
|
49
|
+
oauthToken?: string;
|
|
50
|
+
token?: string;
|
|
51
|
+
jobToken?: string;
|
|
52
|
+
host?: string;
|
|
53
|
+
prefixUrl?: string;
|
|
54
|
+
rejectUnauthorized?: boolean;
|
|
55
|
+
camelize?: C;
|
|
56
|
+
requesterFn?: (resourceOptions: DefaultResourceOptions) => RequesterType;
|
|
57
|
+
requestTimeout?: number;
|
|
58
|
+
profileToken?: string;
|
|
59
|
+
sudo?: string | number;
|
|
60
|
+
profileMode?: 'execution' | 'memory';
|
|
61
|
+
}
|
|
62
|
+
declare class BaseResource<C extends boolean = false> {
|
|
63
|
+
readonly url: string;
|
|
64
|
+
readonly requester: RequesterType;
|
|
65
|
+
readonly requestTimeout: number;
|
|
66
|
+
readonly headers: {
|
|
67
|
+
[header: string]: string;
|
|
68
|
+
};
|
|
69
|
+
readonly camelize: C | undefined;
|
|
70
|
+
readonly rejectUnauthorized: boolean;
|
|
71
|
+
constructor({ token, jobToken, oauthToken, sudo, profileToken, requesterFn, camelize, profileMode, host, prefixUrl, rejectUnauthorized, requestTimeout, }?: BaseResourceOptions<C>);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export { BaseResource, BaseResourceOptions, Constructable, DefaultRequestOptions, DefaultResourceOptions, FormattedResponse, OptionsHandlerFn, RequestHandlerFn, RequestOptions, RequesterType, ResponseBodyTypes, createRequesterFn, defaultOptionsHandler, formatQuery, presetResourceArguments };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { decamelizeKeys } from 'xcase';
|
|
2
|
+
import { stringify } from 'qs';
|
|
3
|
+
|
|
4
|
+
// src/RequesterUtils.ts
|
|
5
|
+
function formatQuery(params = {}) {
|
|
6
|
+
const decamelized = decamelizeKeys(params);
|
|
7
|
+
return stringify(decamelized, { arrayFormat: "brackets" });
|
|
8
|
+
}
|
|
9
|
+
function isFormData(object) {
|
|
10
|
+
return typeof object === "object" && object.constructor.name === "FormData";
|
|
11
|
+
}
|
|
12
|
+
function defaultOptionsHandler(resourceOptions, { body, searchParams, sudo, asStream = false, method = "get" } = {}) {
|
|
13
|
+
const { headers: preconfiguredHeaders, requestTimeout, url } = resourceOptions;
|
|
14
|
+
const headers = { ...preconfiguredHeaders };
|
|
15
|
+
const defaultOptions = {
|
|
16
|
+
headers,
|
|
17
|
+
timeout: requestTimeout,
|
|
18
|
+
method,
|
|
19
|
+
asStream,
|
|
20
|
+
prefixUrl: url
|
|
21
|
+
};
|
|
22
|
+
if (sudo)
|
|
23
|
+
defaultOptions.headers.sudo = sudo;
|
|
24
|
+
if (body) {
|
|
25
|
+
if (isFormData(body)) {
|
|
26
|
+
defaultOptions.body = body;
|
|
27
|
+
} else {
|
|
28
|
+
defaultOptions.body = JSON.stringify(decamelizeKeys(body));
|
|
29
|
+
headers["content-type"] = "application/json";
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
const q = formatQuery(searchParams);
|
|
33
|
+
if (q)
|
|
34
|
+
defaultOptions.searchParams = q;
|
|
35
|
+
return Promise.resolve(defaultOptions);
|
|
36
|
+
}
|
|
37
|
+
function createRequesterFn(optionsHandler, requestHandler) {
|
|
38
|
+
const methods = ["get", "post", "put", "patch", "delete"];
|
|
39
|
+
return (serviceOptions) => {
|
|
40
|
+
const requester = {};
|
|
41
|
+
methods.forEach((m) => {
|
|
42
|
+
requester[m] = async (endpoint, options) => {
|
|
43
|
+
const requestOptions = await optionsHandler(serviceOptions, { ...options, method: m });
|
|
44
|
+
return requestHandler(endpoint, requestOptions);
|
|
45
|
+
};
|
|
46
|
+
});
|
|
47
|
+
return requester;
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
function extendClass(Base, customConfig = {}) {
|
|
51
|
+
return class extends Base {
|
|
52
|
+
constructor(...options) {
|
|
53
|
+
const [config, ...opts] = options;
|
|
54
|
+
super({ ...customConfig, ...config }, ...opts);
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
function presetResourceArguments(resources, customConfig = {}) {
|
|
59
|
+
const updated = {};
|
|
60
|
+
Object.entries(resources).filter(([, s]) => typeof s === "function").forEach(([k, r]) => {
|
|
61
|
+
updated[k] = extendClass(r, customConfig);
|
|
62
|
+
});
|
|
63
|
+
return updated;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// src/BaseResource.ts
|
|
67
|
+
var BaseResource = class {
|
|
68
|
+
url;
|
|
69
|
+
requester;
|
|
70
|
+
requestTimeout;
|
|
71
|
+
headers;
|
|
72
|
+
camelize;
|
|
73
|
+
rejectUnauthorized;
|
|
74
|
+
constructor({
|
|
75
|
+
token,
|
|
76
|
+
jobToken,
|
|
77
|
+
oauthToken,
|
|
78
|
+
sudo,
|
|
79
|
+
profileToken,
|
|
80
|
+
requesterFn,
|
|
81
|
+
camelize,
|
|
82
|
+
profileMode = "execution",
|
|
83
|
+
host = "https://gitlab.com",
|
|
84
|
+
prefixUrl = "",
|
|
85
|
+
rejectUnauthorized = true,
|
|
86
|
+
requestTimeout = 3e5
|
|
87
|
+
} = {}) {
|
|
88
|
+
if (!requesterFn)
|
|
89
|
+
throw new ReferenceError("requesterFn must be passed");
|
|
90
|
+
this.url = [host, "api", "v4", prefixUrl].join("/");
|
|
91
|
+
this.headers = {};
|
|
92
|
+
this.rejectUnauthorized = rejectUnauthorized;
|
|
93
|
+
this.camelize = camelize;
|
|
94
|
+
this.requestTimeout = requestTimeout;
|
|
95
|
+
if (oauthToken)
|
|
96
|
+
this.headers.authorization = `Bearer ${oauthToken}`;
|
|
97
|
+
else if (jobToken)
|
|
98
|
+
this.headers["job-token"] = jobToken;
|
|
99
|
+
else if (token)
|
|
100
|
+
this.headers["private-token"] = token;
|
|
101
|
+
if (profileToken) {
|
|
102
|
+
this.headers["X-Profile-Token"] = profileToken;
|
|
103
|
+
this.headers["X-Profile-Mode"] = profileMode;
|
|
104
|
+
}
|
|
105
|
+
if (sudo)
|
|
106
|
+
this.headers.Sudo = `${sudo}`;
|
|
107
|
+
this.requester = requesterFn({ ...this });
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
export { BaseResource, createRequesterFn, defaultOptionsHandler, formatQuery, presetResourceArguments };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gitbeaker/requester-utils",
|
|
3
3
|
"description": "Utility functions for requester implementatons used in @gitbeaker",
|
|
4
|
-
"version": "35.8.
|
|
4
|
+
"version": "35.8.1",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Justin Dalrymple"
|
|
7
7
|
},
|
|
@@ -46,6 +46,5 @@
|
|
|
46
46
|
"build": "rollup -c",
|
|
47
47
|
"test:integration": "node --expose-gc ../../node_modules/.bin/jest --runInBand --logHeapUsage test/integration",
|
|
48
48
|
"test:unit": "node --expose-gc ../../node_modules/.bin/jest --runInBand --logHeapUsage test/unit"
|
|
49
|
-
}
|
|
50
|
-
"gitHead": "f42202715f3b6d327bf63b20bffdea41d0d72009"
|
|
49
|
+
}
|
|
51
50
|
}
|
package/LICENSE.md
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
# The MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c)
|
|
4
|
-
**2020 Justin Dalrymple**
|
|
5
|
-
|
|
6
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
8
|
-
in the Software without restriction, including without limitation the rights
|
|
9
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
11
|
-
furnished to do so, subject to the following conditions:
|
|
12
|
-
|
|
13
|
-
The above copyright notice and this permission notice shall be included in
|
|
14
|
-
all copies or substantial portions of the Software.
|
|
15
|
-
|
|
16
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
22
|
-
THE SOFTWARE.
|