{"version":3,"file":"use-resource.cjs","names":[],"sources":["../../src/data/use-resource.ts"],"sourcesContent":["import { useMutation, useQuery, useQueryClient } from \"@tanstack/react-query\";\nimport type {\n    UseMutationOptions,\n    UseMutationResult,\n    UseQueryOptions,\n    UseQueryResult,\n} from \"@tanstack/react-query\";\n\nimport type { OffsetPage } from \"@/query/pagination\";\n\nimport type { GetListParams } from \"./create-data-provider\";\nimport { useDataProvider } from \"./data-provider-context\";\n\n/** Extra TanStack Query options for {@link useList} (key + fn are provided). */\nexport type UseListOptions<T> = Omit<\n    UseQueryOptions<OffsetPage<T>, Error, OffsetPage<T>>,\n    \"queryKey\" | \"queryFn\"\n>;\n\n/** Extra TanStack Query options for {@link useOne} (key + fn + enabled are provided). */\nexport type UseOneOptions<T> = Omit<\n    UseQueryOptions<T, Error, T>,\n    \"queryKey\" | \"queryFn\" | \"enabled\"\n> & { enabled?: boolean };\n\n/**\n * Build the cache key for a resource list query.\n *\n * @param resource - The resource name.\n * @param params - The list params (page/sort/filters).\n * @returns A stable TanStack Query key.\n */\nexport function listQueryKey(resource: string, params?: GetListParams): unknown[] {\n    return [\"data\", resource, \"list\", params];\n}\n\n/**\n * Build the cache key for a single-record query.\n *\n * @param resource - The resource name.\n * @param id - The record id.\n * @returns A stable TanStack Query key.\n */\nexport function oneQueryKey(resource: string, id: string | number | null | undefined): unknown[] {\n    return [\"data\", resource, \"one\", id];\n}\n\n/**\n * Query a paginated list of a resource through the active {@link useDataProvider}.\n *\n * @param resource - The resource name.\n * @param params - Pagination, sort and filter parameters.\n * @param options - Extra TanStack Query options.\n * @returns The TanStack Query result for the offset page.\n */\nexport function useList<T>(\n    resource: string,\n    params?: GetListParams,\n    options?: UseListOptions<T>,\n): UseQueryResult<OffsetPage<T>, Error> {\n    const provider = useDataProvider();\n    return useQuery<OffsetPage<T>, Error>({\n        queryKey: listQueryKey(resource, params),\n        queryFn: () => provider.getList<T>(resource, params),\n        ...options,\n    });\n}\n\n/**\n * Query a single record by id through the active {@link useDataProvider}.\n *\n * The query is disabled while `id` is `null`/`undefined`.\n *\n * @param resource - The resource name.\n * @param id - The record id.\n * @param options - Extra TanStack Query options.\n * @returns The TanStack Query result for the record.\n */\nexport function useOne<T>(\n    resource: string,\n    id: string | number | null | undefined,\n    options?: UseOneOptions<T>,\n): UseQueryResult<T, Error> {\n    const provider = useDataProvider();\n    const { enabled, ...rest } = options ?? {};\n    return useQuery<T, Error>({\n        queryKey: oneQueryKey(resource, id),\n        queryFn: () => provider.getOne<T>(resource, id as string | number),\n        enabled: id != null && (enabled ?? true),\n        ...rest,\n    });\n}\n\n/** Options for the create mutation (mutationFn + onSuccess are provided). */\nexport type UseCreateOptions<T> = Omit<UseMutationOptions<T, Error, unknown>, \"mutationFn\">;\n\n/**\n * Create a record and invalidate the resource list cache on success.\n *\n * @param resource - The resource name.\n * @param options - Extra TanStack Mutation options.\n * @returns The mutation; `mutate(data)` creates the record.\n */\nexport function useCreate<T>(\n    resource: string,\n    options?: UseCreateOptions<T>,\n): UseMutationResult<T, Error, unknown> {\n    const provider = useDataProvider();\n    const queryClient = useQueryClient();\n    return useMutation<T, Error, unknown>({\n        mutationFn: (data: unknown) => provider.create<T>(resource, data),\n        onSuccess: (...args) => {\n            void queryClient.invalidateQueries({ queryKey: [\"data\", resource, \"list\"] });\n            options?.onSuccess?.(...args);\n        },\n        ...options,\n    });\n}\n\n/** Variables accepted by the update mutation. */\nexport interface UpdateVariables {\n    /** The record id to update. */\n    id: string | number;\n    /** The update payload. */\n    data: unknown;\n}\n\n/** Options for the update mutation (mutationFn + onSuccess are provided). */\nexport type UseUpdateOptions<T> = Omit<UseMutationOptions<T, Error, UpdateVariables>, \"mutationFn\">;\n\n/**\n * Update a record and invalidate both the list and the single-record caches.\n *\n * @param resource - The resource name.\n * @param options - Extra TanStack Mutation options.\n * @returns The mutation; `mutate({ id, data })` updates the record.\n */\nexport function useUpdate<T>(\n    resource: string,\n    options?: UseUpdateOptions<T>,\n): UseMutationResult<T, Error, UpdateVariables> {\n    const provider = useDataProvider();\n    const queryClient = useQueryClient();\n    return useMutation<T, Error, UpdateVariables>({\n        mutationFn: ({ id, data }: UpdateVariables) => provider.update<T>(resource, id, data),\n        onSuccess: (...args) => {\n            const [, variables] = args;\n            void queryClient.invalidateQueries({ queryKey: [\"data\", resource, \"list\"] });\n            void queryClient.invalidateQueries({ queryKey: oneQueryKey(resource, variables.id) });\n            options?.onSuccess?.(...args);\n        },\n        ...options,\n    });\n}\n\n/** Options for the delete mutation (mutationFn + onSuccess are provided). */\nexport type UseDeleteOptions<T> = Omit<UseMutationOptions<T, Error, string | number>, \"mutationFn\">;\n\n/**\n * Delete a record by id and invalidate the resource list cache on success.\n *\n * @param resource - The resource name.\n * @param options - Extra TanStack Mutation options.\n * @returns The mutation; `mutate(id)` deletes the record.\n */\nexport function useDelete<T>(\n    resource: string,\n    options?: UseDeleteOptions<T>,\n): UseMutationResult<T, Error, string | number> {\n    const provider = useDataProvider();\n    const queryClient = useQueryClient();\n    return useMutation<T, Error, string | number>({\n        mutationFn: (id: string | number) => provider.deleteOne<T>(resource, id),\n        onSuccess: (...args) => {\n            void queryClient.invalidateQueries({ queryKey: [\"data\", resource, \"list\"] });\n            options?.onSuccess?.(...args);\n        },\n        ...options,\n    });\n}\n"],"mappings":"sFAgCA,SAAgB,EAAa,EAAkB,EAAmC,CAC9E,MAAO,CAAC,OAAQ,EAAU,OAAQ,CAAM,CAC5C,CASA,SAAgB,EAAY,EAAkB,EAAmD,CAC7F,MAAO,CAAC,OAAQ,EAAU,MAAO,CAAE,CACvC,CAUA,SAAgB,EACZ,EACA,EACA,EACoC,CACpC,IAAM,EAAW,EAAA,gBAAgB,EACjC,OAAA,EAAO,EAAA,SAAA,CAA+B,CAClC,SAAU,EAAa,EAAU,CAAM,EACvC,YAAe,EAAS,QAAW,EAAU,CAAM,EACnD,GAAG,CACP,CAAC,CACL,CAYA,SAAgB,EACZ,EACA,EACA,EACwB,CACxB,IAAM,EAAW,EAAA,gBAAgB,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