|
| 1 | +/* tslint:disable */ |
| 2 | +/* eslint-disable */ |
| 3 | + |
| 4 | +/* |
| 5 | + * --------------------------------------------------------------- |
| 6 | + * ## THIS FILE WAS GENERATED VIA SWAGGER-TYPESCRIPT-API ## |
| 7 | + * ## ## |
| 8 | + * ## AUTHOR: acacode ## |
| 9 | + * ## SOURCE: https://github.com/acacode/swagger-typescript-api ## |
| 10 | + * --------------------------------------------------------------- |
| 11 | + */ |
| 12 | + |
| 13 | +export type Test = XAB & { y?: string }; |
| 14 | + |
| 15 | +export type RequestParams = Omit<RequestInit, "body" | "method"> & { |
| 16 | + secure?: boolean; |
| 17 | +}; |
| 18 | + |
| 19 | +type ApiConfig<SecurityDataType> = { |
| 20 | + baseUrl?: string; |
| 21 | + baseApiParams?: RequestParams; |
| 22 | + securityWorker?: (securityData: SecurityDataType) => RequestParams; |
| 23 | +}; |
| 24 | + |
| 25 | +const enum BodyType { |
| 26 | + Json, |
| 27 | +} |
| 28 | + |
| 29 | +class HttpClient<SecurityDataType> { |
| 30 | + public baseUrl: string = ""; |
| 31 | + private securityData: SecurityDataType = null as any; |
| 32 | + private securityWorker: ApiConfig<SecurityDataType>["securityWorker"] = (() => {}) as any; |
| 33 | + |
| 34 | + private baseApiParams: RequestParams = { |
| 35 | + credentials: "same-origin", |
| 36 | + headers: { |
| 37 | + "Content-Type": "application/json", |
| 38 | + }, |
| 39 | + redirect: "follow", |
| 40 | + referrerPolicy: "no-referrer", |
| 41 | + }; |
| 42 | + |
| 43 | + constructor({ baseUrl, baseApiParams, securityWorker }: ApiConfig<SecurityDataType> = {}) { |
| 44 | + this.baseUrl = baseUrl || this.baseUrl; |
| 45 | + this.baseApiParams = baseApiParams || this.baseApiParams; |
| 46 | + this.securityWorker = securityWorker || this.securityWorker; |
| 47 | + } |
| 48 | + |
| 49 | + public setSecurityData = (data: SecurityDataType) => { |
| 50 | + this.securityData = data; |
| 51 | + }; |
| 52 | + |
| 53 | + private bodyFormatters: Record<BodyType, (input: any) => any> = { |
| 54 | + [BodyType.Json]: JSON.stringify, |
| 55 | + }; |
| 56 | + |
| 57 | + private mergeRequestOptions(params: RequestParams, securityParams?: RequestParams): RequestParams { |
| 58 | + return { |
| 59 | + ...this.baseApiParams, |
| 60 | + ...params, |
| 61 | + ...(securityParams || {}), |
| 62 | + headers: { |
| 63 | + ...(this.baseApiParams.headers || {}), |
| 64 | + ...(params.headers || {}), |
| 65 | + ...((securityParams && securityParams.headers) || {}), |
| 66 | + }, |
| 67 | + }; |
| 68 | + } |
| 69 | + |
| 70 | + private safeParseResponse = <T = any, E = any>(response: Response): Promise<T> => |
| 71 | + response |
| 72 | + .json() |
| 73 | + .then((data) => data) |
| 74 | + .catch((e) => response.text); |
| 75 | + |
| 76 | + public request = <T = any, E = any>( |
| 77 | + path: string, |
| 78 | + method: string, |
| 79 | + { secure, ...params }: RequestParams = {}, |
| 80 | + body?: any, |
| 81 | + bodyType?: BodyType, |
| 82 | + secureByDefault?: boolean, |
| 83 | + ): Promise<T> => |
| 84 | + fetch(`${this.baseUrl}${path}`, { |
| 85 | + // @ts-ignore |
| 86 | + ...this.mergeRequestOptions(params, (secureByDefault || secure) && this.securityWorker(this.securityData)), |
| 87 | + method, |
| 88 | + body: body ? this.bodyFormatters[bodyType || BodyType.Json](body) : null, |
| 89 | + }).then(async (response) => { |
| 90 | + const data = await this.safeParseResponse<T, E>(response); |
| 91 | + if (!response.ok) throw data; |
| 92 | + return data; |
| 93 | + }); |
| 94 | +} |
| 95 | + |
| 96 | +/** |
| 97 | + * @title Test |
| 98 | + * @version test |
| 99 | + */ |
| 100 | +export class Api<SecurityDataType = any> extends HttpClient<SecurityDataType> {} |
0 commit comments