UNPKG

5.75 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 = ((() => MaybePromise<Middleware>) | Middleware | ((() => MaybePromise<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 * Custom validate status code
135 *
136 * If status code not in range, throw a `FatcherError`
137 *
138 * @default
139 * ```
140 * (statusCode) => 200 <= statusCode < 300
141 * ```
142 */
143 validateCode?: (statusCode: number) => boolean;
144}
145
146/**
147 * Read a readable stream by chunk
148 * @param readableStream
149 * @param callback
150 * @returns
151 */
152declare function readStreamByChunk<T = Uint8Array, K = void>(readableStream: ReadableStream<T>, callback: (chunk: T) => MaybePromise<K>): Promise<void>;
153/**
154 * Read a readable stream by chunk
155 * @param readableStream
156 * @param callback
157 * @returns
158 *
159 * @deprecated Use `readStreamByChunk` instead.
160 */
161declare const chunkStreamReader: typeof readStreamByChunk;
162
163/**
164 * Fatcher error
165 */
166declare class FatcherError extends Error {
167 constructor(context: Context, response: Response);
168 readonly name = "FatcherError";
169 readonly __isFatcherError__ = true;
170 private readonly _response;
171 private readonly _context;
172 toJSON(): {
173 status: number;
174 statusText: string;
175 context: Context;
176 headers: Record<string, string>;
177 data: ReadableStream<Uint8Array> | null;
178 };
179}
180
181/**
182 * Confirm an error whether is FatcherError
183 * @param error
184 * @returns
185 */
186declare function isFatcherError(error: Error): error is FatcherError;
187
188declare function mergeOptions(options: RequestOptions, ...patchOptions: Partial<RequestOptions>[]): RequestOptions;
189
190/**
191 * Set Default Fatcher Request Options.
192 *
193 * Tips:
194 *
195 * If change default options, will `effect` any fatcher instances.
196 *
197 * If you want to have a scope options with fatcher. Use `createScopedRequest` instead.
198 *
199 * Inline Options `>` Scoped Options `>` Default Options
200 */
201declare function setDefaultOptions<T extends RequestOptions>(patchRequestOptions: Partial<T>): void;
202
203/**
204 * Send HTTP request with custom options.
205 */
206declare function fatcher<T = any>(inlineOptions?: RequestOptions): Promise<ResponseResult<T>>;
207
208/**
209 * Create a scope fatcher request.
210 * @param scopedOptions
211 * @returns
212 */
213declare function createScopedRequest<K = any>(scopedOptions?: RequestOptions): <T = K>(url: string, payload?: any, inlineOptions?: RequestOptions) => Promise<ResponseResult<T>>;
214
215export { Context, FatcherError, MaybePromise, Middleware, MiddlewareNext, MiddlewareResult, PatchContext, RequestHeaders, RequestMethod, RequestOptions, ResponseResult, UnregisteredMiddlewares, canActivate, chunkStreamReader, createScopedRequest, fatcher, isAbortError, isFatcherError, mergeOptions, readStreamByChunk, setDefaultOptions };
216
\No newline at end of file