1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | declare function canActivate(data: unknown): data is Response;
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | declare function isAbortError(error: unknown): error is DOMException;
|
16 |
|
17 | declare type MaybePromise<T> = T | Promise<T>;
|
18 |
|
19 |
|
20 |
|
21 | declare type RequestMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'HEAD' | 'OPTIONS' | 'PATCH';
|
22 |
|
23 |
|
24 |
|
25 | interface Context extends Omit<RequestOptions, 'middlewares'> {
|
26 | body?: BodyInit | null;
|
27 | |
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | requestHeaders: Headers;
|
34 | |
35 |
|
36 |
|
37 |
|
38 |
|
39 | headers?: RequestHeaders;
|
40 | }
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | declare type RequestHeaders = Record<string, string | null>;
|
47 |
|
48 |
|
49 |
|
50 | interface ResponseResult<T = any> {
|
51 | data: T;
|
52 | headers: Headers;
|
53 | status: number;
|
54 | statusText: string;
|
55 | options: RequestOptions;
|
56 | url: string;
|
57 | }
|
58 |
|
59 |
|
60 |
|
61 | interface MiddlewareResult extends Omit<ResponseResult, 'options' | 'data'> {
|
62 | data?: any;
|
63 | }
|
64 | declare type PatchContext = Partial<Context>;
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 | declare type MiddlewareNext = (patchContext?: PatchContext) => MaybePromise<MiddlewareResult>;
|
71 |
|
72 |
|
73 |
|
74 | interface Middleware {
|
75 | name: `fatcher-middleware-${string}`;
|
76 | use(context: Readonly<Context>, next: MiddlewareNext): MaybePromise<MiddlewareResult>;
|
77 | |
78 |
|
79 |
|
80 | presets?: UnregisteredMiddlewares;
|
81 | }
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 | declare type UnregisteredMiddlewares = ((() => MaybePromise<Middleware>) | Middleware | ((() => MaybePromise<Middleware>) | Middleware)[])[];
|
88 | /**
|
89 | * Request Options for fetch
|
90 | */
|
91 | interface 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 | */
|
152 | declare 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 | */
|
161 | declare const chunkStreamReader: typeof readStreamByChunk;
|
162 |
|
163 | /**
|
164 | * Fatcher error
|
165 | */
|
166 | declare 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 | */
|
186 | declare function isFatcherError(error: Error): error is FatcherError;
|
187 |
|
188 | declare 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 | */
|
201 | declare function setDefaultOptions<T extends RequestOptions>(patchRequestOptions: Partial<T>): void;
|
202 |
|
203 | /**
|
204 | * Send HTTP request with custom options.
|
205 | */
|
206 | declare function fatcher<T = any>(inlineOptions?: RequestOptions): Promise<ResponseResult<T>>;
|
207 |
|
208 | /**
|
209 | * Create a scope fatcher request.
|
210 | * @param scopedOptions
|
211 | * @returns
|
212 | */
|
213 | declare function createScopedRequest<K = any>(scopedOptions?: RequestOptions): <T = K>(url: string, payload?: any, inlineOptions?: RequestOptions) => Promise<ResponseResult<T>>;
|
214 |
|
215 | export { 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 |