import type { Denormalize, Schema, UpdateFunction } from '@rest-hooks/react'; import { FetchShape } from './shapes.js'; export type ResultShape = RS extends { schema: infer U } ? U : never; export type SelectReturn = RS extends { select: (...args: any[]) => infer U; } ? U : never; export type AlwaysSelect = NonNullable>; export type ParamArg = RS extends { getFetchKey: (params: infer U) => any; } ? U : never; export type BodyArg = RS extends { fetch: (url: any, body: infer U) => any; } ? U : never; /** Sets a FetchShape's Param type. * Useful to constrain acceptable params (second arg) in hooks like useResource(). * * @param [Shape] FetchShape to act upon * @param [Params] what to set the Params to */ export type SetShapeParams< Shape extends FetchShape, Params extends Readonly, > = { [K in keyof Shape]: Shape[K]; } & (Shape['fetch'] extends (first: any, ...rest: infer Args) => infer Return ? { fetch: (first: Params, ...rest: Args) => Return } : never); /** Get the Params type for a given Shape */ export type ParamsFromShape = S extends { fetch: (first: infer A, ...rest: any) => any; } ? A : S extends { getFetchKey: (first: infer A, ...rest: any) => any } ? A : never; /** Get the Schema type for a given Shape */ export type SchemaFromShape< F extends FetchShape, > = F['schema']; /** Get the Body type for a given Shape */ export type BodyFromShape> = Parameters< F['fetch'] >[1]; export type OptimisticUpdateParams< SourceSchema extends Schema | undefined, DestShape extends FetchShape, > = [ DestShape, ParamsFromShape, UpdateFunction>, ]; export type ReturnFromShape> = ReturnType< S['fetch'] > extends unknown ? Promise> : ReturnType;