-
-
Notifications
You must be signed in to change notification settings - Fork 367
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2b6db23
commit 8fb9a1a
Showing
16 changed files
with
70,670 additions
and
3 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,7 @@ const SCHEMA_TYPES = { | |
|
||
const HTTP_CLIENT = { | ||
FETCH: 'fetch', | ||
OFETCH: 'ofetch', | ||
AXIOS: 'axios', | ||
}; | ||
|
||
|
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,126 @@ | ||
<% | ||
const { apiConfig, generateResponses, config } = it; | ||
%> | ||
|
||
import type { $Fetch, FetchOptions } from 'ofetch' | ||
import { $fetch } from 'ofetch' | ||
|
||
export type QueryParamsType = Record<string | number, any>; | ||
export type ResponseFormat = keyof Omit<Body, "body" | "bodyUsed">; | ||
|
||
export interface CustomFetchOptions extends FetchOptions { | ||
/** set parameter to `true` for call `securityWorker` for this request */ | ||
secure?: boolean | ||
} | ||
|
||
export type RequestParams = Omit<CustomFetchOptions, 'body' | 'method'> | ||
|
||
export interface ApiConfig<SecurityDataType = unknown> { | ||
baseURL?: string; | ||
basePath?: string; | ||
baseSiteId?: string; | ||
baseApiParams?: Omit<RequestParams, "baseURL" | "cancelToken" | "signal">; | ||
securityWorker?: (securityData: SecurityDataType | null) => Promise<RequestParams | void> | RequestParams | void; | ||
customFetch?: $Fetch; | ||
} | ||
|
||
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 = "<%~ apiConfig.baseUrl %>"; | ||
private securityData: SecurityDataType | null = null; | ||
private securityWorker?: ApiConfig<SecurityDataType>["securityWorker"]; | ||
private abortControllers = new Map<CancelToken, AbortController>(); | ||
private customFetch = (url: string, fetchParams: FetchOptions) => $fetch(url, fetchParams) | ||
public basePath: string = "<%~ apiConfig.baseUrl.replace('https://{url}', '') %>"; | ||
public baseSiteId: string = 'roller'; | ||
|
||
private baseApiParams: RequestParams = { | ||
credentials: 'same-origin', | ||
headers: {}, | ||
redirect: 'follow', | ||
referrerPolicy: 'no-referrer', | ||
} | ||
|
||
constructor(apiConfig: ApiConfig<SecurityDataType> = {}) { | ||
Object.assign(this, apiConfig); | ||
} | ||
|
||
public setSecurityData = (data: SecurityDataType | null) => { | ||
this.securityData = data; | ||
} | ||
|
||
protected mergeRequestParams(params1: RequestParams, params2?: RequestParams): RequestParams { | ||
return { | ||
...this.baseApiParams, | ||
...params1, | ||
...(params2 || {}), | ||
headers: { | ||
...(this.baseApiParams.headers || {}), | ||
...(params1.headers || {}), | ||
...((params2 && params2.headers) || {}), | ||
}, | ||
}; | ||
} | ||
|
||
protected 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 = async <T = any>(url: string, { | ||
body, | ||
secure, | ||
method, | ||
baseURL, | ||
signal, | ||
params, | ||
...options | ||
<% if (config.unwrapResponseData) { %> | ||
}: CustomFetchOptions): Promise<T> => { | ||
<% } else { %> | ||
}: CustomFetchOptions): Promise<T> => { | ||
<% } %> | ||
const secureParams = ((typeof secure === 'boolean' ? secure : this.baseApiParams.secure) && this.securityWorker && await this.securityWorker(this.securityData)) || {}; | ||
const requestOptions = this.mergeRequestParams(options, secureParams) | ||
|
||
return this.customFetch( | ||
`${baseURL || this.baseURL || ""}${this.basePath ? `${this.basePath}` : ''}${url}`, | ||
{ | ||
params, | ||
method, | ||
...requestOptions, | ||
signal, | ||
body, | ||
} | ||
<% if (config.unwrapResponseData) { %> | ||
).then((response: T) => response.data) | ||
<% } else { %> | ||
).then((response: T) => response) | ||
<% } %> | ||
}; | ||
} |
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.