{"version":3,"file":"index.cjs","sources":["../src/query-base.ts","../src/immutable.ts","../src/infinite.ts","../src/mutate.ts","../src/query.ts"],"sourcesContent":["import type { Client } from \"openapi-fetch\";\nimport type {\n  MediaType,\n  PathsWithMethod,\n  RequiredKeysOf,\n} from \"openapi-typescript-helpers\";\nimport { useCallback, useDebugValue, useMemo } from \"react\";\nimport type { Fetcher, SWRHook } from \"swr\";\nimport type { Exact } from \"type-fest\";\nimport type { TypesForGetRequest } from \"./types.js\";\n\n/**\n * @private\n */\nexport function configureBaseQueryHook(useHook: SWRHook) {\n  return function createQueryBaseHook<\n    Paths extends {},\n    IMediaType extends MediaType,\n    Prefix extends string,\n    FetcherError = never,\n  >(client: Client<Paths, IMediaType>, prefix: Prefix) {\n    return function useQuery<\n      Path extends PathsWithMethod<Paths, \"get\">,\n      R extends TypesForGetRequest<Paths, Path>,\n      Init extends Exact<R[\"Init\"], Init>,\n      Data extends R[\"Data\"],\n      Error extends R[\"Error\"] | FetcherError,\n      Config extends R[\"SWRConfig\"],\n    >(\n      path: Path,\n      ...[init, config]: RequiredKeysOf<Init> extends never\n        ? [(Init | null)?, Config?]\n        : [Init | null, Config?]\n    ) {\n      // oxlint-disable-next-line no-unsafe-type-assertion\n      useDebugValue(`${prefix} - ${path as string}`);\n\n      const key = useMemo(\n        () => (init !== null ? ([prefix, path, init] as const) : null),\n        [prefix, path, init],\n      );\n\n      type Key = typeof key;\n\n      // TODO: Lift up fetcher to and remove useCallback\n      const fetcher: Fetcher<Data, Key> = useCallback(\n        async ([_, path, init]) => {\n          // @ts-expect-error TODO: Improve internal init types\n          const res = await client.GET(path, init);\n          if (res.error) {\n            throw res.error;\n          }\n          // oxlint-disable-next-line no-unsafe-type-assertion\n          return res.data as Data;\n        },\n        [client],\n      );\n\n      // @ts-expect-error TODO: Improve internal config types\n      return useHook<Data, Error, Key, Config>(key, fetcher, config);\n    };\n  };\n}\n","import useSWRImmutable from \"swr/immutable\";\nimport { configureBaseQueryHook } from \"./query-base.js\";\n\n/**\n * Produces a typed wrapper for [`useSWRImmutable`](https://swr.vercel.app/docs/revalidation.en-US#disable-automatic-revalidations).\n *\n * ```ts\n * import createClient from \"openapi-fetch\";\n * const client = createClient();\n *\n * const useImmutable = createImmutableHook(client, \"<unique-key>\");\n *\n * // Fetch the query\n * useImmutable(\"/pets\");\n *\n * // Skip the query\n * useImmutable(\"/pets\", null);\n *\n * // Fetch the query with parameters\n * useImmutable(\"/pets\", {\n *   params: { query: { limit: 10 } }\n * });\n *\n * // Fetch the query with parameters and SWR configuration\n * useImmutable(\n *   \"/pets\",\n *   { params: { query: { limit: 10 } } },\n *   { errorRetryCount: 2 },\n * );\n *\n * // Fetch the query with no parameters and SWR configuration\n * useImmutable(\n *   \"/pets\",\n *   {},\n *   { errorRetryCount: 2 },\n * );\n * ```\n */\nexport const createImmutableHook = configureBaseQueryHook(useSWRImmutable);\n","import type { Client } from \"openapi-fetch\";\nimport type { MediaType, PathsWithMethod } from \"openapi-typescript-helpers\";\nimport { useCallback, useDebugValue } from \"react\";\nimport useSWRInfinite, {\n  type SWRInfiniteConfiguration,\n  type SWRInfiniteFetcher,\n  type SWRInfiniteKeyLoader,\n} from \"swr/infinite\";\nimport type { Exact } from \"type-fest\";\nimport type { TypesForGetRequest } from \"./types.js\";\n\n/**\n * Produces a typed wrapper for [`useSWRInfinite`](https://swr.vercel.app/docs/pagination#useswrinfinite).\n *\n * ```ts\n * import createClient from \"openapi-fetch\";\n * const client = createClient();\n *\n * const useInfinite = createInfiniteHook(client, \"<unique-key>\");\n *\n * useInfinite(\"/pets\", (index, previousPage) => {\n *   if (previousPage && !previousPage.hasMore) {\n *     return null;\n *   }\n *\n *   return {\n *     params: {\n *       query: {\n *         limit: 10,\n *         offset: index * 10,\n *       },\n *     },\n *   };\n * });\n * ```\n */\nexport function createInfiniteHook<\n  Paths extends {},\n  IMediaType extends MediaType,\n  Prefix extends string,\n  FetcherError = never,\n>(client: Client<Paths, IMediaType>, prefix: Prefix) {\n  return function useInfinite<\n    Path extends PathsWithMethod<Paths, \"get\">,\n    R extends TypesForGetRequest<Paths, Path>,\n    Init extends Exact<R[\"Init\"], Init>,\n    Data extends R[\"Data\"],\n    Error extends R[\"Error\"] | FetcherError,\n    Config extends SWRInfiniteConfiguration<Data, Error>,\n  >(\n    path: Path,\n    getInit: SWRInfiniteKeyLoader<Data, Init | null>,\n    config?: Config,\n  ) {\n    type Key = [Prefix, Path, Init | undefined] | null;\n    type KeyLoader = SWRInfiniteKeyLoader<Data, Key>;\n\n    // oxlint-disable-next-line no-unsafe-type-assertion\n    useDebugValue(`${prefix} - ${path as string}`);\n\n    const fetcher: SWRInfiniteFetcher<Data, KeyLoader> = useCallback(\n      async ([_, path, init]) => {\n        // @ts-expect-error TODO: Improve internal init types\n        const res = await client.GET(path, init);\n        if (res.error) {\n          throw res.error;\n        }\n        // oxlint-disable-next-line no-unsafe-type-assertion\n        return res.data as Data;\n      },\n      [client],\n    );\n\n    const getKey: KeyLoader = (index, previousPageData) => {\n      const init = getInit(index, previousPageData);\n      if (init === null) {\n        return null;\n      }\n      const key: Key = [prefix, path, init];\n      return key;\n    };\n\n    return useSWRInfinite<Data, Error, KeyLoader>(getKey, fetcher, config);\n  };\n}\n","import type { Client } from \"openapi-fetch\";\nimport type { MediaType, PathsWithMethod } from \"openapi-typescript-helpers\";\nimport { useCallback, useDebugValue } from \"react\";\nimport { type MutatorCallback, type MutatorOptions, useSWRConfig } from \"swr\";\nimport type { Exact, PartialDeep } from \"type-fest\";\nimport type { TypesForGetRequest } from \"./types.js\";\n\n// Types are loose here to support ecosystem utilities like `_.isMatch`\nexport type CompareFn = (init: any, partialInit: any) => boolean;\n\n/**\n * Produces a typed wrapper for [`useSWRConfig#mutate`](https://swr.vercel.app/docs/mutation).\n *\n * ```ts\n * import createClient from \"openapi-fetch\";\n * import { isMatch } from \"lodash\";\n *\n * const client = createClient();\n *\n * const useMutate = createMutateHook(client, \"<unique-key>\", isMatch);\n *\n * const mutate = useMutate();\n *\n * // Revalidate all keys matching this path\n * await mutate([\"/pets\"]);\n * await mutate([\"/pets\"], newData);\n * await mutate([\"/pets\"], undefined, { revalidate: true });\n *\n * // Revlidate all keys matching this path and this subset of options\n * await mutate(\n *   [\"/pets\", { query: { limit: 10 } }],\n *   newData,\n *   { revalidate: false }\n * );\n * ```\n */\nexport function createMutateHook<\n  Paths extends {},\n  IMediaType extends MediaType,\n>(client: Client<Paths, IMediaType>, prefix: string, compare: CompareFn) {\n  return function useMutate() {\n    const { mutate: swrMutate } = useSWRConfig();\n\n    useDebugValue(prefix);\n\n    return useCallback(\n      function mutate<\n        Path extends PathsWithMethod<Paths, \"get\">,\n        R extends TypesForGetRequest<Paths, Path>,\n        Init extends Exact<R[\"Init\"], Init>,\n      >(\n        [path, init]: [Path, PartialDeep<Init>?],\n        data?: R[\"Data\"] | Promise<R[\"Data\"]> | MutatorCallback<R[\"Data\"]>,\n        opts?: boolean | MutatorOptions<R[\"Data\"]>,\n      ) {\n        return swrMutate<R[\"Data\"], R[\"Data\"]>(\n          (key) => {\n            if (\n              // Must be array\n              !Array.isArray(key) ||\n              // Must have 2 or 3 elements (prefix, path, optional init)\n              ![2, 3].includes(key.length)\n            ) {\n              return false;\n            }\n\n            const [keyPrefix, keyPath, keyOptions] = key as unknown[];\n\n            return (\n              // Matching prefix\n              keyPrefix === prefix &&\n              // Matching path\n              keyPath === path &&\n              // Matching options\n              (init ? compare(keyOptions, init) : true)\n            );\n          },\n          data,\n          opts,\n        );\n      },\n      [swrMutate, prefix, compare],\n    );\n  };\n}\n","import useSWR from \"swr\";\nimport { configureBaseQueryHook } from \"./query-base.js\";\n\n/**\n * Produces a typed wrapper for [`useSWR`](https://swr.vercel.app/docs/api).\n *\n * ```ts\n * import createClient from \"openapi-fetch\";\n *\n * const client = createClient();\n *\n * const useQuery = createQueryHook(client, \"<unique-key>\");\n *\n * // Fetch the query\n * useQuery(\"/pets\");\n *\n * // Skip the query\n * useQuery(\"/pets\", null);\n *\n * // Fetch the query with parameters\n * useQuery(\"/pets\", {\n *   params: { query: { limit: 10 } }\n * });\n *\n * // Fetch the query with parameters and SWR configuration\n * useQuery(\n *   \"/pets\",\n *   { params: { query: { limit: 10 } } },\n *   { errorRetryCount: 2 },\n * );\n *\n * // Fetch the query with no parameters and SWR configuration\n * useQuery(\n *   \"/pets\",\n *   {},\n *   { errorRetryCount: 2 },\n * );\n * ```\n */\nexport const createQueryHook = configureBaseQueryHook(useSWR);\n"],"names":["useDebugValue","useMemo","useCallback","path","init","useSWRImmutable","useSWRInfinite","useSWRConfig","useSWR"],"mappings":";;;;;;;;;;;;;AAcO,SAAS,uBAAuB,OAAA,EAAkB;AACvD,EAAA,OAAO,SAAS,mBAAA,CAKd,MAAA,EAAmC,MAAA,EAAgB;AACnD,IAAA,OAAO,SAAS,QAAA,CAQd,IAAA,EAAA,GACG,CAAC,IAAA,EAAM,MAAM,CAAA,EAGhB;AAEA,MAAAA,mBAAA,CAAc,CAAA,EAAG,MAAM,CAAA,GAAA,EAAM,IAAc,CAAA,CAAE,CAAA;AAE7C,MAAA,MAAM,GAAA,GAAMC,aAAA;AAAA,QACV,MAAO,IAAA,KAAS,IAAA,GAAQ,CAAC,MAAA,EAAQ,IAAA,EAAM,IAAI,CAAA,GAAc,IAAA;AAAA,QACzD,CAAC,MAAA,EAAQ,IAAA,EAAM,IAAI;AAAA,OACrB;AAKA,MAAA,MAAM,OAAA,GAA8BC,iBAAA;AAAA,QAClC,OAAO,CAAC,CAAA,EAAGC,KAAAA,EAAMC,KAAI,CAAA,KAAM;AAEzB,UAAA,MAAM,GAAA,GAAM,MAAM,MAAA,CAAO,GAAA,CAAID,OAAMC,KAAI,CAAA;AACvC,UAAA,IAAI,IAAI,KAAA,EAAO;AACb,YAAA,MAAM,GAAA,CAAI,KAAA;AAAA,UACZ;AAEA,UAAA,OAAO,GAAA,CAAI,IAAA;AAAA,QACb,CAAA;AAAA,QACA,CAAC,MAAM;AAAA,OACT;AAGA,MAAA,OAAO,OAAA,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