-
-
Notifications
You must be signed in to change notification settings - Fork 351
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: made directory for
swr
sample apps (#1556)
- Loading branch information
1 parent
8f555c0
commit b7d51f6
Showing
33 changed files
with
210 additions
and
29 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
181 changes: 181 additions & 0 deletions
181
samples/react-app-with-swr/basic/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 |
---|---|---|
@@ -0,0 +1,181 @@ | ||
/** | ||
* Generated by orval v7.0.0 🍺 | ||
* Do not edit manually. | ||
* Swagger Petstore | ||
* OpenAPI spec version: 1.0.0 | ||
*/ | ||
import useSwr from 'swr'; | ||
import type { Key, SWRConfiguration } from 'swr'; | ||
import useSWRMutation from 'swr/mutation'; | ||
import type { SWRMutationConfiguration } from 'swr/mutation'; | ||
import type { | ||
CreatePetsBody, | ||
Error, | ||
ListPetsParams, | ||
Pet, | ||
Pets, | ||
} from '../model'; | ||
import { customInstance } from '../mutator/custom-instance'; | ||
|
||
type AwaitedInput<T> = PromiseLike<T> | T; | ||
|
||
type Awaited<O> = O extends AwaitedInput<infer T> ? T : never; | ||
|
||
/** | ||
* @summary List all pets | ||
*/ | ||
export const listPets = (params?: ListPetsParams, version: number = 1) => { | ||
return customInstance<Pets>({ | ||
url: `/v${version}/pets`, | ||
method: 'GET', | ||
params, | ||
}); | ||
}; | ||
|
||
export const getListPetsKey = (params?: ListPetsParams, version: number = 1) => | ||
[`/v${version}/pets`, ...(params ? [params] : [])] as const; | ||
|
||
export type ListPetsQueryResult = NonNullable< | ||
Awaited<ReturnType<typeof listPets>> | ||
>; | ||
export type ListPetsQueryError = Error; | ||
|
||
/** | ||
* @summary List all pets | ||
*/ | ||
export const useListPets = <TError = Error>( | ||
params?: ListPetsParams, | ||
version: number = 1, | ||
options?: { | ||
swr?: SWRConfiguration<Awaited<ReturnType<typeof listPets>>, TError> & { | ||
swrKey?: Key; | ||
enabled?: boolean; | ||
}; | ||
}, | ||
) => { | ||
const { swr: swrOptions } = options ?? {}; | ||
|
||
const isEnabled = swrOptions?.enabled !== false && !!version; | ||
const swrKey = | ||
swrOptions?.swrKey ?? | ||
(() => (isEnabled ? getListPetsKey(params, version) : null)); | ||
const swrFn = () => listPets(params, version); | ||
|
||
const query = useSwr<Awaited<ReturnType<typeof swrFn>>, TError>( | ||
swrKey, | ||
swrFn, | ||
swrOptions, | ||
); | ||
|
||
return { | ||
swrKey, | ||
...query, | ||
}; | ||
}; | ||
|
||
/** | ||
* @summary Create a pet | ||
*/ | ||
export const createPets = ( | ||
createPetsBody: CreatePetsBody, | ||
version: number = 1, | ||
) => { | ||
return customInstance<Pet>({ | ||
url: `/v${version}/pets`, | ||
method: 'POST', | ||
headers: { 'Content-Type': 'application/json' }, | ||
data: createPetsBody, | ||
}); | ||
}; | ||
|
||
export const getCreatePetsMutationFetcher = (version: number = 1) => { | ||
return (_: Key, { arg }: { arg: CreatePetsBody }): Promise<Pet> => { | ||
return createPets(arg, version); | ||
}; | ||
}; | ||
export const getCreatePetsMutationKey = (version: number = 1) => | ||
[`/v${version}/pets`] as const; | ||
|
||
export type CreatePetsMutationResult = NonNullable< | ||
Awaited<ReturnType<typeof createPets>> | ||
>; | ||
export type CreatePetsMutationError = Error; | ||
|
||
/** | ||
* @summary Create a pet | ||
*/ | ||
export const useCreatePets = <TError = Error>( | ||
version: number = 1, | ||
options?: { | ||
swr?: SWRMutationConfiguration< | ||
Awaited<ReturnType<typeof createPets>>, | ||
TError, | ||
Key, | ||
CreatePetsBody, | ||
Awaited<ReturnType<typeof createPets>> | ||
> & { swrKey?: string }; | ||
}, | ||
) => { | ||
const { swr: swrOptions } = options ?? {}; | ||
|
||
const swrKey = swrOptions?.swrKey ?? getCreatePetsMutationKey(version); | ||
const swrFn = getCreatePetsMutationFetcher(version); | ||
|
||
const query = useSWRMutation(swrKey, swrFn, swrOptions); | ||
|
||
return { | ||
swrKey, | ||
...query, | ||
}; | ||
}; | ||
|
||
/** | ||
* @summary Info for a specific pet | ||
*/ | ||
export const showPetById = (petId: string, version: number = 1) => { | ||
return customInstance<Pet>({ | ||
url: `/v${version}/pets/${petId}`, | ||
method: 'GET', | ||
}); | ||
}; | ||
|
||
export const getShowPetByIdKey = (petId: string, version: number = 1) => | ||
[`/v${version}/pets/${petId}`] as const; | ||
|
||
export type ShowPetByIdQueryResult = NonNullable< | ||
Awaited<ReturnType<typeof showPetById>> | ||
>; | ||
export type ShowPetByIdQueryError = Error; | ||
|
||
/** | ||
* @summary Info for a specific pet | ||
*/ | ||
export const useShowPetById = <TError = Error>( | ||
petId: string, | ||
version: number = 1, | ||
options?: { | ||
swr?: SWRConfiguration<Awaited<ReturnType<typeof showPetById>>, TError> & { | ||
swrKey?: Key; | ||
enabled?: boolean; | ||
}; | ||
}, | ||
) => { | ||
const { swr: swrOptions } = options ?? {}; | ||
|
||
const isEnabled = swrOptions?.enabled !== false && !!(version && petId); | ||
const swrKey = | ||
swrOptions?.swrKey ?? | ||
(() => (isEnabled ? getShowPetByIdKey(petId, version) : null)); | ||
const swrFn = () => showPetById(petId, version); | ||
|
||
const query = useSwr<Awaited<ReturnType<typeof swrFn>>, TError>( | ||
swrKey, | ||
swrFn, | ||
swrOptions, | ||
); | ||
|
||
return { | ||
swrKey, | ||
...query, | ||
}; | ||
}; |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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