UNPKG

5.47 kBTypeScriptView Raw
1/**
2 * Confirm a data can be transform.
3 * @param response
4 * @returns
5 */
6declare function canActivate(data: unknown): data is Response;
7
8/**
9 * Confirm an error whether is DOMException
10 * @param error
11 * @returns
12 *
13 * @deprecated use `@import('@fatcherjs/middleware-aborter').isAbortError` instead.
14 */
15declare function isAbortError(error: unknown): error is DOMException;
16
17declare type MaybePromise<T> = T | Promise<T>;
18/**
19 * HTTP Request Methods
20 */
21declare type RequestMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'HEAD' | 'OPTIONS' | 'PATCH';
22/**
23 * Middleware Context
24 */
25interface Context extends Omit<RequestOptions, 'middlewares'> {
26 body?: BodyInit | null;
27 /**
28 * A map of http request headers.
29 *
30 * @description Some headers name cannot be modified programmatically.
31 * @see https://developer.mozilla.org/en-US/docs/Glossary/Forbidden_header_name
32 */
33 requestHeaders: Headers;
34 /**
35 * Request Headers
36 *
37 * @deprecated use `requestHeaders` instead.
38 */
39 headers?: RequestHeaders;
40}
41/**
42 * Request Headers
43 *
44 * Will filter null value before fetch.
45 */
46declare type RequestHeaders = Record<string, string | null>;
47/**
48 * Response Result
49 */
50interface ResponseResult<T = any> {
51 data: T;
52 headers: Headers;
53 status: number;
54 statusText: string;
55 options: RequestOptions;
56 url: string;
57}
58/**
59 * Middlewares Response Result
60 */
61interface MiddlewareResult extends Omit<ResponseResult, 'options' | 'data'> {
62 data?: any;
63}
64declare type PatchContext = Partial<Context>;
65/**
66 * Middleware Next
67 *
68 * Should call by using middleware for get response.
69 */
70declare type MiddlewareNext = (patchContext?: PatchContext) => MaybePromise<MiddlewareResult>;
71/**
72 * Middleware
73 */
74interface Middleware {
75 name: `fatcher-middleware-${string}`;
76 use(context: Readonly<Context>, next: MiddlewareNext): MaybePromise<MiddlewareResult>;
77 /**
78 * Current middleware needs some middlewares
79 */
80 presets?: UnregisteredMiddlewares;
81}
82/**
83 * A Middleware or a series of Middlewares
84 *
85 * Will flatten into a array of middlewares.
86 */
87declare type UnregisteredMiddlewares = ((() => Middleware) | Middleware | ((() => Middleware) | Middleware)[])[];
88/**
89 * Request Options for fetch
90 */
91interface RequestOptions extends Omit<RequestInit, 'body' | 'headers'> {
92 /**
93 * Base url to fetch.
94 *
95 * @default '/'
96 */
97 baseUrl?: string;
98 /**
99 * Url to fetch
100 */
101 url?: string;
102 /**
103 * HTTP Request Method for current request.
104 *
105 * @default 'GET'
106 */
107 method?: RequestMethod;
108 /**
109 * Query string for request url
110 */
111 params?: Record<string, string>;
112 /**
113 * An array of function with `pre` or `post` request.
114 *
115 * Middlewares will compose into an async function.
116 *
117 * @default []
118 */
119 middlewares?: UnregisteredMiddlewares;
120 /**
121 * Request Payload
122 */
123 payload?: Record<string, any> | null;
124 /**
125 * Request Headers
126 *
127 * @default
128 * {
129 * 'Content-Type': 'application/x-www-form-urlencoded'
130 * }
131 */
132 headers?: RequestHeaders;
133}
134
135/**
136 * Read a readable stream by chunk
137 * @param readableStream
138 * @param callback
139 * @returns
140 */
141declare function readStreamByChunk<T = Uint8Array, K = void>(readableStream: ReadableStream<T>, callback: (chunk: T) => MaybePromise<K>): Promise<void>;
142/**
143 * Read a readable stream by chunk
144 * @param readableStream
145 * @param callback
146 * @returns
147 *
148 * @deprecated Use `readStreamByChunk` instead.
149 */
150declare const chunkStreamReader: typeof readStreamByChunk;
151
152/**
153 * Fatcher error
154 */
155declare class FatcherError extends Error {
156 constructor(context: Context, response: Response);
157 readonly name = "FatcherError";
158 readonly __isFatcherError__ = true;
159 private readonly _response;
160 private readonly _context;
161 toJSON(): {
162 status: number;
163 statusText: string;
164 context: Context;
165 headers: Record<string, string>;
166 data: ReadableStream<Uint8Array> | null;
167 };
168}
169
170/**
171 * Confirm an error whether is FatcherError
172 * @param error
173 * @returns
174 */
175declare function isFatcherError(error: Error): error is FatcherError;
176
177declare function mergeOptions(options: RequestOptions, ...patchOptions: Partial<RequestOptions>[]): RequestOptions;
178
179/**
180 * Set Default Fatcher Request Options.
181 *
182 * Tips:
183 *
184 * If change default options, will `effect` any fatcher instances.
185 *
186 * If you want to have a scope options with fatcher. Use `createScopedRequest` instead.
187 *
188 * Inline Options `>` Scoped Options `>` Default Options
189 */
190declare function setDefaultOptions<T extends RequestOptions>(patchRequestOptions: Partial<T>): void;
191
192/**
193 * Send HTTP request with custom options.
194 */
195declare function fatcher<T = any>(inlineOptions?: RequestOptions): Promise<ResponseResult<T>>;
196
197/**
198 * Create a scope fatcher request.
199 * @param scopedOptions
200 * @returns
201 */
202declare function createScopedRequest<K = any>(scopedOptions?: RequestOptions): <T = K>(url: string, payload?: any, inlineOptions?: RequestOptions) => Promise<ResponseResult<T>>;
203
204export { Context, FatcherError, MaybePromise, Middleware, MiddlewareNext, MiddlewareResult, PatchContext, RequestHeaders, RequestMethod, RequestOptions, ResponseResult, UnregisteredMiddlewares, canActivate, chunkStreamReader, createScopedRequest, fatcher, isAbortError, isFatcherError, mergeOptions, readStreamByChunk, setDefaultOptions };