|
|
|
@ -2,7 +2,8 @@
|
|
|
|
|
const { apiConfig, generateResponses, config } = it; |
|
|
|
|
%> |
|
|
|
|
|
|
|
|
|
import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse, ResponseType } from "axios"; |
|
|
|
|
import type { AxiosInstance, AxiosRequestConfig, HeadersDefaults, ResponseType, AxiosResponse } from "axios"; |
|
|
|
|
import axios from "axios"; |
|
|
|
|
|
|
|
|
|
export type QueryParamsType = Record<string | number, any>; |
|
|
|
|
|
|
|
|
@ -23,7 +24,7 @@ export interface FullRequestParams extends Omit<AxiosRequestConfig, "data" | "pa
|
|
|
|
|
body?: unknown; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export type RequestParams = Omit<FullRequestParams, "body" | "method" | "path">; |
|
|
|
|
export type RequestParams = Omit<FullRequestParams, "body" | "method" | "query" | "path">; |
|
|
|
|
|
|
|
|
|
export interface ApiConfig<SecurityDataType = unknown> extends Omit<AxiosRequestConfig, "data" | "cancelToken"> { |
|
|
|
|
securityWorker?: (securityData: SecurityDataType | null) => Promise<AxiosRequestConfig | void> | AxiosRequestConfig | void; |
|
|
|
@ -35,6 +36,7 @@ export enum ContentType {
|
|
|
|
|
Json = "application/json", |
|
|
|
|
FormData = "multipart/form-data", |
|
|
|
|
UrlEncoded = "application/x-www-form-urlencoded", |
|
|
|
|
Text = "text/plain", |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export class HttpClient<SecurityDataType = unknown> { |
|
|
|
@ -55,43 +57,44 @@ export class HttpClient<SecurityDataType = unknown> {
|
|
|
|
|
this.securityData = data |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private mergeRequestParams(params1: AxiosRequestConfig, params2?: AxiosRequestConfig): AxiosRequestConfig { |
|
|
|
|
return { |
|
|
|
|
...this.instance.defaults, |
|
|
|
|
...params1, |
|
|
|
|
...(params2 || {}), |
|
|
|
|
headers: { |
|
|
|
|
...(this.instance.defaults.headers || {}), |
|
|
|
|
...(params1.headers || {}), |
|
|
|
|
...((params2 && params2.headers) || {}), |
|
|
|
|
}, |
|
|
|
|
}; |
|
|
|
|
protected mergeRequestParams(params1: AxiosRequestConfig, params2?: AxiosRequestConfig): AxiosRequestConfig { |
|
|
|
|
const method = params1.method || (params2 && params2.method) |
|
|
|
|
|
|
|
|
|
return { |
|
|
|
|
...this.instance.defaults, |
|
|
|
|
...params1, |
|
|
|
|
...(params2 || {}), |
|
|
|
|
headers: { |
|
|
|
|
...((method && this.instance.defaults.headers[method.toLowerCase() as keyof HeadersDefaults]) || {}), |
|
|
|
|
...(params1.headers || {}), |
|
|
|
|
...((params2 && params2.headers) || {}), |
|
|
|
|
}, |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
protected stringifyFormItem(formItem: unknown) { |
|
|
|
|
if (typeof formItem === "object" && formItem !== null) { |
|
|
|
|
return JSON.stringify(formItem); |
|
|
|
|
} else { |
|
|
|
|
return `${formItem}`; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
protected createFormData(input: Record<string, unknown>): FormData { |
|
|
|
|
if (input instanceof FormData) { |
|
|
|
|
return input; |
|
|
|
|
return Object.keys(input || {}).reduce((formData, key) => { |
|
|
|
|
const property = input[key]; |
|
|
|
|
const propertyContent: any[] = (property instanceof Array) ? property : [property] |
|
|
|
|
|
|
|
|
|
for (const formItem of propertyContent) { |
|
|
|
|
const isFileType = formItem instanceof Blob || formItem instanceof File; |
|
|
|
|
formData.append( |
|
|
|
|
key, |
|
|
|
|
isFileType ? formItem : this.stringifyFormItem(formItem) |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
return Object.keys(input || {}).reduce((formData, key) => { |
|
|
|
|
const property = input[key]; |
|
|
|
|
|
|
|
|
|
if (property instanceof Blob) { |
|
|
|
|
formData.append(key, property) |
|
|
|
|
} else if (typeof property === 'object' && property !== null) { |
|
|
|
|
if (Array.isArray(property)) { |
|
|
|
|
// eslint-disable-next-line functional/no-loop-statement |
|
|
|
|
for (const prop of property) { |
|
|
|
|
formData.append(`${key}[]`, prop) |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
formData.append(key, JSON.stringify(property)) |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
formData.append(key, `${property}`) |
|
|
|
|
} |
|
|
|
|
return formData; |
|
|
|
|
}, new FormData()); |
|
|
|
|
|
|
|
|
|
return formData; |
|
|
|
|
}, new FormData()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public request = async <T = any, _E = any>({ |
|
|
|
@ -110,21 +113,21 @@ export class HttpClient<SecurityDataType = unknown> {
|
|
|
|
|
<% } %> |
|
|
|
|
const secureParams = ((typeof secure === 'boolean' ? secure : this.secure) && this.securityWorker && (await this.securityWorker(this.securityData))) || {}; |
|
|
|
|
const requestParams = this.mergeRequestParams(params, secureParams); |
|
|
|
|
const responseFormat = (format && this.format) || void 0; |
|
|
|
|
const responseFormat = (format || this.format) || undefined; |
|
|
|
|
|
|
|
|
|
if (type === ContentType.FormData && body && body !== null && typeof body === "object") { |
|
|
|
|
requestParams.headers.common = { Accept: "*/*" }; |
|
|
|
|
requestParams.headers.post = {}; |
|
|
|
|
requestParams.headers.put = {}; |
|
|
|
|
|
|
|
|
|
body = this.createFormData(body as Record<string, unknown>); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (type === ContentType.Text && body && body !== null && typeof body !== "string") { |
|
|
|
|
body = JSON.stringify(body); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return this.instance.request({ |
|
|
|
|
...requestParams, |
|
|
|
|
headers: { |
|
|
|
|
...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}), |
|
|
|
|
...(requestParams.headers || {}), |
|
|
|
|
...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}), |
|
|
|
|
}, |
|
|
|
|
params: query, |
|
|
|
|
responseType: responseFormat, |
|
|
|
@ -139,4 +142,4 @@ export class HttpClient<SecurityDataType = unknown> {
|
|
|
|
|
}); |
|
|
|
|
<% } %> |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
} |