forked from orval-labs/orval
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: manage enum plus number (orval-labs#213)
* fix: manage plus number in enum and use isIdentifierNameES5 for key * test: add an enum in petstore
- Loading branch information
1 parent
09ea830
commit 9f61489
Showing
11 changed files
with
146 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
221 changes: 109 additions & 112 deletions
221
samples/react-app-with-react-query/src/api/endpoints/petstoreFromFileSpecWithTransformer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,135 +1,132 @@ | ||
/** | ||
* Generated by orval v6.0.0 🍺 | ||
* Generated by orval v6.1.1 🍺 | ||
* Do not edit manually. | ||
* Swagger Petstore | ||
* OpenAPI spec version: 1.0.0 | ||
*/ | ||
import { | ||
useMutation, | ||
UseMutationOptions, | ||
useQuery, | ||
useMutation, | ||
UseQueryOptions, | ||
} from 'react-query'; | ||
UseMutationOptions | ||
} from 'react-query' | ||
import type { | ||
CreatePetsBody, | ||
Pets, | ||
Error, | ||
ListPetsParams, | ||
Pet, | ||
Pets, | ||
} from '../model'; | ||
import { customInstance } from '../mutator/custom-instance'; | ||
|
||
type AsyncReturnType<T extends (...args: any) => Promise<any>> = T extends ( | ||
...args: any | ||
) => Promise<infer R> | ||
? R | ||
: any; | ||
|
||
export const listPets = (params?: ListPetsParams, version = 1) => { | ||
return customInstance<Pets>({ | ||
url: `/v${version}/pets`, | ||
method: 'get', | ||
params, | ||
}); | ||
}; | ||
|
||
export const getListPetsQueryKey = (params?: ListPetsParams, version = 1) => [ | ||
`/v${version}/pets`, | ||
...(params ? [params] : []), | ||
]; | ||
|
||
export const useListPets = < | ||
TData = AsyncReturnType<typeof listPets>, | ||
TError = Error, | ||
>( | ||
params?: ListPetsParams, | ||
version = 1, | ||
options?: { | ||
query?: UseQueryOptions<AsyncReturnType<typeof listPets>, TError, TData>; | ||
}, | ||
) => { | ||
const { query: queryOptions } = options || {}; | ||
CreatePetsBody | ||
} from '../model' | ||
import { customInstance } from '../mutator/custom-instance' | ||
|
||
const queryKey = | ||
queryOptions?.queryKey ?? getListPetsQueryKey(params, version); | ||
const queryFn = () => listPets(params, version); | ||
type AsyncReturnType< | ||
T extends (...args: any) => Promise<any> | ||
> = T extends (...args: any) => Promise<infer R> ? R : any; | ||
|
||
const query = useQuery<AsyncReturnType<typeof queryFn>, TError, TData>( | ||
queryKey, | ||
queryFn, | ||
{ enabled: !!version, ...queryOptions }, | ||
); | ||
|
||
/** | ||
* @summary List all pets | ||
*/ | ||
export const listPets = ( | ||
params?: ListPetsParams, | ||
version= 1, | ||
) => { | ||
return customInstance<Pets>( | ||
{url: `/v${version}/pets`, method: 'get', | ||
params, | ||
}, | ||
); | ||
} | ||
|
||
|
||
export const getListPetsQueryKey = (params?: ListPetsParams, | ||
version= 1,) => [`/v${version}/pets`, ...(params ? [params]: [])]; | ||
|
||
|
||
export const useListPets = <TData = AsyncReturnType<typeof listPets>, TError = Error>( | ||
params?: ListPetsParams, | ||
version= 1, options?: { query?:UseQueryOptions<AsyncReturnType<typeof listPets>, TError, TData>, } | ||
|
||
) => { | ||
|
||
const {query: queryOptions} = options || {} | ||
|
||
const queryKey = queryOptions?.queryKey ?? getListPetsQueryKey(params,version); | ||
const queryFn = () => listPets(params,version, ); | ||
|
||
const query = useQuery<AsyncReturnType<typeof queryFn>, TError, TData>(queryKey, queryFn, {enabled: !!(version), ...queryOptions}) | ||
|
||
return { | ||
queryKey, | ||
...query, | ||
}; | ||
}; | ||
|
||
export const createPets = (createPetsBody: CreatePetsBody, version = 1) => { | ||
return customInstance<Pet>({ | ||
url: `/v${version}/pets`, | ||
method: 'post', | ||
data: createPetsBody, | ||
}); | ||
}; | ||
|
||
export const useCreatePets = <TError = Error, TContext = unknown>(options?: { | ||
mutation?: UseMutationOptions< | ||
AsyncReturnType<typeof createPets>, | ||
TError, | ||
{ data: CreatePetsBody; version?: number }, | ||
TContext | ||
>; | ||
}) => { | ||
const { mutation: mutationOptions } = options || {}; | ||
|
||
return useMutation< | ||
AsyncReturnType<typeof createPets>, | ||
TError, | ||
{ data: CreatePetsBody; version?: number }, | ||
TContext | ||
>((props) => { | ||
const { data, version } = props || {}; | ||
|
||
return createPets(data, version); | ||
}, mutationOptions); | ||
}; | ||
export const showPetById = (petId: string, version = 1) => { | ||
return customInstance<Pet>({ | ||
url: `/v${version}/pets/${petId}`, | ||
method: 'get', | ||
}); | ||
}; | ||
|
||
export const getShowPetByIdQueryKey = (petId: string, version = 1) => [ | ||
`/v${version}/pets/${petId}`, | ||
]; | ||
|
||
export const useShowPetById = < | ||
TData = AsyncReturnType<typeof showPetById>, | ||
TError = Error, | ||
>( | ||
petId: string, | ||
version = 1, | ||
options?: { | ||
query?: UseQueryOptions<AsyncReturnType<typeof showPetById>, TError, TData>; | ||
}, | ||
...query | ||
} | ||
} | ||
|
||
|
||
/** | ||
* @summary Create a pet | ||
*/ | ||
export const createPets = ( | ||
createPetsBody: CreatePetsBody, | ||
version= 1, | ||
) => { | ||
return customInstance<Pet>( | ||
{url: `/v${version}/pets`, method: 'post', | ||
data: createPetsBody | ||
}, | ||
); | ||
} | ||
|
||
|
||
|
||
export const useCreatePets = <TError = Error, | ||
|
||
TContext = unknown>(options?: { mutation?:UseMutationOptions<AsyncReturnType<typeof createPets>, TError,{data: CreatePetsBody;version?: number}, TContext>, } | ||
) => { | ||
const { query: queryOptions } = options || {}; | ||
const {mutation: mutationOptions} = options || {} | ||
|
||
const queryKey = | ||
queryOptions?.queryKey ?? getShowPetByIdQueryKey(petId, version); | ||
const queryFn = () => showPetById(petId, version); | ||
return useMutation<AsyncReturnType<typeof createPets>, TError, {data: CreatePetsBody;version?: number}, TContext>((props) => { | ||
const {data,version} = props || {}; | ||
|
||
const query = useQuery<AsyncReturnType<typeof queryFn>, TError, TData>( | ||
queryKey, | ||
queryFn, | ||
{ enabled: !!(version && petId), ...queryOptions }, | ||
); | ||
return createPets(data,version,) | ||
}, mutationOptions) | ||
} | ||
|
||
/** | ||
* @summary Info for a specific pet | ||
*/ | ||
export const showPetById = ( | ||
petId: string, | ||
version= 1, | ||
) => { | ||
return customInstance<Pet>( | ||
{url: `/v${version}/pets/${petId}`, method: 'get' | ||
}, | ||
); | ||
} | ||
|
||
|
||
export const getShowPetByIdQueryKey = (petId: string, | ||
version= 1,) => [`/v${version}/pets/${petId}`]; | ||
|
||
|
||
export const useShowPetById = <TData = AsyncReturnType<typeof showPetById>, TError = Error>( | ||
petId: string, | ||
version= 1, options?: { query?:UseQueryOptions<AsyncReturnType<typeof showPetById>, TError, TData>, } | ||
|
||
) => { | ||
|
||
const {query: queryOptions} = options || {} | ||
|
||
const queryKey = queryOptions?.queryKey ?? getShowPetByIdQueryKey(petId,version); | ||
const queryFn = () => showPetById(petId,version, ); | ||
|
||
const query = useQuery<AsyncReturnType<typeof queryFn>, TError, TData>(queryKey, queryFn, {enabled: !!(version && petId), ...queryOptions}) | ||
|
||
return { | ||
queryKey, | ||
...query, | ||
}; | ||
}; | ||
...query | ||
} | ||
} | ||
|
||
|
2 changes: 1 addition & 1 deletion
2
samples/react-app-with-react-query/src/api/model/createPetsBody.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
samples/react-app-with-react-query/src/api/model/listPetsParams.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,16 @@ | ||
/** | ||
* Generated by orval v6.0.0 🍺 | ||
* Generated by orval v6.1.1 🍺 | ||
* Do not edit manually. | ||
* Swagger Petstore | ||
* OpenAPI spec version: 1.0.0 | ||
*/ | ||
import type { PetCallingCode } from './petCallingCode'; | ||
|
||
export interface Pet { | ||
'@id'?: string; | ||
id: number; | ||
name: string; | ||
tag?: string; | ||
email?: string; | ||
callingCode?: PetCallingCode; | ||
} |
14 changes: 14 additions & 0 deletions
14
samples/react-app-with-react-query/src/api/model/petCallingCode.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/** | ||
* Generated by orval v6.1.1 🍺 | ||
* Do not edit manually. | ||
* Swagger Petstore | ||
* OpenAPI spec version: 1.0.0 | ||
*/ | ||
|
||
export type PetCallingCode = '+33' | '+420'; | ||
|
||
|
||
export const PetCallingCode = { | ||
NUMBER_PLUS_33: '+33' as PetCallingCode, | ||
NUMBER_PLUS_420: '+420' as PetCallingCode, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters