|
| 1 | +/* eslint-disable */ |
| 2 | +/* tslint:disable */ |
| 3 | +/* |
| 4 | + * --------------------------------------------------------------- |
| 5 | + * ## THIS FILE WAS GENERATED VIA SWAGGER-TYPESCRIPT-API ## |
| 6 | + * ## ## |
| 7 | + * ## AUTHOR: acacode ## |
| 8 | + * ## SOURCE: https://github.com/acacode/swagger-typescript-api ## |
| 9 | + * --------------------------------------------------------------- |
| 10 | + */ |
| 11 | + |
| 12 | +export type QueryParamsType = Record<string | number, any>; |
| 13 | +export type ResponseFormat = keyof Omit<Body, "body" | "bodyUsed">; |
| 14 | + |
| 15 | +export interface FullRequestParams extends Omit<RequestInit, "body"> { |
| 16 | + /** set parameter to `true` for call `securityWorker` for this request */ |
| 17 | + secure?: boolean; |
| 18 | + /** request path */ |
| 19 | + path: string; |
| 20 | + /** content type of request body */ |
| 21 | + type?: ContentType; |
| 22 | + /** query params */ |
| 23 | + query?: QueryParamsType; |
| 24 | + /** format of response (i.e. response.json() -> format: "json") */ |
| 25 | + format?: ResponseFormat; |
| 26 | + /** request body */ |
| 27 | + body?: unknown; |
| 28 | + /** base url */ |
| 29 | + baseUrl?: string; |
| 30 | + /** request cancellation token */ |
| 31 | + cancelToken?: CancelToken; |
| 32 | +} |
| 33 | + |
| 34 | +export type RequestParams = Omit<FullRequestParams, "body" | "method" | "query" | "path">; |
| 35 | + |
| 36 | +export interface ApiConfig<SecurityDataType = unknown> { |
| 37 | + baseUrl?: string; |
| 38 | + baseApiParams?: Omit<RequestParams, "baseUrl" | "cancelToken" | "signal">; |
| 39 | + securityWorker?: (securityData: SecurityDataType | null) => Promise<RequestParams | void> | RequestParams | void; |
| 40 | + customFetch?: typeof fetch; |
| 41 | +} |
| 42 | + |
| 43 | +export interface HttpResponse<D extends unknown, E extends unknown = unknown> extends Response { |
| 44 | + data: D; |
| 45 | + error: E; |
| 46 | +} |
| 47 | + |
| 48 | +type CancelToken = Symbol | string | number; |
| 49 | + |
| 50 | +export enum ContentType { |
| 51 | + Json = "application/json", |
| 52 | + FormData = "multipart/form-data", |
| 53 | + UrlEncoded = "application/x-www-form-urlencoded", |
| 54 | + Text = "text/plain", |
| 55 | +} |
| 56 | + |
| 57 | +export class HttpClient<SecurityDataType = unknown> { |
| 58 | + public baseUrl: string = "http://petstore.swagger.io/api"; |
| 59 | + private securityData: SecurityDataType | null = null; |
| 60 | + private securityWorker?: ApiConfig<SecurityDataType>["securityWorker"]; |
| 61 | + private abortControllers = new Map<CancelToken, AbortController>(); |
| 62 | + private customFetch = (...fetchParams: Parameters<typeof fetch>) => fetch(...fetchParams); |
| 63 | + |
| 64 | + private baseApiParams: RequestParams = { |
| 65 | + credentials: "same-origin", |
| 66 | + headers: {}, |
| 67 | + redirect: "follow", |
| 68 | + referrerPolicy: "no-referrer", |
| 69 | + }; |
| 70 | + |
| 71 | + constructor(apiConfig: ApiConfig<SecurityDataType> = {}) { |
| 72 | + Object.assign(this, apiConfig); |
| 73 | + } |
| 74 | + |
| 75 | + public setSecurityData = (data: SecurityDataType | null) => { |
| 76 | + this.securityData = data; |
| 77 | + }; |
| 78 | + |
| 79 | + protected encodeQueryParam(key: string, value: any) { |
| 80 | + const encodedKey = encodeURIComponent(key); |
| 81 | + return `${encodedKey}=${encodeURIComponent(typeof value === "number" ? value : `${value}`)}`; |
| 82 | + } |
| 83 | + |
| 84 | + protected addQueryParam(query: QueryParamsType, key: string) { |
| 85 | + return this.encodeQueryParam(key, query[key]); |
| 86 | + } |
| 87 | + |
| 88 | + protected addArrayQueryParam(query: QueryParamsType, key: string) { |
| 89 | + const value = query[key]; |
| 90 | + return value.map((v: any) => this.encodeQueryParam(key, v)).join("&"); |
| 91 | + } |
| 92 | + |
| 93 | + protected toQueryString(rawQuery?: QueryParamsType): string { |
| 94 | + const query = rawQuery || {}; |
| 95 | + const keys = Object.keys(query).filter((key) => "undefined" !== typeof query[key]); |
| 96 | + return keys |
| 97 | + .map((key) => (Array.isArray(query[key]) ? this.addArrayQueryParam(query, key) : this.addQueryParam(query, key))) |
| 98 | + .join("&"); |
| 99 | + } |
| 100 | + |
| 101 | + protected addQueryParams(rawQuery?: QueryParamsType): string { |
| 102 | + const queryString = this.toQueryString(rawQuery); |
| 103 | + return queryString ? `?${queryString}` : ""; |
| 104 | + } |
| 105 | + |
| 106 | + private contentFormatters: Record<ContentType, (input: any) => any> = { |
| 107 | + [ContentType.Json]: (input: any) => |
| 108 | + input !== null && (typeof input === "object" || typeof input === "string") ? JSON.stringify(input) : input, |
| 109 | + [ContentType.Text]: (input: any) => (input !== null && typeof input !== "string" ? JSON.stringify(input) : input), |
| 110 | + [ContentType.FormData]: (input: any) => |
| 111 | + Object.keys(input || {}).reduce((formData, key) => { |
| 112 | + const property = input[key]; |
| 113 | + formData.append( |
| 114 | + key, |
| 115 | + property instanceof Blob |
| 116 | + ? property |
| 117 | + : typeof property === "object" && property !== null |
| 118 | + ? JSON.stringify(property) |
| 119 | + : `${property}`, |
| 120 | + ); |
| 121 | + return formData; |
| 122 | + }, new FormData()), |
| 123 | + [ContentType.UrlEncoded]: (input: any) => this.toQueryString(input), |
| 124 | + }; |
| 125 | + |
| 126 | + protected mergeRequestParams(params1: RequestParams, params2?: RequestParams): RequestParams { |
| 127 | + return { |
| 128 | + ...this.baseApiParams, |
| 129 | + ...params1, |
| 130 | + ...(params2 || {}), |
| 131 | + headers: { |
| 132 | + ...(this.baseApiParams.headers || {}), |
| 133 | + ...(params1.headers || {}), |
| 134 | + ...((params2 && params2.headers) || {}), |
| 135 | + }, |
| 136 | + }; |
| 137 | + } |
| 138 | + |
| 139 | + protected createAbortSignal = (cancelToken: CancelToken): AbortSignal | undefined => { |
| 140 | + if (this.abortControllers.has(cancelToken)) { |
| 141 | + const abortController = this.abortControllers.get(cancelToken); |
| 142 | + if (abortController) { |
| 143 | + return abortController.signal; |
| 144 | + } |
| 145 | + return void 0; |
| 146 | + } |
| 147 | + |
| 148 | + const abortController = new AbortController(); |
| 149 | + this.abortControllers.set(cancelToken, abortController); |
| 150 | + return abortController.signal; |
| 151 | + }; |
| 152 | + |
| 153 | + public abortRequest = (cancelToken: CancelToken) => { |
| 154 | + const abortController = this.abortControllers.get(cancelToken); |
| 155 | + |
| 156 | + if (abortController) { |
| 157 | + abortController.abort(); |
| 158 | + this.abortControllers.delete(cancelToken); |
| 159 | + } |
| 160 | + }; |
| 161 | + |
| 162 | + public request = async <T = any, E = any>({ |
| 163 | + body, |
| 164 | + secure, |
| 165 | + path, |
| 166 | + type, |
| 167 | + query, |
| 168 | + format, |
| 169 | + baseUrl, |
| 170 | + cancelToken, |
| 171 | + ...params |
| 172 | + }: FullRequestParams): Promise<HttpResponse<T, E>> => { |
| 173 | + const secureParams = |
| 174 | + ((typeof secure === "boolean" ? secure : this.baseApiParams.secure) && |
| 175 | + this.securityWorker && |
| 176 | + (await this.securityWorker(this.securityData))) || |
| 177 | + {}; |
| 178 | + const requestParams = this.mergeRequestParams(params, secureParams); |
| 179 | + const queryString = query && this.toQueryString(query); |
| 180 | + const payloadFormatter = this.contentFormatters[type || ContentType.Json]; |
| 181 | + const responseFormat = format || requestParams.format; |
| 182 | + |
| 183 | + return this.customFetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { |
| 184 | + ...requestParams, |
| 185 | + headers: { |
| 186 | + ...(requestParams.headers || {}), |
| 187 | + ...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}), |
| 188 | + }, |
| 189 | + signal: cancelToken ? this.createAbortSignal(cancelToken) : requestParams.signal, |
| 190 | + body: typeof body === "undefined" || body === null ? null : payloadFormatter(body), |
| 191 | + }).then(async (response) => { |
| 192 | + const r = response as HttpResponse<T, E>; |
| 193 | + r.data = null as unknown as T; |
| 194 | + r.error = null as unknown as E; |
| 195 | + |
| 196 | + const data = !responseFormat |
| 197 | + ? r |
| 198 | + : await response[responseFormat]() |
| 199 | + .then((data) => { |
| 200 | + if (r.ok) { |
| 201 | + r.data = data; |
| 202 | + } else { |
| 203 | + r.error = data; |
| 204 | + } |
| 205 | + return r; |
| 206 | + }) |
| 207 | + .catch((e) => { |
| 208 | + r.error = e; |
| 209 | + return r; |
| 210 | + }); |
| 211 | + |
| 212 | + if (cancelToken) { |
| 213 | + this.abortControllers.delete(cancelToken); |
| 214 | + } |
| 215 | + |
| 216 | + if (!response.ok) throw data; |
| 217 | + return data; |
| 218 | + }); |
| 219 | + }; |
| 220 | +} |
| 221 | + |
| 222 | +/** |
| 223 | + * @title Swagger Petstore |
| 224 | + * @version 1.0.0 |
| 225 | + * @license MIT |
| 226 | + * @termsOfService http://swagger.io/terms/ |
| 227 | + * @baseUrl http://petstore.swagger.io/api |
| 228 | + * @contact Swagger API Team |
| 229 | + * |
| 230 | + * A sample API that uses a petstore as an example to demonstrate features in the swagger-2.0 specification |
| 231 | + */ |
| 232 | +export class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDataType> { |
| 233 | + wrongPathParams1 = { |
| 234 | + /** |
| 235 | + * @description DDD |
| 236 | + * |
| 237 | + * @tags key, delete |
| 238 | + * @name WrongPathParams1 |
| 239 | + * @request DELETE:/wrong-path-params1/{pathParam1}/{path_param2}/{path_param3}/:pathParam4 |
| 240 | + */ |
| 241 | + wrongPathParams1: ( |
| 242 | + pathParam1: string, |
| 243 | + pathParam2: string, |
| 244 | + pathParam3: string, |
| 245 | + pathParam4: string, |
| 246 | + params: RequestParams = {}, |
| 247 | + ) => |
| 248 | + this.request<void, any>({ |
| 249 | + path: `/wrong-path-params1/${encodeURIComponent(pathParam1)}/${encodeURIComponent( |
| 250 | + pathParam2, |
| 251 | + )}/${encodeURIComponent(pathParam3)}/${encodeURIComponent(pathParam4)}`, |
| 252 | + method: "DELETE", |
| 253 | + ...params, |
| 254 | + }), |
| 255 | + }; |
| 256 | +} |
0 commit comments