|
| 1 | +<% |
| 2 | +const { apiConfig, generateResponses, config } = it; |
| 3 | +%> |
| 4 | + |
| 5 | +import type { $Fetch, FetchOptions } from 'ofetch' |
| 6 | +import { $fetch } from 'ofetch' |
| 7 | + |
| 8 | +export type QueryParamsType = Record<string | number, any>; |
| 9 | +export type ResponseFormat = keyof Omit<Body, "body" | "bodyUsed">; |
| 10 | + |
| 11 | +export interface CustomFetchOptions extends FetchOptions { |
| 12 | + /** set parameter to `true` for call `securityWorker` for this request */ |
| 13 | + secure?: boolean |
| 14 | +} |
| 15 | + |
| 16 | +export type RequestParams = Omit<CustomFetchOptions, 'body' | 'method'> |
| 17 | + |
| 18 | +export interface ApiConfig<SecurityDataType = unknown> { |
| 19 | + baseURL?: string; |
| 20 | + basePath?: string; |
| 21 | + baseApiParams?: Omit<RequestParams, "baseURL" | "cancelToken" | "signal">; |
| 22 | + securityWorker?: (securityData: SecurityDataType | null) => Promise<RequestParams | void> | RequestParams | void; |
| 23 | + customFetch?: $Fetch; |
| 24 | +} |
| 25 | + |
| 26 | +type CancelToken = Symbol | string | number; |
| 27 | + |
| 28 | +export enum ContentType { |
| 29 | + Json = "application/json", |
| 30 | + FormData = "multipart/form-data", |
| 31 | + UrlEncoded = "application/x-www-form-urlencoded", |
| 32 | +} |
| 33 | + |
| 34 | +export class HttpClient<SecurityDataType = unknown> { |
| 35 | + public baseURL: string = "<%~ apiConfig.baseUrl %>"; |
| 36 | + private securityData: SecurityDataType | null = null; |
| 37 | + private securityWorker?: ApiConfig<SecurityDataType>["securityWorker"]; |
| 38 | + private abortControllers = new Map<CancelToken, AbortController>(); |
| 39 | + private customFetch = (url: string, fetchParams: FetchOptions) => $fetch(url, fetchParams) |
| 40 | + |
| 41 | + private baseApiParams: RequestParams = { |
| 42 | + credentials: 'same-origin', |
| 43 | + headers: {}, |
| 44 | + redirect: 'follow', |
| 45 | + referrerPolicy: 'no-referrer', |
| 46 | + } |
| 47 | + |
| 48 | + constructor(apiConfig: ApiConfig<SecurityDataType> = {}) { |
| 49 | + Object.assign(this, apiConfig); |
| 50 | + } |
| 51 | + |
| 52 | + public setSecurityData = (data: SecurityDataType | null) => { |
| 53 | + this.securityData = data; |
| 54 | + } |
| 55 | + |
| 56 | + protected mergeRequestParams(params1: RequestParams, params2?: RequestParams): RequestParams { |
| 57 | + return { |
| 58 | + ...this.baseApiParams, |
| 59 | + ...params1, |
| 60 | + ...(params2 || {}), |
| 61 | + headers: { |
| 62 | + ...(this.baseApiParams.headers || {}), |
| 63 | + ...(params1.headers || {}), |
| 64 | + ...((params2 && params2.headers) || {}), |
| 65 | + }, |
| 66 | + }; |
| 67 | + } |
| 68 | + |
| 69 | + protected createAbortSignal = (cancelToken: CancelToken): AbortSignal | undefined => { |
| 70 | + if (this.abortControllers.has(cancelToken)) { |
| 71 | + const abortController = this.abortControllers.get(cancelToken); |
| 72 | + if (abortController) { |
| 73 | + return abortController.signal; |
| 74 | + } |
| 75 | + return void 0; |
| 76 | + } |
| 77 | + |
| 78 | + const abortController = new AbortController(); |
| 79 | + this.abortControllers.set(cancelToken, abortController); |
| 80 | + return abortController.signal; |
| 81 | + } |
| 82 | + |
| 83 | + public abortRequest = (cancelToken: CancelToken) => { |
| 84 | + const abortController = this.abortControllers.get(cancelToken) |
| 85 | + |
| 86 | + if (abortController) { |
| 87 | + abortController.abort(); |
| 88 | + this.abortControllers.delete(cancelToken); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + public request = async <T = any>(url: string, { |
| 93 | + body, |
| 94 | + secure, |
| 95 | + method, |
| 96 | + baseURL, |
| 97 | + signal, |
| 98 | + params, |
| 99 | + ...options |
| 100 | +<% if (config.unwrapResponseData) { %> |
| 101 | + }: CustomFetchOptions): Promise<T> => { |
| 102 | +<% } else { %> |
| 103 | + }: CustomFetchOptions): Promise<T> => { |
| 104 | +<% } %> |
| 105 | + const secureParams = ((typeof secure === 'boolean' ? secure : this.baseApiParams.secure) && this.securityWorker && await this.securityWorker(this.securityData)) || {}; |
| 106 | + const requestOptions = this.mergeRequestParams(options, secureParams) |
| 107 | + |
| 108 | + return this.customFetch( |
| 109 | + `${baseURL || this.baseURL || ""}${this.basePath ? `${this.basePath}` : ''}${url}`, |
| 110 | + { |
| 111 | + params, |
| 112 | + method, |
| 113 | + ...requestOptions, |
| 114 | + signal, |
| 115 | + body, |
| 116 | + } |
| 117 | + <% if (config.unwrapResponseData) { %> |
| 118 | + ).then((response: T) => response.data) |
| 119 | +<% } else { %> |
| 120 | + ).then((response: T) => response) |
| 121 | +<% } %> |
| 122 | + }; |
| 123 | +} |
0 commit comments