windmill-utils-internal 1.0.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/constants.d.ts +16 -0
- package/dist/constants.js +44 -0
- package/dist/gen/core/ApiError.d.ts +10 -0
- package/dist/gen/core/ApiError.js +20 -0
- package/dist/gen/core/ApiRequestOptions.d.ts +14 -0
- package/dist/gen/core/ApiRequestOptions.js +2 -0
- package/dist/gen/core/ApiResult.d.ts +7 -0
- package/dist/gen/core/ApiResult.js +2 -0
- package/dist/gen/core/CancelablePromise.d.ts +26 -0
- package/dist/gen/core/CancelablePromise.js +100 -0
- package/dist/gen/core/OpenAPI.d.ts +27 -0
- package/dist/gen/core/OpenAPI.js +39 -0
- package/dist/gen/core/request.d.ts +29 -0
- package/dist/gen/core/request.js +323 -0
- package/dist/gen/index.d.ts +5 -0
- package/dist/gen/index.js +27 -0
- package/dist/gen/services.gen.d.ts +4894 -0
- package/dist/gen/services.gen.js +10485 -0
- package/dist/gen/types.gen.d.ts +7319 -0
- package/dist/gen/types.gen.js +3 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +32 -0
- package/dist/inline-scripts/extractor.d.ts +30 -0
- package/dist/inline-scripts/extractor.js +85 -0
- package/dist/inline-scripts/index.d.ts +2 -0
- package/dist/inline-scripts/index.js +18 -0
- package/dist/inline-scripts/replacer.d.ts +16 -0
- package/dist/inline-scripts/replacer.js +97 -0
- package/dist/parse/index.d.ts +1 -0
- package/dist/parse/index.js +17 -0
- package/dist/parse/parse-schema.d.ts +93 -0
- package/dist/parse/parse-schema.js +205 -0
- package/dist/path-utils/index.d.ts +1 -0
- package/dist/path-utils/index.js +17 -0
- package/dist/path-utils/path-assigner.d.ts +28 -0
- package/dist/path-utils/path-assigner.js +60 -0
- package/package.json +24 -0
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cross-platform path separator constants
|
|
3
|
+
* Compatible with both Node.js and Deno environments
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Path separator constant - equivalent to Deno's SEPARATOR or Node's path.sep
|
|
7
|
+
* On Windows: "\"
|
|
8
|
+
* On Unix-like systems: "/"
|
|
9
|
+
*/
|
|
10
|
+
export declare const SEP: string;
|
|
11
|
+
/**
|
|
12
|
+
* Path delimiter constant for environment variables like PATH
|
|
13
|
+
* On Windows: ";"
|
|
14
|
+
* On Unix-like systems: ":"
|
|
15
|
+
*/
|
|
16
|
+
export declare const DELIMITER: string;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// constants.ts
|
|
3
|
+
/**
|
|
4
|
+
* Cross-platform path separator constants
|
|
5
|
+
* Compatible with both Node.js and Deno environments
|
|
6
|
+
*/
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.DELIMITER = exports.SEP = void 0;
|
|
9
|
+
/**
|
|
10
|
+
* Detects if the current platform is Windows by checking various environment indicators.
|
|
11
|
+
* Uses multiple fallback methods to ensure compatibility across Node.js, Deno, and browser environments.
|
|
12
|
+
*
|
|
13
|
+
* @returns True if running on Windows, false otherwise
|
|
14
|
+
*/
|
|
15
|
+
const isWindows = (() => {
|
|
16
|
+
// Try Node.js process.platform first (most reliable)
|
|
17
|
+
if (typeof process !== "undefined" && process.platform) {
|
|
18
|
+
return process.platform === "win32";
|
|
19
|
+
}
|
|
20
|
+
// Try Deno specific detection via globalThis
|
|
21
|
+
if (typeof globalThis !== "undefined" && "Deno" in globalThis) {
|
|
22
|
+
return globalThis.Deno?.build?.os === "windows";
|
|
23
|
+
}
|
|
24
|
+
// Try navigator.platform (browser/some Deno contexts)
|
|
25
|
+
// @ts-ignore
|
|
26
|
+
if (typeof navigator !== "undefined" && navigator.platform) {
|
|
27
|
+
// @ts-ignore
|
|
28
|
+
return navigator.platform.toLowerCase().includes("win");
|
|
29
|
+
}
|
|
30
|
+
// Fallback - assume Unix-like (safest default)
|
|
31
|
+
return false;
|
|
32
|
+
})();
|
|
33
|
+
/**
|
|
34
|
+
* Path separator constant - equivalent to Deno's SEPARATOR or Node's path.sep
|
|
35
|
+
* On Windows: "\"
|
|
36
|
+
* On Unix-like systems: "/"
|
|
37
|
+
*/
|
|
38
|
+
exports.SEP = isWindows ? "\\" : "/";
|
|
39
|
+
/**
|
|
40
|
+
* Path delimiter constant for environment variables like PATH
|
|
41
|
+
* On Windows: ";"
|
|
42
|
+
* On Unix-like systems: ":"
|
|
43
|
+
*/
|
|
44
|
+
exports.DELIMITER = isWindows ? ";" : ":";
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { ApiRequestOptions } from './ApiRequestOptions';
|
|
2
|
+
import type { ApiResult } from './ApiResult';
|
|
3
|
+
export declare class ApiError extends Error {
|
|
4
|
+
readonly url: string;
|
|
5
|
+
readonly status: number;
|
|
6
|
+
readonly statusText: string;
|
|
7
|
+
readonly body: unknown;
|
|
8
|
+
readonly request: ApiRequestOptions;
|
|
9
|
+
constructor(request: ApiRequestOptions, response: ApiResult, message: string);
|
|
10
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ApiError = void 0;
|
|
4
|
+
class ApiError extends Error {
|
|
5
|
+
url;
|
|
6
|
+
status;
|
|
7
|
+
statusText;
|
|
8
|
+
body;
|
|
9
|
+
request;
|
|
10
|
+
constructor(request, response, message) {
|
|
11
|
+
super(message);
|
|
12
|
+
this.name = 'ApiError';
|
|
13
|
+
this.url = response.url;
|
|
14
|
+
this.status = response.status;
|
|
15
|
+
this.statusText = response.statusText;
|
|
16
|
+
this.body = response.body;
|
|
17
|
+
this.request = request;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
exports.ApiError = ApiError;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export type ApiRequestOptions<T = unknown> = {
|
|
2
|
+
readonly body?: any;
|
|
3
|
+
readonly cookies?: Record<string, unknown>;
|
|
4
|
+
readonly errors?: Record<number | string, string>;
|
|
5
|
+
readonly formData?: Record<string, unknown> | any[] | Blob | File;
|
|
6
|
+
readonly headers?: Record<string, unknown>;
|
|
7
|
+
readonly mediaType?: string;
|
|
8
|
+
readonly method: 'DELETE' | 'GET' | 'HEAD' | 'OPTIONS' | 'PATCH' | 'POST' | 'PUT';
|
|
9
|
+
readonly path?: Record<string, unknown>;
|
|
10
|
+
readonly query?: Record<string, unknown>;
|
|
11
|
+
readonly responseHeader?: string;
|
|
12
|
+
readonly responseTransformer?: (data: unknown) => Promise<T>;
|
|
13
|
+
readonly url: string;
|
|
14
|
+
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export declare class CancelError extends Error {
|
|
2
|
+
constructor(message: string);
|
|
3
|
+
get isCancelled(): boolean;
|
|
4
|
+
}
|
|
5
|
+
export interface OnCancel {
|
|
6
|
+
readonly isResolved: boolean;
|
|
7
|
+
readonly isRejected: boolean;
|
|
8
|
+
readonly isCancelled: boolean;
|
|
9
|
+
(cancelHandler: () => void): void;
|
|
10
|
+
}
|
|
11
|
+
export declare class CancelablePromise<T> implements Promise<T> {
|
|
12
|
+
private _isResolved;
|
|
13
|
+
private _isRejected;
|
|
14
|
+
private _isCancelled;
|
|
15
|
+
readonly cancelHandlers: (() => void)[];
|
|
16
|
+
readonly promise: Promise<T>;
|
|
17
|
+
private _resolve?;
|
|
18
|
+
private _reject?;
|
|
19
|
+
constructor(executor: (resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: unknown) => void, onCancel: OnCancel) => void);
|
|
20
|
+
get [Symbol.toStringTag](): string;
|
|
21
|
+
then<TResult1 = T, TResult2 = never>(onFulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null, onRejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | null): Promise<TResult1 | TResult2>;
|
|
22
|
+
catch<TResult = never>(onRejected?: ((reason: unknown) => TResult | PromiseLike<TResult>) | null): Promise<T | TResult>;
|
|
23
|
+
finally(onFinally?: (() => void) | null): Promise<T>;
|
|
24
|
+
cancel(): void;
|
|
25
|
+
get isCancelled(): boolean;
|
|
26
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CancelablePromise = exports.CancelError = void 0;
|
|
4
|
+
class CancelError extends Error {
|
|
5
|
+
constructor(message) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.name = 'CancelError';
|
|
8
|
+
}
|
|
9
|
+
get isCancelled() {
|
|
10
|
+
return true;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
exports.CancelError = CancelError;
|
|
14
|
+
class CancelablePromise {
|
|
15
|
+
_isResolved;
|
|
16
|
+
_isRejected;
|
|
17
|
+
_isCancelled;
|
|
18
|
+
cancelHandlers;
|
|
19
|
+
promise;
|
|
20
|
+
_resolve;
|
|
21
|
+
_reject;
|
|
22
|
+
constructor(executor) {
|
|
23
|
+
this._isResolved = false;
|
|
24
|
+
this._isRejected = false;
|
|
25
|
+
this._isCancelled = false;
|
|
26
|
+
this.cancelHandlers = [];
|
|
27
|
+
this.promise = new Promise((resolve, reject) => {
|
|
28
|
+
this._resolve = resolve;
|
|
29
|
+
this._reject = reject;
|
|
30
|
+
const onResolve = (value) => {
|
|
31
|
+
if (this._isResolved || this._isRejected || this._isCancelled) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
this._isResolved = true;
|
|
35
|
+
if (this._resolve)
|
|
36
|
+
this._resolve(value);
|
|
37
|
+
};
|
|
38
|
+
const onReject = (reason) => {
|
|
39
|
+
if (this._isResolved || this._isRejected || this._isCancelled) {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
this._isRejected = true;
|
|
43
|
+
if (this._reject)
|
|
44
|
+
this._reject(reason);
|
|
45
|
+
};
|
|
46
|
+
const onCancel = (cancelHandler) => {
|
|
47
|
+
if (this._isResolved || this._isRejected || this._isCancelled) {
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
this.cancelHandlers.push(cancelHandler);
|
|
51
|
+
};
|
|
52
|
+
Object.defineProperty(onCancel, 'isResolved', {
|
|
53
|
+
get: () => this._isResolved,
|
|
54
|
+
});
|
|
55
|
+
Object.defineProperty(onCancel, 'isRejected', {
|
|
56
|
+
get: () => this._isRejected,
|
|
57
|
+
});
|
|
58
|
+
Object.defineProperty(onCancel, 'isCancelled', {
|
|
59
|
+
get: () => this._isCancelled,
|
|
60
|
+
});
|
|
61
|
+
return executor(onResolve, onReject, onCancel);
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
get [Symbol.toStringTag]() {
|
|
65
|
+
return "Cancellable Promise";
|
|
66
|
+
}
|
|
67
|
+
then(onFulfilled, onRejected) {
|
|
68
|
+
return this.promise.then(onFulfilled, onRejected);
|
|
69
|
+
}
|
|
70
|
+
catch(onRejected) {
|
|
71
|
+
return this.promise.catch(onRejected);
|
|
72
|
+
}
|
|
73
|
+
finally(onFinally) {
|
|
74
|
+
return this.promise.finally(onFinally);
|
|
75
|
+
}
|
|
76
|
+
cancel() {
|
|
77
|
+
if (this._isResolved || this._isRejected || this._isCancelled) {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
this._isCancelled = true;
|
|
81
|
+
if (this.cancelHandlers.length) {
|
|
82
|
+
try {
|
|
83
|
+
for (const cancelHandler of this.cancelHandlers) {
|
|
84
|
+
cancelHandler();
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
catch (error) {
|
|
88
|
+
console.warn('Cancellation threw an error', error);
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
this.cancelHandlers.length = 0;
|
|
93
|
+
if (this._reject)
|
|
94
|
+
this._reject(new CancelError('Request aborted'));
|
|
95
|
+
}
|
|
96
|
+
get isCancelled() {
|
|
97
|
+
return this._isCancelled;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
exports.CancelablePromise = CancelablePromise;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { ApiRequestOptions } from './ApiRequestOptions';
|
|
2
|
+
type Headers = Record<string, string>;
|
|
3
|
+
type Middleware<T> = (value: T) => T | Promise<T>;
|
|
4
|
+
type Resolver<T> = (options: ApiRequestOptions<T>) => Promise<T>;
|
|
5
|
+
export declare class Interceptors<T> {
|
|
6
|
+
_fns: Middleware<T>[];
|
|
7
|
+
constructor();
|
|
8
|
+
eject(fn: Middleware<T>): void;
|
|
9
|
+
use(fn: Middleware<T>): void;
|
|
10
|
+
}
|
|
11
|
+
export type OpenAPIConfig = {
|
|
12
|
+
BASE: string;
|
|
13
|
+
CREDENTIALS: 'include' | 'omit' | 'same-origin';
|
|
14
|
+
ENCODE_PATH?: ((path: string) => string) | undefined;
|
|
15
|
+
HEADERS?: Headers | Resolver<Headers> | undefined;
|
|
16
|
+
PASSWORD?: string | Resolver<string> | undefined;
|
|
17
|
+
TOKEN?: string | Resolver<string> | undefined;
|
|
18
|
+
USERNAME?: string | Resolver<string> | undefined;
|
|
19
|
+
VERSION: string;
|
|
20
|
+
WITH_CREDENTIALS: boolean;
|
|
21
|
+
interceptors: {
|
|
22
|
+
request: Interceptors<RequestInit>;
|
|
23
|
+
response: Interceptors<Response>;
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
export declare const OpenAPI: OpenAPIConfig;
|
|
27
|
+
export {};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.OpenAPI = exports.Interceptors = void 0;
|
|
4
|
+
const getEnv = (key) => {
|
|
5
|
+
return process.env[key];
|
|
6
|
+
};
|
|
7
|
+
const baseUrl = getEnv("BASE_INTERNAL_URL") ?? getEnv("BASE_URL") ?? "http://localhost:8000";
|
|
8
|
+
const baseUrlApi = (baseUrl ?? '') + "/api";
|
|
9
|
+
class Interceptors {
|
|
10
|
+
_fns;
|
|
11
|
+
constructor() {
|
|
12
|
+
this._fns = [];
|
|
13
|
+
}
|
|
14
|
+
eject(fn) {
|
|
15
|
+
const index = this._fns.indexOf(fn);
|
|
16
|
+
if (index !== -1) {
|
|
17
|
+
this._fns = [...this._fns.slice(0, index), ...this._fns.slice(index + 1)];
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
use(fn) {
|
|
21
|
+
this._fns = [...this._fns, fn];
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
exports.Interceptors = Interceptors;
|
|
25
|
+
exports.OpenAPI = {
|
|
26
|
+
BASE: baseUrlApi,
|
|
27
|
+
CREDENTIALS: 'include',
|
|
28
|
+
ENCODE_PATH: undefined,
|
|
29
|
+
HEADERS: undefined,
|
|
30
|
+
PASSWORD: undefined,
|
|
31
|
+
TOKEN: getEnv("WM_TOKEN"),
|
|
32
|
+
USERNAME: undefined,
|
|
33
|
+
VERSION: '1.516.0',
|
|
34
|
+
WITH_CREDENTIALS: true,
|
|
35
|
+
interceptors: {
|
|
36
|
+
request: new Interceptors(),
|
|
37
|
+
response: new Interceptors(),
|
|
38
|
+
},
|
|
39
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { ApiRequestOptions } from './ApiRequestOptions';
|
|
2
|
+
import type { ApiResult } from './ApiResult';
|
|
3
|
+
import { CancelablePromise } from './CancelablePromise';
|
|
4
|
+
import type { OnCancel } from './CancelablePromise';
|
|
5
|
+
import type { OpenAPIConfig } from './OpenAPI';
|
|
6
|
+
export declare const isString: (value: unknown) => value is string;
|
|
7
|
+
export declare const isStringWithValue: (value: unknown) => value is string;
|
|
8
|
+
export declare const isBlob: (value: any) => value is Blob;
|
|
9
|
+
export declare const isFormData: (value: unknown) => value is FormData;
|
|
10
|
+
export declare const base64: (str: string) => string;
|
|
11
|
+
export declare const getQueryString: (params: Record<string, unknown>) => string;
|
|
12
|
+
export declare const getFormData: (options: ApiRequestOptions) => FormData | undefined;
|
|
13
|
+
type Resolver<T> = (options: ApiRequestOptions<T>) => Promise<T>;
|
|
14
|
+
export declare const resolve: <T>(options: ApiRequestOptions<T>, resolver?: T | Resolver<T>) => Promise<T | undefined>;
|
|
15
|
+
export declare const getHeaders: <T>(config: OpenAPIConfig, options: ApiRequestOptions<T>) => Promise<Headers>;
|
|
16
|
+
export declare const getRequestBody: (options: ApiRequestOptions) => unknown;
|
|
17
|
+
export declare const sendRequest: (config: OpenAPIConfig, options: ApiRequestOptions, url: string, body: any, formData: FormData | undefined, headers: Headers, onCancel: OnCancel) => Promise<Response>;
|
|
18
|
+
export declare const getResponseHeader: (response: Response, responseHeader?: string) => string | undefined;
|
|
19
|
+
export declare const getResponseBody: (response: Response) => Promise<unknown>;
|
|
20
|
+
export declare const catchErrorCodes: (options: ApiRequestOptions, result: ApiResult) => void;
|
|
21
|
+
/**
|
|
22
|
+
* Request method
|
|
23
|
+
* @param config The OpenAPI configuration object
|
|
24
|
+
* @param options The request options from the service
|
|
25
|
+
* @returns CancelablePromise<T>
|
|
26
|
+
* @throws ApiError
|
|
27
|
+
*/
|
|
28
|
+
export declare const request: <T>(config: OpenAPIConfig, options: ApiRequestOptions<T>) => CancelablePromise<T>;
|
|
29
|
+
export {};
|
|
@@ -0,0 +1,323 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.request = exports.catchErrorCodes = exports.getResponseBody = exports.getResponseHeader = exports.sendRequest = exports.getRequestBody = exports.getHeaders = exports.resolve = exports.getFormData = exports.getQueryString = exports.base64 = exports.isFormData = exports.isBlob = exports.isStringWithValue = exports.isString = void 0;
|
|
4
|
+
const ApiError_1 = require("./ApiError");
|
|
5
|
+
const CancelablePromise_1 = require("./CancelablePromise");
|
|
6
|
+
const isString = (value) => {
|
|
7
|
+
return typeof value === 'string';
|
|
8
|
+
};
|
|
9
|
+
exports.isString = isString;
|
|
10
|
+
const isStringWithValue = (value) => {
|
|
11
|
+
return (0, exports.isString)(value) && value !== '';
|
|
12
|
+
};
|
|
13
|
+
exports.isStringWithValue = isStringWithValue;
|
|
14
|
+
const isBlob = (value) => {
|
|
15
|
+
return value instanceof Blob;
|
|
16
|
+
};
|
|
17
|
+
exports.isBlob = isBlob;
|
|
18
|
+
const isFormData = (value) => {
|
|
19
|
+
return value instanceof FormData;
|
|
20
|
+
};
|
|
21
|
+
exports.isFormData = isFormData;
|
|
22
|
+
const base64 = (str) => {
|
|
23
|
+
try {
|
|
24
|
+
return btoa(str);
|
|
25
|
+
}
|
|
26
|
+
catch (err) {
|
|
27
|
+
// @ts-ignore
|
|
28
|
+
return Buffer.from(str).toString('base64');
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
exports.base64 = base64;
|
|
32
|
+
const getQueryString = (params) => {
|
|
33
|
+
const qs = [];
|
|
34
|
+
const append = (key, value) => {
|
|
35
|
+
qs.push(`${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`);
|
|
36
|
+
};
|
|
37
|
+
const encodePair = (key, value) => {
|
|
38
|
+
if (value === undefined || value === null) {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
if (value instanceof Date) {
|
|
42
|
+
append(key, value.toISOString());
|
|
43
|
+
}
|
|
44
|
+
else if (Array.isArray(value)) {
|
|
45
|
+
value.forEach(v => encodePair(key, v));
|
|
46
|
+
}
|
|
47
|
+
else if (typeof value === 'object') {
|
|
48
|
+
Object.entries(value).forEach(([k, v]) => encodePair(`${key}[${k}]`, v));
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
append(key, value);
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
Object.entries(params).forEach(([key, value]) => encodePair(key, value));
|
|
55
|
+
return qs.length ? `?${qs.join('&')}` : '';
|
|
56
|
+
};
|
|
57
|
+
exports.getQueryString = getQueryString;
|
|
58
|
+
const getUrl = (config, options) => {
|
|
59
|
+
const encoder = config.ENCODE_PATH || encodeURI;
|
|
60
|
+
const path = options.url
|
|
61
|
+
.replace('{api-version}', config.VERSION)
|
|
62
|
+
.replace(/{(.*?)}/g, (substring, group) => {
|
|
63
|
+
if (options.path?.hasOwnProperty(group)) {
|
|
64
|
+
return encoder(String(options.path[group]));
|
|
65
|
+
}
|
|
66
|
+
return substring;
|
|
67
|
+
});
|
|
68
|
+
const url = config.BASE + path;
|
|
69
|
+
return options.query ? url + (0, exports.getQueryString)(options.query) : url;
|
|
70
|
+
};
|
|
71
|
+
const getFormData = (options) => {
|
|
72
|
+
if (options.formData) {
|
|
73
|
+
const formData = new FormData();
|
|
74
|
+
const process = (key, value) => {
|
|
75
|
+
if ((0, exports.isString)(value) || (0, exports.isBlob)(value)) {
|
|
76
|
+
formData.append(key, value);
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
formData.append(key, JSON.stringify(value));
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
Object.entries(options.formData)
|
|
83
|
+
.filter(([, value]) => value !== undefined && value !== null)
|
|
84
|
+
.forEach(([key, value]) => {
|
|
85
|
+
if (Array.isArray(value)) {
|
|
86
|
+
value.forEach(v => process(key, v));
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
process(key, value);
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
return formData;
|
|
93
|
+
}
|
|
94
|
+
return undefined;
|
|
95
|
+
};
|
|
96
|
+
exports.getFormData = getFormData;
|
|
97
|
+
const resolve = async (options, resolver) => {
|
|
98
|
+
if (typeof resolver === 'function') {
|
|
99
|
+
return resolver(options);
|
|
100
|
+
}
|
|
101
|
+
return resolver;
|
|
102
|
+
};
|
|
103
|
+
exports.resolve = resolve;
|
|
104
|
+
const getHeaders = async (config, options) => {
|
|
105
|
+
const [token, username, password, additionalHeaders] = await Promise.all([
|
|
106
|
+
// @ts-ignore
|
|
107
|
+
(0, exports.resolve)(options, config.TOKEN),
|
|
108
|
+
// @ts-ignore
|
|
109
|
+
(0, exports.resolve)(options, config.USERNAME),
|
|
110
|
+
// @ts-ignore
|
|
111
|
+
(0, exports.resolve)(options, config.PASSWORD),
|
|
112
|
+
// @ts-ignore
|
|
113
|
+
(0, exports.resolve)(options, config.HEADERS),
|
|
114
|
+
]);
|
|
115
|
+
const headers = Object.entries({
|
|
116
|
+
Accept: 'application/json',
|
|
117
|
+
...additionalHeaders,
|
|
118
|
+
...options.headers,
|
|
119
|
+
})
|
|
120
|
+
.filter(([, value]) => value !== undefined && value !== null)
|
|
121
|
+
.reduce((headers, [key, value]) => ({
|
|
122
|
+
...headers,
|
|
123
|
+
[key]: String(value),
|
|
124
|
+
}), {});
|
|
125
|
+
if ((0, exports.isStringWithValue)(token)) {
|
|
126
|
+
headers['Authorization'] = `Bearer ${token}`;
|
|
127
|
+
}
|
|
128
|
+
if ((0, exports.isStringWithValue)(username) && (0, exports.isStringWithValue)(password)) {
|
|
129
|
+
const credentials = (0, exports.base64)(`${username}:${password}`);
|
|
130
|
+
headers['Authorization'] = `Basic ${credentials}`;
|
|
131
|
+
}
|
|
132
|
+
if (options.body !== undefined) {
|
|
133
|
+
if (options.mediaType) {
|
|
134
|
+
headers['Content-Type'] = options.mediaType;
|
|
135
|
+
}
|
|
136
|
+
else if ((0, exports.isBlob)(options.body)) {
|
|
137
|
+
headers['Content-Type'] = options.body.type || 'application/octet-stream';
|
|
138
|
+
}
|
|
139
|
+
else if ((0, exports.isString)(options.body)) {
|
|
140
|
+
headers['Content-Type'] = 'text/plain';
|
|
141
|
+
}
|
|
142
|
+
else if (!(0, exports.isFormData)(options.body)) {
|
|
143
|
+
headers['Content-Type'] = 'application/json';
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return new Headers(headers);
|
|
147
|
+
};
|
|
148
|
+
exports.getHeaders = getHeaders;
|
|
149
|
+
const getRequestBody = (options) => {
|
|
150
|
+
if (options.body !== undefined) {
|
|
151
|
+
if (options.mediaType?.includes('application/json') || options.mediaType?.includes('+json')) {
|
|
152
|
+
return JSON.stringify(options.body);
|
|
153
|
+
}
|
|
154
|
+
else if ((0, exports.isString)(options.body) || (0, exports.isBlob)(options.body) || (0, exports.isFormData)(options.body)) {
|
|
155
|
+
return options.body;
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
return JSON.stringify(options.body);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return undefined;
|
|
162
|
+
};
|
|
163
|
+
exports.getRequestBody = getRequestBody;
|
|
164
|
+
const sendRequest = async (config, options, url, body, formData, headers, onCancel) => {
|
|
165
|
+
const controller = new AbortController();
|
|
166
|
+
let request = {
|
|
167
|
+
headers,
|
|
168
|
+
body: body ?? formData,
|
|
169
|
+
method: options.method,
|
|
170
|
+
signal: controller.signal,
|
|
171
|
+
};
|
|
172
|
+
if (config.WITH_CREDENTIALS) {
|
|
173
|
+
request.credentials = config.CREDENTIALS;
|
|
174
|
+
}
|
|
175
|
+
for (const fn of config.interceptors.request._fns) {
|
|
176
|
+
request = await fn(request);
|
|
177
|
+
}
|
|
178
|
+
onCancel(() => controller.abort());
|
|
179
|
+
return await fetch(url, request);
|
|
180
|
+
};
|
|
181
|
+
exports.sendRequest = sendRequest;
|
|
182
|
+
const getResponseHeader = (response, responseHeader) => {
|
|
183
|
+
if (responseHeader) {
|
|
184
|
+
const content = response.headers.get(responseHeader);
|
|
185
|
+
if ((0, exports.isString)(content)) {
|
|
186
|
+
return content;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
return undefined;
|
|
190
|
+
};
|
|
191
|
+
exports.getResponseHeader = getResponseHeader;
|
|
192
|
+
const getResponseBody = async (response) => {
|
|
193
|
+
if (response.status !== 204) {
|
|
194
|
+
try {
|
|
195
|
+
const contentType = response.headers.get('Content-Type');
|
|
196
|
+
if (contentType) {
|
|
197
|
+
const binaryTypes = ['application/octet-stream', 'application/pdf', 'application/zip', 'audio/', 'image/', 'video/'];
|
|
198
|
+
if (contentType.includes('application/json') || contentType.includes('+json')) {
|
|
199
|
+
return await response.json();
|
|
200
|
+
}
|
|
201
|
+
else if (binaryTypes.some(type => contentType.includes(type))) {
|
|
202
|
+
return await response.blob();
|
|
203
|
+
}
|
|
204
|
+
else if (contentType.includes('multipart/form-data')) {
|
|
205
|
+
return await response.formData();
|
|
206
|
+
}
|
|
207
|
+
else if (contentType.includes('text/')) {
|
|
208
|
+
return await response.text();
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
catch (error) {
|
|
213
|
+
console.error(error);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
return undefined;
|
|
217
|
+
};
|
|
218
|
+
exports.getResponseBody = getResponseBody;
|
|
219
|
+
const catchErrorCodes = (options, result) => {
|
|
220
|
+
const errors = {
|
|
221
|
+
400: 'Bad Request',
|
|
222
|
+
401: 'Unauthorized',
|
|
223
|
+
402: 'Payment Required',
|
|
224
|
+
403: 'Forbidden',
|
|
225
|
+
404: 'Not Found',
|
|
226
|
+
405: 'Method Not Allowed',
|
|
227
|
+
406: 'Not Acceptable',
|
|
228
|
+
407: 'Proxy Authentication Required',
|
|
229
|
+
408: 'Request Timeout',
|
|
230
|
+
409: 'Conflict',
|
|
231
|
+
410: 'Gone',
|
|
232
|
+
411: 'Length Required',
|
|
233
|
+
412: 'Precondition Failed',
|
|
234
|
+
413: 'Payload Too Large',
|
|
235
|
+
414: 'URI Too Long',
|
|
236
|
+
415: 'Unsupported Media Type',
|
|
237
|
+
416: 'Range Not Satisfiable',
|
|
238
|
+
417: 'Expectation Failed',
|
|
239
|
+
418: 'Im a teapot',
|
|
240
|
+
421: 'Misdirected Request',
|
|
241
|
+
422: 'Unprocessable Content',
|
|
242
|
+
423: 'Locked',
|
|
243
|
+
424: 'Failed Dependency',
|
|
244
|
+
425: 'Too Early',
|
|
245
|
+
426: 'Upgrade Required',
|
|
246
|
+
428: 'Precondition Required',
|
|
247
|
+
429: 'Too Many Requests',
|
|
248
|
+
431: 'Request Header Fields Too Large',
|
|
249
|
+
451: 'Unavailable For Legal Reasons',
|
|
250
|
+
500: 'Internal Server Error',
|
|
251
|
+
501: 'Not Implemented',
|
|
252
|
+
502: 'Bad Gateway',
|
|
253
|
+
503: 'Service Unavailable',
|
|
254
|
+
504: 'Gateway Timeout',
|
|
255
|
+
505: 'HTTP Version Not Supported',
|
|
256
|
+
506: 'Variant Also Negotiates',
|
|
257
|
+
507: 'Insufficient Storage',
|
|
258
|
+
508: 'Loop Detected',
|
|
259
|
+
510: 'Not Extended',
|
|
260
|
+
511: 'Network Authentication Required',
|
|
261
|
+
...options.errors,
|
|
262
|
+
};
|
|
263
|
+
const error = errors[result.status];
|
|
264
|
+
if (error) {
|
|
265
|
+
throw new ApiError_1.ApiError(options, result, error);
|
|
266
|
+
}
|
|
267
|
+
if (!result.ok) {
|
|
268
|
+
const errorStatus = result.status ?? 'unknown';
|
|
269
|
+
const errorStatusText = result.statusText ?? 'unknown';
|
|
270
|
+
const errorBody = (() => {
|
|
271
|
+
try {
|
|
272
|
+
return JSON.stringify(result.body, null, 2);
|
|
273
|
+
}
|
|
274
|
+
catch (e) {
|
|
275
|
+
return undefined;
|
|
276
|
+
}
|
|
277
|
+
})();
|
|
278
|
+
throw new ApiError_1.ApiError(options, result, `Generic Error: status: ${errorStatus}; status text: ${errorStatusText}; body: ${errorBody}`);
|
|
279
|
+
}
|
|
280
|
+
};
|
|
281
|
+
exports.catchErrorCodes = catchErrorCodes;
|
|
282
|
+
/**
|
|
283
|
+
* Request method
|
|
284
|
+
* @param config The OpenAPI configuration object
|
|
285
|
+
* @param options The request options from the service
|
|
286
|
+
* @returns CancelablePromise<T>
|
|
287
|
+
* @throws ApiError
|
|
288
|
+
*/
|
|
289
|
+
const request = (config, options) => {
|
|
290
|
+
return new CancelablePromise_1.CancelablePromise(async (resolve, reject, onCancel) => {
|
|
291
|
+
try {
|
|
292
|
+
const url = getUrl(config, options);
|
|
293
|
+
const formData = (0, exports.getFormData)(options);
|
|
294
|
+
const body = (0, exports.getRequestBody)(options);
|
|
295
|
+
const headers = await (0, exports.getHeaders)(config, options);
|
|
296
|
+
if (!onCancel.isCancelled) {
|
|
297
|
+
let response = await (0, exports.sendRequest)(config, options, url, body, formData, headers, onCancel);
|
|
298
|
+
for (const fn of config.interceptors.response._fns) {
|
|
299
|
+
response = await fn(response);
|
|
300
|
+
}
|
|
301
|
+
const responseBody = await (0, exports.getResponseBody)(response);
|
|
302
|
+
const responseHeader = (0, exports.getResponseHeader)(response, options.responseHeader);
|
|
303
|
+
let transformedBody = responseBody;
|
|
304
|
+
if (options.responseTransformer && response.ok) {
|
|
305
|
+
transformedBody = await options.responseTransformer(responseBody);
|
|
306
|
+
}
|
|
307
|
+
const result = {
|
|
308
|
+
url,
|
|
309
|
+
ok: response.ok,
|
|
310
|
+
status: response.status,
|
|
311
|
+
statusText: response.statusText,
|
|
312
|
+
body: responseHeader ?? transformedBody,
|
|
313
|
+
};
|
|
314
|
+
(0, exports.catchErrorCodes)(options, result);
|
|
315
|
+
resolve(result.body);
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
catch (error) {
|
|
319
|
+
reject(error);
|
|
320
|
+
}
|
|
321
|
+
});
|
|
322
|
+
};
|
|
323
|
+
exports.request = request;
|