From a9fbc776c3ad587d3309e67619c2ed0ebae94189 Mon Sep 17 00:00:00 2001 From: soartec-lab Date: Sun, 16 Jun 2024 03:35:16 +0000 Subject: [PATCH 1/3] feat(fetch): regenerate client by `orval 6.30.2` --- samples/next-app-with-fetch/app/gen/models/cat.ts | 2 +- samples/next-app-with-fetch/app/gen/models/catType.ts | 2 +- .../app/gen/models/createPetsBodyItem.ts | 2 +- samples/next-app-with-fetch/app/gen/models/dachshund.ts | 2 +- .../next-app-with-fetch/app/gen/models/dachshundBreed.ts | 2 +- samples/next-app-with-fetch/app/gen/models/dog.ts | 2 +- samples/next-app-with-fetch/app/gen/models/dogType.ts | 2 +- samples/next-app-with-fetch/app/gen/models/error.ts | 2 +- samples/next-app-with-fetch/app/gen/models/index.ts | 2 +- samples/next-app-with-fetch/app/gen/models/labradoodle.ts | 2 +- .../app/gen/models/labradoodleBreed.ts | 2 +- .../next-app-with-fetch/app/gen/models/listPetsParams.ts | 2 +- samples/next-app-with-fetch/app/gen/models/pet.ts | 2 +- .../next-app-with-fetch/app/gen/models/petCallingCode.ts | 2 +- samples/next-app-with-fetch/app/gen/models/petCountry.ts | 2 +- samples/next-app-with-fetch/app/gen/models/pets.ts | 2 +- samples/next-app-with-fetch/app/gen/pets/pets.msw.ts | 2 +- samples/next-app-with-fetch/app/gen/pets/pets.ts | 2 +- samples/next-app-with-fetch/orval.config.ts | 8 ++++++++ 19 files changed, 26 insertions(+), 18 deletions(-) diff --git a/samples/next-app-with-fetch/app/gen/models/cat.ts b/samples/next-app-with-fetch/app/gen/models/cat.ts index 730e592d8..164c91c07 100644 --- a/samples/next-app-with-fetch/app/gen/models/cat.ts +++ b/samples/next-app-with-fetch/app/gen/models/cat.ts @@ -1,5 +1,5 @@ /** - * Generated by orval v6.29.1 🍺 + * Generated by orval v6.30.2 🍺 * Do not edit manually. * Swagger Petstore * OpenAPI spec version: 1.0.0 diff --git a/samples/next-app-with-fetch/app/gen/models/catType.ts b/samples/next-app-with-fetch/app/gen/models/catType.ts index d22309944..f36d877f8 100644 --- a/samples/next-app-with-fetch/app/gen/models/catType.ts +++ b/samples/next-app-with-fetch/app/gen/models/catType.ts @@ -1,5 +1,5 @@ /** - * Generated by orval v6.29.1 🍺 + * Generated by orval v6.30.2 🍺 * Do not edit manually. * Swagger Petstore * OpenAPI spec version: 1.0.0 diff --git a/samples/next-app-with-fetch/app/gen/models/createPetsBodyItem.ts b/samples/next-app-with-fetch/app/gen/models/createPetsBodyItem.ts index cc6cefbad..81a43b4f2 100644 --- a/samples/next-app-with-fetch/app/gen/models/createPetsBodyItem.ts +++ b/samples/next-app-with-fetch/app/gen/models/createPetsBodyItem.ts @@ -1,5 +1,5 @@ /** - * Generated by orval v6.29.1 🍺 + * Generated by orval v6.30.2 🍺 * Do not edit manually. * Swagger Petstore * OpenAPI spec version: 1.0.0 diff --git a/samples/next-app-with-fetch/app/gen/models/dachshund.ts b/samples/next-app-with-fetch/app/gen/models/dachshund.ts index d5a13044e..64bfd2f06 100644 --- a/samples/next-app-with-fetch/app/gen/models/dachshund.ts +++ b/samples/next-app-with-fetch/app/gen/models/dachshund.ts @@ -1,5 +1,5 @@ /** - * Generated by orval v6.29.1 🍺 + * Generated by orval v6.30.2 🍺 * Do not edit manually. * Swagger Petstore * OpenAPI spec version: 1.0.0 diff --git a/samples/next-app-with-fetch/app/gen/models/dachshundBreed.ts b/samples/next-app-with-fetch/app/gen/models/dachshundBreed.ts index 32487bd90..5c404dd39 100644 --- a/samples/next-app-with-fetch/app/gen/models/dachshundBreed.ts +++ b/samples/next-app-with-fetch/app/gen/models/dachshundBreed.ts @@ -1,5 +1,5 @@ /** - * Generated by orval v6.29.1 🍺 + * Generated by orval v6.30.2 🍺 * Do not edit manually. * Swagger Petstore * OpenAPI spec version: 1.0.0 diff --git a/samples/next-app-with-fetch/app/gen/models/dog.ts b/samples/next-app-with-fetch/app/gen/models/dog.ts index 68d17ced6..7076adabb 100644 --- a/samples/next-app-with-fetch/app/gen/models/dog.ts +++ b/samples/next-app-with-fetch/app/gen/models/dog.ts @@ -1,5 +1,5 @@ /** - * Generated by orval v6.29.1 🍺 + * Generated by orval v6.30.2 🍺 * Do not edit manually. * Swagger Petstore * OpenAPI spec version: 1.0.0 diff --git a/samples/next-app-with-fetch/app/gen/models/dogType.ts b/samples/next-app-with-fetch/app/gen/models/dogType.ts index fbf545c13..b14817581 100644 --- a/samples/next-app-with-fetch/app/gen/models/dogType.ts +++ b/samples/next-app-with-fetch/app/gen/models/dogType.ts @@ -1,5 +1,5 @@ /** - * Generated by orval v6.29.1 🍺 + * Generated by orval v6.30.2 🍺 * Do not edit manually. * Swagger Petstore * OpenAPI spec version: 1.0.0 diff --git a/samples/next-app-with-fetch/app/gen/models/error.ts b/samples/next-app-with-fetch/app/gen/models/error.ts index c94242768..450b4a822 100644 --- a/samples/next-app-with-fetch/app/gen/models/error.ts +++ b/samples/next-app-with-fetch/app/gen/models/error.ts @@ -1,5 +1,5 @@ /** - * Generated by orval v6.29.1 🍺 + * Generated by orval v6.30.2 🍺 * Do not edit manually. * Swagger Petstore * OpenAPI spec version: 1.0.0 diff --git a/samples/next-app-with-fetch/app/gen/models/index.ts b/samples/next-app-with-fetch/app/gen/models/index.ts index 9623ae611..a73ce00f3 100644 --- a/samples/next-app-with-fetch/app/gen/models/index.ts +++ b/samples/next-app-with-fetch/app/gen/models/index.ts @@ -1,5 +1,5 @@ /** - * Generated by orval v6.29.1 🍺 + * Generated by orval v6.30.2 🍺 * Do not edit manually. * Swagger Petstore * OpenAPI spec version: 1.0.0 diff --git a/samples/next-app-with-fetch/app/gen/models/labradoodle.ts b/samples/next-app-with-fetch/app/gen/models/labradoodle.ts index 4a6f90707..3c0d76c74 100644 --- a/samples/next-app-with-fetch/app/gen/models/labradoodle.ts +++ b/samples/next-app-with-fetch/app/gen/models/labradoodle.ts @@ -1,5 +1,5 @@ /** - * Generated by orval v6.29.1 🍺 + * Generated by orval v6.30.2 🍺 * Do not edit manually. * Swagger Petstore * OpenAPI spec version: 1.0.0 diff --git a/samples/next-app-with-fetch/app/gen/models/labradoodleBreed.ts b/samples/next-app-with-fetch/app/gen/models/labradoodleBreed.ts index f5068efc7..2fd2dac20 100644 --- a/samples/next-app-with-fetch/app/gen/models/labradoodleBreed.ts +++ b/samples/next-app-with-fetch/app/gen/models/labradoodleBreed.ts @@ -1,5 +1,5 @@ /** - * Generated by orval v6.29.1 🍺 + * Generated by orval v6.30.2 🍺 * Do not edit manually. * Swagger Petstore * OpenAPI spec version: 1.0.0 diff --git a/samples/next-app-with-fetch/app/gen/models/listPetsParams.ts b/samples/next-app-with-fetch/app/gen/models/listPetsParams.ts index dca8b0e5f..5e5c423b7 100644 --- a/samples/next-app-with-fetch/app/gen/models/listPetsParams.ts +++ b/samples/next-app-with-fetch/app/gen/models/listPetsParams.ts @@ -1,5 +1,5 @@ /** - * Generated by orval v6.29.1 🍺 + * Generated by orval v6.30.2 🍺 * Do not edit manually. * Swagger Petstore * OpenAPI spec version: 1.0.0 diff --git a/samples/next-app-with-fetch/app/gen/models/pet.ts b/samples/next-app-with-fetch/app/gen/models/pet.ts index 6bd7d9e39..9274c5b64 100644 --- a/samples/next-app-with-fetch/app/gen/models/pet.ts +++ b/samples/next-app-with-fetch/app/gen/models/pet.ts @@ -1,5 +1,5 @@ /** - * Generated by orval v6.29.1 🍺 + * Generated by orval v6.30.2 🍺 * Do not edit manually. * Swagger Petstore * OpenAPI spec version: 1.0.0 diff --git a/samples/next-app-with-fetch/app/gen/models/petCallingCode.ts b/samples/next-app-with-fetch/app/gen/models/petCallingCode.ts index d061724a1..7a357d470 100644 --- a/samples/next-app-with-fetch/app/gen/models/petCallingCode.ts +++ b/samples/next-app-with-fetch/app/gen/models/petCallingCode.ts @@ -1,5 +1,5 @@ /** - * Generated by orval v6.29.1 🍺 + * Generated by orval v6.30.2 🍺 * Do not edit manually. * Swagger Petstore * OpenAPI spec version: 1.0.0 diff --git a/samples/next-app-with-fetch/app/gen/models/petCountry.ts b/samples/next-app-with-fetch/app/gen/models/petCountry.ts index c849e23d7..1bfb22a7d 100644 --- a/samples/next-app-with-fetch/app/gen/models/petCountry.ts +++ b/samples/next-app-with-fetch/app/gen/models/petCountry.ts @@ -1,5 +1,5 @@ /** - * Generated by orval v6.29.1 🍺 + * Generated by orval v6.30.2 🍺 * Do not edit manually. * Swagger Petstore * OpenAPI spec version: 1.0.0 diff --git a/samples/next-app-with-fetch/app/gen/models/pets.ts b/samples/next-app-with-fetch/app/gen/models/pets.ts index 44c47675d..a3f9ffc85 100644 --- a/samples/next-app-with-fetch/app/gen/models/pets.ts +++ b/samples/next-app-with-fetch/app/gen/models/pets.ts @@ -1,5 +1,5 @@ /** - * Generated by orval v6.29.1 🍺 + * Generated by orval v6.30.2 🍺 * Do not edit manually. * Swagger Petstore * OpenAPI spec version: 1.0.0 diff --git a/samples/next-app-with-fetch/app/gen/pets/pets.msw.ts b/samples/next-app-with-fetch/app/gen/pets/pets.msw.ts index 2b22634b9..4d0a11df3 100644 --- a/samples/next-app-with-fetch/app/gen/pets/pets.msw.ts +++ b/samples/next-app-with-fetch/app/gen/pets/pets.msw.ts @@ -1,5 +1,5 @@ /** - * Generated by orval v6.29.1 🍺 + * Generated by orval v6.30.2 🍺 * Do not edit manually. * Swagger Petstore * OpenAPI spec version: 1.0.0 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 59061c134..6acaf84a0 100644 --- a/samples/next-app-with-fetch/app/gen/pets/pets.ts +++ b/samples/next-app-with-fetch/app/gen/pets/pets.ts @@ -1,5 +1,5 @@ /** - * Generated by orval v6.29.1 🍺 + * Generated by orval v6.30.2 🍺 * Do not edit manually. * Swagger Petstore * OpenAPI spec version: 1.0.0 diff --git a/samples/next-app-with-fetch/orval.config.ts b/samples/next-app-with-fetch/orval.config.ts index 0275a5d3f..44276b6d9 100644 --- a/samples/next-app-with-fetch/orval.config.ts +++ b/samples/next-app-with-fetch/orval.config.ts @@ -7,9 +7,17 @@ export default defineConfig({ target: 'app/gen/petstore.ts', schemas: 'app/gen/models', client: 'fetch', + // client: 'axios', baseUrl: 'http://localhost:3000', mock: true, prettier: true, + // override: { + // mutator: { + // path: './custom-fetch.ts', + // // name: 'customInstance', + // name: 'customFetch', + // }, + // }, }, input: { target: './petstore.yaml', From 235108324d35abab630b8e54357fe9adee7397bc Mon Sep 17 00:00:00 2001 From: soartec-lab Date: Sun, 16 Jun 2024 03:38:40 +0000 Subject: [PATCH 2/3] feat(fetch): support custom mutator --- packages/fetch/src/index.ts | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/packages/fetch/src/index.ts b/packages/fetch/src/index.ts index e8aefdb04..b26503aa5 100644 --- a/packages/fetch/src/index.ts +++ b/packages/fetch/src/index.ts @@ -20,6 +20,7 @@ const generateRequestFunction = ( queryParams, operationName, response, + mutator, body, props, verb, @@ -94,17 +95,15 @@ ${ ? `body: JSON.stringify(${requestBodyParams})` : ''; - const fetchResponseImplementation = `const res = await fetch( - ${getUrlFnName}(${getUrlFnProperties}), - {${globalFetchOptions ? '\n' : ''} ${globalFetchOptions} - ${isRequestOptions ? '...options,' : ''} - ${fetchMethodOption}${fetchBodyOption ? ',' : ''} - ${fetchBodyOption} - } - ) - - return res.json() + const fetchFnOptions = `${getUrlFnName}(${getUrlFnProperties}), + {${globalFetchOptions ? '\n' : ''} ${globalFetchOptions} + ${isRequestOptions ? '...options,' : ''} + ${fetchMethodOption}${fetchBodyOption ? ',' : ''} + ${fetchBodyOption} + } `; + const fetchResponseImplementation = `const res = await fetch(${fetchFnOptions})\n\nreturn res.json()`; + const customFetchResponseImplementation = `return ${mutator?.name}<${retrunType}>(${fetchFnOptions});`; const bodyForm = generateFormDataAndUrlEncodedFunction({ formData, @@ -114,8 +113,10 @@ ${ isFormUrlEncoded, }); - const fetchImplementationBody = - `${bodyForm ? ` ${bodyForm}\n` : ''}` + ` ${fetchResponseImplementation}`; + const fetchImplementationBody = mutator + ? customFetchResponseImplementation + : `${bodyForm ? ` ${bodyForm}\n` : ''}` + + ` ${fetchResponseImplementation}`; const fetchImplementation = `export const ${operationName} = async (${args}): ${retrunType} => {\n${fetchImplementationBody}}`; const implementation = From 82940caa09a3b3eaeb1026d7d82221231b1216d6 Mon Sep 17 00:00:00 2001 From: soartec-lab Date: Sun, 16 Jun 2024 03:40:07 +0000 Subject: [PATCH 3/3] chore(fetch): add custom custom mutator to sample app --- .../next-app-with-fetch/app/gen/pets/pets.ts | 17 ++---- samples/next-app-with-fetch/custom-fetch.ts | 57 +++++++++++++++++++ samples/next-app-with-fetch/orval.config.ts | 14 ++--- 3 files changed, 68 insertions(+), 20 deletions(-) create mode 100644 samples/next-app-with-fetch/custom-fetch.ts 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 6acaf84a0..0d192d8d2 100644 --- a/samples/next-app-with-fetch/app/gen/pets/pets.ts +++ b/samples/next-app-with-fetch/app/gen/pets/pets.ts @@ -10,6 +10,7 @@ import type { Pet, Pets, } from '.././models'; +import { customFetch } from '../../../custom-fetch'; // https://stackoverflow.com/questions/49579094/typescript-conditional-types-filter-out-readonly-properties-pick-only-requir/49579497#49579497 type IfEquals = @@ -60,12 +61,10 @@ export const listPets = async ( params?: ListPetsParams, options?: RequestInit, ): Promise => { - const res = await fetch(getListPetsUrl(params), { + return customFetch>(getListPetsUrl(params), { ...options, method: 'GET', }); - - return res.json(); }; /** @@ -79,13 +78,11 @@ export const createPets = async ( createPetsBodyItem: CreatePetsBodyItem[], options?: RequestInit, ): Promise => { - const res = await fetch(getCreatePetsUrl(), { + return customFetch>(getCreatePetsUrl(), { ...options, method: 'POST', body: JSON.stringify(createPetsBodyItem), }); - - return res.json(); }; /** @@ -99,13 +96,11 @@ export const updatePets = async ( pet: NonReadonly, options?: RequestInit, ): Promise => { - const res = await fetch(getUpdatePetsUrl(), { + return customFetch>(getUpdatePetsUrl(), { ...options, method: 'PUT', body: JSON.stringify(pet), }); - - return res.json(); }; /** @@ -119,10 +114,8 @@ export const showPetById = async ( petId: string, options?: RequestInit, ): Promise => { - const res = await fetch(getShowPetByIdUrl(petId), { + return customFetch>(getShowPetByIdUrl(petId), { ...options, method: 'GET', }); - - return res.json(); }; diff --git a/samples/next-app-with-fetch/custom-fetch.ts b/samples/next-app-with-fetch/custom-fetch.ts new file mode 100644 index 000000000..c39851bdb --- /dev/null +++ b/samples/next-app-with-fetch/custom-fetch.ts @@ -0,0 +1,57 @@ +// NOTE: Supports cases where `content-type` is other than `json` +const getBody = (c: Response | Request): Promise => { + const contentType = c.headers.get('content-type'); + + if (contentType && contentType.includes('application/json')) { + return c.json(); + } + + if (contentType && contentType.includes('application/pdf')) { + return c.blob() as Promise; + } + + return c.text() as Promise; +}; + +// NOTE: Update just base url +const getUrl = (contextUrl: string): string => { + const url = new URL(contextUrl); + const pathname = url.pathname; + const search = url.search; + const baseUrl = + process.env.NODE_ENV === 'production' + ? 'productionBaseUrl' + : 'http://localhost:3000'; + + const requestUrl = new URL(`${baseUrl}${pathname}${search}`); + + return requestUrl.toString(); +}; + +// NOTE: Add headers +const getHeaders = (headers?: HeadersInit): HeadersInit => { + return { + ...headers, + Authorization: 'token', + 'Content-Type': 'multipart/form-data', + }; +}; + +export const customFetch = async ( + url: string, + options: RequestInit, +): Promise => { + const requestUrl = getUrl(url); + const requestHeaders = getHeaders(options.headers); + + const requestInit: RequestInit = { + ...options, + headers: requestHeaders, + }; + + const request = new Request(requestUrl, requestInit); + const response = await fetch(request); + const data = await getBody(response); + + return data; +}; diff --git a/samples/next-app-with-fetch/orval.config.ts b/samples/next-app-with-fetch/orval.config.ts index 44276b6d9..330b2f266 100644 --- a/samples/next-app-with-fetch/orval.config.ts +++ b/samples/next-app-with-fetch/orval.config.ts @@ -7,17 +7,15 @@ export default defineConfig({ target: 'app/gen/petstore.ts', schemas: 'app/gen/models', client: 'fetch', - // client: 'axios', baseUrl: 'http://localhost:3000', mock: true, prettier: true, - // override: { - // mutator: { - // path: './custom-fetch.ts', - // // name: 'customInstance', - // name: 'customFetch', - // }, - // }, + override: { + mutator: { + path: './custom-fetch.ts', + name: 'customFetch', + }, + }, }, input: { target: './petstore.yaml',