Skip to content

Commit

Permalink
fix(axios): use builtin form data to ensure blob form data works in n…
Browse files Browse the repository at this point in the history
…ode env
  • Loading branch information
jordanshatford committed Apr 2, 2024
1 parent ca2847f commit f04677b
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/gentle-pets-clean.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hey-api/openapi-ts": patch
---

fix(axios): use builtin form data to ensure blob form data works in node environment
5 changes: 4 additions & 1 deletion src/templates/core/axios/request.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ import type { OpenAPIConfig } from './OpenAPI';
{{>functions/getUrl}}


{{>functions/getFormData}}


{{>functions/resolve}}


Expand Down Expand Up @@ -65,7 +68,7 @@ export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions, ax
return new CancelablePromise(async (resolve, reject, onCancel) => {
try {
const url = getUrl(config, options);
const formData = options.formData;
const formData = getFormData(options.formData);
const body = getRequestBody(options);
const headers = await getHeaders(config, options);

Expand Down
2 changes: 1 addition & 1 deletion src/templates/core/axios/sendRequest.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export const sendRequest = async <T>(
options: ApiRequestOptions,
url: string,
body: unknown,
formData: Record<string, unknown> | undefined,
formData: FormData | undefined,
headers: Record<string, string>,
onCancel: OnCancel,
axiosClient: AxiosInstance
Expand Down
29 changes: 28 additions & 1 deletion test/__snapshots__/v3_axios/core/request.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,33 @@ const getUrl = (config: OpenAPIConfig, options: ApiRequestOptions): string => {
return options.query ? url + getQueryString(options.query) : url;
};

export const getFormData = (options: ApiRequestOptions): FormData | undefined => {
if (options.formData) {
const formData = new FormData();

const process = (key: string, value: unknown) => {
if (isString(value) || isBlob(value)) {
formData.append(key, value);
} else {
formData.append(key, JSON.stringify(value));
}
};

Object.entries(options.formData)
.filter(([, value]) => value !== undefined && value !== null)
.forEach(([key, value]) => {
if (Array.isArray(value)) {
value.forEach(v => process(key, v));
} else {
process(key, value);
}
});

return formData;
}
return undefined;
};

type Resolver<T> = (options: ApiRequestOptions) => Promise<T>;

export const resolve = async <T>(options: ApiRequestOptions, resolver?: T | Resolver<T>): Promise<T | undefined> => {
Expand Down Expand Up @@ -288,7 +315,7 @@ export const request = <T>(
return new CancelablePromise(async (resolve, reject, onCancel) => {
try {
const url = getUrl(config, options);
const formData = options.formData;
const formData = getFormData(options.formData);
const body = getRequestBody(options);
const headers = await getHeaders(config, options);
Expand Down

0 comments on commit f04677b

Please sign in to comment.