Skip to content

Commit

Permalink
feat: use of zod validation for tanstack
Browse files Browse the repository at this point in the history
  • Loading branch information
stijnvanhulle committed Jan 14, 2024
1 parent 4e94f74 commit 38d1fd1
Show file tree
Hide file tree
Showing 70 changed files with 278 additions and 270 deletions.
5 changes: 5 additions & 0 deletions .changeset/chatty-owls-grow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@kubb/swagger-tanstack-query": minor
---

useQuery with Zod validation
62 changes: 62 additions & 0 deletions docs/plugins/swagger-tanstack-query/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,68 @@ export default defineConfig({

:::

### parser

Which parser can be used before returning the data to `@tanstack/query`.

`'zod'` will use `@kubb/swagger-zod` to parse the data. <br/>

::: info type

::: code-group

```typescript ['zod']
export function getPetByIdQueryOptions() {
const queryKey = getPetByIdQueryKey(petId)
return {
queryKey,
queryFn: async () => {
const res = await client({
method: 'get',
url: `/pet/${petId}`,
})

return getPetByIdQueryResponseSchema.parse(res.data)
},
}
}
```

:::

::: info

Type: `'zod'` <br/>

::: code-group

```typescript ['zod']
import { defineConfig } from '@kubb/core'
import createSwagger from '@kubb/swagger'
import createSwaggerTanstackQuery from '@kubb/swagger-tanstack-query'
import createSwaggerTS from '@kubb/swagger-ts'

export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerTS({}),
createSwaggerTanstackQuery(
{
parser: 'zod',
},
),
],
})
```

:::

### framework

Framework to be generated for.
Expand Down
1 change: 1 addition & 0 deletions examples/advanced/configs/kubb.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export default defineConfig(async () => {
},
infinite: {},
dataReturnType: 'full',
parser: 'zod',
},
],
[
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { findPetsByStatusQueryResponseSchema } from '../../../zod/petController/findPetsByStatusSchema'
import client from '../../../../tanstack-query-client.ts'
import { useQuery, useInfiniteQuery } from '@tanstack/react-query'
import type { FindPetsByStatusQueryResponse, FindPetsByStatusQueryParams, FindPetsByStatus400 } from '../../../models/ts/petController/FindPetsByStatus'
Expand Down Expand Up @@ -41,7 +42,7 @@ export function findPetsByStatusQueryOptions<TData = FindPetsByStatus['response'
params,
...options,
})
return res
return { ...res, data: findPetsByStatusQueryResponseSchema.parse(res.data) }
},
}
}
Expand Down Expand Up @@ -92,7 +93,7 @@ export function findPetsByStatusInfiniteQueryOptions<TData = FindPetsByStatus['r
...(options.params || {}),
},
})
return res
return { ...res, data: findPetsByStatusQueryResponseSchema.parse(res.data) }
},
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { findPetsByTagsQueryResponseSchema } from '../../../zod/petController/findPetsByTagsSchema'
import client from '../../../../tanstack-query-client.ts'
import { useQuery, useInfiniteQuery } from '@tanstack/react-query'
import type {
Expand Down Expand Up @@ -48,7 +49,7 @@ export function findPetsByTagsQueryOptions<TData = FindPetsByTags['response'], T
headers: { ...headers, ...options.headers },
...options,
})
return res
return { ...res, data: findPetsByTagsQueryResponseSchema.parse(res.data) }
},
}
}
Expand Down Expand Up @@ -100,7 +101,7 @@ export function findPetsByTagsInfiniteQueryOptions<TData = FindPetsByTags['respo
...(options.params || {}),
},
})
return res
return { ...res, data: findPetsByTagsQueryResponseSchema.parse(res.data) }
},
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getPetByIdQueryResponseSchema } from '../../../zod/petController/getPetByIdSchema'
import client from '../../../../tanstack-query-client.ts'
import { useQuery, useInfiniteQuery } from '@tanstack/react-query'
import type { GetPetByIdQueryResponse, GetPetByIdPathParams, GetPetById400, GetPetById404 } from '../../../models/ts/petController/GetPetById'
Expand Down Expand Up @@ -40,7 +41,7 @@ export function getPetByIdQueryOptions<TData = GetPetById['response'], TQueryDat
url: `/pet/${petId}`,
...options,
})
return res
return { ...res, data: getPetByIdQueryResponseSchema.parse(res.data) }
},
}
}
Expand Down Expand Up @@ -84,7 +85,7 @@ export function getPetByIdInfiniteQueryOptions<TData = GetPetById['response'], T
url: `/pet/${petId}`,
...options,
})
return res
return { ...res, data: getPetByIdQueryResponseSchema.parse(res.data) }
},
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getUserByNameQueryResponseSchema } from '../../../zod/userController/getUserByNameSchema'
import client from '../../../../tanstack-query-client.ts'
import { useQuery, useInfiniteQuery } from '@tanstack/react-query'
import type { GetUserByNameQueryResponse, GetUserByNamePathParams, GetUserByName400, GetUserByName404 } from '../../../models/ts/userController/GetUserByName'
Expand Down Expand Up @@ -40,7 +41,7 @@ export function getUserByNameQueryOptions<TData = GetUserByName['response'], TQu
url: `/user/${username}`,
...options,
})
return res
return { ...res, data: getUserByNameQueryResponseSchema.parse(res.data) }
},
}
}
Expand Down Expand Up @@ -84,7 +85,7 @@ export function getUserByNameInfiniteQueryOptions<TData = GetUserByName['respons
url: `/user/${username}`,
...options,
})
return res
return { ...res, data: getUserByNameQueryResponseSchema.parse(res.data) }
},
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { loginUserQueryResponseSchema } from '../../../zod/userController/loginUserSchema'
import client from '../../../../tanstack-query-client.ts'
import { useQuery, useInfiniteQuery } from '@tanstack/react-query'
import type { LoginUserQueryResponse, LoginUserQueryParams, LoginUser400 } from '../../../models/ts/userController/LoginUser'
Expand Down Expand Up @@ -41,7 +42,7 @@ export function loginUserQueryOptions<TData = LoginUser['response'], TQueryData
params,
...options,
})
return res
return { ...res, data: loginUserQueryResponseSchema.parse(res.data) }
},
}
}
Expand Down Expand Up @@ -89,7 +90,7 @@ export function loginUserInfiniteQueryOptions<TData = LoginUser['response'], TQu
...(options.params || {}),
},
})
return res
return { ...res, data: loginUserQueryResponseSchema.parse(res.data) }
},
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { logoutUserQueryResponseSchema } from '../../../zod/userController/logoutUserSchema'
import client from '../../../../tanstack-query-client.ts'
import { useQuery, useInfiniteQuery } from '@tanstack/react-query'
import type { LogoutUserQueryResponse, LogoutUserError } from '../../../models/ts/userController/LogoutUser'
Expand Down Expand Up @@ -39,7 +40,7 @@ export function logoutUserQueryOptions<TData = LogoutUser['response'], TQueryDat
url: `/user/logout`,
...options,
})
return res
return { ...res, data: logoutUserQueryResponseSchema.parse(res.data) }
},
}
}
Expand Down Expand Up @@ -78,7 +79,7 @@ export function logoutUserInfiniteQueryOptions<TData = LogoutUser['response'], T
url: `/user/logout`,
...options,
})
return res
return { ...res, data: logoutUserQueryResponseSchema.parse(res.data) }
},
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/advanced/src/gen/zod/addressSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ export const addressSchema = z.object({
'city': z.string().optional(),
'state': z.string().optional(),
'zip': z.string().optional(),
}).optional()
})
2 changes: 1 addition & 1 deletion examples/advanced/src/gen/zod/apiResponseSchema.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { z } from 'zod'

