UNPKG

@gw2api/fetch

Version:

Tiny wrapper around fetch that returns type-safe responses

30 lines (29 loc) 1.65 kB
import type { EndpointType, KnownEndpoint, OptionsByEndpoint } from '@gw2api/types/endpoints'; import { SchemaVersion } from '@gw2api/types/schema'; type RequiredKeys<T> = { [K in keyof T]-?: {} extends Pick<T, K> ? never : K; }[keyof T]; type Args<Url extends string, Schema extends SchemaVersion> = RequiredKeys<OptionsByEndpoint<Url>> extends never ? [endpoint: Url, options?: FetchGw2ApiOptions<Schema> & OptionsByEndpoint<Url> & FetchOptions] : [endpoint: Url, options: FetchGw2ApiOptions<Schema> & OptionsByEndpoint<Url> & FetchOptions]; export declare function fetchGw2Api<Url extends KnownEndpoint | (string & {}), Schema extends SchemaVersion = undefined>(...[endpoint, options]: Args<Url, Schema>): Promise<EndpointType<Url, Schema>>; export type FetchGw2ApiOptions<Schema extends SchemaVersion> = { /** The schema to use when making the API request */ schema?: Schema; /** onRequest handler allows to modify the request made to the Guild Wars 2 API. */ onRequest?: (request: Request) => Request | Promise<Request>; /** * onResponse handler. Called for all responses, successful or not. * Make sure to clone the response in case of consuming the body. */ onResponse?: (response: Response) => void | Promise<void>; }; export type FetchOptions = { /** @see https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal */ signal?: AbortSignal; /** @see https://developer.mozilla.org/en-US/docs/Web/API/Request/cache */ cache?: RequestCache; }; export declare class Gw2ApiError extends Error { response: Response; constructor(message: string, response: Response); } export {};