import type { Denormalize, EndpointExtraOptions, EndpointInstanceInterface, Schema, FetchFunction, ResolveType } from '@data-client/endpoint'; import type { ExtractCollection } from './extractCollection.js'; import { OptionsToBodyArgument, OptionsToFunction } from './OptionsToFunction.js'; import { PathArgs, SoftPathArgs } from './pathTypes.js'; import { EndpointUpdateFunction } from './RestEndpointTypeHelp.js'; export type ContentType = 'json' | 'blob' | 'text' | 'arrayBuffer' | 'stream'; interface ContentTypeMap { blob: Blob; text: string; arrayBuffer: ArrayBuffer; stream: ReadableStream; json: any; } type ContentReturnType = ContentTypeMap[C]; type ContentSchemaGuard = O extends { content: 'blob' | 'text' | 'arrayBuffer' | 'stream'; } ? { schema?: undefined; } : {}; export interface RestInstanceBase extends EndpointInstanceInterface { /** @see https://dataclient.io/rest/api/RestEndpoint#body */ readonly body?: 'body' extends keyof O ? O['body'] : any; /** @see https://dataclient.io/rest/api/RestEndpoint#searchParams */ readonly searchParams?: 'searchParams' extends keyof O ? O['searchParams'] : unknown; /** Pattern to construct url based on Url Parameters * @see https://dataclient.io/rest/api/RestEndpoint#path */ readonly path: O['path']; /** Prepended to all urls * @see https://dataclient.io/rest/api/RestEndpoint#urlPrefix */ readonly urlPrefix: string; readonly requestInit: RequestInit; /** HTTP request method * @see https://dataclient.io/rest/api/RestEndpoint#method */ readonly method: (O & { method: string; })['method']; readonly signal: AbortSignal | undefined; /** @see https://dataclient.io/rest/api/RestEndpoint#paginationField */ readonly paginationField?: string; /** @see https://dataclient.io/rest/api/RestEndpoint#content */ readonly content?: ContentType; /** Builds the URL to fetch * @see https://dataclient.io/rest/api/RestEndpoint#url */ url(...args: Parameters): string; /** Encode the searchParams component of the url * @see https://dataclient.io/rest/api/RestEndpoint#searchToString */ searchToString(searchParams: Record): string; /** Prepares RequestInit used in fetch. This is sent to fetchResponse() * @see https://dataclient.io/rest/api/RestEndpoint#getRequestInit */ getRequestInit(this: any, body?: RequestInit['body'] | Record): Promise | RequestInit; /** Called by getRequestInit to determine HTTP Headers * @see https://dataclient.io/rest/api/RestEndpoint#getHeaders */ getHeaders(headers: HeadersInit): Promise | HeadersInit; /** Performs the fetch call * @see https://dataclient.io/rest/api/RestEndpoint#fetchResponse */ fetchResponse(input: RequestInfo, init: RequestInit): Promise; /** Takes the Response and parses via .text() or .json() * @see https://dataclient.io/rest/api/RestEndpoint#parseResponse */ parseResponse(response: Response): Promise; /** Perform any transforms with the parsed result. * @see https://dataclient.io/rest/api/RestEndpoint#process */ process(value: any, ...args: Parameters): ResolveType; /** Returns true if the provided (fetch) key matches this endpoint. * @see https://dataclient.io/rest/api/RestEndpoint#testKey */ testKey(key: string): boolean; /** Creates a child endpoint that inherits from this while overriding provided `options`. * @see https://dataclient.io/rest/api/RestEndpoint#extend */ extend(this: E, options: Readonly & ExtendOptions> & ContentSchemaGuard): RestExtendedEndpoint; } export interface RestInstance extends RestInstanceBase { /** Creates an Endpoint to append the next page extending a list for pagination * @see https://dataclient.io/rest/api/RestEndpoint#paginated */ paginated, A extends any[]>(this: E, removeCursor: (...args: A) => readonly [ ...Parameters ]): PaginationEndpoint; paginated, C extends string>(this: E, cursorField: C): PaginationFieldEndpoint; /** Concatinate the next page of results (GET) * @see https://dataclient.io/rest/api/RestEndpoint#getPage */ getPage: 'paginationField' extends keyof O ? O['paginationField'] extends string ? PaginationFieldEndpoint : undefined : undefined; /** Create a new item (POST) and `push` to the end * @see https://dataclient.io/rest/api/RestEndpoint#push */ push: AddEndpoint['push'], Omit & { body: OptionsToAdderBodyArgument['push']> | OptionsToAdderBodyArgument['push']>[] | FormData; }>; /** Create a new item (POST) and `unshift` to the beginning * @see https://dataclient.io/rest/api/RestEndpoint#unshift */ unshift: AddEndpoint['unshift'], Omit & { body: OptionsToAdderBodyArgument['unshift']> | OptionsToAdderBodyArgument['unshift']>[] | FormData; }>; /** Create new item(s) (POST) and `Object.assign` merge * @see https://dataclient.io/rest/api/RestEndpoint#assign */ assign: AddEndpoint, Omit & { body: Record['assign']>> | FormData; }>; /** Remove item(s) (PATCH) from collection * @see https://dataclient.io/rest/api/RestEndpoint#remove */ remove: RemoveEndpoint['remove'], Omit & { body: Partial['remove']>> | Partial['remove']>>[] | FormData; }>; /** Move item between collections (PATCH) - removes from old, adds to new * @see https://dataclient.io/rest/api/RestEndpoint#move */ move: MoveEndpoint['move'], { path: 'movePath' extends keyof O ? O['movePath'] & string : O['path']; body: Partial['move']>> | FormData; }>; } export type RestEndpointExtendOptions = RestEndpointOptions, 'schema' extends keyof O ? Extract : E['schema']> & Partial>; type OptionsToRestEndpoint = 'path' extends keyof O ? RestType<'searchParams' extends keyof O ? [ O['searchParams'] ] extends [ undefined ] ? PathArgs> : O['searchParams'] & PathArgs> : PathArgs>, OptionsToBodyArgument<'body' extends keyof O ? O : E, 'method' extends keyof O ? O['method'] : E['method']>, 'schema' extends keyof O ? O['schema'] : E['schema'], 'sideEffect' extends keyof O ? Extract : 'method' extends keyof O ? MethodToSide : E['sideEffect'], O['process'] extends {} ? ReturnType : 'content' extends keyof O ? ContentReturnType : ResolveType, { path: Exclude; body: 'body' extends keyof O ? O['body'] : E['body']; searchParams: 'searchParams' extends keyof O ? O['searchParams'] : E['searchParams']; method: 'method' extends keyof O ? O['method'] : E['method']; paginationField: 'paginationField' extends keyof O ? O['paginationField'] : E['paginationField']; }> : 'body' extends keyof O ? RestType<'searchParams' extends keyof O ? [ O['searchParams'] ] extends [ undefined ] ? PathArgs> : O['searchParams'] & PathArgs> : PathArgs>, OptionsToBodyArgument, 'schema' extends keyof O ? O['schema'] : E['schema'], 'sideEffect' extends keyof O ? Extract : 'method' extends keyof O ? MethodToSide : E['sideEffect'], O['process'] extends {} ? ReturnType : 'content' extends keyof O ? ContentReturnType : ResolveType, { path: E['path']; body: O['body']; searchParams: 'searchParams' extends keyof O ? O['searchParams'] : E['searchParams']; method: 'method' extends keyof O ? O['method'] : E['method']; paginationField: 'paginationField' extends keyof O ? O['paginationField'] : Extract; }> : 'searchParams' extends keyof O ? RestType<[ O['searchParams'] ] extends [ undefined ] ? PathArgs> : O['searchParams'] & PathArgs>, OptionsToBodyArgument, 'schema' extends keyof O ? O['schema'] : E['schema'], 'sideEffect' extends keyof O ? Extract : 'method' extends keyof O ? MethodToSide : E['sideEffect'], O['process'] extends {} ? ReturnType : 'content' extends keyof O ? ContentReturnType : ResolveType, { path: E['path']; body: E['body']; searchParams: O['searchParams']; method: 'method' extends keyof O ? O['method'] : E['method']; paginationField: 'paginationField' extends keyof O ? O['paginationField'] : Extract; }> : RestInstance : 'method' extends keyof O ? MethodToSide : E['sideEffect'], { path: 'path' extends keyof O ? Exclude : E['path']; body: 'body' extends keyof O ? O['body'] : E['body']; searchParams: 'searchParams' extends keyof O ? O['searchParams'] : E['searchParams']; method: 'method' extends keyof O ? O['method'] : E['method']; paginationField: 'paginationField' extends keyof O ? O['paginationField'] : E['paginationField']; }>; export type RestExtendedEndpoint = OptionsToRestEndpoint) => O['process'] extends {} ? Promise> : 'content' extends keyof O ? Promise> : ReturnType, 'schema' extends keyof O ? O['schema'] : E['schema'], 'sideEffect' extends keyof O ? Extract : 'method' extends keyof O ? MethodToSide : E['sideEffect']>> & Omit & Omit; export interface PartialRestGenerics { /** @see https://dataclient.io/rest/api/RestEndpoint#path */ readonly path?: string; /** @see https://dataclient.io/rest/api/RestEndpoint#schema */ readonly schema?: Schema | undefined; /** @see https://dataclient.io/rest/api/RestEndpoint#method */ readonly method?: string; /** Only used for types */ /** @see https://dataclient.io/rest/api/RestEndpoint#body */ body?: any; /** Only used for types */ /** @see https://dataclient.io/rest/api/RestEndpoint#searchParams */ searchParams?: any; /** @see https://dataclient.io/rest/api/RestEndpoint#paginationfield */ readonly paginationField?: string; /** @see https://dataclient.io/rest/api/RestEndpoint#process */ process?(value: any, ...args: any): any; /** @see https://dataclient.io/rest/api/RestEndpoint#content */ readonly content?: ContentType; } /** Generic types when constructing a RestEndpoint * * @see https://dataclient.io/rest/api/RestEndpoint#inheritance */ export interface RestGenerics extends PartialRestGenerics { readonly path: string; } export type PaginationEndpoint = RestInstanceBase>, E['schema'], E['sideEffect'], Pick & { searchParams: Omit>; }>; /** Merge pagination field C into body, making it required */ type PaginationIntoBody = Body & { [K in C]: string | number | boolean; }; /** Paginated searchParams type */ type PaginatedSearchParams = { [K in C]: string | number | boolean; } & E['searchParams'] & PathArgs>; /** searchParams version: pagination in searchParams, optional body support */ type PaginationFieldInSearchParams = RestInstanceBase, ResolveType> | ParamFetchWithBody, NonNullable, ResolveType>, E['schema'], E['sideEffect'], Pick & { searchParams: { [K in C]: string | number | boolean; } & E['searchParams']; }> & { paginationField: C; }; /** body version: pagination field is in body (body required) */ type PaginationFieldInBody = RestInstanceBase>, PaginationIntoBody, ResolveType>, E['schema'], E['sideEffect'], Pick & { body: PaginationIntoBody; }> & { paginationField: C; }; /** Retrieves the next page of results by pagination field */ export type PaginationFieldEndpoint = undefined extends E['body'] ? PaginationFieldInSearchParams : C extends keyof E['body'] ? PaginationFieldInBody : PaginationFieldInSearchParams; export type AddEndpoint = RestInstanceBase> : O['searchParams'] & PathArgs> : PathArgs>, O['body'], ResolveType>, S, true, Omit & { method: 'POST'; }>; export type RemoveEndpoint = RestInstanceBase> : O['searchParams'] & PathArgs> : PathArgs>, O['body'], ResolveType>, S, true, Omit & { method: 'PATCH'; }>; export type MoveEndpoint = RestInstanceBase>, O['body'], ResolveType>, S, true, Omit & { method: 'PATCH'; }>; /** When `method` is omitted from `O`, infer it (must stay aligned with `OptionsToBodyArgument`). */ type InferRestMethodWhenOmitted = O extends { sideEffect: true; } ? 'POST' : 'body' extends keyof O ? [ O['body'] ] extends [ undefined ] ? 'GET' : 'POST' : 'GET'; type MethodArgForBodyInference = 'method' extends keyof O ? O['method'] : InferRestMethodWhenOmitted; type OptionsToAdderBodyArgument = 'body' extends keyof O ? O['body'] : Partial>; export interface RestEndpointOptions extends EndpointExtraOptions { /** Prepended to all urls * @see https://dataclient.io/rest/api/RestEndpoint#urlPrefix */ urlPrefix?: string; requestInit?: RequestInit; /** Called by getRequestInit to determine HTTP Headers * @see https://dataclient.io/rest/api/RestEndpoint#getHeaders */ getHeaders?(headers: HeadersInit): Promise | HeadersInit; /** Prepares RequestInit used in fetch. This is sent to fetchResponse() * @see https://dataclient.io/rest/api/RestEndpoint#getRequestInit */ getRequestInit?(body: any): Promise | RequestInit; /** Performs the fetch call * @see https://dataclient.io/rest/api/RestEndpoint#fetchResponse */ fetchResponse?(input: RequestInfo, init: RequestInit): Promise; /** Takes the Response and parses via .text() or .json() * @see https://dataclient.io/rest/api/RestEndpoint#parseResponse */ parseResponse?(response: Response): Promise; /** @see https://dataclient.io/rest/api/RestEndpoint#content */ content?: ContentType; sideEffect?: boolean | undefined; name?: string; signal?: AbortSignal; fetch?: F; key?(...args: Parameters): string; url?(...args: Parameters): string; update?: EndpointUpdateFunction; } export type RestEndpointConstructorOptions = RestEndpointOptions : O['searchParams'] & SoftPathArgs : SoftPathArgs, OptionsToBodyArgument>, O['process'] extends {} ? ReturnType : 'content' extends keyof O ? ContentReturnType : any>, O['schema']>; /** Simplifies endpoint definitions that follow REST patterns * * @see https://dataclient.io/rest/api/RestEndpoint */ export interface RestEndpoint extends RestInstance : O['searchParams'] & PathArgs : PathArgs, OptionsToBodyArgument>, O['process'] extends {} ? ReturnType : 'content' extends keyof O ? ContentReturnType : any>, 'schema' extends keyof O ? O['schema'] : undefined, 'sideEffect' extends keyof O ? Extract : MethodToSide>, 'method' extends keyof O ? O : O & { method: InferRestMethodWhenOmitted; }> { } export interface RestEndpointConstructor { /** Simplifies endpoint definitions that follow REST patterns * * @see https://dataclient.io/rest/api/RestEndpoint */ new ({ method, sideEffect, name, ...options }: RestEndpointConstructorOptions & Readonly & ContentSchemaGuard): RestEndpoint; readonly prototype: RestInstanceBase; } export type MethodToSide = M extends string ? M extends 'GET' ? undefined : true : undefined; /** RestEndpoint types simplified */ export type RestType = IfTypeScriptLooseNull, S, M, O>, Body extends {} ? RestTypeWithBody : RestTypeNoBody>; export type RestTypeWithBody = RestInstance, S, M, O>; export type RestTypeNoBody = RestInstance, S, M, O>; /** Simple parameters, and body fetch functions */ export type RestFetch = IfTypeScriptLooseNull | ParamFetchWithBody, Body extends {} ? ParamFetchWithBody : ParamFetchNoBody>; export type ParamFetchWithBody = P extends undefined ? (this: EndpointInstanceInterface, body: B) => Promise : {} extends P ? keyof P extends never ? (this: EndpointInstanceInterface, body: B) => Promise : ((this: EndpointInstanceInterface, params: P, body: B) => Promise) | ((this: EndpointInstanceInterface, body: B) => Promise) : (this: EndpointInstanceInterface, params: P, body: B) => Promise; export type ParamFetchNoBody = P extends undefined ? (this: EndpointInstanceInterface) => Promise : {} extends P ? keyof P extends never ? (this: EndpointInstanceInterface) => Promise : ((this: EndpointInstanceInterface, params: P) => Promise) | ((this: EndpointInstanceInterface) => Promise) : (this: EndpointInstanceInterface, params: P) => Promise; export type ParamToArgs

