From cb666f4066ce63d36ccec5ab6a952ba56c1db6fe Mon Sep 17 00:00:00 2001 From: Stijn Van Hulle Date: Thu, 5 Dec 2024 20:24:12 +0100 Subject: [PATCH] feat: Support dynamic and customizable data in handlers --- .changeset/clean-wasps-rush.md | 5 ++ docs/changelog.md | 20 ++++++++ .../gen/msw/petController/addPetHandler.ts | 3 +- .../gen/msw/petController/deletePetHandler.ts | 3 +- .../petController/findPetsByStatusHandler.ts | 5 +- .../petController/findPetsByTagsHandler.ts | 3 +- .../msw/petController/getPetByIdHandler.ts | 3 +- .../gen/msw/petController/updatePetHandler.ts | 3 +- .../petController/updatePetWithFormHandler.ts | 3 +- .../msw/petController/uploadFileHandler.ts | 3 +- .../msw/petsController/createPetsHandler.ts | 3 +- .../msw/userController/createUserHandler.ts | 3 +- .../createUsersWithListInputHandler.ts | 5 +- .../msw/userController/deleteUserHandler.ts | 3 +- .../userController/getUserByNameHandler.ts | 3 +- .../msw/userController/loginUserHandler.ts | 3 +- .../msw/userController/logoutUserHandler.ts | 3 +- .../msw/userController/updateUserHandler.ts | 3 +- .../src/gen/msw/pet/Handlers/addPetHandler.ts | 3 +- .../gen/msw/pet/Handlers/deletePetHandler.ts | 3 +- .../pet/Handlers/findPetsByStatusHandler.ts | 3 +- .../msw/pet/Handlers/findPetsByTagsHandler.ts | 3 +- .../gen/msw/pet/Handlers/getPetByIdHandler.ts | 3 +- .../optionsFindPetsByStatusHandler.ts | 5 +- .../gen/msw/pet/Handlers/updatePetHandler.ts | 3 +- .../pet/Handlers/updatePetWithFormHandler.ts | 3 +- .../gen/msw/pet/Handlers/uploadFileHandler.ts | 3 +- .../msw/store/Handlers/deleteOrderHandler.ts | 3 +- .../msw/store/Handlers/getInventoryHandler.ts | 3 +- .../msw/store/Handlers/getOrderByIdHandler.ts | 3 +- .../msw/store/Handlers/placeOrderHandler.ts | 3 +- .../store/Handlers/placeOrderPatchHandler.ts | 3 +- .../msw/user/Handlers/createUserHandler.ts | 3 +- .../createUsersWithListInputHandler.ts | 5 +- .../msw/user/Handlers/deleteUserHandler.ts | 3 +- .../msw/user/Handlers/getUserByNameHandler.ts | 3 +- .../gen/msw/user/Handlers/loginUserHandler.ts | 3 +- .../msw/user/Handlers/logoutUserHandler.ts | 3 +- .../msw/user/Handlers/updateUserHandler.ts | 3 +- examples/simple-single/src/gen2/index.ts | 51 +++++++++++++++++++ packages/plugin-msw/src/components/Mock.tsx | 8 ++- .../src/components/MockWithFaker.tsx | 8 ++- .../src/generators/__snapshots__/createPet.ts | 4 +- .../src/generators/__snapshots__/deletePet.ts | 4 +- .../src/generators/__snapshots__/getPets.ts | 4 +- .../generators/__snapshots__/getPetsFaker.ts | 6 ++- .../generators/__snapshots__/showPetById.ts | 4 +- .../src/generators/mswGenerator.tsx | 3 +- 48 files changed, 187 insertions(+), 49 deletions(-) create mode 100644 .changeset/clean-wasps-rush.md diff --git a/.changeset/clean-wasps-rush.md b/.changeset/clean-wasps-rush.md new file mode 100644 index 000000000..6b44ddeb6 --- /dev/null +++ b/.changeset/clean-wasps-rush.md @@ -0,0 +1,5 @@ +--- +"@kubb/plugin-msw": minor +--- + +Support dynamic and customizable data in handlers diff --git a/docs/changelog.md b/docs/changelog.md index a36b4bc06..ff5bdffed 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -4,7 +4,27 @@ title: Changelog # Changelog +## 3.1.0 +- [`plugin-msw`](/plugins/plugin-msw): Support dynamic and customizable data in handlers +```typescript +findPetsByStatusHandler((info) => { + const { params } = info + if (params.someKey) { + return new Response( + JSON.stringify({ error: 'some error response' }), + { status: 400 } + ); + } + return new Response( + JSON.stringify({ newData: 'new data' }), + { status: 200 } + ); +}) + +``` + ## 3.0.14 +- [`core`](/plugins/core): Upgrade packages ## 3.0.13 - [`core`](/plugins/core): Upgrade packages diff --git a/examples/advanced/src/gen/msw/petController/addPetHandler.ts b/examples/advanced/src/gen/msw/petController/addPetHandler.ts index 9748fcefb..57b2382f7 100644 --- a/examples/advanced/src/gen/msw/petController/addPetHandler.ts +++ b/examples/advanced/src/gen/msw/petController/addPetHandler.ts @@ -1,8 +1,9 @@ import type { AddPetMutationResponse } from '../../models/ts/petController/AddPet.ts' import { http } from 'msw' -export function addPetHandler(data?: AddPetMutationResponse) { +export function addPetHandler(data?: AddPetMutationResponse | ((info: Parameters[1]>[0]) => Response)) { return http.post('*/pet', function handler(info) { + if (typeof data === 'function') return data(info) return new Response(JSON.stringify(data), { headers: { 'Content-Type': 'application/json', diff --git a/examples/advanced/src/gen/msw/petController/deletePetHandler.ts b/examples/advanced/src/gen/msw/petController/deletePetHandler.ts index 8f48bcb44..b90646321 100644 --- a/examples/advanced/src/gen/msw/petController/deletePetHandler.ts +++ b/examples/advanced/src/gen/msw/petController/deletePetHandler.ts @@ -1,8 +1,9 @@ import type { DeletePetMutationResponse } from '../../models/ts/petController/DeletePet.ts' import { http } from 'msw' -export function deletePetHandler(data?: DeletePetMutationResponse) { +export function deletePetHandler(data?: DeletePetMutationResponse | ((info: Parameters[1]>[0]) => Response)) { return http.delete('*/pet/:petId', function handler(info) { + if (typeof data === 'function') return data(info) return new Response(JSON.stringify(data), { headers: { 'Content-Type': 'application/json', diff --git a/examples/advanced/src/gen/msw/petController/findPetsByStatusHandler.ts b/examples/advanced/src/gen/msw/petController/findPetsByStatusHandler.ts index 870d3d9cd..cfeb727a8 100644 --- a/examples/advanced/src/gen/msw/petController/findPetsByStatusHandler.ts +++ b/examples/advanced/src/gen/msw/petController/findPetsByStatusHandler.ts @@ -1,8 +1,9 @@ -import type { FindPetsByStatusQueryResponse } from '../../models/ts/petController/FindPetsByStatus.ts' import { http } from 'msw' +import type { FindPetsByStatusQueryResponse } from '../../models/ts/petController/FindPetsByStatus.ts' -export function findPetsByStatusHandler(data?: FindPetsByStatusQueryResponse) { +export function findPetsByStatusHandler(data?: FindPetsByStatusQueryResponse | ((info: Parameters[1]>[0]) => Response)) { return http.get('*/pet/findByStatus/:step_id', function handler(info) { + if (typeof data === 'function') return data(info) return new Response(JSON.stringify(data), { headers: { 'Content-Type': 'application/json', diff --git a/examples/advanced/src/gen/msw/petController/findPetsByTagsHandler.ts b/examples/advanced/src/gen/msw/petController/findPetsByTagsHandler.ts index ef8ee289b..eaa9a545e 100644 --- a/examples/advanced/src/gen/msw/petController/findPetsByTagsHandler.ts +++ b/examples/advanced/src/gen/msw/petController/findPetsByTagsHandler.ts @@ -1,8 +1,9 @@ import type { FindPetsByTagsQueryResponse } from '../../models/ts/petController/FindPetsByTags.ts' import { http } from 'msw' -export function findPetsByTagsHandler(data?: FindPetsByTagsQueryResponse) { +export function findPetsByTagsHandler(data?: FindPetsByTagsQueryResponse | ((info: Parameters[1]>[0]) => Response)) { return http.get('*/pet/findByTags', function handler(info) { + if (typeof data === 'function') return data(info) return new Response(JSON.stringify(data), { headers: { 'Content-Type': 'application/json', diff --git a/examples/advanced/src/gen/msw/petController/getPetByIdHandler.ts b/examples/advanced/src/gen/msw/petController/getPetByIdHandler.ts index bf24888bf..15b15f996 100644 --- a/examples/advanced/src/gen/msw/petController/getPetByIdHandler.ts +++ b/examples/advanced/src/gen/msw/petController/getPetByIdHandler.ts @@ -1,8 +1,9 @@ import type { GetPetByIdQueryResponse } from '../../models/ts/petController/GetPetById.ts' import { http } from 'msw' -export function getPetByIdHandler(data?: GetPetByIdQueryResponse) { +export function getPetByIdHandler(data?: GetPetByIdQueryResponse | ((info: Parameters[1]>[0]) => Response)) { return http.get('*/pet/:petId', function handler(info) { + if (typeof data === 'function') return data(info) return new Response(JSON.stringify(data), { headers: { 'Content-Type': 'application/json', diff --git a/examples/advanced/src/gen/msw/petController/updatePetHandler.ts b/examples/advanced/src/gen/msw/petController/updatePetHandler.ts index 9d4ae647c..40dd53a4d 100644 --- a/examples/advanced/src/gen/msw/petController/updatePetHandler.ts +++ b/examples/advanced/src/gen/msw/petController/updatePetHandler.ts @@ -1,8 +1,9 @@ import type { UpdatePetMutationResponse } from '../../models/ts/petController/UpdatePet.ts' import { http } from 'msw' -export function updatePetHandler(data?: UpdatePetMutationResponse) { +export function updatePetHandler(data?: UpdatePetMutationResponse | ((info: Parameters[1]>[0]) => Response)) { return http.put('*/pet', function handler(info) { + if (typeof data === 'function') return data(info) return new Response(JSON.stringify(data), { headers: { 'Content-Type': 'application/json', diff --git a/examples/advanced/src/gen/msw/petController/updatePetWithFormHandler.ts b/examples/advanced/src/gen/msw/petController/updatePetWithFormHandler.ts index 3268afb55..82ac082d3 100644 --- a/examples/advanced/src/gen/msw/petController/updatePetWithFormHandler.ts +++ b/examples/advanced/src/gen/msw/petController/updatePetWithFormHandler.ts @@ -1,8 +1,9 @@ import type { UpdatePetWithFormMutationResponse } from '../../models/ts/petController/UpdatePetWithForm.ts' import { http } from 'msw' -export function updatePetWithFormHandler(data?: UpdatePetWithFormMutationResponse) { +export function updatePetWithFormHandler(data?: UpdatePetWithFormMutationResponse | ((info: Parameters[1]>[0]) => Response)) { return http.post('*/pet/:petId', function handler(info) { + if (typeof data === 'function') return data(info) return new Response(JSON.stringify(data), { headers: { 'Content-Type': 'application/json', diff --git a/examples/advanced/src/gen/msw/petController/uploadFileHandler.ts b/examples/advanced/src/gen/msw/petController/uploadFileHandler.ts index bed6db43b..e5cbcca58 100644 --- a/examples/advanced/src/gen/msw/petController/uploadFileHandler.ts +++ b/examples/advanced/src/gen/msw/petController/uploadFileHandler.ts @@ -1,8 +1,9 @@ import type { UploadFileMutationResponse } from '../../models/ts/petController/UploadFile.ts' import { http } from 'msw' -export function uploadFileHandler(data?: UploadFileMutationResponse) { +export function uploadFileHandler(data?: UploadFileMutationResponse | ((info: Parameters[1]>[0]) => Response)) { return http.post('*/pet/:petId/uploadImage', function handler(info) { + if (typeof data === 'function') return data(info) return new Response(JSON.stringify(data), { headers: { 'Content-Type': 'application/json', diff --git a/examples/advanced/src/gen/msw/petsController/createPetsHandler.ts b/examples/advanced/src/gen/msw/petsController/createPetsHandler.ts index b51425b7f..b6ee756f9 100644 --- a/examples/advanced/src/gen/msw/petsController/createPetsHandler.ts +++ b/examples/advanced/src/gen/msw/petsController/createPetsHandler.ts @@ -1,8 +1,9 @@ import type { CreatePetsMutationResponse } from '../../models/ts/petsController/CreatePets.ts' import { http } from 'msw' -export function createPetsHandler(data?: CreatePetsMutationResponse) { +export function createPetsHandler(data?: CreatePetsMutationResponse | ((info: Parameters[1]>[0]) => Response)) { return http.post('*/pets/:uuid', function handler(info) { + if (typeof data === 'function') return data(info) return new Response(JSON.stringify(data), { headers: { 'Content-Type': 'application/json', diff --git a/examples/advanced/src/gen/msw/userController/createUserHandler.ts b/examples/advanced/src/gen/msw/userController/createUserHandler.ts index f8b781909..15ac08a25 100644 --- a/examples/advanced/src/gen/msw/userController/createUserHandler.ts +++ b/examples/advanced/src/gen/msw/userController/createUserHandler.ts @@ -1,8 +1,9 @@ import type { CreateUserMutationResponse } from '../../models/ts/userController/CreateUser.ts' import { http } from 'msw' -export function createUserHandler(data?: CreateUserMutationResponse) { +export function createUserHandler(data?: CreateUserMutationResponse | ((info: Parameters[1]>[0]) => Response)) { return http.post('*/user', function handler(info) { + if (typeof data === 'function') return data(info) return new Response(JSON.stringify(data), { headers: { 'Content-Type': 'application/json', diff --git a/examples/advanced/src/gen/msw/userController/createUsersWithListInputHandler.ts b/examples/advanced/src/gen/msw/userController/createUsersWithListInputHandler.ts index 1f9642eb8..f9493c379 100644 --- a/examples/advanced/src/gen/msw/userController/createUsersWithListInputHandler.ts +++ b/examples/advanced/src/gen/msw/userController/createUsersWithListInputHandler.ts @@ -1,8 +1,11 @@ import type { CreateUsersWithListInputMutationResponse } from '../../models/ts/userController/CreateUsersWithListInput.ts' import { http } from 'msw' -export function createUsersWithListInputHandler(data?: CreateUsersWithListInputMutationResponse) { +export function createUsersWithListInputHandler( + data?: CreateUsersWithListInputMutationResponse | ((info: Parameters[1]>[0]) => Response), +) { return http.post('*/user/createWithList', function handler(info) { + if (typeof data === 'function') return data(info) return new Response(JSON.stringify(data), { headers: { 'Content-Type': 'application/json', diff --git a/examples/advanced/src/gen/msw/userController/deleteUserHandler.ts b/examples/advanced/src/gen/msw/userController/deleteUserHandler.ts index 885f2e604..91c7f92ce 100644 --- a/examples/advanced/src/gen/msw/userController/deleteUserHandler.ts +++ b/examples/advanced/src/gen/msw/userController/deleteUserHandler.ts @@ -1,8 +1,9 @@ import type { DeleteUserMutationResponse } from '../../models/ts/userController/DeleteUser.ts' import { http } from 'msw' -export function deleteUserHandler(data?: DeleteUserMutationResponse) { +export function deleteUserHandler(data?: DeleteUserMutationResponse | ((info: Parameters[1]>[0]) => Response)) { return http.delete('*/user/:username', function handler(info) { + if (typeof data === 'function') return data(info) return new Response(JSON.stringify(data), { headers: { 'Content-Type': 'application/json', diff --git a/examples/advanced/src/gen/msw/userController/getUserByNameHandler.ts b/examples/advanced/src/gen/msw/userController/getUserByNameHandler.ts index 7a38d57bf..890a34fd6 100644 --- a/examples/advanced/src/gen/msw/userController/getUserByNameHandler.ts +++ b/examples/advanced/src/gen/msw/userController/getUserByNameHandler.ts @@ -1,8 +1,9 @@ import type { GetUserByNameQueryResponse } from '../../models/ts/userController/GetUserByName.ts' import { http } from 'msw' -export function getUserByNameHandler(data?: GetUserByNameQueryResponse) { +export function getUserByNameHandler(data?: GetUserByNameQueryResponse | ((info: Parameters[1]>[0]) => Response)) { return http.get('*/user/:username', function handler(info) { + if (typeof data === 'function') return data(info) return new Response(JSON.stringify(data), { headers: { 'Content-Type': 'application/json', diff --git a/examples/advanced/src/gen/msw/userController/loginUserHandler.ts b/examples/advanced/src/gen/msw/userController/loginUserHandler.ts index 6ab16ceaa..cef501bb0 100644 --- a/examples/advanced/src/gen/msw/userController/loginUserHandler.ts +++ b/examples/advanced/src/gen/msw/userController/loginUserHandler.ts @@ -1,8 +1,9 @@ import type { LoginUserQueryResponse } from '../../models/ts/userController/LoginUser.ts' import { http } from 'msw' -export function loginUserHandler(data?: LoginUserQueryResponse) { +export function loginUserHandler(data?: LoginUserQueryResponse | ((info: Parameters[1]>[0]) => Response)) { return http.get('*/user/login', function handler(info) { + if (typeof data === 'function') return data(info) return new Response(JSON.stringify(data), { headers: { 'Content-Type': 'application/json', diff --git a/examples/advanced/src/gen/msw/userController/logoutUserHandler.ts b/examples/advanced/src/gen/msw/userController/logoutUserHandler.ts index 74e3d4e74..997230766 100644 --- a/examples/advanced/src/gen/msw/userController/logoutUserHandler.ts +++ b/examples/advanced/src/gen/msw/userController/logoutUserHandler.ts @@ -1,8 +1,9 @@ import type { LogoutUserQueryResponse } from '../../models/ts/userController/LogoutUser.ts' import { http } from 'msw' -export function logoutUserHandler(data?: LogoutUserQueryResponse) { +export function logoutUserHandler(data?: LogoutUserQueryResponse | ((info: Parameters[1]>[0]) => Response)) { return http.get('*/user/logout', function handler(info) { + if (typeof data === 'function') return data(info) return new Response(JSON.stringify(data), { headers: { 'Content-Type': 'application/json', diff --git a/examples/advanced/src/gen/msw/userController/updateUserHandler.ts b/examples/advanced/src/gen/msw/userController/updateUserHandler.ts index b73d3b294..00f47b8ae 100644 --- a/examples/advanced/src/gen/msw/userController/updateUserHandler.ts +++ b/examples/advanced/src/gen/msw/userController/updateUserHandler.ts @@ -1,8 +1,9 @@ import type { UpdateUserMutationResponse } from '../../models/ts/userController/UpdateUser.ts' import { http } from 'msw' -export function updateUserHandler(data?: UpdateUserMutationResponse) { +export function updateUserHandler(data?: UpdateUserMutationResponse | ((info: Parameters[1]>[0]) => Response)) { return http.put('*/user/:username', function handler(info) { + if (typeof data === 'function') return data(info) return new Response(JSON.stringify(data), { headers: { 'Content-Type': 'application/json', diff --git a/examples/msw/src/gen/msw/pet/Handlers/addPetHandler.ts b/examples/msw/src/gen/msw/pet/Handlers/addPetHandler.ts index c78b0987a..65356c4fc 100644 --- a/examples/msw/src/gen/msw/pet/Handlers/addPetHandler.ts +++ b/examples/msw/src/gen/msw/pet/Handlers/addPetHandler.ts @@ -1,8 +1,9 @@ import type { AddPetMutationResponse } from '../../../models/AddPet.ts' import { http } from 'msw' -export function addPetHandler(data?: AddPetMutationResponse) { +export function addPetHandler(data?: AddPetMutationResponse | ((info: Parameters[1]>[0]) => Response)) { return http.post('*/pet', function handler(info) { + if (typeof data === 'function') return data(info) return new Response(JSON.stringify(data), { headers: { 'Content-Type': 'application/json', diff --git a/examples/msw/src/gen/msw/pet/Handlers/deletePetHandler.ts b/examples/msw/src/gen/msw/pet/Handlers/deletePetHandler.ts index 51304baf9..299163155 100644 --- a/examples/msw/src/gen/msw/pet/Handlers/deletePetHandler.ts +++ b/examples/msw/src/gen/msw/pet/Handlers/deletePetHandler.ts @@ -1,8 +1,9 @@ import type { DeletePetMutationResponse } from '../../../models/DeletePet.ts' import { http } from 'msw' -export function deletePetHandler(data?: DeletePetMutationResponse) { +export function deletePetHandler(data?: DeletePetMutationResponse | ((info: Parameters[1]>[0]) => Response)) { return http.delete('*/pet/:petId', function handler(info) { + if (typeof data === 'function') return data(info) return new Response(JSON.stringify(data), { headers: { 'Content-Type': 'application/json', diff --git a/examples/msw/src/gen/msw/pet/Handlers/findPetsByStatusHandler.ts b/examples/msw/src/gen/msw/pet/Handlers/findPetsByStatusHandler.ts index 697976f22..d2491bdd7 100644 --- a/examples/msw/src/gen/msw/pet/Handlers/findPetsByStatusHandler.ts +++ b/examples/msw/src/gen/msw/pet/Handlers/findPetsByStatusHandler.ts @@ -1,8 +1,9 @@ import type { FindPetsByStatusQueryResponse } from '../../../models/FindPetsByStatus.ts' import { http } from 'msw' -export function findPetsByStatusHandler(data?: FindPetsByStatusQueryResponse) { +export function findPetsByStatusHandler(data?: FindPetsByStatusQueryResponse | ((info: Parameters[1]>[0]) => Response)) { return http.get('*/pet/findByStatus', function handler(info) { + if (typeof data === 'function') return data(info) return new Response(JSON.stringify(data), { headers: { 'Content-Type': 'application/json', diff --git a/examples/msw/src/gen/msw/pet/Handlers/findPetsByTagsHandler.ts b/examples/msw/src/gen/msw/pet/Handlers/findPetsByTagsHandler.ts index b99cc5f93..0e544985e 100644 --- a/examples/msw/src/gen/msw/pet/Handlers/findPetsByTagsHandler.ts +++ b/examples/msw/src/gen/msw/pet/Handlers/findPetsByTagsHandler.ts @@ -1,8 +1,9 @@ import type { FindPetsByTagsQueryResponse } from '../../../models/FindPetsByTags.ts' import { http } from 'msw' -export function findPetsByTagsHandler(data?: FindPetsByTagsQueryResponse) { +export function findPetsByTagsHandler(data?: FindPetsByTagsQueryResponse | ((info: Parameters[1]>[0]) => Response)) { return http.get('*/pet/findByTags', function handler(info) { + if (typeof data === 'function') return data(info) return new Response(JSON.stringify(data), { headers: { 'Content-Type': 'application/json', diff --git a/examples/msw/src/gen/msw/pet/Handlers/getPetByIdHandler.ts b/examples/msw/src/gen/msw/pet/Handlers/getPetByIdHandler.ts index b2e5648d5..32dc71c44 100644 --- a/examples/msw/src/gen/msw/pet/Handlers/getPetByIdHandler.ts +++ b/examples/msw/src/gen/msw/pet/Handlers/getPetByIdHandler.ts @@ -1,8 +1,9 @@ import type { GetPetByIdQueryResponse } from '../../../models/GetPetById.ts' import { http } from 'msw' -export function getPetByIdHandler(data?: GetPetByIdQueryResponse) { +export function getPetByIdHandler(data?: GetPetByIdQueryResponse | ((info: Parameters[1]>[0]) => Response)) { return http.get('*/pet/:petId', function handler(info) { + if (typeof data === 'function') return data(info) return new Response(JSON.stringify(data), { headers: { 'Content-Type': 'application/json', diff --git a/examples/msw/src/gen/msw/pet/Handlers/optionsFindPetsByStatusHandler.ts b/examples/msw/src/gen/msw/pet/Handlers/optionsFindPetsByStatusHandler.ts index d8927be7b..b33861d19 100644 --- a/examples/msw/src/gen/msw/pet/Handlers/optionsFindPetsByStatusHandler.ts +++ b/examples/msw/src/gen/msw/pet/Handlers/optionsFindPetsByStatusHandler.ts @@ -1,8 +1,11 @@ import type { OptionsFindPetsByStatusMutationResponse } from '../../../models/OptionsFindPetsByStatus.ts' import { http } from 'msw' -export function optionsFindPetsByStatusHandler(data?: OptionsFindPetsByStatusMutationResponse) { +export function optionsFindPetsByStatusHandler( + data?: OptionsFindPetsByStatusMutationResponse | ((info: Parameters[1]>[0]) => Response), +) { return http.options('*/pet/findByStatus', function handler(info) { + if (typeof data === 'function') return data(info) return new Response(JSON.stringify(data), { headers: { 'Content-Type': 'application/json', diff --git a/examples/msw/src/gen/msw/pet/Handlers/updatePetHandler.ts b/examples/msw/src/gen/msw/pet/Handlers/updatePetHandler.ts index a43b421eb..4165a4a3c 100644 --- a/examples/msw/src/gen/msw/pet/Handlers/updatePetHandler.ts +++ b/examples/msw/src/gen/msw/pet/Handlers/updatePetHandler.ts @@ -1,8 +1,9 @@ import type { UpdatePetMutationResponse } from '../../../models/UpdatePet.ts' import { http } from 'msw' -export function updatePetHandler(data?: UpdatePetMutationResponse) { +export function updatePetHandler(data?: UpdatePetMutationResponse | ((info: Parameters[1]>[0]) => Response)) { return http.put('*/pet', function handler(info) { + if (typeof data === 'function') return data(info) return new Response(JSON.stringify(data), { headers: { 'Content-Type': 'application/json', diff --git a/examples/msw/src/gen/msw/pet/Handlers/updatePetWithFormHandler.ts b/examples/msw/src/gen/msw/pet/Handlers/updatePetWithFormHandler.ts index dcaa368a6..790834719 100644 --- a/examples/msw/src/gen/msw/pet/Handlers/updatePetWithFormHandler.ts +++ b/examples/msw/src/gen/msw/pet/Handlers/updatePetWithFormHandler.ts @@ -1,8 +1,9 @@ import type { UpdatePetWithFormMutationResponse } from '../../../models/UpdatePetWithForm.ts' import { http } from 'msw' -export function updatePetWithFormHandler(data?: UpdatePetWithFormMutationResponse) { +export function updatePetWithFormHandler(data?: UpdatePetWithFormMutationResponse | ((info: Parameters[1]>[0]) => Response)) { return http.post('*/pet/:petId', function handler(info) { + if (typeof data === 'function') return data(info) return new Response(JSON.stringify(data), { headers: { 'Content-Type': 'application/json', diff --git a/examples/msw/src/gen/msw/pet/Handlers/uploadFileHandler.ts b/examples/msw/src/gen/msw/pet/Handlers/uploadFileHandler.ts index 701d40bf2..ced4b0d87 100644 --- a/examples/msw/src/gen/msw/pet/Handlers/uploadFileHandler.ts +++ b/examples/msw/src/gen/msw/pet/Handlers/uploadFileHandler.ts @@ -1,8 +1,9 @@ import type { UploadFileMutationResponse } from '../../../models/UploadFile.ts' import { http } from 'msw' -export function uploadFileHandler(data?: UploadFileMutationResponse) { +export function uploadFileHandler(data?: UploadFileMutationResponse | ((info: Parameters[1]>[0]) => Response)) { return http.post('*/pet/:petId/uploadImage', function handler(info) { + if (typeof data === 'function') return data(info) return new Response(JSON.stringify(data), { headers: { 'Content-Type': 'application/json', diff --git a/examples/msw/src/gen/msw/store/Handlers/deleteOrderHandler.ts b/examples/msw/src/gen/msw/store/Handlers/deleteOrderHandler.ts index a11672e58..0ff127119 100644 --- a/examples/msw/src/gen/msw/store/Handlers/deleteOrderHandler.ts +++ b/examples/msw/src/gen/msw/store/Handlers/deleteOrderHandler.ts @@ -1,8 +1,9 @@ import type { DeleteOrderMutationResponse } from '../../../models/DeleteOrder.ts' import { http } from 'msw' -export function deleteOrderHandler(data?: DeleteOrderMutationResponse) { +export function deleteOrderHandler(data?: DeleteOrderMutationResponse | ((info: Parameters[1]>[0]) => Response)) { return http.delete('*/store/order/:orderId', function handler(info) { + if (typeof data === 'function') return data(info) return new Response(JSON.stringify(data), { headers: { 'Content-Type': 'application/json', diff --git a/examples/msw/src/gen/msw/store/Handlers/getInventoryHandler.ts b/examples/msw/src/gen/msw/store/Handlers/getInventoryHandler.ts index 411b1ac89..e704fde43 100644 --- a/examples/msw/src/gen/msw/store/Handlers/getInventoryHandler.ts +++ b/examples/msw/src/gen/msw/store/Handlers/getInventoryHandler.ts @@ -1,8 +1,9 @@ import type { GetInventoryQueryResponse } from '../../../models/GetInventory.ts' import { http } from 'msw' -export function getInventoryHandler(data?: GetInventoryQueryResponse) { +export function getInventoryHandler(data?: GetInventoryQueryResponse | ((info: Parameters[1]>[0]) => Response)) { return http.get('*/store/inventory', function handler(info) { + if (typeof data === 'function') return data(info) return new Response(JSON.stringify(data), { headers: { 'Content-Type': 'application/json', diff --git a/examples/msw/src/gen/msw/store/Handlers/getOrderByIdHandler.ts b/examples/msw/src/gen/msw/store/Handlers/getOrderByIdHandler.ts index 4e2d30ec2..48959a972 100644 --- a/examples/msw/src/gen/msw/store/Handlers/getOrderByIdHandler.ts +++ b/examples/msw/src/gen/msw/store/Handlers/getOrderByIdHandler.ts @@ -1,8 +1,9 @@ import type { GetOrderByIdQueryResponse } from '../../../models/GetOrderById.ts' import { http } from 'msw' -export function getOrderByIdHandler(data?: GetOrderByIdQueryResponse) { +export function getOrderByIdHandler(data?: GetOrderByIdQueryResponse | ((info: Parameters[1]>[0]) => Response)) { return http.get('*/store/order/:orderId', function handler(info) { + if (typeof data === 'function') return data(info) return new Response(JSON.stringify(data), { headers: { 'Content-Type': 'application/json', diff --git a/examples/msw/src/gen/msw/store/Handlers/placeOrderHandler.ts b/examples/msw/src/gen/msw/store/Handlers/placeOrderHandler.ts index 34d03ecc2..9bbfd0c5c 100644 --- a/examples/msw/src/gen/msw/store/Handlers/placeOrderHandler.ts +++ b/examples/msw/src/gen/msw/store/Handlers/placeOrderHandler.ts @@ -1,8 +1,9 @@ import type { PlaceOrderMutationResponse } from '../../../models/PlaceOrder.ts' import { http } from 'msw' -export function placeOrderHandler(data?: PlaceOrderMutationResponse) { +export function placeOrderHandler(data?: PlaceOrderMutationResponse | ((info: Parameters[1]>[0]) => Response)) { return http.post('*/store/order', function handler(info) { + if (typeof data === 'function') return data(info) return new Response(JSON.stringify(data), { headers: { 'Content-Type': 'application/json', diff --git a/examples/msw/src/gen/msw/store/Handlers/placeOrderPatchHandler.ts b/examples/msw/src/gen/msw/store/Handlers/placeOrderPatchHandler.ts index aa8963b64..27f4b7f12 100644 --- a/examples/msw/src/gen/msw/store/Handlers/placeOrderPatchHandler.ts +++ b/examples/msw/src/gen/msw/store/Handlers/placeOrderPatchHandler.ts @@ -1,8 +1,9 @@ import type { PlaceOrderPatchMutationResponse } from '../../../models/PlaceOrderPatch.ts' import { http } from 'msw' -export function placeOrderPatchHandler(data?: PlaceOrderPatchMutationResponse) { +export function placeOrderPatchHandler(data?: PlaceOrderPatchMutationResponse | ((info: Parameters[1]>[0]) => Response)) { return http.patch('*/store/order', function handler(info) { + if (typeof data === 'function') return data(info) return new Response(JSON.stringify(data), { headers: { 'Content-Type': 'application/json', diff --git a/examples/msw/src/gen/msw/user/Handlers/createUserHandler.ts b/examples/msw/src/gen/msw/user/Handlers/createUserHandler.ts index a36236c6a..20e9f370a 100644 --- a/examples/msw/src/gen/msw/user/Handlers/createUserHandler.ts +++ b/examples/msw/src/gen/msw/user/Handlers/createUserHandler.ts @@ -1,8 +1,9 @@ import type { CreateUserMutationResponse } from '../../../models/CreateUser.ts' import { http } from 'msw' -export function createUserHandler(data?: CreateUserMutationResponse) { +export function createUserHandler(data?: CreateUserMutationResponse | ((info: Parameters[1]>[0]) => Response)) { return http.post('*/user', function handler(info) { + if (typeof data === 'function') return data(info) return new Response(JSON.stringify(data), { headers: { 'Content-Type': 'application/json', diff --git a/examples/msw/src/gen/msw/user/Handlers/createUsersWithListInputHandler.ts b/examples/msw/src/gen/msw/user/Handlers/createUsersWithListInputHandler.ts index c9e5f5573..e8820582e 100644 --- a/examples/msw/src/gen/msw/user/Handlers/createUsersWithListInputHandler.ts +++ b/examples/msw/src/gen/msw/user/Handlers/createUsersWithListInputHandler.ts @@ -1,8 +1,11 @@ import type { CreateUsersWithListInputMutationResponse } from '../../../models/CreateUsersWithListInput.ts' import { http } from 'msw' -export function createUsersWithListInputHandler(data?: CreateUsersWithListInputMutationResponse) { +export function createUsersWithListInputHandler( + data?: CreateUsersWithListInputMutationResponse | ((info: Parameters[1]>[0]) => Response), +) { return http.post('*/user/createWithList', function handler(info) { + if (typeof data === 'function') return data(info) return new Response(JSON.stringify(data), { headers: { 'Content-Type': 'application/json', diff --git a/examples/msw/src/gen/msw/user/Handlers/deleteUserHandler.ts b/examples/msw/src/gen/msw/user/Handlers/deleteUserHandler.ts index 9bfaab161..7f5210095 100644 --- a/examples/msw/src/gen/msw/user/Handlers/deleteUserHandler.ts +++ b/examples/msw/src/gen/msw/user/Handlers/deleteUserHandler.ts @@ -1,8 +1,9 @@ import type { DeleteUserMutationResponse } from '../../../models/DeleteUser.ts' import { http } from 'msw' -export function deleteUserHandler(data?: DeleteUserMutationResponse) { +export function deleteUserHandler(data?: DeleteUserMutationResponse | ((info: Parameters[1]>[0]) => Response)) { return http.delete('*/user/:username', function handler(info) { + if (typeof data === 'function') return data(info) return new Response(JSON.stringify(data), { headers: { 'Content-Type': 'application/json', diff --git a/examples/msw/src/gen/msw/user/Handlers/getUserByNameHandler.ts b/examples/msw/src/gen/msw/user/Handlers/getUserByNameHandler.ts index 95c864072..c2cca7e9c 100644 --- a/examples/msw/src/gen/msw/user/Handlers/getUserByNameHandler.ts +++ b/examples/msw/src/gen/msw/user/Handlers/getUserByNameHandler.ts @@ -1,8 +1,9 @@ import type { GetUserByNameQueryResponse } from '../../../models/GetUserByName.ts' import { http } from 'msw' -export function getUserByNameHandler(data?: GetUserByNameQueryResponse) { +export function getUserByNameHandler(data?: GetUserByNameQueryResponse | ((info: Parameters[1]>[0]) => Response)) { return http.get('*/user/:username', function handler(info) { + if (typeof data === 'function') return data(info) return new Response(JSON.stringify(data), { headers: { 'Content-Type': 'application/json', diff --git a/examples/msw/src/gen/msw/user/Handlers/loginUserHandler.ts b/examples/msw/src/gen/msw/user/Handlers/loginUserHandler.ts index 794b8553f..f9a9afb70 100644 --- a/examples/msw/src/gen/msw/user/Handlers/loginUserHandler.ts +++ b/examples/msw/src/gen/msw/user/Handlers/loginUserHandler.ts @@ -1,8 +1,9 @@ import type { LoginUserQueryResponse } from '../../../models/LoginUser.ts' import { http } from 'msw' -export function loginUserHandler(data?: LoginUserQueryResponse) { +export function loginUserHandler(data?: LoginUserQueryResponse | ((info: Parameters[1]>[0]) => Response)) { return http.get('*/user/login', function handler(info) { + if (typeof data === 'function') return data(info) return new Response(JSON.stringify(data), { headers: { 'Content-Type': 'application/json', diff --git a/examples/msw/src/gen/msw/user/Handlers/logoutUserHandler.ts b/examples/msw/src/gen/msw/user/Handlers/logoutUserHandler.ts index ba567be61..7c974eea5 100644 --- a/examples/msw/src/gen/msw/user/Handlers/logoutUserHandler.ts +++ b/examples/msw/src/gen/msw/user/Handlers/logoutUserHandler.ts @@ -1,8 +1,9 @@ import type { LogoutUserQueryResponse } from '../../../models/LogoutUser.ts' import { http } from 'msw' -export function logoutUserHandler(data?: LogoutUserQueryResponse) { +export function logoutUserHandler(data?: LogoutUserQueryResponse | ((info: Parameters[1]>[0]) => Response)) { return http.get('*/user/logout', function handler(info) { + if (typeof data === 'function') return data(info) return new Response(JSON.stringify(data), { headers: { 'Content-Type': 'application/json', diff --git a/examples/msw/src/gen/msw/user/Handlers/updateUserHandler.ts b/examples/msw/src/gen/msw/user/Handlers/updateUserHandler.ts index 0ea200dda..a24b1232a 100644 --- a/examples/msw/src/gen/msw/user/Handlers/updateUserHandler.ts +++ b/examples/msw/src/gen/msw/user/Handlers/updateUserHandler.ts @@ -1,8 +1,9 @@ import type { UpdateUserMutationResponse } from '../../../models/UpdateUser.ts' import { http } from 'msw' -export function updateUserHandler(data?: UpdateUserMutationResponse) { +export function updateUserHandler(data?: UpdateUserMutationResponse | ((info: Parameters[1]>[0]) => Response)) { return http.put('*/user/:username', function handler(info) { + if (typeof data === 'function') return data(info) return new Response(JSON.stringify(data), { headers: { 'Content-Type': 'application/json', diff --git a/examples/simple-single/src/gen2/index.ts b/examples/simple-single/src/gen2/index.ts index 2e3630438..b83d8ec97 100644 --- a/examples/simple-single/src/gen2/index.ts +++ b/examples/simple-single/src/gen2/index.ts @@ -207,6 +207,53 @@ export const volumeSnapshotSchema = z.object({ status: z.string().optional(), }) +export const flyContainerConfigSchema = z.object({ + cmd: z.array(z.string()).describe('CmdOverride is used to override the default command of the image.').optional(), + depends_on: z + .array(z.lazy(() => flyContainerDependencySchema)) + .describe( + 'DependsOn can be used to define dependencies between containers. The container will only be\nstarted after all of its dependent conditions have been satisfied.', + ) + .optional(), + entrypoint: z.array(z.string()).describe('EntrypointOverride is used to override the default entrypoint of the image.').optional(), + env: z.object({}).catchall(z.string()).describe('ExtraEnv is used to add additional environment variables to the container.').optional(), + env_from: z + .array(z.lazy(() => flyEnvFromSchema)) + .describe('EnvFrom can be provided to set environment variables from machine fields.') + .optional(), + exec: z + .array(z.string()) + .describe( + 'Image Config overrides - these fields are used to override the image configuration.\nIf not provided, the image configuration will be used.\nExecOverride is used to override the default command of the image.', + ) + .optional(), + files: z + .array(z.lazy(() => flyFileSchema)) + .describe('Files are files that will be written to the container file system.') + .optional(), + image: z.string().describe('Image is the docker image to run.').optional(), + name: z.string().describe('Name is used to identify the container in the machine.').optional(), + restart: z + .lazy(() => flyMachineRestartSchema) + .describe('Restart is used to define the restart policy for the container. NOTE: spot-price is not\nsupported for containers.') + .optional(), + secrets: z + .array(z.lazy(() => flyMachineSecretSchema)) + .describe( + 'Secrets can be provided at the process level to explicitly indicate which secrets should be\nused for the process. If not provided, the secrets provided at the machine level will be used.', + ) + .optional(), + stop: z + .lazy(() => flyStopConfigSchema) + .describe('Stop is used to define the signal and timeout for stopping the container.') + .optional(), + user: z.string().describe('UserOverride is used to override the default user of the image.').optional(), +}) + +export const flyContainerDependencySchema = z.object({ condition: z.lazy(() => flyContainerDependencyConditionSchema).optional(), name: z.string().optional() }) + +export const flyContainerDependencyConditionSchema = z.enum(['exited_successfully', 'healthy', 'started']) + export const flyDnsConfigSchema = z.object({ dns_forward_rules: z.array(z.lazy(() => flyDnsForwardRuleSchema)).optional(), hostname: z.string().optional(), @@ -297,6 +344,10 @@ export const flyMachineConfigSchema = z.object({ .object({}) .catchall(z.lazy(() => flyMachineCheckSchema)) .optional(), + containers: z + .array(z.lazy(() => flyContainerConfigSchema)) + .describe('Containers are a list of containers that will run in the machine. Currently restricted to\nonly specific organizations.') + .optional(), disable_machine_autostart: z.boolean().describe('Deprecated: use Service.Autostart instead').optional(), dns: z.lazy(() => flyDnsConfigSchema).optional(), env: z.object({}).catchall(z.string()).describe('An object filled with key/value pairs to be set as environment variables').optional(), diff --git a/packages/plugin-msw/src/components/Mock.tsx b/packages/plugin-msw/src/components/Mock.tsx index 6b97691ba..666113928 100644 --- a/packages/plugin-msw/src/components/Mock.tsx +++ b/packages/plugin-msw/src/components/Mock.tsx @@ -14,10 +14,12 @@ type Props = { method: HttpMethod } -export function Mock({ name, fakerName, typeName, url, method }: Props): ReactNode { +export function Mock({ name, typeName, url, method }: Props): ReactNode { const params = FunctionParams.factory({ data: { - type: typeName, + type: `${typeName} | (( + info: Parameters[1]>[0], + ) => Response)`, optional: true, }, }) @@ -26,6 +28,8 @@ export function Mock({ name, fakerName, typeName, url, method }: Props): ReactNo {`return http.${method}('*${url}', function handler(info) { + if(typeof data === 'function') return data(info) + return new Response(JSON.stringify(data), { headers: { 'Content-Type': 'application/json', diff --git a/packages/plugin-msw/src/components/MockWithFaker.tsx b/packages/plugin-msw/src/components/MockWithFaker.tsx index 34b9f6436..ee7e31e09 100644 --- a/packages/plugin-msw/src/components/MockWithFaker.tsx +++ b/packages/plugin-msw/src/components/MockWithFaker.tsx @@ -17,7 +17,9 @@ type Props = { export function MockWithFaker({ name, fakerName, typeName, url, method }: Props): ReactNode { const params = FunctionParams.factory({ data: { - type: typeName, + type: `${typeName} | (( + info: Parameters[1]>[0], + ) => Response)`, optional: true, }, }) @@ -26,7 +28,9 @@ export function MockWithFaker({ name, fakerName, typeName, url, method }: Props) {`return http.${method}('*${url}', function handler(info) { - return new Response(JSON.stringify(${fakerName}(data)), { + if(typeof data === 'function') return data(info) + + return new Response(JSON.stringify(data || ${fakerName}(data)), { headers: { 'Content-Type': 'application/json', }, diff --git a/packages/plugin-msw/src/generators/__snapshots__/createPet.ts b/packages/plugin-msw/src/generators/__snapshots__/createPet.ts index a99db8af2..2dcf303b2 100644 --- a/packages/plugin-msw/src/generators/__snapshots__/createPet.ts +++ b/packages/plugin-msw/src/generators/__snapshots__/createPet.ts @@ -1,7 +1,9 @@ import { http } from "msw"; - export function createPets(data?: CreatePetsMutationResponse) { + export function createPets(data?: CreatePetsMutationResponse | ((info: Parameters[1]>[0]) => Response)) { return http.post("*/pets", function handler(info) { + if (typeof data === "function") + return data(info); return new Response(JSON.stringify(data), { headers: { "Content-Type": "application/json", diff --git a/packages/plugin-msw/src/generators/__snapshots__/deletePet.ts b/packages/plugin-msw/src/generators/__snapshots__/deletePet.ts index 3d46c6bef..0de6f854d 100644 --- a/packages/plugin-msw/src/generators/__snapshots__/deletePet.ts +++ b/packages/plugin-msw/src/generators/__snapshots__/deletePet.ts @@ -1,7 +1,9 @@ import { http } from "msw"; - export function deletePetsPetid(data?: DeletePetsPetidMutationResponse) { + export function deletePetsPetid(data?: DeletePetsPetidMutationResponse | ((info: Parameters[1]>[0]) => Response)) { return http.delete("*/pets/:petId", function handler(info) { + if (typeof data === "function") + return data(info); return new Response(JSON.stringify(data), { headers: { "Content-Type": "application/json", diff --git a/packages/plugin-msw/src/generators/__snapshots__/getPets.ts b/packages/plugin-msw/src/generators/__snapshots__/getPets.ts index 2ea6e5df0..e7f739e14 100644 --- a/packages/plugin-msw/src/generators/__snapshots__/getPets.ts +++ b/packages/plugin-msw/src/generators/__snapshots__/getPets.ts @@ -1,7 +1,9 @@ import { http } from "msw"; - export function listPets(data?: ListPetsQueryResponse) { + export function listPets(data?: ListPetsQueryResponse | ((info: Parameters[1]>[0]) => Response)) { return http.get("*/pets", function handler(info) { + if (typeof data === "function") + return data(info); return new Response(JSON.stringify(data), { headers: { "Content-Type": "application/json", diff --git a/packages/plugin-msw/src/generators/__snapshots__/getPetsFaker.ts b/packages/plugin-msw/src/generators/__snapshots__/getPetsFaker.ts index 3ad5b5abf..186fbe9d6 100644 --- a/packages/plugin-msw/src/generators/__snapshots__/getPetsFaker.ts +++ b/packages/plugin-msw/src/generators/__snapshots__/getPetsFaker.ts @@ -1,8 +1,10 @@ import { http } from "msw"; - export function listPets(data?: ListPetsQueryResponse) { + export function listPets(data?: ListPetsQueryResponse | ((info: Parameters[1]>[0]) => Response)) { return http.get("*/pets", function handler(info) { - return new Response(JSON.stringify(listPetsQueryResponse(data)), { + if (typeof data === "function") + return data(info); + return new Response(JSON.stringify(data || listPetsQueryResponse(data)), { headers: { "Content-Type": "application/json", }, diff --git a/packages/plugin-msw/src/generators/__snapshots__/showPetById.ts b/packages/plugin-msw/src/generators/__snapshots__/showPetById.ts index 168bb0423..a8eb3a977 100644 --- a/packages/plugin-msw/src/generators/__snapshots__/showPetById.ts +++ b/packages/plugin-msw/src/generators/__snapshots__/showPetById.ts @@ -1,7 +1,9 @@ import { http } from "msw"; - export function showPetById(data?: ShowPetByIdQueryResponse) { + export function showPetById(data?: ShowPetByIdQueryResponse | ((info: Parameters[1]>[0]) => Response)) { return http.get("*/pets/:petId", function handler(info) { + if (typeof data === "function") + return data(info); return new Response(JSON.stringify(data), { headers: { "Content-Type": "application/json", diff --git a/packages/plugin-msw/src/generators/mswGenerator.tsx b/packages/plugin-msw/src/generators/mswGenerator.tsx index a91fcb3af..0d5b9b736 100644 --- a/packages/plugin-msw/src/generators/mswGenerator.tsx +++ b/packages/plugin-msw/src/generators/mswGenerator.tsx @@ -34,7 +34,8 @@ export const mswGenerator = createReactGenerator({ return ( - + + {parser === 'faker' && faker.file && faker.schemas.response && (