@gitbeaker/requester-utils 36.0.1-next.1 → 36.0.2--canary.2258.813948675.0
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.js +117 -0
- package/dist/index.mjs +111 -0
- package/package.json +2 -2
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.js
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var xcase = require('xcase');
|
|
4
|
+
var qs = require('qs');
|
|
5
|
+
|
|
6
|
+
// src/RequesterUtils.ts
|
|
7
|
+
function formatQuery(params = {}) {
|
|
8
|
+
const decamelized = xcase.decamelizeKeys(params);
|
|
9
|
+
return qs.stringify(decamelized, { arrayFormat: "brackets" });
|
|
10
|
+
}
|
|
11
|
+
function isFormData(object) {
|
|
12
|
+
return typeof object === "object" && object.constructor.name === "FormData";
|
|
13
|
+
}
|
|
14
|
+
function defaultOptionsHandler(resourceOptions, { body, searchParams, sudo, asStream = false, method = "get" } = {}) {
|
|
15
|
+
const { headers: preconfiguredHeaders, requestTimeout, url } = resourceOptions;
|
|
16
|
+
const headers = { ...preconfiguredHeaders };
|
|
17
|
+
const defaultOptions = {
|
|
18
|
+
headers,
|
|
19
|
+
timeout: requestTimeout,
|
|
20
|
+
method,
|
|
21
|
+
asStream,
|
|
22
|
+
prefixUrl: url
|
|
23
|
+
};
|
|
24
|
+
if (sudo)
|
|
25
|
+
defaultOptions.headers.sudo = sudo;
|
|
26
|
+
if (body) {
|
|
27
|
+
if (isFormData(body)) {
|
|
28
|
+
defaultOptions.body = body;
|
|
29
|
+
} else {
|
|
30
|
+
defaultOptions.body = JSON.stringify(xcase.decamelizeKeys(body));
|
|
31
|
+
headers["content-type"] = "application/json";
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
const q = formatQuery(searchParams);
|
|
35
|
+
if (q)
|
|
36
|
+
defaultOptions.searchParams = q;
|
|
37
|
+
return Promise.resolve(defaultOptions);
|
|
38
|
+
}
|
|
39
|
+
function createRequesterFn(optionsHandler, requestHandler) {
|
|
40
|
+
const methods = ["get", "post", "put", "patch", "delete"];
|
|
41
|
+
return (serviceOptions) => {
|
|
42
|
+
const requester = {};
|
|
43
|
+
methods.forEach((m) => {
|
|
44
|
+
requester[m] = async (endpoint, options) => {
|
|
45
|
+
const requestOptions = await optionsHandler(serviceOptions, { ...options, method: m });
|
|
46
|
+
return requestHandler(endpoint, requestOptions);
|
|
47
|
+
};
|
|
48
|
+
});
|
|
49
|
+
return requester;
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
function extendClass(Base, customConfig = {}) {
|
|
53
|
+
return class extends Base {
|
|
54
|
+
constructor(...options) {
|
|
55
|
+
const [config, ...opts] = options;
|
|
56
|
+
super({ ...customConfig, ...config }, ...opts);
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
function presetResourceArguments(resources, customConfig = {}) {
|
|
61
|
+
const updated = {};
|
|
62
|
+
Object.entries(resources).filter(([, s]) => typeof s === "function").forEach(([k, r]) => {
|
|
63
|
+
updated[k] = extendClass(r, customConfig);
|
|
64
|
+
});
|
|
65
|
+
return updated;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// src/BaseResource.ts
|
|
69
|
+
var BaseResource = class {
|
|
70
|
+
url;
|
|
71
|
+
requester;
|
|
72
|
+
requestTimeout;
|
|
73
|
+
headers;
|
|
74
|
+
camelize;
|
|
75
|
+
rejectUnauthorized;
|
|
76
|
+
constructor({
|
|
77
|
+
token,
|
|
78
|
+
jobToken,
|
|
79
|
+
oauthToken,
|
|
80
|
+
sudo,
|
|
81
|
+
profileToken,
|
|
82
|
+
requesterFn,
|
|
83
|
+
camelize,
|
|
84
|
+
profileMode = "execution",
|
|
85
|
+
host = "https://gitlab.com",
|
|
86
|
+
prefixUrl = "",
|
|
87
|
+
rejectUnauthorized = true,
|
|
88
|
+
requestTimeout = 3e5
|
|
89
|
+
} = {}) {
|
|
90
|
+
if (!requesterFn)
|
|
91
|
+
throw new ReferenceError("requesterFn must be passed");
|
|
92
|
+
this.url = [host, "api", "v4", prefixUrl].join("/");
|
|
93
|
+
this.headers = {};
|
|
94
|
+
this.rejectUnauthorized = rejectUnauthorized;
|
|
95
|
+
this.camelize = camelize;
|
|
96
|
+
this.requestTimeout = requestTimeout;
|
|
97
|
+
if (oauthToken)
|
|
98
|
+
this.headers.authorization = `Bearer ${oauthToken}`;
|
|
99
|
+
else if (jobToken)
|
|
100
|
+
this.headers["job-token"] = jobToken;
|
|
101
|
+
else if (token)
|
|
102
|
+
this.headers["private-token"] = token;
|
|
103
|
+
if (profileToken) {
|
|
104
|
+
this.headers["X-Profile-Token"] = profileToken;
|
|
105
|
+
this.headers["X-Profile-Mode"] = profileMode;
|
|
106
|
+
}
|
|
107
|
+
if (sudo)
|
|
108
|
+
this.headers.Sudo = `${sudo}`;
|
|
109
|
+
this.requester = requesterFn({ ...this });
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
exports.BaseResource = BaseResource;
|
|
114
|
+
exports.createRequesterFn = createRequesterFn;
|
|
115
|
+
exports.defaultOptionsHandler = defaultOptionsHandler;
|
|
116
|
+
exports.formatQuery = formatQuery;
|
|
117
|
+
exports.presetResourceArguments = 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,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gitbeaker/requester-utils",
|
|
3
|
-
"version": "36.0.
|
|
3
|
+
"version": "36.0.2--canary.2258.813948675.0",
|
|
4
4
|
"description": "Utility functions for requester implementatons used in @gitbeaker",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"engines": {
|
|
@@ -59,5 +59,5 @@
|
|
|
59
59
|
"tsup": "^6.6.3",
|
|
60
60
|
"typescript": "^4.9.5"
|
|
61
61
|
},
|
|
62
|
-
"gitHead": "
|
|
62
|
+
"gitHead": "e20aca772da76413d83cb2e9135bf4ddbcf9cd41"
|
|
63
63
|
}
|