Skip to content

Commit

Permalink
chore: made directory for swr sample apps (#1556)
Browse files Browse the repository at this point in the history
  • Loading branch information
soartec-lab authored Aug 1, 2024
1 parent 8f555c0 commit b7d51f6
Show file tree
Hide file tree
Showing 33 changed files with 210 additions and 29 deletions.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "swr",
"name": "swr-basic",
"version": "0.1.0",
"private": true,
"dependencies": {
Expand All @@ -16,14 +16,14 @@
"typescript": "^4.1.3"
},
"devDependencies": {
"orval": "link:../../packages/orval/dist",
"orval": "link:../../../packages/orval/dist",
"prettier": "^3.0.3"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"eject": "react-scripts eject",
"generate-api": "node ../../packages/orval/dist/bin/orval.js"
"generate-api": "node ../../../packages/orval/dist/bin/orval.js"
},
"eslintConfig": {
"extends": "react-app"
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
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.
52 changes: 26 additions & 26 deletions samples/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -17300,6 +17300,12 @@ __metadata:
languageName: node
linkType: soft

"orval@link:../../../packages/orval/dist::locator=swr-basic%40workspace%3Areact-app-with-swr%2Fbasic":
version: 0.0.0-use.local
resolution: "orval@link:../../../packages/orval/dist::locator=swr-basic%40workspace%3Areact-app-with-swr%2Fbasic"
languageName: node
linkType: soft

"orval@link:../../../packages/orval/dist::locator=vue-query-custom-fetch%40workspace%3Avue-query%2Fcustom-fetch":
version: 0.0.0-use.local
resolution: "orval@link:../../../packages/orval/dist::locator=vue-query-custom-fetch%40workspace%3Avue-query%2Fcustom-fetch"
Expand Down Expand Up @@ -17336,12 +17342,6 @@ __metadata:
languageName: node
linkType: soft

"orval@link:../../packages/orval/dist::locator=swr%40workspace%3Areact-app-with-swr":
version: 0.0.0-use.local
resolution: "orval@link:../../packages/orval/dist::locator=swr%40workspace%3Areact-app-with-swr"
languageName: node
linkType: soft

"orval@link:../../packages/orval/dist::locator=swr-with-zod%40workspace%3Aswr-with-zod":
version: 0.0.0-use.local
resolution: "orval@link:../../packages/orval/dist::locator=swr-with-zod%40workspace%3Aswr-with-zod"
Expand Down Expand Up @@ -22204,6 +22204,26 @@ __metadata:
languageName: node
linkType: hard

"swr-basic@workspace:react-app-with-swr/basic":
version: 0.0.0-use.local
resolution: "swr-basic@workspace:react-app-with-swr/basic"
dependencies:
"@faker-js/faker": "npm:^8.0.2"
"@types/node": "npm:^14.14.13"
"@types/react": "npm:^17.0.0"
"@types/react-dom": "npm:^17.0.0"
axios: "npm:^0.26.1"
msw: "npm:^2.0.2"
orval: "link:../../../packages/orval/dist"
prettier: "npm:^3.0.3"
react: "npm:^17.0.1"
react-dom: "npm:^17.0.1"
react-scripts: "npm:^5.0.1"
swr: "npm:^1.2.2"
typescript: "npm:^4.1.3"
languageName: unknown
linkType: soft

"swr-with-zod@workspace:swr-with-zod":
version: 0.0.0-use.local
resolution: "swr-with-zod@workspace:swr-with-zod"
Expand Down Expand Up @@ -22247,26 +22267,6 @@ __metadata:
languageName: node
linkType: hard

"swr@workspace:react-app-with-swr":
version: 0.0.0-use.local
resolution: "swr@workspace:react-app-with-swr"
dependencies:
"@faker-js/faker": "npm:^8.0.2"
"@types/node": "npm:^14.14.13"
"@types/react": "npm:^17.0.0"
"@types/react-dom": "npm:^17.0.0"
axios: "npm:^0.26.1"
msw: "npm:^2.0.2"
orval: "link:../../packages/orval/dist"
prettier: "npm:^3.0.3"
react: "npm:^17.0.1"
react-dom: "npm:^17.0.1"
react-scripts: "npm:^5.0.1"
swr: "npm:^1.2.2"
typescript: "npm:^4.1.3"
languageName: unknown
linkType: soft

"symbol-observable@npm:4.0.0":
version: 4.0.0
resolution: "symbol-observable@npm:4.0.0"
Expand Down

0 comments on commit b7d51f6

Please sign in to comment.