|
| 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 interface Bar { |
| 13 | + A?: string; |
| 14 | + |
| 15 | + /** @format int32 */ |
| 16 | + B: number; |
| 17 | + |
| 18 | + /** @format date-time */ |
| 19 | + C: string; |
| 20 | + Baz?: Baz; |
| 21 | +} |
| 22 | + |
| 23 | +export interface Baz { |
| 24 | + /** @format decimal */ |
| 25 | + D: number; |
| 26 | + Color: Color; |
| 27 | +} |
| 28 | + |
| 29 | +export enum Color { |
| 30 | + RED = 0, |
| 31 | + GREEN = 1, |
| 32 | + BLUE = 2, |
| 33 | +} |
| 34 | + |
| 35 | +export type QueryParamsType = Record<string | number, any>; |
| 36 | +export type ResponseFormat = keyof Omit<Body, "body" | "bodyUsed">; |
| 37 | + |
| 38 | +export interface FullRequestParams extends Omit<RequestInit, "body"> { |
| 39 | + /** set parameter to `true` for call `securityWorker` for this request */ |
| 40 | + secure?: boolean; |
| 41 | + /** request path */ |
| 42 | + path: string; |
| 43 | + /** content type of request body */ |
| 44 | + type?: ContentType; |
| 45 | + /** query params */ |
| 46 | + query?: QueryParamsType; |
| 47 | + /** format of response (i.e. response.json() -> format: "json") */ |
| 48 | + format?: keyof Omit<Body, "body" | "bodyUsed">; |
| 49 | + /** request body */ |
| 50 | + body?: unknown; |
| 51 | + /** base url */ |
| 52 | + baseUrl?: string; |
| 53 | + /** request cancellation token */ |
| 54 | + cancelToken?: CancelToken; |
| 55 | +} |
| 56 | + |
| 57 | +export type RequestParams = Omit<FullRequestParams, "body" | "method" | "query" | "path">; |
| 58 | + |
| 59 | +export interface ApiConfig<SecurityDataType = unknown> { |
| 60 | + baseUrl?: string; |
| 61 | + baseApiParams?: Omit<RequestParams, "baseUrl" | "cancelToken" | "signal">; |
| 62 | + securityWorker?: (securityData: SecurityDataType) => RequestParams | void; |
| 63 | +} |
| 64 | + |
| 65 | +export interface HttpResponse<D extends unknown, E extends unknown = unknown> extends Response { |
| 66 | + data: D; |
| 67 | + error: E; |
| 68 | +} |
| 69 | + |
| 70 | +type CancelToken = Symbol | string | number; |
| 71 | + |
| 72 | +export enum ContentType { |
| 73 | + Json = "application/json", |
| 74 | + FormData = "multipart/form-data", |
| 75 | + UrlEncoded = "application/x-www-form-urlencoded", |
| 76 | +} |
| 77 | + |
| 78 | +export class HttpClient<SecurityDataType = unknown> { |
| 79 | + public baseUrl: string = ""; |
| 80 | + private securityData: SecurityDataType = null as any; |
| 81 | + private securityWorker: null | ApiConfig<SecurityDataType>["securityWorker"] = null; |
| 82 | + private abortControllers = new Map<CancelToken, AbortController>(); |
| 83 | + |
| 84 | + private baseApiParams: RequestParams = { |
| 85 | + credentials: "same-origin", |
| 86 | + headers: {}, |
| 87 | + redirect: "follow", |
| 88 | + referrerPolicy: "no-referrer", |
| 89 | + }; |
| 90 | + |
| 91 | + constructor(apiConfig: ApiConfig<SecurityDataType> = {}) { |
| 92 | + Object.assign(this, apiConfig); |
| 93 | + } |
| 94 | + |
| 95 | + public setSecurityData = (data: SecurityDataType) => { |
| 96 | + this.securityData = data; |
| 97 | + }; |
| 98 | + |
| 99 | + private addQueryParam(query: QueryParamsType, key: string) { |
| 100 | + const value = query[key]; |
| 101 | + |
| 102 | + return ( |
| 103 | + encodeURIComponent(key) + |
| 104 | + "=" + |
| 105 | + encodeURIComponent(Array.isArray(value) ? value.join(",") : typeof value === "number" ? value : `${value}`) |
| 106 | + ); |
| 107 | + } |
| 108 | + |
| 109 | + protected toQueryString(rawQuery?: QueryParamsType): string { |
| 110 | + const query = rawQuery || {}; |
| 111 | + const keys = Object.keys(query).filter((key) => "undefined" !== typeof query[key]); |
| 112 | + return keys |
| 113 | + .map((key) => |
| 114 | + typeof query[key] === "object" && !Array.isArray(query[key]) |
| 115 | + ? this.toQueryString(query[key] as QueryParamsType) |
| 116 | + : this.addQueryParam(query, key), |
| 117 | + ) |
| 118 | + .join("&"); |
| 119 | + } |
| 120 | + |
| 121 | + protected addQueryParams(rawQuery?: QueryParamsType): string { |
| 122 | + const queryString = this.toQueryString(rawQuery); |
| 123 | + return queryString ? `?${queryString}` : ""; |
| 124 | + } |
| 125 | + |
| 126 | + private contentFormatters: Record<ContentType, (input: any) => any> = { |
| 127 | + [ContentType.Json]: (input: any) => (input !== null && typeof input === "object" ? JSON.stringify(input) : input), |
| 128 | + [ContentType.FormData]: (input: any) => |
| 129 | + Object.keys(input || {}).reduce((data, key) => { |
| 130 | + data.append(key, input[key]); |
| 131 | + return data; |
| 132 | + }, new FormData()), |
| 133 | + [ContentType.UrlEncoded]: (input: any) => this.toQueryString(input), |
| 134 | + }; |
| 135 | + |
| 136 | + private mergeRequestParams(params1: RequestParams, params2?: RequestParams): RequestParams { |
| 137 | + return { |
| 138 | + ...this.baseApiParams, |
| 139 | + ...params1, |
| 140 | + ...(params2 || {}), |
| 141 | + headers: { |
| 142 | + ...(this.baseApiParams.headers || {}), |
| 143 | + ...(params1.headers || {}), |
| 144 | + ...((params2 && params2.headers) || {}), |
| 145 | + }, |
| 146 | + }; |
| 147 | + } |
| 148 | + |
| 149 | + private createAbortSignal = (cancelToken: CancelToken): AbortSignal | undefined => { |
| 150 | + if (this.abortControllers.has(cancelToken)) { |
| 151 | + const abortController = this.abortControllers.get(cancelToken); |
| 152 | + if (abortController) { |
| 153 | + return abortController.signal; |
| 154 | + } |
| 155 | + return void 0; |
| 156 | + } |
| 157 | + |
| 158 | + const abortController = new AbortController(); |
| 159 | + this.abortControllers.set(cancelToken, abortController); |
| 160 | + return abortController.signal; |
| 161 | + }; |
| 162 | + |
| 163 | + public abortRequest = (cancelToken: CancelToken) => { |
| 164 | + const abortController = this.abortControllers.get(cancelToken); |
| 165 | + |
| 166 | + if (abortController) { |
| 167 | + abortController.abort(); |
| 168 | + this.abortControllers.delete(cancelToken); |
| 169 | + } |
| 170 | + }; |
| 171 | + |
| 172 | + public request = <T = any, E = any>({ |
| 173 | + body, |
| 174 | + secure, |
| 175 | + path, |
| 176 | + type, |
| 177 | + query, |
| 178 | + format = "json", |
| 179 | + baseUrl, |
| 180 | + cancelToken, |
| 181 | + ...params |
| 182 | + }: FullRequestParams): Promise<HttpResponse<T, E>> => { |
| 183 | + const secureParams = (secure && this.securityWorker && this.securityWorker(this.securityData)) || {}; |
| 184 | + const requestParams = this.mergeRequestParams(params, secureParams); |
| 185 | + const queryString = query && this.toQueryString(query); |
| 186 | + const payloadFormatter = this.contentFormatters[type || ContentType.Json]; |
| 187 | + |
| 188 | + return fetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { |
| 189 | + headers: { |
| 190 | + ...(type ? { "Content-Type": type } : {}), |
| 191 | + ...(requestParams.headers || {}), |
| 192 | + }, |
| 193 | + ...requestParams, |
| 194 | + signal: cancelToken ? this.createAbortSignal(cancelToken) : void 0, |
| 195 | + body: typeof body === "undefined" || body === null ? null : payloadFormatter(body), |
| 196 | + }).then(async (response) => { |
| 197 | + const r = response as HttpResponse<T, E>; |
| 198 | + r.data = (null as unknown) as T; |
| 199 | + r.error = (null as unknown) as E; |
| 200 | + |
| 201 | + const data = await response[format]() |
| 202 | + .then((data) => { |
| 203 | + if (r.ok) { |
| 204 | + r.data = data; |
| 205 | + } else { |
| 206 | + r.error = data; |
| 207 | + } |
| 208 | + return r; |
| 209 | + }) |
| 210 | + .catch((e) => { |
| 211 | + r.error = e; |
| 212 | + return r; |
| 213 | + }); |
| 214 | + |
| 215 | + if (cancelToken) { |
| 216 | + this.abortControllers.delete(cancelToken); |
| 217 | + } |
| 218 | + |
| 219 | + if (!response.ok) throw data; |
| 220 | + return data; |
| 221 | + }); |
| 222 | + }; |
| 223 | +} |
| 224 | + |
| 225 | +/** |
| 226 | + * @title No title |
| 227 | + */ |
| 228 | +export class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDataType> { |
| 229 | + api = { |
| 230 | + /** |
| 231 | + * No description |
| 232 | + * |
| 233 | + * @tags Foo |
| 234 | + * @name FooGetBarDescriptions |
| 235 | + * @request GET:/api/Foo/GetBarDescriptions |
| 236 | + */ |
| 237 | + fooGetBarDescriptions: (params: RequestParams = {}) => |
| 238 | + this.request<string[], any>({ |
| 239 | + path: `/api/Foo/GetBarDescriptions`, |
| 240 | + method: "GET", |
| 241 | + format: "json", |
| 242 | + ...params, |
| 243 | + }), |
| 244 | + |
| 245 | + /** |
| 246 | + * No description |
| 247 | + * |
| 248 | + * @tags Foo |
| 249 | + * @name FooGetBar |
| 250 | + * @request GET:/api/Foo/GetBar |
| 251 | + */ |
| 252 | + fooGetBar: (query: { id: number }, params: RequestParams = {}) => |
| 253 | + this.request<Bar, any>({ |
| 254 | + path: `/api/Foo/GetBar`, |
| 255 | + method: "GET", |
| 256 | + query: query, |
| 257 | + format: "json", |
| 258 | + ...params, |
| 259 | + }), |
| 260 | + |
| 261 | + /** |
| 262 | + * No description |
| 263 | + * |
| 264 | + * @tags Foo |
| 265 | + * @name FooSetBar |
| 266 | + * @request POST:/api/Foo/SetBar |
| 267 | + */ |
| 268 | + fooSetBar: (value: Bar, params: RequestParams = {}) => |
| 269 | + this.request<void, any>({ |
| 270 | + path: `/api/Foo/SetBar`, |
| 271 | + method: "POST", |
| 272 | + body: value, |
| 273 | + type: ContentType.Json, |
| 274 | + ...params, |
| 275 | + }), |
| 276 | + }; |
| 277 | +} |
0 commit comments