export const apiResponseSchema = z.object({ 'code': z.number().optional(), 'type': z.string().optional(), 'message': z.string().optional() }).optional()
export const apiResponseSchema = z.object({ 'code': z.number().optional(), 'type': z.string().optional(), 'message': z.string().optional() })
2 changes: 1 addition & 1 deletion examples/advanced/src/gen/zod/categorySchema.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { z } from 'zod'

export const categorySchema = z.object({ 'id': z.number().optional(), 'name': z.string().optional() }).optional()
export const categorySchema = z.object({ 'id': z.number().optional(), 'name': z.string().optional() })
2 changes: 1 addition & 1 deletion examples/advanced/src/gen/zod/customerSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ export const customerSchema = z.object({
'id': z.number().optional(),
'username': z.string().optional(),
'address': z.array(z.lazy(() => addressSchema)).optional(),
}).optional()
})
2 changes: 1 addition & 1 deletion examples/advanced/src/gen/zod/orderSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ export const orderSchema = z.object({
'status': z.enum([`placed`, `approved`, `delivered`]).describe(`Order Status`).optional(),
'http_status': z.enum([`ok`, `not_found`]).describe(`HTTP Status`).optional(),
'complete': z.boolean().optional(),
}).optional()
})
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { z } from 'zod'
import { addPetRequestSchema } from '../addPetRequestSchema'
import { petSchema } from '../petSchema'

