|
| 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 | + |
| 14 | +export interface PetByAge { |
| 15 | + age?: number; |
| 16 | + nickname?: string; |
| 17 | +} |
| 18 | + |
| 19 | +export interface PetByType { |
| 20 | + pet_type?: "Cat" | "Dog"; |
| 21 | + hunts?: boolean; |
| 22 | +} |
| 23 | + |
| 24 | +export type RequestParams = Omit<RequestInit, "body" | "method"> & { |
| 25 | + secure?: boolean; |
| 26 | +} |
| 27 | + |
| 28 | +type ApiConfig<SecurityDataType> = { |
| 29 | + baseUrl?: string, |
| 30 | + baseApiParams?: RequestParams, |
| 31 | + securityWorker?: (securityData: SecurityDataType) => RequestParams, |
| 32 | +} |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | +export class Api<SecurityDataType> { |
| 37 | + |
| 38 | + public baseUrl = ""; |
| 39 | + public title = "Anyof Example"; |
| 40 | + public version = "1.0.0"; |
| 41 | + |
| 42 | + private securityData: SecurityDataType = (null as any); |
| 43 | + private securityWorker: ApiConfig<SecurityDataType>["securityWorker"] = (() => {}) as any |
| 44 | + |
| 45 | + private baseApiParams: RequestParams = { |
| 46 | + credentials: 'same-origin', |
| 47 | + headers: { |
| 48 | + 'Content-Type': 'application/json' |
| 49 | + }, |
| 50 | + redirect: 'follow', |
| 51 | + referrerPolicy: 'no-referrer', |
| 52 | + } |
| 53 | + |
| 54 | + constructor({ baseUrl,baseApiParams,securityWorker, }: ApiConfig<SecurityDataType> = {}) { |
| 55 | + this.baseUrl = baseUrl || this.baseUrl; |
| 56 | + this.baseApiParams = baseApiParams || this.baseApiParams; |
| 57 | + this.securityWorker = securityWorker || this.securityWorker; |
| 58 | + } |
| 59 | + |
| 60 | + public setSecurityData = (data: SecurityDataType) => { |
| 61 | + this.securityData = data |
| 62 | + } |
| 63 | + |
| 64 | + |
| 65 | + private mergeRequestOptions(params: RequestParams, securityParams?: RequestParams): RequestParams { |
| 66 | + return { |
| 67 | + ...this.baseApiParams, |
| 68 | + ...params, |
| 69 | + ...(securityParams || {}), |
| 70 | + headers: { |
| 71 | + ...(this.baseApiParams.headers || {}), |
| 72 | + ...(params.headers || {}), |
| 73 | + ...((securityParams && securityParams.headers) || {}) |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + private safeParseResponse = <T = any>(response: Response): Promise<T> => |
| 79 | + response.json() |
| 80 | + .then(data => data) |
| 81 | + .catch(e => response.text); |
| 82 | + |
| 83 | + public request = <T = any>( |
| 84 | + path: string, |
| 85 | + method: string, |
| 86 | + { secure, ...params }: RequestParams = {}, |
| 87 | + body?: any, |
| 88 | + secureByDefault?: boolean, |
| 89 | + ): Promise<T> => |
| 90 | + fetch(`${this.baseUrl}${path}`, { |
| 91 | + // @ts-ignore |
| 92 | + ...this.mergeRequestOptions(params, (secureByDefault || secure) && this.securityWorker(this.securityData)), |
| 93 | + method, |
| 94 | + body: body ? JSON.stringify(body) : null, |
| 95 | + }).then(async response => { |
| 96 | + const data = await this.safeParseResponse<T>(response); |
| 97 | + if (!response.ok) throw data |
| 98 | + return data |
| 99 | + }) |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | + pets = { |
| 104 | + |
| 105 | + |
| 106 | + /** |
| 107 | + * @name patch |
| 108 | + * @request PATCH:/pets |
| 109 | + */ |
| 110 | + patch: (data: PetByAge | PetByType | (PetByAge & PetByType), params?: RequestParams) => |
| 111 | + this.request<any>(`/pets`, "PATCH", params, data), |
| 112 | + } |
| 113 | + |
| 114 | +} |
0 commit comments