import { createContext } from 'react'; export interface Model { relationships: Record; } export type UUID = string; /** * Marks the provided relationships keys as present in the given model * rather than being optional to improve typing responses. */ export type WithRelationships = Omit & { relationships: Omit & { [K in R]: NonNullable; }; }; /** * Helper type that allows you to infer the type of an object by giving * it the specific API request function with a return type. For example: * * type EggT = InferModel; */ export type InferModel any> = ReturnType extends Promise ? U : T; /** * Helper function that just returns the model you pass in, but types the model * such that TypeScript understands the relationships on it. This is just to help * reduce the amount of duplicated type casting all over the codebase. */ export const withRelationships = (model: M, ..._keys: R[]) => { return model as unknown as WithRelationships; }; export interface ListContext { page: number; setPage: (page: ((p: number) => number) | number) => void; filters: T | null; setFilters: (filters: ((f: T | null) => T | null) | T | null) => void; sort: string | null; setSort: (sort: string | null) => void; sortDirection: boolean; setSortDirection: (direction: ((p: boolean) => boolean) | boolean) => void; } function create() { return createContext>({ page: 1, setPage: () => 1, filters: null, setFilters: () => null, sort: null, setSort: () => null, sortDirection: false, setSortDirection: () => false, }); } export { create as createContext };