= P extends undefined ? [ ] : {} extends P ? keyof P extends never ? [ ] : [ ] | [ P ] : [ P ]; type IfTypeScriptLooseNull = 1 | undefined extends 1 ? Y : N; export type KeyofRestEndpoint = keyof RestInstance; export type FromFallBack = K extends keyof O ? O[K] : E[K]; export type FetchMutate = (this: RestInstance, ...args: A) => Promise; export type FetchGet = (this: RestInstance, ...args: A) => Promise; export type Defaults = { [K in keyof O | keyof D]: K extends keyof O ? Exclude : D[Extract]; }; export type GetEndpoint = RestTypeNoBody<'searchParams' extends keyof O ? [ O['searchParams'] ] extends [ undefined ] ? PathArgs : O['searchParams'] & PathArgs : PathArgs, O['schema'], undefined, any, O & { method: 'GET'; }>; export type MutateEndpoint = RestTypeWithBody<'searchParams' extends keyof O ? [ O['searchParams'] ] extends [ undefined ] ? PathArgs : O['searchParams'] & PathArgs : PathArgs, O['schema'], true, O['body'], any, O & { body: any; method: 'POST' | 'PUT' | 'PATCH' | 'DELETE'; }>; export {}; //# sourceMappingURL=RestEndpointTypes.d.ts.map