-
-
Notifications
You must be signed in to change notification settings - Fork 368
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: linter disable rules is not working * bump: up project version to 5.1.2
- Loading branch information
Showing
64 changed files
with
527 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,277 @@ | ||
/* eslint-disable */ | ||
/* tslint:disable */ | ||
/* | ||
* --------------------------------------------------------------- | ||
* ## THIS FILE WAS GENERATED VIA SWAGGER-TYPESCRIPT-API ## | ||
* ## ## | ||
* ## AUTHOR: acacode ## | ||
* ## SOURCE: https://github.com/acacode/swagger-typescript-api ## | ||
* --------------------------------------------------------------- | ||
*/ | ||
|
||
export interface Bar { | ||
A?: string; | ||
|
||
/** @format int32 */ | ||
B: number; | ||
|
||
/** @format date-time */ | ||
C: string; | ||
Baz?: Baz; | ||
} | ||
|
||
export interface Baz { | ||
/** @format decimal */ | ||
D: number; | ||
Color: Color; | ||
} | ||
|
||
export enum Color { | ||
RED = 0, | ||
GREEN = 1, | ||
BLUE = 2, | ||
} | ||
|
||
export type QueryParamsType = Record<string | number, any>; | ||
export type ResponseFormat = keyof Omit<Body, "body" | "bodyUsed">; | ||
|
||
export interface FullRequestParams extends Omit<RequestInit, "body"> { | ||
/** set parameter to `true` for call `securityWorker` for this request */ | ||
secure?: boolean; | ||
/** request path */ | ||
path: string; | ||
/** content type of request body */ | ||
type?: ContentType; | ||
/** query params */ | ||
query?: QueryParamsType; | ||
/** format of response (i.e. response.json() -> format: "json") */ | ||
format?: keyof Omit<Body, "body" | "bodyUsed">; | ||
/** request body */ | ||
body?: unknown; | ||
/** base url */ | ||
baseUrl?: string; | ||
/** request cancellation token */ | ||
cancelToken?: CancelToken; | ||
} | ||
|
||
export type RequestParams = Omit<FullRequestParams, "body" | "method" | "query" | "path">; | ||
|
||
export interface ApiConfig<SecurityDataType = unknown> { | ||
baseUrl?: string; | ||
baseApiParams?: Omit<RequestParams, "baseUrl" | "cancelToken" | "signal">; | ||
securityWorker?: (securityData: SecurityDataType) => RequestParams | void; | ||
} | ||
|
||
export interface HttpResponse<D extends unknown, E extends unknown = unknown> extends Response { | ||
data: D; | ||
error: E; | ||
} | ||
|
||
type CancelToken = Symbol | string | number; | ||
|
||
export enum ContentType { | ||
Json = "application/json", | ||
FormData = "multipart/form-data", | ||
UrlEncoded = "application/x-www-form-urlencoded", | ||
} | ||
|
||
export class HttpClient<SecurityDataType = unknown> { | ||
public baseUrl: string = ""; | ||
private securityData: SecurityDataType = null as any; | ||
private securityWorker: null | ApiConfig<SecurityDataType>["securityWorker"] = null; | ||
private abortControllers = new Map<CancelToken, AbortController>(); | ||
|
||
private baseApiParams: RequestParams = { | ||
credentials: "same-origin", | ||
headers: {}, | ||
redirect: "follow", | ||
referrerPolicy: "no-referrer", | ||
}; | ||
|
||
constructor(apiConfig: ApiConfig<SecurityDataType> = {}) { | ||
Object.assign(this, apiConfig); | ||
} | ||
|
||
public setSecurityData = (data: SecurityDataType) => { | ||
this.securityData = data; | ||
}; | ||
|
||
private addQueryParam(query: QueryParamsType, key: string) { | ||
const value = query[key]; | ||
|
||
return ( | ||
encodeURIComponent(key) + | ||
"=" + | ||
encodeURIComponent(Array.isArray(value) ? value.join(",") : typeof value === "number" ? value : `${value}`) | ||
); | ||
} | ||
|
||
protected toQueryString(rawQuery?: QueryParamsType): string { | ||
const query = rawQuery || {}; | ||
const keys = Object.keys(query).filter((key) => "undefined" !== typeof query[key]); | ||
return keys | ||
.map((key) => | ||
typeof query[key] === "object" && !Array.isArray(query[key]) | ||
? this.toQueryString(query[key] as QueryParamsType) | ||
: this.addQueryParam(query, key), | ||
) | ||
.join("&"); | ||
} | ||
|
||
protected addQueryParams(rawQuery?: QueryParamsType): string { | ||
const queryString = this.toQueryString(rawQuery); | ||
return queryString ? `?${queryString}` : ""; | ||
} | ||
|
||
private contentFormatters: Record<ContentType, (input: any) => any> = { | ||
[ContentType.Json]: (input: any) => (input !== null && typeof input === "object" ? JSON.stringify(input) : input), | ||
[ContentType.FormData]: (input: any) => | ||
Object.keys(input || {}).reduce((data, key) => { | ||
data.append(key, input[key]); | ||
return data; | ||
}, new FormData()), | ||
[ContentType.UrlEncoded]: (input: any) => this.toQueryString(input), | ||
}; | ||
|
||
private mergeRequestParams(params1: RequestParams, params2?: RequestParams): RequestParams { | ||
return { | ||
...this.baseApiParams, | ||
...params1, | ||
...(params2 || {}), | ||
headers: { | ||
...(this.baseApiParams.headers || {}), | ||
...(params1.headers || {}), | ||
...((params2 && params2.headers) || {}), | ||
}, | ||
}; | ||
} | ||
|
||
private createAbortSignal = (cancelToken: CancelToken): AbortSignal | undefined => { | ||
if (this.abortControllers.has(cancelToken)) { | ||
const abortController = this.abortControllers.get(cancelToken); | ||
if (abortController) { | ||
return abortController.signal; | ||
} | ||
return void 0; | ||
} | ||
|
||
const abortController = new AbortController(); | ||
this.abortControllers.set(cancelToken, abortController); | ||
return abortController.signal; | ||
}; | ||
|
||
public abortRequest = (cancelToken: CancelToken) => { | ||
const abortController = this.abortControllers.get(cancelToken); | ||
|
||
if (abortController) { | ||
abortController.abort(); | ||
this.abortControllers.delete(cancelToken); | ||
} | ||
}; | ||
|
||
public request = <T = any, E = any>({ | ||
body, | ||
secure, | ||
path, | ||
type, | ||
query, | ||
format = "json", | ||
baseUrl, | ||
cancelToken, | ||
...params | ||
}: FullRequestParams): Promise<HttpResponse<T, E>> => { | ||
const secureParams = (secure && this.securityWorker && this.securityWorker(this.securityData)) || {}; | ||
const requestParams = this.mergeRequestParams(params, secureParams); | ||
const queryString = query && this.toQueryString(query); | ||
const payloadFormatter = this.contentFormatters[type || ContentType.Json]; | ||
|
||
return fetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { | ||
headers: { | ||
...(type ? { "Content-Type": type } : {}), | ||
...(requestParams.headers || {}), | ||
}, | ||
...requestParams, | ||
signal: cancelToken ? this.createAbortSignal(cancelToken) : void 0, | ||
body: typeof body === "undefined" || body === null ? null : payloadFormatter(body), | ||
}).then(async (response) => { | ||
const r = response as HttpResponse<T, E>; | ||
r.data = (null as unknown) as T; | ||
r.error = (null as unknown) as E; | ||
|
||
const data = await response[format]() | ||
.then((data) => { | ||
if (r.ok) { | ||
r.data = data; | ||
} else { | ||
r.error = data; | ||
} | ||
return r; | ||
}) | ||
.catch((e) => { | ||
r.error = e; | ||
return r; | ||
}); | ||
|
||
if (cancelToken) { | ||
this.abortControllers.delete(cancelToken); | ||
} | ||
|
||
if (!response.ok) throw data; | ||
return data; | ||
}); | ||
}; | ||
} | ||
|
||
/** | ||
* @title No title | ||
*/ | ||
export class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDataType> { | ||
api = { | ||
/** | ||
* No description | ||
* | ||
* @tags Foo | ||
* @name FooGetBarDescriptions | ||
* @request GET:/api/Foo/GetBarDescriptions | ||
*/ | ||
fooGetBarDescriptions: (params: RequestParams = {}) => | ||
this.request<string[], any>({ | ||
path: `/api/Foo/GetBarDescriptions`, | ||
method: "GET", | ||
format: "json", | ||
...params, | ||
}), | ||
|
||
/** | ||
* No description | ||
* | ||
* @tags Foo | ||
* @name FooGetBar | ||
* @request GET:/api/Foo/GetBar | ||
*/ | ||
fooGetBar: (query: { id: number }, params: RequestParams = {}) => | ||
this.request<Bar, any>({ | ||
path: `/api/Foo/GetBar`, | ||
method: "GET", | ||
query: query, | ||
format: "json", | ||
...params, | ||
}), | ||
|
||
/** | ||
* No description | ||
* | ||
* @tags Foo | ||
* @name FooSetBar | ||
* @request POST:/api/Foo/SetBar | ||
*/ | ||
fooSetBar: (value: Bar, params: RequestParams = {}) => | ||
this.request<void, any>({ | ||
path: `/api/Foo/SetBar`, | ||
method: "POST", | ||
body: value, | ||
type: ContentType.Json, | ||
...params, | ||
}), | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.