mirror of
https://github.com/tiqi-group/pydase_service_base.git
synced 2025-04-22 09:10:02 +02:00
chore: formatting
This commit is contained in:
parent
2e9f95211f
commit
e84c19a4be
@ -2,7 +2,6 @@
|
|||||||
/* tslint:disable */
|
/* tslint:disable */
|
||||||
/* eslint-disable */
|
/* eslint-disable */
|
||||||
export class CancelError extends Error {
|
export class CancelError extends Error {
|
||||||
|
|
||||||
constructor(message: string) {
|
constructor(message: string) {
|
||||||
super(message);
|
super(message);
|
||||||
this.name = 'CancelError';
|
this.name = 'CancelError';
|
||||||
@ -71,15 +70,15 @@ export class CancelablePromise<T> implements Promise<T> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Object.defineProperty(onCancel, 'isResolved', {
|
Object.defineProperty(onCancel, 'isResolved', {
|
||||||
get: (): boolean => this._isResolved,
|
get: (): boolean => this._isResolved
|
||||||
});
|
});
|
||||||
|
|
||||||
Object.defineProperty(onCancel, 'isRejected', {
|
Object.defineProperty(onCancel, 'isRejected', {
|
||||||
get: (): boolean => this._isRejected,
|
get: (): boolean => this._isRejected
|
||||||
});
|
});
|
||||||
|
|
||||||
Object.defineProperty(onCancel, 'isCancelled', {
|
Object.defineProperty(onCancel, 'isCancelled', {
|
||||||
get: (): boolean => this._isCancelled,
|
get: (): boolean => this._isCancelled
|
||||||
});
|
});
|
||||||
|
|
||||||
return executor(onResolve, onReject, onCancel as OnCancel);
|
return executor(onResolve, onReject, onCancel as OnCancel);
|
||||||
|
@ -27,5 +27,5 @@ export const OpenAPI: OpenAPIConfig = {
|
|||||||
USERNAME: undefined,
|
USERNAME: undefined,
|
||||||
PASSWORD: undefined,
|
PASSWORD: undefined,
|
||||||
HEADERS: undefined,
|
HEADERS: undefined,
|
||||||
ENCODE_PATH: undefined,
|
ENCODE_PATH: undefined
|
||||||
};
|
};
|
||||||
|
@ -8,7 +8,9 @@ import { CancelablePromise } from './CancelablePromise';
|
|||||||
import type { OnCancel } from './CancelablePromise';
|
import type { OnCancel } from './CancelablePromise';
|
||||||
import type { OpenAPIConfig } from './OpenAPI';
|
import type { OpenAPIConfig } from './OpenAPI';
|
||||||
|
|
||||||
const isDefined = <T>(value: T | null | undefined): value is Exclude<T, null | undefined> => {
|
const isDefined = <T>(
|
||||||
|
value: T | null | undefined
|
||||||
|
): value is Exclude<T, null | undefined> => {
|
||||||
return value !== undefined && value !== null;
|
return value !== undefined && value !== null;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -56,7 +58,7 @@ const getQueryString = (params: Record<string, any>): string => {
|
|||||||
const process = (key: string, value: any) => {
|
const process = (key: string, value: any) => {
|
||||||
if (isDefined(value)) {
|
if (isDefined(value)) {
|
||||||
if (Array.isArray(value)) {
|
if (Array.isArray(value)) {
|
||||||
value.forEach(v => {
|
value.forEach((v) => {
|
||||||
process(key, v);
|
process(key, v);
|
||||||
});
|
});
|
||||||
} else if (typeof value === 'object') {
|
} else if (typeof value === 'object') {
|
||||||
@ -115,7 +117,7 @@ const getFormData = (options: ApiRequestOptions): FormData | undefined => {
|
|||||||
.filter(([_, value]) => isDefined(value))
|
.filter(([_, value]) => isDefined(value))
|
||||||
.forEach(([key, value]) => {
|
.forEach(([key, value]) => {
|
||||||
if (Array.isArray(value)) {
|
if (Array.isArray(value)) {
|
||||||
value.forEach(v => process(key, v));
|
value.forEach((v) => process(key, v));
|
||||||
} else {
|
} else {
|
||||||
process(key, value);
|
process(key, value);
|
||||||
}
|
}
|
||||||
@ -128,14 +130,20 @@ const getFormData = (options: ApiRequestOptions): FormData | undefined => {
|
|||||||
|
|
||||||
type Resolver<T> = (options: ApiRequestOptions) => Promise<T>;
|
type Resolver<T> = (options: ApiRequestOptions) => Promise<T>;
|
||||||
|
|
||||||
const resolve = async <T>(options: ApiRequestOptions, resolver?: T | Resolver<T>): Promise<T | undefined> => {
|
const resolve = async <T>(
|
||||||
|
options: ApiRequestOptions,
|
||||||
|
resolver?: T | Resolver<T>
|
||||||
|
): Promise<T | undefined> => {
|
||||||
if (typeof resolver === 'function') {
|
if (typeof resolver === 'function') {
|
||||||
return (resolver as Resolver<T>)(options);
|
return (resolver as Resolver<T>)(options);
|
||||||
}
|
}
|
||||||
return resolver;
|
return resolver;
|
||||||
};
|
};
|
||||||
|
|
||||||
const getHeaders = async (config: OpenAPIConfig, options: ApiRequestOptions): Promise<Headers> => {
|
const getHeaders = async (
|
||||||
|
config: OpenAPIConfig,
|
||||||
|
options: ApiRequestOptions
|
||||||
|
): Promise<Headers> => {
|
||||||
const token = await resolve(options, config.TOKEN);
|
const token = await resolve(options, config.TOKEN);
|
||||||
const username = await resolve(options, config.USERNAME);
|
const username = await resolve(options, config.USERNAME);
|
||||||
const password = await resolve(options, config.PASSWORD);
|
const password = await resolve(options, config.PASSWORD);
|
||||||
@ -144,13 +152,16 @@ const getHeaders = async (config: OpenAPIConfig, options: ApiRequestOptions): Pr
|
|||||||
const headers = Object.entries({
|
const headers = Object.entries({
|
||||||
Accept: 'application/json',
|
Accept: 'application/json',
|
||||||
...additionalHeaders,
|
...additionalHeaders,
|
||||||
...options.headers,
|
...options.headers
|
||||||
})
|
})
|
||||||
.filter(([_, value]) => isDefined(value))
|
.filter(([_, value]) => isDefined(value))
|
||||||
.reduce((headers, [key, value]) => ({
|
.reduce(
|
||||||
|
(headers, [key, value]) => ({
|
||||||
...headers,
|
...headers,
|
||||||
[key]: String(value),
|
[key]: String(value)
|
||||||
}), {} as Record<string, string>);
|
}),
|
||||||
|
{} as Record<string, string>
|
||||||
|
);
|
||||||
|
|
||||||
if (isStringWithValue(token)) {
|
if (isStringWithValue(token)) {
|
||||||
headers['Authorization'] = `Bearer ${token}`;
|
headers['Authorization'] = `Bearer ${token}`;
|
||||||
@ -179,8 +190,12 @@ const getHeaders = async (config: OpenAPIConfig, options: ApiRequestOptions): Pr
|
|||||||
const getRequestBody = (options: ApiRequestOptions): any => {
|
const getRequestBody = (options: ApiRequestOptions): any => {
|
||||||
if (options.body) {
|
if (options.body) {
|
||||||
if (options.mediaType?.includes('/json')) {
|
if (options.mediaType?.includes('/json')) {
|
||||||
return JSON.stringify(options.body)
|
return JSON.stringify(options.body);
|
||||||
} else if (isString(options.body) || isBlob(options.body) || isFormData(options.body)) {
|
} else if (
|
||||||
|
isString(options.body) ||
|
||||||
|
isBlob(options.body) ||
|
||||||
|
isFormData(options.body)
|
||||||
|
) {
|
||||||
return options.body;
|
return options.body;
|
||||||
} else {
|
} else {
|
||||||
return JSON.stringify(options.body);
|
return JSON.stringify(options.body);
|
||||||
@ -204,7 +219,7 @@ export const sendRequest = async (
|
|||||||
headers,
|
headers,
|
||||||
body: body ?? formData,
|
body: body ?? formData,
|
||||||
method: options.method,
|
method: options.method,
|
||||||
signal: controller.signal,
|
signal: controller.signal
|
||||||
};
|
};
|
||||||
|
|
||||||
if (config.WITH_CREDENTIALS) {
|
if (config.WITH_CREDENTIALS) {
|
||||||
@ -216,7 +231,10 @@ export const sendRequest = async (
|
|||||||
return await fetch(url, request);
|
return await fetch(url, request);
|
||||||
};
|
};
|
||||||
|
|
||||||
const getResponseHeader = (response: Response, responseHeader?: string): string | undefined => {
|
const getResponseHeader = (
|
||||||
|
response: Response,
|
||||||
|
responseHeader?: string
|
||||||
|
): string | undefined => {
|
||||||
if (responseHeader) {
|
if (responseHeader) {
|
||||||
const content = response.headers.get(responseHeader);
|
const content = response.headers.get(responseHeader);
|
||||||
if (isString(content)) {
|
if (isString(content)) {
|
||||||
@ -254,8 +272,8 @@ const catchErrorCodes = (options: ApiRequestOptions, result: ApiResult): void =>
|
|||||||
500: 'Internal Server Error',
|
500: 'Internal Server Error',
|
||||||
502: 'Bad Gateway',
|
502: 'Bad Gateway',
|
||||||
503: 'Service Unavailable',
|
503: 'Service Unavailable',
|
||||||
...options.errors,
|
...options.errors
|
||||||
}
|
};
|
||||||
|
|
||||||
const error = errors[result.status];
|
const error = errors[result.status];
|
||||||
if (error) {
|
if (error) {
|
||||||
@ -274,7 +292,10 @@ const catchErrorCodes = (options: ApiRequestOptions, result: ApiResult): void =>
|
|||||||
* @returns CancelablePromise<T>
|
* @returns CancelablePromise<T>
|
||||||
* @throws ApiError
|
* @throws ApiError
|
||||||
*/
|
*/
|
||||||
export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions): CancelablePromise<T> => {
|
export const request = <T>(
|
||||||
|
config: OpenAPIConfig,
|
||||||
|
options: ApiRequestOptions
|
||||||
|
): CancelablePromise<T> => {
|
||||||
return new CancelablePromise(async (resolve, reject, onCancel) => {
|
return new CancelablePromise(async (resolve, reject, onCancel) => {
|
||||||
try {
|
try {
|
||||||
const url = getUrl(config, options);
|
const url = getUrl(config, options);
|
||||||
@ -283,7 +304,15 @@ export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions): C
|
|||||||
const headers = await getHeaders(config, options);
|
const headers = await getHeaders(config, options);
|
||||||
|
|
||||||
if (!onCancel.isCancelled) {
|
if (!onCancel.isCancelled) {
|
||||||
const response = await sendRequest(config, options, url, body, formData, headers, onCancel);
|
const response = await sendRequest(
|
||||||
|
config,
|
||||||
|
options,
|
||||||
|
url,
|
||||||
|
body,
|
||||||
|
formData,
|
||||||
|
headers,
|
||||||
|
onCancel
|
||||||
|
);
|
||||||
const responseBody = await getResponseBody(response);
|
const responseBody = await getResponseBody(response);
|
||||||
const responseHeader = getResponseHeader(response, options.responseHeader);
|
const responseHeader = getResponseHeader(response, options.responseHeader);
|
||||||
|
|
||||||
@ -292,7 +321,7 @@ export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions): C
|
|||||||
ok: response.ok,
|
ok: response.ok,
|
||||||
status: response.status,
|
status: response.status,
|
||||||
statusText: response.statusText,
|
statusText: response.statusText,
|
||||||
body: responseHeader ?? responseBody,
|
body: responseHeader ?? responseBody
|
||||||
};
|
};
|
||||||
|
|
||||||
catchErrorCodes(options, result);
|
catchErrorCodes(options, result);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user