Skip to content

Commit

Permalink
feat: Support dynamic and customizable data in handlers (#1439)
Browse files Browse the repository at this point in the history
  • Loading branch information
stijnvanhulle authored Dec 5, 2024
1 parent 41f009c commit 5012135
Show file tree
Hide file tree
Showing 48 changed files with 187 additions and 49 deletions.
5 changes: 5 additions & 0 deletions .changeset/clean-wasps-rush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@kubb/plugin-msw": minor
---

Support dynamic and customizable data in handlers
20 changes: 20 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion examples/advanced/src/gen/msw/petController/addPetHandler.ts
Original file line number Diff line number Diff line change
@@ -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<Parameters<typeof http.post>[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',
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Parameters<typeof http.delete>[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',
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Parameters<typeof http.get>[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',
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Parameters<typeof http.get>[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',
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Parameters<typeof http.get>[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',
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Parameters<typeof http.put>[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',
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Parameters<typeof http.post>[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',
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Parameters<typeof http.post>[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',
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Parameters<typeof http.post>[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',
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Parameters<typeof http.post>[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',
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Parameters<typeof http.post>[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',
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Parameters<typeof http.delete>[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',
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Parameters<typeof http.get>[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',
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Parameters<typeof http.get>[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',
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Parameters<typeof http.get>[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',
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Parameters<typeof http.put>[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',
Expand Down
3 changes: 2 additions & 1 deletion examples/msw/src/gen/msw/pet/Handlers/addPetHandler.ts
Original file line number Diff line number Diff line change
@@ -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<Parameters<typeof http.post>[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',
Expand Down
3 changes: 2 additions & 1 deletion examples/msw/src/gen/msw/pet/Handlers/deletePetHandler.ts
Original file line number Diff line number Diff line change
@@ -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<Parameters<typeof http.delete>[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',
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Parameters<typeof http.get>[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',
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Parameters<typeof http.get>[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',
Expand Down
3 changes: 2 additions & 1 deletion examples/msw/src/gen/msw/pet/Handlers/getPetByIdHandler.ts
Original file line number Diff line number Diff line change
@@ -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<Parameters<typeof http.get>[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',
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Parameters<typeof http.options>[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',
Expand Down
3 changes: 2 additions & 1 deletion examples/msw/src/gen/msw/pet/Handlers/updatePetHandler.ts
Original file line number Diff line number Diff line change
@@ -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<Parameters<typeof http.put>[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',
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Parameters<typeof http.post>[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',
Expand Down
3 changes: 2 additions & 1 deletion examples/msw/src/gen/msw/pet/Handlers/uploadFileHandler.ts
Original file line number Diff line number Diff line change
@@ -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<Parameters<typeof http.post>[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',
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Parameters<typeof http.delete>[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',
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Parameters<typeof http.get>[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',
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Parameters<typeof http.get>[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',
Expand Down
3 changes: 2 additions & 1 deletion examples/msw/src/gen/msw/store/Handlers/placeOrderHandler.ts
Original file line number Diff line number Diff line change
@@ -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<Parameters<typeof http.post>[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',
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Parameters<typeof http.patch>[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',
Expand Down
3 changes: 2 additions & 1 deletion examples/msw/src/gen/msw/user/Handlers/createUserHandler.ts
Original file line number Diff line number Diff line change
@@ -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<Parameters<typeof http.post>[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',
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Parameters<typeof http.post>[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',
Expand Down
Loading

0 comments on commit 5012135

Please sign in to comment.