|
| 1 | +// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html |
| 2 | + |
| 3 | +exports[`operationId starting with number > should handle operationIds that start with numbers 1`] = ` |
| 4 | +"/* eslint-disable */ |
| 5 | +/* tslint:disable */ |
| 6 | +// @ts-nocheck |
| 7 | +/* |
| 8 | + * --------------------------------------------------------------- |
| 9 | + * ## THIS FILE WAS GENERATED VIA SWAGGER-TYPESCRIPT-API ## |
| 10 | + * ## ## |
| 11 | + * ## AUTHOR: acacode ## |
| 12 | + * ## SOURCE: https://github.com/acacode/swagger-typescript-api ## |
| 13 | + * --------------------------------------------------------------- |
| 14 | + */ |
| 15 | +
|
| 16 | +
|
| 17 | +
|
| 18 | +
|
| 19 | +
|
| 20 | +
|
| 21 | +
|
| 22 | + |
| 23 | + export namespace Users { |
| 24 | + |
| 25 | +/** |
| 26 | + * No description |
| 27 | + * @name 123GetUser |
| 28 | + * @summary Get user |
| 29 | + * @request GET:/users |
| 30 | +*/ |
| 31 | +export namespace "123GetUser" { |
| 32 | + export type RequestParams = {}; |
| 33 | + export type RequestQuery = {}; |
| 34 | + export type RequestBody = never; |
| 35 | + export type RequestHeaders = {}; |
| 36 | + export type ResponseBody = { |
| 37 | + id?: number, |
| 38 | + name?: string, |
| 39 | +
|
| 40 | +}; |
| 41 | +} |
| 42 | + } |
| 43 | +
|
| 44 | + export namespace Posts { |
| 45 | + |
| 46 | +/** |
| 47 | + * No description |
| 48 | + * @name 456CreatePost |
| 49 | + * @summary Create post |
| 50 | + * @request POST:/posts |
| 51 | +*/ |
| 52 | +export namespace "456CreatePost" { |
| 53 | + export type RequestParams = {}; |
| 54 | + export type RequestQuery = {}; |
| 55 | + export type RequestBody = { |
| 56 | + title?: string, |
| 57 | + content?: string, |
| 58 | +
|
| 59 | +}; |
| 60 | + export type RequestHeaders = {}; |
| 61 | + export type ResponseBody = { |
| 62 | + id?: number, |
| 63 | + title?: string, |
| 64 | +
|
| 65 | +}; |
| 66 | +} |
| 67 | + } |
| 68 | +
|
| 69 | + |
| 70 | +
|
| 71 | +export type QueryParamsType = Record<string | number, any>; |
| 72 | +export type ResponseFormat = keyof Omit<Body, "body" | "bodyUsed">; |
| 73 | +
|
| 74 | +export interface FullRequestParams extends Omit<RequestInit, "body"> { |
| 75 | + /** set parameter to \`true\` for call \`securityWorker\` for this request */ |
| 76 | + secure?: boolean; |
| 77 | + /** request path */ |
| 78 | + path: string; |
| 79 | + /** content type of request body */ |
| 80 | + type?: ContentType; |
| 81 | + /** query params */ |
| 82 | + query?: QueryParamsType; |
| 83 | + /** format of response (i.e. response.json() -> format: "json") */ |
| 84 | + format?: ResponseFormat; |
| 85 | + /** request body */ |
| 86 | + body?: unknown; |
| 87 | + /** base url */ |
| 88 | + baseUrl?: string; |
| 89 | + /** request cancellation token */ |
| 90 | + cancelToken?: CancelToken; |
| 91 | +} |
| 92 | +
|
| 93 | +export type RequestParams = Omit<FullRequestParams, "body" | "method" | "query" | "path"> |
| 94 | +
|
| 95 | +
|
| 96 | +export interface ApiConfig<SecurityDataType = unknown> { |
| 97 | + baseUrl?: string; |
| 98 | + baseApiParams?: Omit<RequestParams, "baseUrl" | "cancelToken" | "signal">; |
| 99 | + securityWorker?: (securityData: SecurityDataType | null) => Promise<RequestParams | void> | RequestParams | void; |
| 100 | + customFetch?: typeof fetch; |
| 101 | +} |
| 102 | +
|
| 103 | +export interface HttpResponse<D extends unknown, E extends unknown = unknown> extends Response { |
| 104 | + data: D; |
| 105 | + error: E; |
| 106 | +} |
| 107 | +
|
| 108 | +type CancelToken = Symbol | string | number; |
| 109 | +
|
| 110 | +export enum ContentType { |
| 111 | + Json = "application/json", |
| 112 | + JsonApi = "application/vnd.api+json", |
| 113 | + FormData = "multipart/form-data", |
| 114 | + UrlEncoded = "application/x-www-form-urlencoded", |
| 115 | + Text = "text/plain", |
| 116 | +} |
| 117 | +
|
| 118 | +export class HttpClient<SecurityDataType = unknown> { |
| 119 | + public baseUrl: string = ""; |
| 120 | + private securityData: SecurityDataType | null = null; |
| 121 | + private securityWorker?: ApiConfig<SecurityDataType>["securityWorker"]; |
| 122 | + private abortControllers = new Map<CancelToken, AbortController>(); |
| 123 | + private customFetch = (...fetchParams: Parameters<typeof fetch>) => fetch(...fetchParams); |
| 124 | +
|
| 125 | + private baseApiParams: RequestParams = { |
| 126 | + credentials: 'same-origin', |
| 127 | + headers: {}, |
| 128 | + redirect: 'follow', |
| 129 | + referrerPolicy: 'no-referrer', |
| 130 | + } |
| 131 | +
|
| 132 | + constructor(apiConfig: ApiConfig<SecurityDataType> = {}) { |
| 133 | + Object.assign(this, apiConfig); |
| 134 | + } |
| 135 | +
|
| 136 | + public setSecurityData = (data: SecurityDataType | null) => { |
| 137 | + this.securityData = data; |
| 138 | + } |
| 139 | +
|
| 140 | + protected encodeQueryParam(key: string, value: any) { |
| 141 | + const encodedKey = encodeURIComponent(key); |
| 142 | + return \`\${encodedKey}=\${encodeURIComponent(typeof value === "number" ? value : \`\${value}\`)}\`; |
| 143 | + } |
| 144 | +
|
| 145 | + protected addQueryParam(query: QueryParamsType, key: string) { |
| 146 | + return this.encodeQueryParam(key, query[key]); |
| 147 | + } |
| 148 | +
|
| 149 | + protected addArrayQueryParam(query: QueryParamsType, key: string) { |
| 150 | + const value = query[key]; |
| 151 | + return value.map((v: any) => this.encodeQueryParam(key, v)).join("&"); |
| 152 | + } |
| 153 | +
|
| 154 | + protected toQueryString(rawQuery?: QueryParamsType): string { |
| 155 | + const query = rawQuery || {}; |
| 156 | + const keys = Object.keys(query).filter((key) => "undefined" !== typeof query[key]); |
| 157 | + return keys |
| 158 | + .map((key) => |
| 159 | + Array.isArray(query[key]) |
| 160 | + ? this.addArrayQueryParam(query, key) |
| 161 | + : this.addQueryParam(query, key), |
| 162 | + ) |
| 163 | + .join("&"); |
| 164 | + } |
| 165 | +
|
| 166 | + protected addQueryParams(rawQuery?: QueryParamsType): string { |
| 167 | + const queryString = this.toQueryString(rawQuery); |
| 168 | + return queryString ? \`?\${queryString}\` : ""; |
| 169 | + } |
| 170 | +
|
| 171 | + private contentFormatters: Record<ContentType, (input: any) => any> = { |
| 172 | + [ContentType.Json]: (input:any) => input !== null && (typeof input === "object" || typeof input === "string") ? JSON.stringify(input) : input, |
| 173 | + [ContentType.JsonApi]: (input:any) => input !== null && (typeof input === "object" || typeof input === "string") ? JSON.stringify(input) : input, |
| 174 | + [ContentType.Text]: (input:any) => input !== null && typeof input !== "string" ? JSON.stringify(input) : input, |
| 175 | + [ContentType.FormData]: (input: any) => |
| 176 | + Object.keys(input || {}).reduce((formData, key) => { |
| 177 | + const property = input[key]; |
| 178 | + formData.append( |
| 179 | + key, |
| 180 | + property instanceof Blob ? |
| 181 | + property : |
| 182 | + typeof property === "object" && property !== null ? |
| 183 | + JSON.stringify(property) : |
| 184 | + \`\${property}\` |
| 185 | + ); |
| 186 | + return formData; |
| 187 | + }, new FormData()), |
| 188 | + [ContentType.UrlEncoded]: (input: any) => this.toQueryString(input), |
| 189 | + } |
| 190 | +
|
| 191 | + protected mergeRequestParams(params1: RequestParams, params2?: RequestParams): RequestParams { |
| 192 | + return { |
| 193 | + ...this.baseApiParams, |
| 194 | + ...params1, |
| 195 | + ...(params2 || {}), |
| 196 | + headers: { |
| 197 | + ...(this.baseApiParams.headers || {}), |
| 198 | + ...(params1.headers || {}), |
| 199 | + ...((params2 && params2.headers) || {}), |
| 200 | + }, |
| 201 | + }; |
| 202 | + } |
| 203 | +
|
| 204 | + protected createAbortSignal = (cancelToken: CancelToken): AbortSignal | undefined => { |
| 205 | + if (this.abortControllers.has(cancelToken)) { |
| 206 | + const abortController = this.abortControllers.get(cancelToken); |
| 207 | + if (abortController) { |
| 208 | + return abortController.signal; |
| 209 | + } |
| 210 | + return void 0; |
| 211 | + } |
| 212 | +
|
| 213 | + const abortController = new AbortController(); |
| 214 | + this.abortControllers.set(cancelToken, abortController); |
| 215 | + return abortController.signal; |
| 216 | + } |
| 217 | +
|
| 218 | + public abortRequest = (cancelToken: CancelToken) => { |
| 219 | + const abortController = this.abortControllers.get(cancelToken) |
| 220 | +
|
| 221 | + if (abortController) { |
| 222 | + abortController.abort(); |
| 223 | + this.abortControllers.delete(cancelToken); |
| 224 | + } |
| 225 | + } |
| 226 | +
|
| 227 | + public request = async <T = any, E = any>({ |
| 228 | + body, |
| 229 | + secure, |
| 230 | + path, |
| 231 | + type, |
| 232 | + query, |
| 233 | + format, |
| 234 | + baseUrl, |
| 235 | + cancelToken, |
| 236 | + ...params |
| 237 | + }: FullRequestParams): Promise<HttpResponse<T, E>> => { |
| 238 | + const secureParams = ((typeof secure === 'boolean' ? secure : this.baseApiParams.secure) && this.securityWorker && await this.securityWorker(this.securityData)) || {}; |
| 239 | + const requestParams = this.mergeRequestParams(params, secureParams); |
| 240 | + const queryString = query && this.toQueryString(query); |
| 241 | + const payloadFormatter = this.contentFormatters[type || ContentType.Json]; |
| 242 | + const responseFormat = format || requestParams.format; |
| 243 | +
|
| 244 | + return this.customFetch( |
| 245 | + \`\${baseUrl || this.baseUrl || ""}\${path}\${queryString ? \`?\${queryString}\` : ""}\`, |
| 246 | + { |
| 247 | + ...requestParams, |
| 248 | + headers: { |
| 249 | + ...(requestParams.headers || {}), |
| 250 | + ...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}), |
| 251 | + }, |
| 252 | + signal: (cancelToken ? this.createAbortSignal(cancelToken) : requestParams.signal) || null, |
| 253 | + body: typeof body === "undefined" || body === null ? null : payloadFormatter(body), |
| 254 | + } |
| 255 | + ).then(async (response) => { |
| 256 | + const r = response.clone() as HttpResponse<T, E>; |
| 257 | + r.data = (null as unknown) as T; |
| 258 | + r.error = (null as unknown) as E; |
| 259 | +
|
| 260 | + const data = !responseFormat ? r : await response[responseFormat]() |
| 261 | + .then((data) => { |
| 262 | + if (r.ok) { |
| 263 | + r.data = data; |
| 264 | + } else { |
| 265 | + r.error = data; |
| 266 | + } |
| 267 | + return r; |
| 268 | + }) |
| 269 | + .catch((e) => { |
| 270 | + r.error = e; |
| 271 | + return r; |
| 272 | + }); |
| 273 | +
|
| 274 | + if (cancelToken) { |
| 275 | + this.abortControllers.delete(cancelToken); |
| 276 | + } |
| 277 | +
|
| 278 | + if (!response.ok) throw data; |
| 279 | + return data; |
| 280 | + }); |
| 281 | + }; |
| 282 | +} |
| 283 | +
|
| 284 | +
|
| 285 | +
|
| 286 | +/** |
| 287 | +* @title Test API with operationId starting with number |
| 288 | +* @version 1.0.0 |
| 289 | +*/ |
| 290 | +export class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDataType> { |
| 291 | +
|
| 292 | +
|
| 293 | +
|
| 294 | + |
| 295 | + users = { |
| 296 | + |
| 297 | + /** |
| 298 | + * No description |
| 299 | + * |
| 300 | + * @name 123GetUser |
| 301 | + * @summary Get user |
| 302 | + * @request GET:/users |
| 303 | + */ |
| 304 | +"123GetUser": (params: RequestParams = {}) => |
| 305 | + this.request<{ |
| 306 | + id?: number, |
| 307 | + name?: string, |
| 308 | +
|
| 309 | +}, any>({ |
| 310 | + path: \`/users\`, |
| 311 | + method: 'GET', |
| 312 | + format: "json", ...params, |
| 313 | + }), |
| 314 | + } |
| 315 | + posts = { |
| 316 | + |
| 317 | + /** |
| 318 | + * No description |
| 319 | + * |
| 320 | + * @name 456CreatePost |
| 321 | + * @summary Create post |
| 322 | + * @request POST:/posts |
| 323 | + */ |
| 324 | +"456CreatePost": (data: { |
| 325 | + title?: string, |
| 326 | + content?: string, |
| 327 | +
|
| 328 | +}, params: RequestParams = {}) => |
| 329 | + this.request<{ |
| 330 | + id?: number, |
| 331 | + title?: string, |
| 332 | +
|
| 333 | +}, any>({ |
| 334 | + path: \`/posts\`, |
| 335 | + method: 'POST', |
| 336 | + body: data, type: ContentType.Json, format: "json", ...params, |
| 337 | + }), |
| 338 | + } |
| 339 | + } |
| 340 | +" |
| 341 | +`; |
0 commit comments