export const addPet405Schema = z.object({ 'code': z.number().optional(), 'message': z.string().optional() }).optional()
export const addPet405Schema = z.object({ 'code': z.number().optional(), 'message': z.string().optional() })

/**
* @description Create a new pet in the store
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import { z } from 'zod'
* @description Invalid pet value
*/
export const deletePet400Schema = z.any()
export const deletePetHeaderParamsSchema = z.object({ 'api_key': z.string().optional() }).optional()
export const deletePetHeaderParamsSchema = z.object({ 'api_key': z.string().optional() })
export const deletePetMutationResponseSchema = z.any()
export const deletePetPathParamsSchema = z.object({ 'petId': z.number().describe(`Pet id to delete`) })
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { petSchema } from '../petSchema'
export const findPetsByStatus400Schema = z.any()
export const findPetsByStatusQueryParamsSchema = z.object({
'status': z.enum([`available`, `pending`, `sold`]).default('available').describe(`Status values that need to be considered for filter`).optional(),
}).optional()
})

/**
* @description successful operation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const findPetsByTagsQueryParamsSchema = z.object({
'tags': z.array(z.string()).describe(`Tags to filter by`).optional(),
'page': z.string().describe(`to request with required page number or pagination`).optional(),
'pageSize': z.string().describe(`to request with required page size`).optional(),
}).optional()
})

/**
* @description successful operation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ export const updatePetWithFormPathParamsSchema = z.object({ 'petId': z.number().
export const updatePetWithFormQueryParamsSchema = z.object({
'name': z.string().describe(`Name of pet that needs to be updated`).optional(),
'status': z.string().describe(`Status of pet that needs to be updated`).optional(),
}).optional()
})
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { apiResponseSchema } from '../apiResponseSchema'

export const uploadFileMutationRequestSchema = z.string()
export const uploadFilePathParamsSchema = z.object({ 'petId': z.number().describe(`ID of pet to update`) })
export const uploadFileQueryParamsSchema = z.object({ 'additionalMetadata': z.string().describe(`Additional Metadata`).optional() }).optional()
export const uploadFileQueryParamsSchema = z.object({ 'additionalMetadata': z.string().describe(`Additional Metadata`).optional() })

/**
* @description successful operation
Expand Down
2 changes: 1 addition & 1 deletion examples/advanced/src/gen/zod/petNotFoundSchema.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { z } from 'zod'

export const petNotFoundSchema = z.object({ 'code': z.number().optional(), 'message': z.string().optional() }).optional()
export const petNotFoundSchema = z.object({ 'code': z.number().optional(), 'message': z.string().optional() })
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const createPetsHeaderParamsSchema = z.object({ 'X-EXAMPLE': z.enum([`ONE
export const createPetsMutationRequestSchema = z.object({ 'name': z.string(), 'tag': z.string() })
export const createPetsMutationResponseSchema = z.any()
export const createPetsPathParamsSchema = z.object({ 'uuid': z.string().describe(`UUID`) })
export const createPetsQueryParamsSchema = z.object({ 'offset': z.number().describe(`Offset`).optional() }).optional()
export const createPetsQueryParamsSchema = z.object({ 'offset': z.number().describe(`Offset`).optional() })

/**
* @description unexpected error
Expand Down
2 changes: 1 addition & 1 deletion examples/advanced/src/gen/zod/tagSchema.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { z } from 'zod'

export const tagSchema = z.object({ 'id': z.number().optional(), 'name': z.string().optional() }).optional()
export const tagSchema = z.object({ 'id': z.number().optional(), 'name': z.string().optional() })
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const loginUser400Schema = z.any()
export const loginUserQueryParamsSchema = z.object({
'username': z.string().describe(`The user name for login`).optional(),
'password': z.string().describe(`The password for login in clear text`).optional(),
}).optional()
})

/**
* @description successful operation
Expand Down
2 changes: 1 addition & 1 deletion examples/advanced/src/gen/zod/userSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ export const userSchema = z.object({
'password': z.string().optional(),
'phone': z.string().optional(),
'userStatus': z.number().describe(`User Status`).optional(),
}).optional()
})
Loading

0 comments on commit 38d1fd1

Please sign in to comment.