import { Component, ComponentClass, ComponentState, StatelessComponent, } from "react"; //////////////////////// // PromiseState //////////////////////// // Similar to PromiseLike export type PromiseStateLike = T | PromiseState; export interface PromiseStateStatic { create(meta?: any): PromiseState; refresh(previous?: PromiseState, meta?: any): PromiseState; resolve(value?: PromiseStateLike, meta?: any): PromiseState; reject(reason?: any, meta?: any): PromiseState; all(iterable: Iterable>): PromiseState; race(iterable: Iterable>): PromiseState; } export interface PromiseState { readonly pending: boolean; readonly refreshing: boolean; readonly fulfilled: boolean; readonly rejected: boolean; readonly settled: boolean; readonly value: T; readonly reason: any; readonly meta: any; then: ( onFulfilled?: ( value: PromiseStateLike, ) => PromiseStateLike, onRejected?: (reason: any) => PromiseStateLike, ) => | PromiseStateLike | PromiseStateLike | PromiseStateLike; catch: ( onRejected?: (reason: any) => PromiseStateLike, ) => PromiseStateLike | PromiseStateLike; } export const PromiseState: Readonly; //////////////////////// // connect //////////////////////// interface RequestType { prototype: Request; new (input: RequestInfo, init?: RequestInit): Request; } export interface Connect { (map: MapPropsToRequestsToProps): ( component: ComponentClass | StatelessComponent, ) => ComponentClass & WithRefetch; defaults: (newDefaults: Mapping) => Connect; options: (newOptions: ConnectOptions) => Connect; } export interface ConnectOptions { withRef?: boolean; } export type MapPropsToRequestsToProps = ( props: T ) => PropsMap; // String or PromiseState type PromiseStateMapping< TProps, TProp extends keyof TProps > = TProps[TProp] extends PromiseState ? string | Mapping : never; // Function type FunctionMapping< TProps, TProp extends keyof TProps > = TProps[TProp] extends ((...args: infer TArgs) => void) ? ((...args: TArgs) => PropsMap) : never; export type PropsMap = { [TProp in keyof TProps]?: | PromiseStateMapping | FunctionMapping }; export interface Mapping { buildRequest?: (mapping: Mapping) => any; fetch?: (request: any) => any; handleResponse?: (response: any) => Promise; Request?: RequestType; url?: string; method?: string; headers?: { [key: string]: string | (() => string) }; credentials?: "omit" | "same-origin" | "include"; body?: string; redirect?: "follow" | "error" | "manual"; mode?: "cors" | "no-cors" | "same-origin" | "navigate"; refreshInterval?: number; refreshing?: boolean | ((value: TValue) => TValue); force?: boolean; comparison?: any; then?: ( value: TValue, meta: any, ) => Mapping | void; catch?: (reason: any) => Mapping | void; andThen?: (value: TValue) => PropsMap; andCatch?: (rason: any) => PropsMap; value?: TValue | PromiseLike; meta?: any; // Everything else is passed through unmodified [key: string]: any; [key: number]: any; } export interface WithRefetch { WrappedComponent: ComponentClass; new (props: TProps): Component & WithRefetchInstance; } export interface WithRefetchInstance { getWrappedInstance(): Component; } export const connect: Connect;