Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(fetch): support form data and form url encoded request #1529

20 changes: 14 additions & 6 deletions packages/fetch/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,20 +94,25 @@ ${
? `${stringify(override?.requestOptions)?.slice(1, -1)?.trim()}`
: '';
const fetchMethodOption = `method: '${verb.toUpperCase()}'`;

const fetchHeadersOption = body.contentType
? `headers: { 'Content-Type': '${body.contentType}' }`
: '';
const requestBodyParams = generateBodyOptions(
body,
isFormData,
isFormUrlEncoded,
);
const fetchBodyOption = requestBodyParams
? `body: JSON.stringify(${requestBodyParams})`
? (isFormData && body.formData) || (isFormUrlEncoded && body.formUrlEncoded)
? `body: ${requestBodyParams}`
: `body: JSON.stringify(${requestBodyParams})`
: '';

const fetchFnOptions = `${getUrlFnName}(${getUrlFnProperties}),
{${globalFetchOptions ? '\n' : ''} ${globalFetchOptions}
${isRequestOptions ? '...options,' : ''}
${fetchMethodOption}${fetchBodyOption ? ',' : ''}
${fetchMethodOption}${fetchHeadersOption ? ',' : ''}
${fetchHeadersOption}${fetchBodyOption ? ',' : ''}
${fetchBodyOption}
}
`;
Expand All @@ -129,9 +134,12 @@ ${

const fetchImplementationBody = mutator
? customFetchResponseImplementation
: `${bodyForm ? ` ${bodyForm}\n` : ''}` +
` ${fetchResponseImplementation}`;
const fetchImplementation = `export const ${operationName} = async (${args}): ${retrunType} => {\n${fetchImplementationBody}}`;
: fetchResponseImplementation;

const fetchImplementation = `export const ${operationName} = async (${args}): ${retrunType} => {
${bodyForm ? ` ${bodyForm}` : ''}
${fetchImplementationBody}}
`;

const implementation =
`${responseTypeImplementation}\n\n` +
Expand Down
2 changes: 2 additions & 0 deletions samples/next-app-with-fetch/app/gen/pets/pets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export const createPets = async (
return customFetch<Promise<createPetsResponse>>(getCreatePetsUrl(), {
...options,
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(createPetsBodyItem),
});
};
Expand All @@ -116,6 +117,7 @@ export const updatePets = async (
return customFetch<Promise<updatePetsResponse>>(getUpdatePetsUrl(), {
...options,
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(pet),
});
};
Expand Down
2 changes: 2 additions & 0 deletions samples/react-query/custom-fetch/src/gen/pets/pets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ export const createPets = async (
return customFetch<Promise<createPetsResponse>>(getCreatePetsUrl(), {
...options,
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(createPetsBodyItem),
});
};
Expand Down Expand Up @@ -305,6 +306,7 @@ export const updatePets = async (
return customFetch<Promise<updatePetsResponse>>(getUpdatePetsUrl(), {
...options,
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(pet),
});
};
Expand Down
2 changes: 2 additions & 0 deletions samples/svelte-query/custom-fetch/src/gen/pets/pets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ export const createPets = async (
return customFetch<Promise<createPetsResponse>>(getCreatePetsUrl(), {
...options,
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(createPetsBodyItem),
});
};
Expand Down Expand Up @@ -255,6 +256,7 @@ export const updatePets = async (
return customFetch<Promise<updatePetsResponse>>(getUpdatePetsUrl(), {
...options,
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(pet),
});
};
Expand Down
2 changes: 2 additions & 0 deletions samples/vue-query/custom-fetch/src/gen/pets/pets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ export const createPets = async (
return customFetch<Promise<createPetsResponse>>(getCreatePetsUrl(), {
...options,
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(createPetsBodyItem),
});
};
Expand Down Expand Up @@ -257,6 +258,7 @@ export const updatePets = async (
return customFetch<Promise<updatePetsResponse>>(getUpdatePetsUrl(), {
...options,
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(pet),
});
};
Expand Down
65 changes: 64 additions & 1 deletion tests/configs/fetch.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default defineConfig({
target: '../generated/fetch/petstore/endpoints.ts',
schemas: '../generated/fetch/petstore/model',
mock: true,
client: 'axios',
client: 'fetch',
},
input: {
target: '../specifications/petstore.yaml',
Expand Down Expand Up @@ -89,4 +89,67 @@ export default defineConfig({
target: '../specifications/petstore.yaml',
},
},
formData: {
output: {
target: '../generated/fetch/form-data-optional-request/endpoints.ts',
schemas: '../generated/fetch/form-data-optional-request/model',
client: 'fetch',
mock: true,
},
input: {
target: '../specifications/form-data-optional-request.yaml',
override: {
transformer: '../transformers/add-version.js',
},
},
},
formDataWithCustomFetch: {
output: {
target: '../generated/fetch/form-data-with-custom-fetch/endpoints.ts',
schemas: '../generated/fetch/form-data-with-custom-fetch/model',
client: 'fetch',
mock: true,
override: {
mutator: {
path: '../mutators/custom-fetch.ts',
name: 'customFetch',
},
},
},
input: {
target: '../specifications/form-data-optional-request.yaml',
override: {
transformer: '../transformers/add-version.js',
},
},
},
formUrlEncoded: {
output: {
target: '../generated/fetch/form-url-encoded/endpoints.ts',
schemas: '../generated/fetch/form-url-encoded/model',
client: 'fetch',
mock: true,
},
input: {
target: '../specifications/form-url-encoded.yaml',
},
},
formUrlEncodedCustomFetch: {
output: {
target:
'../generated/fetch/form-url-encoded-with-custom-fetch/endpoints.ts',
schemas: '../generated/fetch/form-url-encoded-with-custom-fetch/model',
client: 'fetch',
mock: true,
override: {
mutator: {
path: '../mutators/custom-fetch.ts',
name: 'customFetch',
},
},
},
input: {
target: '../specifications/form-url-encoded.yaml',
},
},
});