-
-
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.
Generate a client that uses this library: https://github.com/sindresorhus/ky
- Loading branch information
1 parent
2b6db23
commit 8be147d
Showing
10 changed files
with
157 additions
and
2 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
<% | ||
const { apiConfig, generateResponses, config } = it; | ||
%> | ||
|
||
import type { KyInstance, Options as KyOptions, KyResponse } from "ky"; | ||
import ky from "ky"; | ||
|
||
export type QueryParamsType = Record<string | number, any>; | ||
export type ResponseFormat = keyof Omit<Body, "body" | "bodyUsed">; | ||
|
||
export interface FullRequestParams extends Omit<KyOptions, "json" | "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?: ResponseFormat; | ||
/** request body */ | ||
body?: unknown; | ||
} | ||
|
||
export type RequestParams = Omit<FullRequestParams, "body" | "method" | "query" | "path">; | ||
|
||
export interface ApiConfig<SecurityDataType = unknown> extends Omit<KyOptions, "data" | "cancelToken"> { | ||
securityWorker?: (securityData: SecurityDataType | null) => Promise<KyOptions | void> | KyOptions | void; | ||
secure?: boolean; | ||
format?: ResponseType; | ||
} | ||
|
||
export enum ContentType { | ||
Json = "application/json", | ||
FormData = "multipart/form-data", | ||
UrlEncoded = "application/x-www-form-urlencoded", | ||
Text = "text/plain", | ||
} | ||
|
||
export class HttpClient<SecurityDataType = unknown> { | ||
public instance: KyInstance; | ||
private securityData: SecurityDataType | null = null; | ||
private securityWorker?: ApiConfig<SecurityDataType>["securityWorker"]; | ||
private secure?: boolean; | ||
private format?: ResponseType; | ||
|
||
constructor({ securityWorker, secure, format, ...options }: ApiConfig<SecurityDataType> = {}) { | ||
this.instance = axios.create({ ...options, prefixUrl: options.prefixUrl || "<%~ apiConfig.baseUrl %>" }) | ||
this.secure = secure; | ||
this.format = format; | ||
this.securityWorker = securityWorker; | ||
} | ||
|
||
public setSecurityData = (data: SecurityDataType | null) => { | ||
this.securityData = data | ||
} | ||
|
||
protected mergeRequestParams(params1: KyOptions, params2?: KyOptions): KyOptions { | ||
return { | ||
...params1, | ||
...params2, | ||
headers: { | ||
...(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 { | ||
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 formData; | ||
}, new FormData()); | ||
} | ||
|
||
public request = async <T = any, _E = any>({ | ||
secure, | ||
path, | ||
type, | ||
query, | ||
format, | ||
body, | ||
...params | ||
<% if (config.unwrapResponseData) { %> | ||
}: FullRequestParams): Promise<T> => { | ||
<% } else { %> | ||
}: FullRequestParams): KyResponse<T> => { | ||
<% } %> | ||
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) || undefined; | ||
|
||
if (type === ContentType.FormData && body && body !== null && typeof body === "object") { | ||
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: { | ||
...(requestParams.headers || {}), | ||
...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}), | ||
}, | ||
params: query, | ||
responseType: responseFormat, | ||
data: body, | ||
url: path, | ||
<% if (config.unwrapResponseData) { %> | ||
}).json(); | ||
<% } else { %> | ||
}); | ||
<% } %> | ||
}; | ||
} |
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