From cf199dfb0f958bf5c76b7103b549d49d4e108660 Mon Sep 17 00:00:00 2001 From: soartec-lab Date: Sun, 21 Jul 2024 01:48:07 +0000 Subject: [PATCH 1/7] feat: remove `JSON.stringify` when using form data or form url encoded --- packages/fetch/src/index.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/fetch/src/index.ts b/packages/fetch/src/index.ts index 911fe751e..44b87e7f5 100644 --- a/packages/fetch/src/index.ts +++ b/packages/fetch/src/index.ts @@ -101,7 +101,9 @@ ${ isFormUrlEncoded, ); const fetchBodyOption = requestBodyParams - ? `body: JSON.stringify(${requestBodyParams})` + ? isFormData || isFormUrlEncoded + ? `body: ${requestBodyParams}` + : `body: JSON.stringify(${requestBodyParams})` : ''; const fetchFnOptions = `${getUrlFnName}(${getUrlFnProperties}), From b1e3ac3162d3a381dd128dcea5dd2d7a1a92c26f Mon Sep 17 00:00:00 2001 From: soartec-lab Date: Sun, 21 Jul 2024 02:08:30 +0000 Subject: [PATCH 2/7] fix: add body form implementation when all case --- packages/fetch/src/index.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/packages/fetch/src/index.ts b/packages/fetch/src/index.ts index 44b87e7f5..27c2313a0 100644 --- a/packages/fetch/src/index.ts +++ b/packages/fetch/src/index.ts @@ -131,9 +131,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` + From ee6163580cd8dc9018abd8bb2943e74922e9765f Mon Sep 17 00:00:00 2001 From: soartec-lab Date: Sun, 21 Jul 2024 02:13:18 +0000 Subject: [PATCH 3/7] fix: fix condition for using `JSON.stringify` --- packages/fetch/src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/fetch/src/index.ts b/packages/fetch/src/index.ts index 27c2313a0..ab0a6ed32 100644 --- a/packages/fetch/src/index.ts +++ b/packages/fetch/src/index.ts @@ -101,7 +101,7 @@ ${ isFormUrlEncoded, ); const fetchBodyOption = requestBodyParams - ? isFormData || isFormUrlEncoded + ? (isFormData && body.formData) || (isFormUrlEncoded && body.formUrlEncoded) ? `body: ${requestBodyParams}` : `body: JSON.stringify(${requestBodyParams})` : ''; From 9faec67d7524e8b0c6437d98059e709f53f29b68 Mon Sep 17 00:00:00 2001 From: soartec-lab Date: Sun, 21 Jul 2024 02:24:14 +0000 Subject: [PATCH 4/7] feat: add `Content-Type` header to fetch client options when specified in `OpenApi` --- packages/fetch/src/index.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/fetch/src/index.ts b/packages/fetch/src/index.ts index ab0a6ed32..396bf8421 100644 --- a/packages/fetch/src/index.ts +++ b/packages/fetch/src/index.ts @@ -94,7 +94,9 @@ ${ ? `${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, @@ -109,7 +111,8 @@ ${ const fetchFnOptions = `${getUrlFnName}(${getUrlFnProperties}), {${globalFetchOptions ? '\n' : ''} ${globalFetchOptions} ${isRequestOptions ? '...options,' : ''} - ${fetchMethodOption}${fetchBodyOption ? ',' : ''} + ${fetchMethodOption}${fetchHeadersOption ? ',' : ''} + ${fetchHeadersOption}${fetchBodyOption ? ',' : ''} ${fetchBodyOption} } `; From c5b99284cca18008edbea3bd762d4bbe61d4d6a5 Mon Sep 17 00:00:00 2001 From: soartec-lab Date: Sun, 21 Jul 2024 02:29:37 +0000 Subject: [PATCH 5/7] chore: add test cases for form data and form encoded url with `fetch` --- tests/configs/fetch.config.ts | 65 ++++++++++++++++++++++++++++++++++- 1 file changed, 64 insertions(+), 1 deletion(-) diff --git a/tests/configs/fetch.config.ts b/tests/configs/fetch.config.ts index 76bfc00cb..f66a75e31 100644 --- a/tests/configs/fetch.config.ts +++ b/tests/configs/fetch.config.ts @@ -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', @@ -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: 'swr', + 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', + }, + }, }); From 8a3a5a6a7e3c8b4cb684a4c890ccd5b4f56be38c Mon Sep 17 00:00:00 2001 From: soartec-lab Date: Sun, 21 Jul 2024 02:47:28 +0000 Subject: [PATCH 6/7] chore: update samples --- samples/next-app-with-fetch/app/gen/pets/pets.ts | 2 ++ samples/react-query/custom-fetch/src/gen/pets/pets.ts | 2 ++ samples/svelte-query/custom-fetch/src/gen/pets/pets.ts | 2 ++ samples/vue-query/custom-fetch/src/gen/pets/pets.ts | 2 ++ 4 files changed, 8 insertions(+) diff --git a/samples/next-app-with-fetch/app/gen/pets/pets.ts b/samples/next-app-with-fetch/app/gen/pets/pets.ts index 3161ba20a..a15201544 100644 --- a/samples/next-app-with-fetch/app/gen/pets/pets.ts +++ b/samples/next-app-with-fetch/app/gen/pets/pets.ts @@ -93,6 +93,7 @@ export const createPets = async ( return customFetch>(getCreatePetsUrl(), { ...options, method: 'POST', + headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(createPetsBodyItem), }); }; @@ -116,6 +117,7 @@ export const updatePets = async ( return customFetch>(getUpdatePetsUrl(), { ...options, method: 'PUT', + headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(pet), }); }; diff --git a/samples/react-query/custom-fetch/src/gen/pets/pets.ts b/samples/react-query/custom-fetch/src/gen/pets/pets.ts index c401916fb..27d50dacb 100644 --- a/samples/react-query/custom-fetch/src/gen/pets/pets.ts +++ b/samples/react-query/custom-fetch/src/gen/pets/pets.ts @@ -224,6 +224,7 @@ export const createPets = async ( return customFetch>(getCreatePetsUrl(), { ...options, method: 'POST', + headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(createPetsBodyItem), }); }; @@ -305,6 +306,7 @@ export const updatePets = async ( return customFetch>(getUpdatePetsUrl(), { ...options, method: 'PUT', + headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(pet), }); }; diff --git a/samples/svelte-query/custom-fetch/src/gen/pets/pets.ts b/samples/svelte-query/custom-fetch/src/gen/pets/pets.ts index 1b27159fd..06770c9cd 100644 --- a/samples/svelte-query/custom-fetch/src/gen/pets/pets.ts +++ b/samples/svelte-query/custom-fetch/src/gen/pets/pets.ts @@ -174,6 +174,7 @@ export const createPets = async ( return customFetch>(getCreatePetsUrl(), { ...options, method: 'POST', + headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(createPetsBodyItem), }); }; @@ -255,6 +256,7 @@ export const updatePets = async ( return customFetch>(getUpdatePetsUrl(), { ...options, method: 'PUT', + headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(pet), }); }; diff --git a/samples/vue-query/custom-fetch/src/gen/pets/pets.ts b/samples/vue-query/custom-fetch/src/gen/pets/pets.ts index 9be2dd53e..05bba0980 100644 --- a/samples/vue-query/custom-fetch/src/gen/pets/pets.ts +++ b/samples/vue-query/custom-fetch/src/gen/pets/pets.ts @@ -176,6 +176,7 @@ export const createPets = async ( return customFetch>(getCreatePetsUrl(), { ...options, method: 'POST', + headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(createPetsBodyItem), }); }; @@ -257,6 +258,7 @@ export const updatePets = async ( return customFetch>(getUpdatePetsUrl(), { ...options, method: 'PUT', + headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(pet), }); }; From 9f7288e61af3d8b830f5ced0390eb92e09ed9993 Mon Sep 17 00:00:00 2001 From: soartec-lab Date: Sun, 21 Jul 2024 02:53:40 +0000 Subject: [PATCH 7/7] fix: test condition for `fetch` --- tests/configs/fetch.config.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/configs/fetch.config.ts b/tests/configs/fetch.config.ts index f66a75e31..0c680da17 100644 --- a/tests/configs/fetch.config.ts +++ b/tests/configs/fetch.config.ts @@ -93,7 +93,7 @@ export default defineConfig({ output: { target: '../generated/fetch/form-data-optional-request/endpoints.ts', schemas: '../generated/fetch/form-data-optional-request/model', - client: 'swr', + client: 'fetch', mock: true, }, input: {