mirror of
https://github.com/Monadical-SAS/reflector.git
synced 2025-12-21 04:39:06 +00:00
Migrate to the latest openapi-ts
This commit is contained in:
@@ -1,8 +1,28 @@
|
||||
import type { AxiosRequestConfig, AxiosResponse } from "axios";
|
||||
import type { ApiRequestOptions } from "./ApiRequestOptions";
|
||||
import type { TConfig, TResult } from "./types";
|
||||
|
||||
type Resolver<T> = (options: ApiRequestOptions) => Promise<T>;
|
||||
type Headers = Record<string, string>;
|
||||
type Middleware<T> = (value: T) => T | Promise<T>;
|
||||
type Resolver<T> = (options: ApiRequestOptions<T>) => Promise<T>;
|
||||
|
||||
export class Interceptors<T> {
|
||||
_fns: Middleware<T>[];
|
||||
|
||||
constructor() {
|
||||
this._fns = [];
|
||||
}
|
||||
|
||||
eject(fn: Middleware<T>): void {
|
||||
const index = this._fns.indexOf(fn);
|
||||
if (index !== -1) {
|
||||
this._fns = [...this._fns.slice(0, index), ...this._fns.slice(index + 1)];
|
||||
}
|
||||
}
|
||||
|
||||
use(fn: Middleware<T>): void {
|
||||
this._fns = [...this._fns, fn];
|
||||
}
|
||||
}
|
||||
|
||||
export type OpenAPIConfig = {
|
||||
BASE: string;
|
||||
@@ -10,11 +30,14 @@ export type OpenAPIConfig = {
|
||||
ENCODE_PATH?: ((path: string) => string) | undefined;
|
||||
HEADERS?: Headers | Resolver<Headers> | undefined;
|
||||
PASSWORD?: string | Resolver<string> | undefined;
|
||||
RESULT?: TResult;
|
||||
TOKEN?: string | Resolver<string> | undefined;
|
||||
USERNAME?: string | Resolver<string> | undefined;
|
||||
VERSION: string;
|
||||
WITH_CREDENTIALS: boolean;
|
||||
interceptors: {
|
||||
request: Interceptors<AxiosRequestConfig>;
|
||||
response: Interceptors<AxiosResponse>;
|
||||
};
|
||||
};
|
||||
|
||||
export const OpenAPI: OpenAPIConfig = {
|
||||
@@ -23,26 +46,12 @@ export const OpenAPI: OpenAPIConfig = {
|
||||
ENCODE_PATH: undefined,
|
||||
HEADERS: undefined,
|
||||
PASSWORD: undefined,
|
||||
RESULT: "body",
|
||||
TOKEN: undefined,
|
||||
USERNAME: undefined,
|
||||
VERSION: "0.1.0",
|
||||
WITH_CREDENTIALS: false,
|
||||
};
|
||||
|
||||
export const mergeOpenApiConfig = <T extends TResult>(
|
||||
config: OpenAPIConfig,
|
||||
overrides: TConfig<T>,
|
||||
) => {
|
||||
const merged = { ...config };
|
||||
Object.entries(overrides)
|
||||
.filter(([key]) => key.startsWith("_"))
|
||||
.forEach(([key, value]) => {
|
||||
const k = key.slice(1).toLocaleUpperCase() as keyof typeof merged;
|
||||
if (merged.hasOwnProperty(k)) {
|
||||
// @ts-ignore
|
||||
merged[k] = value;
|
||||
}
|
||||
});
|
||||
return merged;
|
||||
interceptors: {
|
||||
request: new Interceptors(),
|
||||
response: new Interceptors(),
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user