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 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | declare function readStreamByChunk<T = Uint8Array>(readableStream: ReadableStream<T>, callback: (chunk: T) => void): Promise<void>;
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | declare const chunkStreamReader: typeof readStreamByChunk;
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | declare type RequestMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'HEAD' | 'OPTIONS' | 'PATCH';
|
38 |
|
39 |
|
40 |
|
41 | interface Context extends Omit<RequestOptions, 'middlewares'> {
|
42 | body?: BodyInit | null;
|
43 | |
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 | requestHeaders: Headers;
|
50 | |
51 |
|
52 |
|
53 |
|
54 |
|
55 | headers?: RequestHeaders;
|
56 | }
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 | declare type RequestHeaders = Record<string, string | null>;
|
63 |
|
64 |
|
65 |
|
66 | interface ResponseResult<T = any> {
|
67 | data: T;
|
68 | headers: Headers;
|
69 | status: number;
|
70 | statusText: string;
|
71 | options: RequestOptions;
|
72 | url: string;
|
73 | }
|
74 |
|
75 |
|
76 |
|
77 | interface MiddlewareResult extends Omit<ResponseResult, 'options' | 'data'> {
|
78 | data?: any;
|
79 | }
|
80 | declare type PatchContext = Partial<Context>;
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 | declare type MiddlewareNext = (patchContext?: PatchContext) => Promise<MiddlewareResult> | MiddlewareResult;
|
87 |
|
88 |
|
89 |
|
90 | interface Middleware {
|
91 | name: `fatcher-middleware-${string}`;
|
92 | use(context: Readonly<Context>, next: MiddlewareNext): Promise<MiddlewareResult> | MiddlewareResult;
|
93 | |
94 |
|
95 |
|
96 | presets?: UnregisteredMiddlewares;
|
97 | }
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 | declare type UnregisteredMiddlewares = ((() => Middleware) | Middleware | ((() => Middleware) | Middleware)[])[];
|
104 | /**
|
105 | * Request Options for fetch
|
106 | */
|
107 | interface 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 | */
|
154 | declare 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 | */
|
174 | declare function isFatcherError(error: Error): error is FatcherError;
|
175 |
|
176 | declare 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 | */
|
189 | declare function setDefaultOptions<T extends RequestOptions>(patchRequestOptions: Partial<T>): void;
|
190 |
|
191 | /**
|
192 | * Send HTTP request with custom options.
|
193 | */
|
194 | declare function fatcher<T = any>(inlineOptions?: RequestOptions): Promise<ResponseResult<T>>;
|
195 |
|
196 | /**
|
197 | * Create a scope fatcher request.
|
198 | * @param scopedOptions
|
199 | * @returns
|
200 | */
|
201 | declare function createScopedRequest<K = any>(scopedOptions?: RequestOptions): <T = K>(url: string, payload?: any, inlineOptions?: RequestOptions) => Promise<ResponseResult<T>>;
|
202 |
|
203 | export { Context, FatcherError, Middleware, MiddlewareNext, MiddlewareResult, PatchContext, RequestHeaders, RequestMethod, RequestOptions, ResponseResult, UnregisteredMiddlewares, canActivate, chunkStreamReader, createScopedRequest, fatcher, isAbortError, isFatcherError, mergeOptions, readStreamByChunk, setDefaultOptions };
|