UNPKG

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