import * as swr_mutation from 'swr/mutation'; import { SWRMutationConfiguration } from 'swr/mutation'; import * as react from 'react'; import { ModelMeta } from '@zenstackhq/runtime/cross'; import { SWRConfiguration, Fetcher, SWRResponse } from 'swr'; import { ScopedMutator } from 'swr/_internal'; import { SWRInfiniteConfiguration, SWRInfiniteFetcher, SWRInfiniteResponse } from 'swr/infinite'; type Enumerable = T | Array; type _TupleToUnion = T extends (infer E)[] ? E : never; type TupleToUnion = _TupleToUnion; type MaybeTupleToUnion = T extends any[] ? TupleToUnion : T; type PickEnumerable | keyof T> = Pick>; type SelectAndInclude = { select: any; include: any; }; type HasSelect = { select: any; }; type HasInclude = { include: any; }; type CheckSelect = T extends SelectAndInclude ? 'Please either choose `select` or `include`' : T extends HasSelect ? U : T extends HasInclude ? U : S; /** * Function signature for `fetch`. */ type FetchFn = (url: string, options?: RequestInit) => Promise; /** * Context type for configuring react hooks. */ type RequestHandlerContext = { /** * The endpoint to use for the queries. */ endpoint?: string; /** * A custom fetch function for sending the HTTP requests. */ fetch?: FetchFn; /** * If logging is enabled. */ logging?: boolean; }; /** * Context for configuring react hooks. */ declare const RequestHandlerContext: react.Context; /** * Context provider. */ declare const Provider: react.Provider; /** * Hooks context. */ declare function useHooksContext(): { /** * A custom fetch function for sending the HTTP requests. */ fetch?: FetchFn; /** * If logging is enabled. */ logging?: boolean; endpoint: string; }; /** * Regular query options. */ type QueryOptions = { /** * Disable data fetching */ disabled?: boolean; /** * Whether to enable automatic optimistic update. Defaults to `true`. */ optimisticUpdate?: boolean; } & Omit>, 'fetcher'>; /** * Infinite query options. */ type InfiniteQueryOptions = { /** * Disable data fetching */ disabled?: boolean; } & Omit>, 'fetcher'>; /** * Result of optimistic data provider. */ type OptimisticDataProviderResult = { /** * Kind of the result. * - Update: use the `data` field to update the query cache. * - Skip: skip the optimistic update for this query. * - ProceedDefault: proceed with the default optimistic update. */ kind: 'Update' | 'Skip' | 'ProceedDefault'; /** * Data to update the query cache. Only applicable if `kind` is 'Update'. * * If the data is an object with fields updated, it should have a `$optimistic` * field set to `true`. If it's an array and an element object is created or updated, * the element should have a `$optimistic` field set to `true`. */ data?: any; }; /** * Optimistic data provider. * * @param args Arguments. * @param args.queryModel The model of the query. * @param args.queryOperation The operation of the query, `findMany`, `count`, etc. * @param args.queryArgs The arguments of the query. * @param args.currentData The current cache data for the query. * @param args.mutationArgs The arguments of the mutation. */ type OptimisticDataProvider = (args: { queryModel: string; queryOperation: string; queryArgs: any; currentData: any; mutationArgs: any; }) => OptimisticDataProviderResult | Promise; /** * Mutation options. */ type MutationOptions = { /** * Whether to automatically optimistic-update queries potentially impacted. Defaults to `false`. */ optimisticUpdate?: boolean; /** * A callback for computing optimistic update data for each query cache entry. */ optimisticDataProvider?: OptimisticDataProvider; } & Omit, 'fetcher'>; /** * Computes query key for the given model, operation, query args, and options. */ declare function getQueryKey(model: string, operation: string, args?: unknown, infinite?: boolean, optimisticUpdate?: boolean): string; /** * Makes a model query with SWR. * * @param model Model name * @param operation Prisma operation (e.g, `findMany`) * @param args The request args object, which will be superjson-stringified and appended as "?q=" parameter * @param options Query options * @returns SWR response */ declare function useModelQuery(model: string, operation: string, args?: unknown, options?: QueryOptions): SWRResponse; /** * Function for computing the query args for fetching a page during an infinite query. */ type GetNextArgs = (pageIndex: number, previousPageData: Result | null) => Args | null; /** * Makes an infinite GET request with SWR. * * @param model Model name * @param operation Prisma operation (e.g, `findMany`) * @param getNextArgs Function for computing the query args for a page * @param options Query options * @returns SWR infinite query response */ declare function useInfiniteModelQuery(model: string, operation: string, getNextArgs: GetNextArgs, options?: InfiniteQueryOptions): SWRInfiniteResponse; declare function useModelMutation(model: string, method: 'POST' | 'PUT' | 'DELETE', operation: string, modelMeta: ModelMeta, options?: MutationOptions, checkReadBack?: CheckReadBack): swr_mutation.SWRMutationResponse; /** * Makes a mutation request. * * @param url The request URL * @param data The request data * @param invalidate Function for invalidating a query */ declare function mutationRequest(method: 'POST' | 'PUT' | 'DELETE', url: string, data: unknown, invalidate?: Invalidator, fetch?: FetchFn, checkReadBack?: C): Promise; type Invalidator = (operation: string, args?: unknown) => ReturnType; declare function useInvalidation(model: string, modelMeta: ModelMeta): Invalidator; /** * Makes fetch request for queries and mutations. */ declare function fetcher(url: string, options?: RequestInit, customFetch?: FetchFn, checkReadBack?: C): Promise; export { type CheckSelect, type Enumerable, type FetchFn, type GetNextArgs, type InfiniteQueryOptions, type MaybeTupleToUnion, type MutationOptions, type OptimisticDataProvider, type OptimisticDataProviderResult, type PickEnumerable, Provider, type QueryOptions, RequestHandlerContext, type TupleToUnion, fetcher, getQueryKey, mutationRequest, useHooksContext, useInfiniteModelQuery, useInvalidation, useModelMutation, useModelQuery };