import { StringifyOptions } from 'query-string'; export type Method = 'get' | 'GET' | 'delete' | 'DELETE' | 'head' | 'HEAD' | 'options' | 'OPTIONS' | 'post' | 'POST' | 'put' | 'PUT' | 'patch' | 'PATCH' | 'purge' | 'PURGE' | 'link' | 'LINK' | 'unlink' | 'UNLINK'; /** * preset content-type headers * json: application/json;charset=utf-8 * form: application/x-www-form-urlencoded */ export type RequestType = 'json' | 'form'; /** * request config */ export interface RequestConfig { url?: string; method?: Method | string; headers?: any; params?: any; data?: D; /** * set Content-Type header * @default 'json' */ requestType?: RequestType; /** * serializer request data * @param data request data * @param requestType request type */ dataSerializer?: (data: D, requestType: RequestType, options?: StringifyOptions) => any; } /** * response */ export interface Response { data: T; status: number; statusText: string; headers: any; config: RequestConfig; request?: any; } /** * fetch promise */ export interface FetchPromise extends Promise> { } /** * fetch client */ export interface FetchClient { request>(config: RequestConfig): Promise; get>(url: string, config?: RequestConfig): Promise; delete>(url: string, config?: RequestConfig): Promise; head>(url: string, config?: RequestConfig): Promise; options>(url: string, config?: RequestConfig): Promise; post>(url: string, data?: any, config?: RequestConfig): Promise; put>(url: string, data?: any, config?: RequestConfig): Promise; patch>(url: string, data?: any, config?: RequestConfig): Promise; } /** * method url definition */ export type MethodUrl = string | MethodUrlWithConfig | MethodUrlFn; /** * method url definition with local config */ export type MethodUrlWithConfig = [Partial, string]; /** * functional method url definition */ export type MethodUrlFn = (params: P) => string | MethodUrlWithConfig; /** * type isNever = CheckNever => 'no' * type isNever = CheckNever => 'yes' */ type CheckNever = T extends never ? 'yes' : 'no'; /** * type isAny = CheckAny => 'yes' * type isAny = CheckAny => 'no' */ type CheckAny = CheckNever extends 'no' ? 'no' : 'yes'; /** * request custom config */ export interface RequestCustomConfig { } /** * plugin options */ type OptionsInPlugin> = {} & O; export interface RegisterPluginContext = Record> { /** * regist api */ registApis: RegistApi; } /** * Custom registApis properties extends from plugin */ export interface RegistApiCustomProperties = Record> { } /** * plugin definition */ export interface PluginDefinition, C extends Record = Record> { (options?: OptionsInPlugin): (context: RegisterPluginContext) => RegistApi & RegistApiCustomProperties; } /** * method url transfor to request definition */ export type RequestDefinition = MethodUrl extends MethodUrlFn ? CheckAny extends 'yes' ? (config?: RequestCustomConfig & Partial & { params?: Params; data?: Data; }>) => FetchPromise : (config: RequestCustomConfig & Partial> & { params: Params; data?: Data; }) => FetchPromise : (config?: RequestCustomConfig & Partial) => FetchPromise; /** * Return type of 'registApi' result */ export type RegistApi = Record> = { [P in keyof C]: RequestDefinition; }; type ValueOf = T[keyof T]; export type Request = ValueOf; export type Prefix = string | ((path: string) => string); export {};