import type { SWRConfiguration } from 'swr'; import type { CommerceError } from './errors'; import type { ResponseState } from './use-data'; import { Provider } from '../commerce'; /** * Returns the properties in T with the properties in type K, overriding properties defined in T */ export type Override = Omit & K; /** * Returns the properties in T with the properties in type K changed from optional to required */ export type PickRequired = Omit & { [P in K]-?: NonNullable; }; /** * Core fetcher added by CommerceProvider */ export type Fetcher = (options: FetcherOptions) => T | Promise; export type FetcherOptions = { url?: string; query?: string; method?: string; variables?: any; body?: Body; }; export type HookFetcher = (options: HookFetcherOptions | null, input: Input, fetch: (options: FetcherOptions) => Promise) => Data | Promise; export type HookFetcherFn = (context: HookFetcherContext) => H['data'] | Promise; export type HookFetcherContext = { options: HookFetcherOptions; input: H['fetcherInput']; fetch: (options: FetcherOptions) => Promise; provider?: Provider; }; export type HookFetcherOptions = { method?: string; } & ({ query: string; url?: string; } | { query?: string; url: string; }); export type HookInputValue = string | number | boolean | undefined; export type HookSWRInput = [string, HookInputValue][]; export type HookFetchInput = { [k: string]: HookInputValue; }; export type HookFunction = keyof Input extends never ? () => T : Partial extends Input ? (input?: Input) => T : (input: Input) => T; export type HookSchemaBase = { data: any; input?: {}; fetcherInput?: {}; body?: {}; fetchData?: any; }; export type SWRHookSchemaBase = HookSchemaBase & { swrState?: {}; mutations?: Record['useHook']>>; }; export type MutationSchemaBase = HookSchemaBase & { actionInput?: {}; }; /** * Generates a SWR hook handler based on the schema of a hook */ export type SWRHook = { useHook(context: SWRHookContext): HookFunction; }, ResponseState & H['swrState'] & H['mutations']>; fetchOptions: HookFetcherOptions; fetcher?: HookFetcherFn; }; export type SWRHookContext = { useData(context?: { input?: HookFetchInput | HookSWRInput; swrOptions?: SwrOptions; }): ResponseState; }; /** * Generates a mutation hook handler based on the schema of a hook */ export type MutationHook = { useHook(context: MutationHookContext): HookFunction>>; fetchOptions: HookFetcherOptions; fetcher?: HookFetcherFn; }; export type MutationHookContext = { fetch: keyof H['fetcherInput'] extends never ? () => H['data'] | Promise : Partial extends H['fetcherInput'] ? (context?: { input?: H['fetcherInput']; }) => H['data'] | Promise : (context: { input: H['fetcherInput']; }) => H['data'] | Promise; }; export type SwrOptions = SWRConfiguration>; export type CommerceExtraFeatures = { includeSubCategories?: boolean; };