diff --git a/packages/mock/src/msw/index.ts b/packages/mock/src/msw/index.ts index 1d25ac3b9..f8189d3f7 100644 --- a/packages/mock/src/msw/index.ts +++ b/packages/mock/src/msw/index.ts @@ -113,13 +113,9 @@ const generateDefinition = ( const delay = getDelay(override, !isFunction(mock) ? mock : undefined); const isHandlerOverridden = isReturnHttpResponse && !isTextPlain; - const infoParam = isHandlerOverridden ? 'info' : ''; + const infoParam = 'info'; const handlerImplementation = ` -export const ${handlerName} = (${ - isHandlerOverridden - ? `overrideResponse?: ${returnType} | ((${infoParam}: Parameters[1]>[0]) => Promise<${returnType}> | ${returnType})` - : '' - }) => { +export const ${handlerName} = (overrideResponse?: ${returnType} | ((${infoParam}: Parameters[1]>[0]) => Promise<${returnType}> | ${returnType})) => { return http.${verb}('${route}', ${ (isReturnHttpResponse && !isTextPlain) || delay !== false ? 'async' : '' } (${infoParam}) => {${ @@ -127,6 +123,7 @@ export const ${handlerName} = (${ ? `await delay(${isFunction(delay) ? `(${delay})()` : delay});` : '' } + ${isReturnHttpResponse ? '' : `if (typeof overrideResponse === 'function') {await overrideResponse(info); }`} return new HttpResponse(${ isReturnHttpResponse ? isTextPlain diff --git a/samples/angular-app/src/api/endpoints/pets/pets.msw.ts b/samples/angular-app/src/api/endpoints/pets/pets.msw.ts index 1582a4a5f..1223cb56d 100644 --- a/samples/angular-app/src/api/endpoints/pets/pets.msw.ts +++ b/samples/angular-app/src/api/endpoints/pets/pets.msw.ts @@ -34,6 +34,7 @@ export const getListPetsMockHandler = ( ) => { return http.get('*/v:version/pets', async (info) => { await delay(1000); + return new HttpResponse( JSON.stringify( overrideResponse !== undefined @@ -47,9 +48,18 @@ export const getListPetsMockHandler = ( }); }; -export const getCreatePetsMockHandler = () => { - return http.post('*/v:version/pets', async () => { +export const getCreatePetsMockHandler = ( + overrideResponse?: + | void + | (( + info: Parameters[1]>[0], + ) => Promise | void), +) => { + return http.post('*/v:version/pets', async (info) => { await delay(1000); + if (typeof overrideResponse === 'function') { + await overrideResponse(info); + } return new HttpResponse(null, { status: 201 }); }); }; @@ -63,6 +73,7 @@ export const getShowPetByIdMockHandler = ( ) => { return http.get('*/v:version/pets/:petId', async (info) => { await delay(1000); + return new HttpResponse( JSON.stringify( overrideResponse !== undefined diff --git a/samples/basic/api/endpoints/petstoreFromFileSpecWithTransformer.ts b/samples/basic/api/endpoints/petstoreFromFileSpecWithTransformer.ts index 66dc5a6e2..f0dbeb669 100644 --- a/samples/basic/api/endpoints/petstoreFromFileSpecWithTransformer.ts +++ b/samples/basic/api/endpoints/petstoreFromFileSpecWithTransformer.ts @@ -147,6 +147,7 @@ export const getListPetsMockHandler = ( ) => { return http.get('*/v:version/pets', async (info) => { await delay(1000); + return new HttpResponse( JSON.stringify( overrideResponse !== undefined @@ -160,9 +161,18 @@ export const getListPetsMockHandler = ( }); }; -export const getCreatePetsMockHandler = () => { - return http.post('*/v:version/pets', async () => { +export const getCreatePetsMockHandler = ( + overrideResponse?: + | void + | (( + info: Parameters[1]>[0], + ) => Promise | void), +) => { + return http.post('*/v:version/pets', async (info) => { await delay(1000); + if (typeof overrideResponse === 'function') { + await overrideResponse(info); + } return new HttpResponse(null, { status: 201 }); }); }; @@ -176,6 +186,7 @@ export const getListPetsNestedArrayMockHandler = ( ) => { return http.get('*/v:version/pets-nested-array', async (info) => { await delay(1000); + return new HttpResponse( JSON.stringify( overrideResponse !== undefined @@ -198,6 +209,7 @@ export const getShowPetByIdMockHandler = ( ) => { return http.get('*/v:version/pets/:petId', async (info) => { await delay(1000); + return new HttpResponse( JSON.stringify( overrideResponse !== undefined 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 d25944eae..7369d6129 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 @@ -418,6 +418,7 @@ export const getListPetsMockHandler = ( ) => { return http.get('*/pets', async (info) => { await delay(1000); + return new HttpResponse( JSON.stringify( overrideResponse !== undefined @@ -440,6 +441,7 @@ export const getCreatePetsMockHandler = ( ) => { return http.post('*/pets', async (info) => { await delay(1000); + return new HttpResponse( JSON.stringify( overrideResponse !== undefined @@ -462,6 +464,7 @@ export const getUpdatePetsMockHandler = ( ) => { return http.put('*/pets', async (info) => { await delay(1000); + return new HttpResponse( JSON.stringify( overrideResponse !== undefined @@ -484,6 +487,7 @@ export const getShowPetByIdMockHandler = ( ) => { return http.get('*/pets/:petId', async (info) => { await delay(1000); + return new HttpResponse( JSON.stringify( overrideResponse !== undefined diff --git a/samples/react-app-with-swr/src/api/endpoints/petstoreFromFileSpecWithTransformer.msw.ts b/samples/react-app-with-swr/src/api/endpoints/petstoreFromFileSpecWithTransformer.msw.ts index 429d01744..ee334639a 100644 --- a/samples/react-app-with-swr/src/api/endpoints/petstoreFromFileSpecWithTransformer.msw.ts +++ b/samples/react-app-with-swr/src/api/endpoints/petstoreFromFileSpecWithTransformer.msw.ts @@ -47,6 +47,7 @@ export const getListPetsMockHandler = ( ) => { return http.get('*/v:version/pets', async (info) => { await delay(1000); + return new HttpResponse( JSON.stringify( overrideResponse !== undefined @@ -69,6 +70,7 @@ export const getCreatePetsMockHandler = ( ) => { return http.post('*/v:version/pets', async (info) => { await delay(1000); + return new HttpResponse( JSON.stringify( overrideResponse !== undefined @@ -91,6 +93,7 @@ export const getShowPetByIdMockHandler = ( ) => { return http.get('*/v:version/pets/:petId', async (info) => { await delay(1000); + return new HttpResponse( JSON.stringify( overrideResponse !== undefined diff --git a/samples/react-app/src/api/endpoints/petstoreFromFileSpecWithTransformer.msw.ts b/samples/react-app/src/api/endpoints/petstoreFromFileSpecWithTransformer.msw.ts index 6fa4e436f..c1222bc67 100644 --- a/samples/react-app/src/api/endpoints/petstoreFromFileSpecWithTransformer.msw.ts +++ b/samples/react-app/src/api/endpoints/petstoreFromFileSpecWithTransformer.msw.ts @@ -34,6 +34,7 @@ export const getListPetsMockHandler = ( ) => { return http.get('*/v:version/pets', async (info) => { await delay(1000); + return new HttpResponse( JSON.stringify( overrideResponse !== undefined @@ -47,9 +48,18 @@ export const getListPetsMockHandler = ( }); }; -export const getCreatePetsMockHandler = () => { - return http.post('*/v:version/pets', async () => { +export const getCreatePetsMockHandler = ( + overrideResponse?: + | void + | (( + info: Parameters[1]>[0], + ) => Promise | void), +) => { + return http.post('*/v:version/pets', async (info) => { await delay(1000); + if (typeof overrideResponse === 'function') { + await overrideResponse(info); + } return new HttpResponse(null, { status: 201 }); }); }; @@ -63,6 +73,7 @@ export const getShowPetByIdMockHandler = ( ) => { return http.get('*/v:version/pets/:petId', async (info) => { await delay(1000); + return new HttpResponse( JSON.stringify( overrideResponse !== undefined diff --git a/samples/react-query/basic/src/api/endpoints/petstoreFromFileSpecWithTransformer.msw.ts b/samples/react-query/basic/src/api/endpoints/petstoreFromFileSpecWithTransformer.msw.ts index f18effc46..0e8940274 100644 --- a/samples/react-query/basic/src/api/endpoints/petstoreFromFileSpecWithTransformer.msw.ts +++ b/samples/react-query/basic/src/api/endpoints/petstoreFromFileSpecWithTransformer.msw.ts @@ -86,6 +86,7 @@ export const getListPetsMockHandler = ( ) => { return http.get('*/v:version/pets', async (info) => { await delay(1000); + return new HttpResponse( JSON.stringify( overrideResponse !== undefined @@ -99,9 +100,18 @@ export const getListPetsMockHandler = ( }); }; -export const getCreatePetsMockHandler = () => { - return http.post('*/v:version/pets', async () => { +export const getCreatePetsMockHandler = ( + overrideResponse?: + | void + | (( + info: Parameters[1]>[0], + ) => Promise | void), +) => { + return http.post('*/v:version/pets', async (info) => { await delay(1000); + if (typeof overrideResponse === 'function') { + await overrideResponse(info); + } return new HttpResponse(null, { status: 201 }); }); }; @@ -115,6 +125,7 @@ export const getListPetsNestedArrayMockHandler = ( ) => { return http.get('*/v:version/pets-nested-array', async (info) => { await delay(1000); + return new HttpResponse( JSON.stringify( overrideResponse !== undefined @@ -137,6 +148,7 @@ export const getShowPetByIdMockHandler = ( ) => { return http.get('*/v:version/pets/:petId', async (info) => { await delay(1000); + return new HttpResponse( JSON.stringify( overrideResponse !== undefined diff --git a/samples/react-query/custom-client/src/api/endpoints/petstoreFromFileSpecWithTransformer.msw.ts b/samples/react-query/custom-client/src/api/endpoints/petstoreFromFileSpecWithTransformer.msw.ts index f18effc46..0e8940274 100644 --- a/samples/react-query/custom-client/src/api/endpoints/petstoreFromFileSpecWithTransformer.msw.ts +++ b/samples/react-query/custom-client/src/api/endpoints/petstoreFromFileSpecWithTransformer.msw.ts @@ -86,6 +86,7 @@ export const getListPetsMockHandler = ( ) => { return http.get('*/v:version/pets', async (info) => { await delay(1000); + return new HttpResponse( JSON.stringify( overrideResponse !== undefined @@ -99,9 +100,18 @@ export const getListPetsMockHandler = ( }); }; -export const getCreatePetsMockHandler = () => { - return http.post('*/v:version/pets', async () => { +export const getCreatePetsMockHandler = ( + overrideResponse?: + | void + | (( + info: Parameters[1]>[0], + ) => Promise | void), +) => { + return http.post('*/v:version/pets', async (info) => { await delay(1000); + if (typeof overrideResponse === 'function') { + await overrideResponse(info); + } return new HttpResponse(null, { status: 201 }); }); }; @@ -115,6 +125,7 @@ export const getListPetsNestedArrayMockHandler = ( ) => { return http.get('*/v:version/pets-nested-array', async (info) => { await delay(1000); + return new HttpResponse( JSON.stringify( overrideResponse !== undefined @@ -137,6 +148,7 @@ export const getShowPetByIdMockHandler = ( ) => { return http.get('*/v:version/pets/:petId', async (info) => { await delay(1000); + return new HttpResponse( JSON.stringify( overrideResponse !== undefined diff --git a/samples/svelte-query/src/api/endpoints/petstoreFromFileSpecWithTransformer.msw.ts b/samples/svelte-query/src/api/endpoints/petstoreFromFileSpecWithTransformer.msw.ts index c11d5eed7..13ecfb767 100644 --- a/samples/svelte-query/src/api/endpoints/petstoreFromFileSpecWithTransformer.msw.ts +++ b/samples/svelte-query/src/api/endpoints/petstoreFromFileSpecWithTransformer.msw.ts @@ -34,6 +34,7 @@ export const getListPetsMockHandler = ( ) => { return http.get('*/v:version/pets', async (info) => { await delay(1000); + return new HttpResponse( JSON.stringify( overrideResponse !== undefined @@ -47,9 +48,18 @@ export const getListPetsMockHandler = ( }); }; -export const getCreatePetsMockHandler = () => { - return http.post('*/v:version/pets', async () => { +export const getCreatePetsMockHandler = ( + overrideResponse?: + | void + | (( + info: Parameters[1]>[0], + ) => Promise | void), +) => { + return http.post('*/v:version/pets', async (info) => { await delay(1000); + if (typeof overrideResponse === 'function') { + await overrideResponse(info); + } return new HttpResponse(null, { status: 201 }); }); }; @@ -63,6 +73,7 @@ export const getShowPetByIdMockHandler = ( ) => { return http.get('*/v:version/pets/:petId', async (info) => { await delay(1000); + return new HttpResponse( JSON.stringify( overrideResponse !== undefined diff --git a/samples/vue-query/src/api/endpoints/petstoreFromFileSpecWithTransformer.msw.ts b/samples/vue-query/src/api/endpoints/petstoreFromFileSpecWithTransformer.msw.ts index 16ca132bc..59b378826 100644 --- a/samples/vue-query/src/api/endpoints/petstoreFromFileSpecWithTransformer.msw.ts +++ b/samples/vue-query/src/api/endpoints/petstoreFromFileSpecWithTransformer.msw.ts @@ -45,6 +45,7 @@ export const getListPetsMockHandler = ( ) => { return http.get('*/v:version/pets', async (info) => { await delay(1000); + return new HttpResponse( JSON.stringify( overrideResponse !== undefined @@ -67,6 +68,7 @@ export const getCreatePetsMockHandler = ( ) => { return http.post('*/v:version/pets', async (info) => { await delay(1000); + return new HttpResponse( JSON.stringify( overrideResponse !== undefined @@ -89,6 +91,7 @@ export const getShowPetByIdMockHandler = ( ) => { return http.get('*/v:version/pets/:petId', async (info) => { await delay(1000); + return new HttpResponse( JSON.stringify( overrideResponse !== undefined @@ -102,9 +105,18 @@ export const getShowPetByIdMockHandler = ( }); }; -export const getPostApiV1UserLogoutMockHandler = () => { - return http.post('*/api/v1/user/logout', async () => { +export const getPostApiV1UserLogoutMockHandler = ( + overrideResponse?: + | void + | (( + info: Parameters[1]>[0], + ) => Promise | void), +) => { + return http.post('*/api/v1/user/logout', async (info) => { await delay(1000); + if (typeof overrideResponse === 'function') { + await overrideResponse(info); + } return new HttpResponse(null, { status: 200 }); }); }; diff --git a/tests/specifications/any-of.yaml b/tests/specifications/any-of.yaml index 3ef725fe4..c324717ae 100644 --- a/tests/specifications/any-of.yaml +++ b/tests/specifications/any-of.yaml @@ -19,6 +19,9 @@ paths: enum: ['B'] - type: string enum: ['C'] + responses: + '204': + description: Ok components: schemas: A: