Skip to content

Commit

Permalink
chore: humanize swr output
Browse files Browse the repository at this point in the history
  • Loading branch information
stijnvanhulle committed Sep 8, 2024
1 parent c6888de commit 90ad72b
Show file tree
Hide file tree
Showing 150 changed files with 1,466 additions and 1,626 deletions.
60 changes: 29 additions & 31 deletions examples/advanced/src/gen/clients/swr/petSWRController/useAddPet.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,45 @@
import client from '../../../../swr-client.ts'
import useSWRMutation from 'swr/mutation'
import type { RequestConfig } from '../../../../swr-client.ts'
import type { AddPetMutationRequest, AddPetMutationResponse, AddPet405 } from '../../../models/ts/petController/AddPet.ts'
import type { Key } from 'swr'
import type { SWRMutationConfiguration, SWRMutationResponse } from 'swr/mutation'
import type { SWRMutationConfiguration } from 'swr/mutation'
import { addPetMutationResponseSchema } from '../../../zod/petController/addPetSchema.ts'

type AddPetClient = typeof client<AddPetMutationResponse, AddPet405, AddPetMutationRequest>

type AddPet = {
data: AddPetMutationResponse
error: AddPet405
request: AddPetMutationRequest
pathParams: never
queryParams: never
headerParams: never
response: AddPetMutationResponse
client: {
parameters: Partial<Parameters<AddPetClient>[0]>
return: Awaited<ReturnType<AddPetClient>>
}
/**
* @description Add a new pet to the store
* @summary Add a new pet to the store
* @link /pet
*/
async function addPet(data: AddPetMutationRequest, config: Partial<RequestConfig<AddPetMutationRequest>> = {}) {
const res = await client<AddPetMutationResponse, AddPet405, AddPetMutationRequest>({
method: 'post',
url: '/pet',
baseURL: 'https://petstore3.swagger.io/api/v3',
data,
...config,
})
return addPetMutationResponseSchema.parse(res.data)
}

/**
* @description Add a new pet to the store
* @summary Add a new pet to the store
* @link /pet
*/
export function useAddPet(options?: {
mutation?: SWRMutationConfiguration<AddPet['response'], AddPet['error']>
client?: AddPet['client']['parameters']
shouldFetch?: boolean
}): SWRMutationResponse<AddPet['response'], AddPet['error']> {
const { mutation: mutationOptions, client: clientOptions = {}, shouldFetch = true } = options ?? {}
const url = '/pet' as const
return useSWRMutation<AddPet['response'], AddPet['error'], Key>(
shouldFetch ? url : null,
export function useAddPet(
options: {
mutation?: SWRMutationConfiguration<AddPetMutationResponse, AddPet405>
client?: Partial<RequestConfig<AddPetMutationRequest>>
shouldFetch?: boolean
} = {},
) {
const { mutation: mutationOptions, client: config = {}, shouldFetch = true } = options ?? {}
const swrKey = ['/pet'] as const
return useSWRMutation<AddPetMutationResponse, AddPet405, Key>(
shouldFetch ? swrKey : null,
async (_url, { arg: data }) => {
const res = await client<AddPet['data'], AddPet['error'], AddPet['request']>({
method: 'post',
url,
data,
...clientOptions,
})
return res.data
return addPet(data, config)
},
mutationOptions,
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
import client from '../../../../swr-client.ts'
import useSWRMutation from 'swr/mutation'
import type { RequestConfig } from '../../../../swr-client.ts'
import type { DeletePetMutationResponse, DeletePetPathParams, DeletePetHeaderParams, DeletePet400 } from '../../../models/ts/petController/DeletePet.ts'
import type { Key } from 'swr'
import type { SWRMutationConfiguration, SWRMutationResponse } from 'swr/mutation'
import type { SWRMutationConfiguration } from 'swr/mutation'
import { deletePetMutationResponseSchema } from '../../../zod/petController/deletePetSchema.ts'

type DeletePetClient = typeof client<DeletePetMutationResponse, DeletePet400, never>

type DeletePet = {
data: DeletePetMutationResponse
error: DeletePet400
request: never
pathParams: DeletePetPathParams
queryParams: never
headerParams: DeletePetHeaderParams
response: DeletePetMutationResponse
client: {
parameters: Partial<Parameters<DeletePetClient>[0]>
return: Awaited<ReturnType<DeletePetClient>>
}
/**
* @description delete a pet
* @summary Deletes a pet
* @link /pet/:petId
*/
async function deletePet(petId: DeletePetPathParams['petId'], headers?: DeletePetHeaderParams, config: Partial<RequestConfig> = {}) {
const res = await client<DeletePetMutationResponse, DeletePet400, unknown>({
method: 'delete',
url: `/pet/${petId}`,
baseURL: 'https://petstore3.swagger.io/api/v3',
headers: { ...headers, ...config.headers },
...config,
})
return deletePetMutationResponseSchema.parse(res.data)
}

/**
Expand All @@ -27,25 +29,19 @@ type DeletePet = {
*/
export function useDeletePet(
petId: DeletePetPathParams['petId'],
headers?: DeletePet['headerParams'],
options?: {
mutation?: SWRMutationConfiguration<DeletePet['response'], DeletePet['error']>
client?: DeletePet['client']['parameters']
headers?: DeletePetHeaderParams,
options: {
mutation?: SWRMutationConfiguration<DeletePetMutationResponse, DeletePet400>
client?: Partial<RequestConfig>
shouldFetch?: boolean
},
): SWRMutationResponse<DeletePet['response'], DeletePet['error']> {
const { mutation: mutationOptions, client: clientOptions = {}, shouldFetch = true } = options ?? {}
const url = `/pet/${petId}` as const
return useSWRMutation<DeletePet['response'], DeletePet['error'], Key>(
shouldFetch ? url : null,
} = {},
) {
const { mutation: mutationOptions, client: config = {}, shouldFetch = true } = options ?? {}
const swrKey = [`/pet/${petId}`] as const
return useSWRMutation<DeletePetMutationResponse, DeletePet400, Key>(
shouldFetch ? swrKey : null,
async (_url) => {
const res = await client<DeletePet['data'], DeletePet['error']>({
method: 'delete',
url,
headers: { ...headers, ...clientOptions.headers },
...clientOptions,
})
return res.data
return deletePet(petId, headers, config)
},
mutationOptions,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import client from '../../../../swr-client.ts'
import useSWR from 'swr'
import type { RequestConfig } from '../../../../swr-client.ts'
import type { FindPetsByStatusQueryResponse, FindPetsByStatusQueryParams, FindPetsByStatus400 } from '../../../models/ts/petController/FindPetsByStatus.ts'
import type { SWRConfiguration } from 'swr'
import type { Key, SWRConfiguration } from 'swr'
import { findPetsByStatusQueryResponseSchema } from '../../../zod/petController/findPetsByStatusSchema.ts'

/**
Expand All @@ -18,7 +18,7 @@ async function findPetsByStatus(params?: FindPetsByStatusQueryParams, config: Pa
params,
...config,
})
return { ...res, data: findPetsByStatusQueryResponseSchema.parse(res.data) }
return findPetsByStatusQueryResponseSchema.parse(res.data)
}

export function findPetsByStatusQueryOptions(params?: FindPetsByStatusQueryParams, config: Partial<RequestConfig> = {}) {
Expand All @@ -34,17 +34,17 @@ export function findPetsByStatusQueryOptions(params?: FindPetsByStatusQueryParam
* @summary Finds Pets by status
* @link /pet/findByStatus
*/
export function useFindPetsByStatus<TData = FindPetsByStatusQueryResponse>(
export function useFindPetsByStatus(
params?: FindPetsByStatusQueryParams,
options: {
query?: SWRConfiguration<TData, FindPetsByStatus400>
query?: SWRConfiguration<FindPetsByStatusQueryResponse, FindPetsByStatus400>
client?: Partial<RequestConfig>
shouldFetch?: boolean
} = {},
) {
const { query: queryOptions, client: config = {}, shouldFetch = true } = options ?? {}
const url = '/pet/findByStatus'
return useSWR<TData, FindPetsByStatus400, typeof url | null>(shouldFetch ? url : null, {
const swrKey = ['/pet/findByStatus', params] as const
return useSWR<FindPetsByStatusQueryResponse, FindPetsByStatus400, Key>(shouldFetch ? swrKey : null, {
...findPetsByStatusQueryOptions(params, config),
...queryOptions,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type {
FindPetsByTagsHeaderParams,
FindPetsByTags400,
} from '../../../models/ts/petController/FindPetsByTags.ts'
import type { SWRConfiguration } from 'swr'
import type { Key, SWRConfiguration } from 'swr'
import { findPetsByTagsQueryResponseSchema } from '../../../zod/petController/findPetsByTagsSchema.ts'

/**
Expand All @@ -24,7 +24,7 @@ async function findPetsByTags(headers: FindPetsByTagsHeaderParams, params?: Find
headers: { ...headers, ...config.headers },
...config,
})
return { ...res, data: findPetsByTagsQueryResponseSchema.parse(res.data) }
return findPetsByTagsQueryResponseSchema.parse(res.data)
}

export function findPetsByTagsQueryOptions(headers: FindPetsByTagsHeaderParams, params?: FindPetsByTagsQueryParams, config: Partial<RequestConfig> = {}) {
Expand All @@ -40,18 +40,18 @@ export function findPetsByTagsQueryOptions(headers: FindPetsByTagsHeaderParams,
* @summary Finds Pets by tags
* @link /pet/findByTags
*/
export function useFindPetsByTags<TData = FindPetsByTagsQueryResponse>(
export function useFindPetsByTags(
headers: FindPetsByTagsHeaderParams,
params?: FindPetsByTagsQueryParams,
options: {
query?: SWRConfiguration<TData, FindPetsByTags400>
query?: SWRConfiguration<FindPetsByTagsQueryResponse, FindPetsByTags400>
client?: Partial<RequestConfig>
shouldFetch?: boolean
} = {},
) {
const { query: queryOptions, client: config = {}, shouldFetch = true } = options ?? {}
const url = '/pet/findByTags'
return useSWR<TData, FindPetsByTags400, typeof url | null>(shouldFetch ? url : null, {
const swrKey = ['/pet/findByTags', params] as const
return useSWR<FindPetsByTagsQueryResponse, FindPetsByTags400, Key>(shouldFetch ? swrKey : null, {
...findPetsByTagsQueryOptions(headers, params, config),
...queryOptions,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import client from '../../../../swr-client.ts'
import useSWR from 'swr'
import type { RequestConfig } from '../../../../swr-client.ts'
import type { GetPetByIdQueryResponse, GetPetByIdPathParams, GetPetById400, GetPetById404 } from '../../../models/ts/petController/GetPetById.ts'
import type { SWRConfiguration } from 'swr'
import type { Key, SWRConfiguration } from 'swr'
import { getPetByIdQueryResponseSchema } from '../../../zod/petController/getPetByIdSchema.ts'

/**
Expand All @@ -17,7 +17,7 @@ async function getPetById(petId: GetPetByIdPathParams['petId'], config: Partial<
baseURL: 'https://petstore3.swagger.io/api/v3',
...config,
})
return { ...res, data: getPetByIdQueryResponseSchema.parse(res.data) }
return getPetByIdQueryResponseSchema.parse(res.data)
}

export function getPetByIdQueryOptions(petId: GetPetByIdPathParams['petId'], config: Partial<RequestConfig> = {}) {
Expand All @@ -33,17 +33,17 @@ export function getPetByIdQueryOptions(petId: GetPetByIdPathParams['petId'], con
* @summary Find pet by ID
* @link /pet/:petId
*/
export function useGetPetById<TData = GetPetByIdQueryResponse>(
export function useGetPetById(
petId: GetPetByIdPathParams['petId'],
options: {
query?: SWRConfiguration<TData, GetPetById400 | GetPetById404>
query?: SWRConfiguration<GetPetByIdQueryResponse, GetPetById400 | GetPetById404>
client?: Partial<RequestConfig>
shouldFetch?: boolean
} = {},
) {
const { query: queryOptions, client: config = {}, shouldFetch = true } = options ?? {}
const url = `/pet/${petId}`
return useSWR<TData, GetPetById400 | GetPetById404, typeof url | null>(shouldFetch ? url : null, {
const swrKey = [`/pet/${petId}`] as const
return useSWR<GetPetByIdQueryResponse, GetPetById400 | GetPetById404, Key>(shouldFetch ? swrKey : null, {
...getPetByIdQueryOptions(petId, config),
...queryOptions,
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import client from '../../../../swr-client.ts'
import useSWRMutation from 'swr/mutation'
import type { RequestConfig } from '../../../../swr-client.ts'
import type {
UpdatePetMutationRequest,
UpdatePetMutationResponse,
Expand All @@ -8,46 +9,43 @@ import type {
UpdatePet405,
} from '../../../models/ts/petController/UpdatePet.ts'
import type { Key } from 'swr'
import type { SWRMutationConfiguration, SWRMutationResponse } from 'swr/mutation'
import type { SWRMutationConfiguration } from 'swr/mutation'
import { updatePetMutationResponseSchema } from '../../../zod/petController/updatePetSchema.ts'

type UpdatePetClient = typeof client<UpdatePetMutationResponse, UpdatePet400 | UpdatePet404 | UpdatePet405, UpdatePetMutationRequest>

type UpdatePet = {
data: UpdatePetMutationResponse
error: UpdatePet400 | UpdatePet404 | UpdatePet405
request: UpdatePetMutationRequest
pathParams: never
queryParams: never
headerParams: never
response: UpdatePetMutationResponse
client: {
parameters: Partial<Parameters<UpdatePetClient>[0]>
return: Awaited<ReturnType<UpdatePetClient>>
}
/**
* @description Update an existing pet by Id
* @summary Update an existing pet
* @link /pet
*/
async function updatePet(data: UpdatePetMutationRequest, config: Partial<RequestConfig<UpdatePetMutationRequest>> = {}) {
const res = await client<UpdatePetMutationResponse, UpdatePet400 | UpdatePet404 | UpdatePet405, UpdatePetMutationRequest>({
method: 'put',
url: '/pet',
baseURL: 'https://petstore3.swagger.io/api/v3',
data,
...config,
})
return updatePetMutationResponseSchema.parse(res.data)
}

/**
* @description Update an existing pet by Id
* @summary Update an existing pet
* @link /pet
*/
export function useUpdatePet(options?: {
mutation?: SWRMutationConfiguration<UpdatePet['response'], UpdatePet['error']>
client?: UpdatePet['client']['parameters']
shouldFetch?: boolean
}): SWRMutationResponse<UpdatePet['response'], UpdatePet['error']> {
const { mutation: mutationOptions, client: clientOptions = {}, shouldFetch = true } = options ?? {}
const url = '/pet' as const
return useSWRMutation<UpdatePet['response'], UpdatePet['error'], Key>(
shouldFetch ? url : null,
export function useUpdatePet(
options: {
mutation?: SWRMutationConfiguration<UpdatePetMutationResponse, UpdatePet400 | UpdatePet404 | UpdatePet405>
client?: Partial<RequestConfig<UpdatePetMutationRequest>>
shouldFetch?: boolean
} = {},
) {
const { mutation: mutationOptions, client: config = {}, shouldFetch = true } = options ?? {}
const swrKey = ['/pet'] as const
return useSWRMutation<UpdatePetMutationResponse, UpdatePet400 | UpdatePet404 | UpdatePet405, Key>(
shouldFetch ? swrKey : null,
async (_url, { arg: data }) => {
const res = await client<UpdatePet['data'], UpdatePet['error'], UpdatePet['request']>({
method: 'put',
url,
data,
...clientOptions,
})
return res.data
return updatePet(data, config)
},
mutationOptions,
)
Expand Down
Loading

0 comments on commit 90ad72b

Please sign in to comment.