Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: possible to generate a function using useSWRInfinite on the swr client. #1138

Merged
merged 10 commits into from
Jan 3, 2024

Conversation

soartec-lab
Copy link
Collaborator

@soartec-lab soartec-lab commented Jan 3, 2024

Happy New Year everyone 🙌

Status

READY

I squash commits if necessary when all reviews are complete.

Description

fix #796

I enabled the swr client to define functions using useSWRInfinite. These are generated by specifying them in the configuration file using the override option as shown below:

output: {
  client: 'swr',
  target: 'src/gen/endpoints',
  schemas: 'src/gen/model',
  override: {
    swr: {
      useInfinite: true,
    },
  },
},

Along with this, we are also making some refactorings:

  • Most of the implementations were combined in one string definition, but I decided to separate them as variables and combine them in the end.
  • The isEnabled judgment process and the key definition have been separated.

Related PRs

none

Todos

  • Tests
  • Documentation
  • Changelog Entry (unreleased)

Steps to Test or Reproduce

  1. Please define override.swr.useInfinite to true in the configuration file as below:
output: {
  client: 'swr',
  target: 'src/gen/endpoints',
  schemas: 'src/gen/model',
  override: {
    swr: {
      useInfinite: true,
    },
  },
},
  1. orval execite
orval
  1. An implementation definition using useSWRInfinite is generated
export const getListPetsInfiniteKeyLoader = (params?: ListPetsParams,) => {
  return (_: number, previousPageData: Awaited<ReturnType<typeof listPets>>) => {
    if (previousPageData && !previousPageData.data) return null

    return [`http://localhost:8000/pets`, ...(params ? [params]: [])] as const;
  }
}

export type ListPetsInfiniteQueryResult = NonNullable<Awaited<ReturnType<typeof listPets>>>
export type ListPetsInfiniteError = AxiosError<Error>

/**
 * @summary List all pets
 */
export const useListPetsInfinite = <TError = AxiosError<Error>>(
  params?: ListPetsParams, options?: { swr?:SWRInfiniteConfiguration<Awaited<ReturnType<typeof listPets>>, TError> & { swrKeyLoader?: SWRInfiniteKeyLoader, enabled?: boolean }, axios?: AxiosRequestConfig }
) => {
  const {swr: swrOptions, axios: axiosOptions} = options ?? {}

  const isEnabled = swrOptions?.enabled !== false
  const swrKeyLoader = swrOptions?.swrKeyLoader ?? (() => isEnabled ? getListPetsInfiniteKeyLoader(params) : null);
  const swrFn = () => listPets(params, axiosOptions);

  const query = useSWRInfinite<Awaited<ReturnType<typeof swrFn>>, TError>(swrKeyLoader, swrFn, swrOptions)

  return {
    swrKeyLoader,
    ...query
  }
}

@soartec-lab soartec-lab marked this pull request as ready for review January 3, 2024 03:12
@melloware melloware added this to the 6.24.0 milestone Jan 3, 2024
@melloware
Copy link
Collaborator

@soartec-lab great PR! Don't worry about sqashing I always Squash PRs on Merge like this...

image

@soartec-lab
Copy link
Collaborator Author

@melloware

Thank you for your review. Also, thank you for teaching me about handling commits. relieved.

@melloware melloware merged commit 67bf84f into anymaniax:master Jan 3, 2024
2 checks passed
@melloware
Copy link
Collaborator

Squashed and merged!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feature: add useSWRInfinite as additional client swr
2 participants