typetify 2.2.0 → 4.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +102 -0
- package/README.md +123 -1
- package/dist/chunk-2YRFWQ6M.mjs +1 -0
- package/dist/chunk-35CB7HNN.js +1 -0
- package/dist/chunk-6WGRWYAD.js +1 -0
- package/dist/chunk-7UMXGQE4.mjs +1 -0
- package/dist/chunk-7XHBEA63.js +1 -0
- package/dist/chunk-BLUG7TSP.js +1 -0
- package/dist/chunk-C5X2N4X4.js +2 -0
- package/dist/chunk-CX7GJR55.js +1 -0
- package/dist/chunk-F76ECQKE.js +1 -0
- package/dist/chunk-FHCBXSNA.js +1 -0
- package/dist/chunk-GQIJLGEZ.mjs +1 -0
- package/dist/chunk-H7MLD632.mjs +2 -0
- package/dist/chunk-MB77QAOC.mjs +1 -0
- package/dist/chunk-MCQFAXUX.mjs +1 -0
- package/dist/chunk-RCSO5S56.js +2 -0
- package/dist/chunk-U6XM3X5N.mjs +2 -0
- package/dist/chunk-U7MK5AR5.mjs +1 -0
- package/dist/chunk-WWU7P2L2.mjs +1 -0
- package/dist/chunk-X6JSYVIW.mjs +1 -0
- package/dist/chunk-XKTDG6LH.js +1 -0
- package/dist/collection/index.d.mts +125 -1
- package/dist/collection/index.d.ts +125 -1
- package/dist/collection/index.js +1 -1
- package/dist/collection/index.mjs +1 -1
- package/dist/color/index.d.mts +274 -0
- package/dist/color/index.d.ts +274 -0
- package/dist/color/index.js +1 -0
- package/dist/color/index.mjs +1 -0
- package/dist/datetime/index.d.mts +320 -0
- package/dist/datetime/index.d.ts +320 -0
- package/dist/datetime/index.js +1 -0
- package/dist/datetime/index.mjs +1 -0
- package/dist/dom/index.d.mts +353 -0
- package/dist/dom/index.d.ts +353 -0
- package/dist/dom/index.js +1 -0
- package/dist/dom/index.mjs +1 -0
- package/dist/encrypt/index.d.mts +208 -0
- package/dist/encrypt/index.d.ts +208 -0
- package/dist/encrypt/index.js +1 -0
- package/dist/encrypt/index.mjs +1 -0
- package/dist/http/index.d.mts +301 -0
- package/dist/http/index.d.ts +301 -0
- package/dist/http/index.js +1 -0
- package/dist/http/index.mjs +1 -0
- package/dist/index.d.mts +591 -413
- package/dist/index.d.ts +591 -413
- package/dist/index.js +2 -2
- package/dist/index.mjs +2 -2
- package/dist/object/index.d.mts +137 -1
- package/dist/object/index.d.ts +137 -1
- package/dist/object/index.js +1 -1
- package/dist/object/index.mjs +1 -1
- package/dist/path/index.d.mts +178 -0
- package/dist/path/index.d.ts +178 -0
- package/dist/path/index.js +1 -0
- package/dist/path/index.mjs +1 -0
- package/dist/storage/index.d.mts +197 -0
- package/dist/storage/index.d.ts +197 -0
- package/dist/storage/index.js +1 -0
- package/dist/storage/index.mjs +1 -0
- package/dist/string/index.d.mts +201 -1
- package/dist/string/index.d.ts +201 -1
- package/dist/string/index.js +1 -1
- package/dist/string/index.mjs +1 -1
- package/package.json +36 -1
- package/dist/chunk-4HQERWI6.mjs +0 -1
- package/dist/chunk-5LWNMYHB.js +0 -1
- package/dist/chunk-7V3EDYJD.mjs +0 -1
- package/dist/chunk-FYQNKDT3.mjs +0 -1
- package/dist/chunk-MSOFO6QN.js +0 -1
- package/dist/chunk-WQPSXQT5.js +0 -1
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTTP request options.
|
|
3
|
+
*/
|
|
4
|
+
interface RequestOptions extends Omit<RequestInit, 'body'> {
|
|
5
|
+
baseUrl?: string;
|
|
6
|
+
timeout?: number;
|
|
7
|
+
params?: Record<string, string | number | boolean | undefined>;
|
|
8
|
+
body?: unknown;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* HTTP response with typed data.
|
|
12
|
+
*/
|
|
13
|
+
interface HttpResponse<T> {
|
|
14
|
+
data: T;
|
|
15
|
+
status: number;
|
|
16
|
+
statusText: string;
|
|
17
|
+
headers: Headers;
|
|
18
|
+
ok: boolean;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* HTTP error with response details.
|
|
22
|
+
*/
|
|
23
|
+
declare class HttpError extends Error {
|
|
24
|
+
status: number;
|
|
25
|
+
statusText: string;
|
|
26
|
+
response?: unknown | undefined;
|
|
27
|
+
constructor(message: string, status: number, statusText: string, response?: unknown | undefined);
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Builds URL with query parameters.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* buildUrl('https://api.example.com/users', { page: 1, limit: 10 });
|
|
34
|
+
* // => 'https://api.example.com/users?page=1&limit=10'
|
|
35
|
+
*/
|
|
36
|
+
declare function buildUrl(url: string, params?: Record<string, string | number | boolean | undefined>): string;
|
|
37
|
+
/**
|
|
38
|
+
* Makes an HTTP request with timeout and error handling.
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* const response = await request<User>('https://api.example.com/users/1');
|
|
42
|
+
* console.log(response.data); // User object
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* // POST request
|
|
46
|
+
* const response = await request<User>('https://api.example.com/users', {
|
|
47
|
+
* method: 'POST',
|
|
48
|
+
* body: { name: 'John', email: 'john@example.com' }
|
|
49
|
+
* });
|
|
50
|
+
*/
|
|
51
|
+
declare function request<T>(url: string, options?: RequestOptions): Promise<HttpResponse<T>>;
|
|
52
|
+
/**
|
|
53
|
+
* Makes a GET request.
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* const users = await get<User[]>('https://api.example.com/users');
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* // With query parameters
|
|
60
|
+
* const users = await get<User[]>('https://api.example.com/users', {
|
|
61
|
+
* params: { page: 1, limit: 10 }
|
|
62
|
+
* });
|
|
63
|
+
*/
|
|
64
|
+
declare function get<T>(url: string, options?: Omit<RequestOptions, 'method' | 'body'>): Promise<HttpResponse<T>>;
|
|
65
|
+
/**
|
|
66
|
+
* Makes a POST request.
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* const user = await post<User>('https://api.example.com/users', {
|
|
70
|
+
* body: { name: 'John', email: 'john@example.com' }
|
|
71
|
+
* });
|
|
72
|
+
*/
|
|
73
|
+
declare function post<T>(url: string, options?: RequestOptions): Promise<HttpResponse<T>>;
|
|
74
|
+
/**
|
|
75
|
+
* Makes a PUT request.
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* const user = await put<User>('https://api.example.com/users/1', {
|
|
79
|
+
* body: { name: 'John Updated' }
|
|
80
|
+
* });
|
|
81
|
+
*/
|
|
82
|
+
declare function put<T>(url: string, options?: RequestOptions): Promise<HttpResponse<T>>;
|
|
83
|
+
/**
|
|
84
|
+
* Makes a PATCH request.
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* const user = await patch<User>('https://api.example.com/users/1', {
|
|
88
|
+
* body: { name: 'John Updated' }
|
|
89
|
+
* });
|
|
90
|
+
*/
|
|
91
|
+
declare function patch<T>(url: string, options?: RequestOptions): Promise<HttpResponse<T>>;
|
|
92
|
+
/**
|
|
93
|
+
* Makes a DELETE request.
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* await del('https://api.example.com/users/1');
|
|
97
|
+
*/
|
|
98
|
+
declare function del<T = void>(url: string, options?: Omit<RequestOptions, 'body'>): Promise<HttpResponse<T>>;
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* HTTP client configuration.
|
|
102
|
+
*/
|
|
103
|
+
interface HttpClientConfig {
|
|
104
|
+
baseUrl: string;
|
|
105
|
+
timeout?: number;
|
|
106
|
+
headers?: Record<string, string>;
|
|
107
|
+
interceptors?: {
|
|
108
|
+
request?: (options: RequestOptions) => RequestOptions | Promise<RequestOptions>;
|
|
109
|
+
response?: <T>(response: HttpResponse<T>) => HttpResponse<T> | Promise<HttpResponse<T>>;
|
|
110
|
+
error?: (error: Error) => Error | Promise<Error>;
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* HTTP client instance.
|
|
115
|
+
*/
|
|
116
|
+
interface HttpClient {
|
|
117
|
+
get: <T>(url: string, options?: Omit<RequestOptions, 'method' | 'body'>) => Promise<HttpResponse<T>>;
|
|
118
|
+
post: <T>(url: string, options?: RequestOptions) => Promise<HttpResponse<T>>;
|
|
119
|
+
put: <T>(url: string, options?: RequestOptions) => Promise<HttpResponse<T>>;
|
|
120
|
+
patch: <T>(url: string, options?: RequestOptions) => Promise<HttpResponse<T>>;
|
|
121
|
+
delete: <T = void>(url: string, options?: Omit<RequestOptions, 'body'>) => Promise<HttpResponse<T>>;
|
|
122
|
+
request: <T>(url: string, options?: RequestOptions) => Promise<HttpResponse<T>>;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Creates an HTTP client with base configuration.
|
|
126
|
+
*
|
|
127
|
+
* @example
|
|
128
|
+
* const api = createHttpClient({
|
|
129
|
+
* baseUrl: 'https://api.example.com',
|
|
130
|
+
* timeout: 5000,
|
|
131
|
+
* headers: {
|
|
132
|
+
* 'Authorization': 'Bearer token123'
|
|
133
|
+
* }
|
|
134
|
+
* });
|
|
135
|
+
*
|
|
136
|
+
* // All requests use the base configuration
|
|
137
|
+
* const users = await api.get<User[]>('/users');
|
|
138
|
+
* const user = await api.post<User>('/users', { body: { name: 'John' } });
|
|
139
|
+
*
|
|
140
|
+
* @example
|
|
141
|
+
* // With interceptors
|
|
142
|
+
* const api = createHttpClient({
|
|
143
|
+
* baseUrl: 'https://api.example.com',
|
|
144
|
+
* interceptors: {
|
|
145
|
+
* request: (options) => {
|
|
146
|
+
* options.headers = {
|
|
147
|
+
* ...options.headers,
|
|
148
|
+
* 'X-Request-Id': crypto.randomUUID()
|
|
149
|
+
* };
|
|
150
|
+
* return options;
|
|
151
|
+
* },
|
|
152
|
+
* response: (response) => {
|
|
153
|
+
* console.log('Response:', response.status);
|
|
154
|
+
* return response;
|
|
155
|
+
* },
|
|
156
|
+
* error: (error) => {
|
|
157
|
+
* console.error('Request failed:', error);
|
|
158
|
+
* return error;
|
|
159
|
+
* }
|
|
160
|
+
* }
|
|
161
|
+
* });
|
|
162
|
+
*/
|
|
163
|
+
declare function createHttpClient(config: HttpClientConfig): HttpClient;
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Retry configuration options.
|
|
167
|
+
*/
|
|
168
|
+
interface RetryOptions {
|
|
169
|
+
maxRetries?: number;
|
|
170
|
+
delay?: number;
|
|
171
|
+
backoff?: 'linear' | 'exponential';
|
|
172
|
+
retryOn?: (error: Error, attempt: number) => boolean;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Makes an HTTP request with automatic retry on failure.
|
|
176
|
+
*
|
|
177
|
+
* @example
|
|
178
|
+
* // Basic retry with defaults (3 retries, 1s delay)
|
|
179
|
+
* const response = await requestWithRetry<User>('https://api.example.com/users/1');
|
|
180
|
+
*
|
|
181
|
+
* @example
|
|
182
|
+
* // Custom retry configuration
|
|
183
|
+
* const response = await requestWithRetry<User>('https://api.example.com/users/1', {
|
|
184
|
+
* maxRetries: 5,
|
|
185
|
+
* delay: 2000,
|
|
186
|
+
* backoff: 'exponential'
|
|
187
|
+
* });
|
|
188
|
+
*
|
|
189
|
+
* @example
|
|
190
|
+
* // Custom retry condition
|
|
191
|
+
* const response = await requestWithRetry<User>('https://api.example.com/users/1', {
|
|
192
|
+
* retryOn: (error, attempt) => {
|
|
193
|
+
* if (error instanceof HttpError && error.status === 429) {
|
|
194
|
+
* return attempt < 5; // Retry rate limiting up to 5 times
|
|
195
|
+
* }
|
|
196
|
+
* return false;
|
|
197
|
+
* }
|
|
198
|
+
* });
|
|
199
|
+
*/
|
|
200
|
+
declare function requestWithRetry<T>(url: string, options?: RequestOptions & RetryOptions): Promise<HttpResponse<T>>;
|
|
201
|
+
/**
|
|
202
|
+
* Creates a retry wrapper for any async function.
|
|
203
|
+
*
|
|
204
|
+
* @example
|
|
205
|
+
* const fetchWithRetry = withRetry(async (id: string) => {
|
|
206
|
+
* const response = await fetch(`/api/users/${id}`);
|
|
207
|
+
* return response.json();
|
|
208
|
+
* }, { maxRetries: 3 });
|
|
209
|
+
*
|
|
210
|
+
* const user = await fetchWithRetry('123');
|
|
211
|
+
*/
|
|
212
|
+
declare function withRetry<T extends (...args: unknown[]) => Promise<unknown>>(fn: T, options?: RetryOptions): T;
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Common HTTP headers.
|
|
216
|
+
*/
|
|
217
|
+
declare const HttpHeaders: {
|
|
218
|
+
readonly ContentType: "Content-Type";
|
|
219
|
+
readonly Accept: "Accept";
|
|
220
|
+
readonly Authorization: "Authorization";
|
|
221
|
+
readonly CacheControl: "Cache-Control";
|
|
222
|
+
readonly UserAgent: "User-Agent";
|
|
223
|
+
readonly AcceptLanguage: "Accept-Language";
|
|
224
|
+
readonly ContentLength: "Content-Length";
|
|
225
|
+
readonly XRequestId: "X-Request-Id";
|
|
226
|
+
readonly XForwardedFor: "X-Forwarded-For";
|
|
227
|
+
readonly XRealIp: "X-Real-IP";
|
|
228
|
+
};
|
|
229
|
+
/**
|
|
230
|
+
* Common content types.
|
|
231
|
+
*/
|
|
232
|
+
declare const ContentTypes: {
|
|
233
|
+
readonly JSON: "application/json";
|
|
234
|
+
readonly FormData: "multipart/form-data";
|
|
235
|
+
readonly FormUrlEncoded: "application/x-www-form-urlencoded";
|
|
236
|
+
readonly Text: "text/plain";
|
|
237
|
+
readonly HTML: "text/html";
|
|
238
|
+
readonly XML: "application/xml";
|
|
239
|
+
readonly Binary: "application/octet-stream";
|
|
240
|
+
};
|
|
241
|
+
/**
|
|
242
|
+
* Parses authorization header.
|
|
243
|
+
*
|
|
244
|
+
* @example
|
|
245
|
+
* parseAuthHeader('Bearer token123');
|
|
246
|
+
* // => { type: 'Bearer', credentials: 'token123' }
|
|
247
|
+
*
|
|
248
|
+
* @example
|
|
249
|
+
* parseAuthHeader('Basic dXNlcjpwYXNz');
|
|
250
|
+
* // => { type: 'Basic', credentials: 'dXNlcjpwYXNz' }
|
|
251
|
+
*/
|
|
252
|
+
declare function parseAuthHeader(header: string | null): {
|
|
253
|
+
type: string;
|
|
254
|
+
credentials: string;
|
|
255
|
+
} | null;
|
|
256
|
+
/**
|
|
257
|
+
* Creates a Bearer authorization header.
|
|
258
|
+
*
|
|
259
|
+
* @example
|
|
260
|
+
* bearerAuth('token123');
|
|
261
|
+
* // => 'Bearer token123'
|
|
262
|
+
*/
|
|
263
|
+
declare function bearerAuth(token: string): string;
|
|
264
|
+
/**
|
|
265
|
+
* Creates a Basic authorization header.
|
|
266
|
+
*
|
|
267
|
+
* @example
|
|
268
|
+
* basicAuth('user', 'password');
|
|
269
|
+
* // => 'Basic dXNlcjpwYXNzd29yZA=='
|
|
270
|
+
*/
|
|
271
|
+
declare function basicAuth(username: string, password: string): string;
|
|
272
|
+
/**
|
|
273
|
+
* Parses cookies from a Cookie header string.
|
|
274
|
+
*
|
|
275
|
+
* @example
|
|
276
|
+
* parseCookieHeader('session=abc123; user=john');
|
|
277
|
+
* // => { session: 'abc123', user: 'john' }
|
|
278
|
+
*/
|
|
279
|
+
declare function parseCookieHeader(header: string): Record<string, string>;
|
|
280
|
+
/**
|
|
281
|
+
* Builds a Cookie header string from an object.
|
|
282
|
+
*
|
|
283
|
+
* @example
|
|
284
|
+
* buildCookieHeader({ session: 'abc123', user: 'john' });
|
|
285
|
+
* // => 'session=abc123; user=john'
|
|
286
|
+
*/
|
|
287
|
+
declare function buildCookieHeader(cookies: Record<string, string>): string;
|
|
288
|
+
/**
|
|
289
|
+
* Merges multiple header objects.
|
|
290
|
+
*
|
|
291
|
+
* @example
|
|
292
|
+
* mergeHeaders(
|
|
293
|
+
* { 'Content-Type': 'application/json' },
|
|
294
|
+
* { 'Authorization': 'Bearer token' },
|
|
295
|
+
* { 'X-Custom': 'value' }
|
|
296
|
+
* );
|
|
297
|
+
* // => { 'Content-Type': 'application/json', 'Authorization': 'Bearer token', 'X-Custom': 'value' }
|
|
298
|
+
*/
|
|
299
|
+
declare function mergeHeaders(...headerObjects: (Record<string, string> | undefined)[]): Record<string, string>;
|
|
300
|
+
|
|
301
|
+
export { ContentTypes, type HttpClient, type HttpClientConfig, HttpError, HttpHeaders, type HttpResponse, type RequestOptions, type RetryOptions, basicAuth, bearerAuth, buildCookieHeader, buildUrl, createHttpClient, del, get, mergeHeaders, parseAuthHeader, parseCookieHeader, patch, post, put, request, requestWithRetry, withRetry };
|
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTTP request options.
|
|
3
|
+
*/
|
|
4
|
+
interface RequestOptions extends Omit<RequestInit, 'body'> {
|
|
5
|
+
baseUrl?: string;
|
|
6
|
+
timeout?: number;
|
|
7
|
+
params?: Record<string, string | number | boolean | undefined>;
|
|
8
|
+
body?: unknown;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* HTTP response with typed data.
|
|
12
|
+
*/
|
|
13
|
+
interface HttpResponse<T> {
|
|
14
|
+
data: T;
|
|
15
|
+
status: number;
|
|
16
|
+
statusText: string;
|
|
17
|
+
headers: Headers;
|
|
18
|
+
ok: boolean;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* HTTP error with response details.
|
|
22
|
+
*/
|
|
23
|
+
declare class HttpError extends Error {
|
|
24
|
+
status: number;
|
|
25
|
+
statusText: string;
|
|
26
|
+
response?: unknown | undefined;
|
|
27
|
+
constructor(message: string, status: number, statusText: string, response?: unknown | undefined);
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Builds URL with query parameters.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* buildUrl('https://api.example.com/users', { page: 1, limit: 10 });
|
|
34
|
+
* // => 'https://api.example.com/users?page=1&limit=10'
|
|
35
|
+
*/
|
|
36
|
+
declare function buildUrl(url: string, params?: Record<string, string | number | boolean | undefined>): string;
|
|
37
|
+
/**
|
|
38
|
+
* Makes an HTTP request with timeout and error handling.
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* const response = await request<User>('https://api.example.com/users/1');
|
|
42
|
+
* console.log(response.data); // User object
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* // POST request
|
|
46
|
+
* const response = await request<User>('https://api.example.com/users', {
|
|
47
|
+
* method: 'POST',
|
|
48
|
+
* body: { name: 'John', email: 'john@example.com' }
|
|
49
|
+
* });
|
|
50
|
+
*/
|
|
51
|
+
declare function request<T>(url: string, options?: RequestOptions): Promise<HttpResponse<T>>;
|
|
52
|
+
/**
|
|
53
|
+
* Makes a GET request.
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* const users = await get<User[]>('https://api.example.com/users');
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* // With query parameters
|
|
60
|
+
* const users = await get<User[]>('https://api.example.com/users', {
|
|
61
|
+
* params: { page: 1, limit: 10 }
|
|
62
|
+
* });
|
|
63
|
+
*/
|
|
64
|
+
declare function get<T>(url: string, options?: Omit<RequestOptions, 'method' | 'body'>): Promise<HttpResponse<T>>;
|
|
65
|
+
/**
|
|
66
|
+
* Makes a POST request.
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* const user = await post<User>('https://api.example.com/users', {
|
|
70
|
+
* body: { name: 'John', email: 'john@example.com' }
|
|
71
|
+
* });
|
|
72
|
+
*/
|
|
73
|
+
declare function post<T>(url: string, options?: RequestOptions): Promise<HttpResponse<T>>;
|
|
74
|
+
/**
|
|
75
|
+
* Makes a PUT request.
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* const user = await put<User>('https://api.example.com/users/1', {
|
|
79
|
+
* body: { name: 'John Updated' }
|
|
80
|
+
* });
|
|
81
|
+
*/
|
|
82
|
+
declare function put<T>(url: string, options?: RequestOptions): Promise<HttpResponse<T>>;
|
|
83
|
+
/**
|
|
84
|
+
* Makes a PATCH request.
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* const user = await patch<User>('https://api.example.com/users/1', {
|
|
88
|
+
* body: { name: 'John Updated' }
|
|
89
|
+
* });
|
|
90
|
+
*/
|
|
91
|
+
declare function patch<T>(url: string, options?: RequestOptions): Promise<HttpResponse<T>>;
|
|
92
|
+
/**
|
|
93
|
+
* Makes a DELETE request.
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* await del('https://api.example.com/users/1');
|
|
97
|
+
*/
|
|
98
|
+
declare function del<T = void>(url: string, options?: Omit<RequestOptions, 'body'>): Promise<HttpResponse<T>>;
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* HTTP client configuration.
|
|
102
|
+
*/
|
|
103
|
+
interface HttpClientConfig {
|
|
104
|
+
baseUrl: string;
|
|
105
|
+
timeout?: number;
|
|
106
|
+
headers?: Record<string, string>;
|
|
107
|
+
interceptors?: {
|
|
108
|
+
request?: (options: RequestOptions) => RequestOptions | Promise<RequestOptions>;
|
|
109
|
+
response?: <T>(response: HttpResponse<T>) => HttpResponse<T> | Promise<HttpResponse<T>>;
|
|
110
|
+
error?: (error: Error) => Error | Promise<Error>;
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* HTTP client instance.
|
|
115
|
+
*/
|
|
116
|
+
interface HttpClient {
|
|
117
|
+
get: <T>(url: string, options?: Omit<RequestOptions, 'method' | 'body'>) => Promise<HttpResponse<T>>;
|
|
118
|
+
post: <T>(url: string, options?: RequestOptions) => Promise<HttpResponse<T>>;
|
|
119
|
+
put: <T>(url: string, options?: RequestOptions) => Promise<HttpResponse<T>>;
|
|
120
|
+
patch: <T>(url: string, options?: RequestOptions) => Promise<HttpResponse<T>>;
|
|
121
|
+
delete: <T = void>(url: string, options?: Omit<RequestOptions, 'body'>) => Promise<HttpResponse<T>>;
|
|
122
|
+
request: <T>(url: string, options?: RequestOptions) => Promise<HttpResponse<T>>;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Creates an HTTP client with base configuration.
|
|
126
|
+
*
|
|
127
|
+
* @example
|
|
128
|
+
* const api = createHttpClient({
|
|
129
|
+
* baseUrl: 'https://api.example.com',
|
|
130
|
+
* timeout: 5000,
|
|
131
|
+
* headers: {
|
|
132
|
+
* 'Authorization': 'Bearer token123'
|
|
133
|
+
* }
|
|
134
|
+
* });
|
|
135
|
+
*
|
|
136
|
+
* // All requests use the base configuration
|
|
137
|
+
* const users = await api.get<User[]>('/users');
|
|
138
|
+
* const user = await api.post<User>('/users', { body: { name: 'John' } });
|
|
139
|
+
*
|
|
140
|
+
* @example
|
|
141
|
+
* // With interceptors
|
|
142
|
+
* const api = createHttpClient({
|
|
143
|
+
* baseUrl: 'https://api.example.com',
|
|
144
|
+
* interceptors: {
|
|
145
|
+
* request: (options) => {
|
|
146
|
+
* options.headers = {
|
|
147
|
+
* ...options.headers,
|
|
148
|
+
* 'X-Request-Id': crypto.randomUUID()
|
|
149
|
+
* };
|
|
150
|
+
* return options;
|
|
151
|
+
* },
|
|
152
|
+
* response: (response) => {
|
|
153
|
+
* console.log('Response:', response.status);
|
|
154
|
+
* return response;
|
|
155
|
+
* },
|
|
156
|
+
* error: (error) => {
|
|
157
|
+
* console.error('Request failed:', error);
|
|
158
|
+
* return error;
|
|
159
|
+
* }
|
|
160
|
+
* }
|
|
161
|
+
* });
|
|
162
|
+
*/
|
|
163
|
+
declare function createHttpClient(config: HttpClientConfig): HttpClient;
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Retry configuration options.
|
|
167
|
+
*/
|
|
168
|
+
interface RetryOptions {
|
|
169
|
+
maxRetries?: number;
|
|
170
|
+
delay?: number;
|
|
171
|
+
backoff?: 'linear' | 'exponential';
|
|
172
|
+
retryOn?: (error: Error, attempt: number) => boolean;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Makes an HTTP request with automatic retry on failure.
|
|
176
|
+
*
|
|
177
|
+
* @example
|
|
178
|
+
* // Basic retry with defaults (3 retries, 1s delay)
|
|
179
|
+
* const response = await requestWithRetry<User>('https://api.example.com/users/1');
|
|
180
|
+
*
|
|
181
|
+
* @example
|
|
182
|
+
* // Custom retry configuration
|
|
183
|
+
* const response = await requestWithRetry<User>('https://api.example.com/users/1', {
|
|
184
|
+
* maxRetries: 5,
|
|
185
|
+
* delay: 2000,
|
|
186
|
+
* backoff: 'exponential'
|
|
187
|
+
* });
|
|
188
|
+
*
|
|
189
|
+
* @example
|
|
190
|
+
* // Custom retry condition
|
|
191
|
+
* const response = await requestWithRetry<User>('https://api.example.com/users/1', {
|
|
192
|
+
* retryOn: (error, attempt) => {
|
|
193
|
+
* if (error instanceof HttpError && error.status === 429) {
|
|
194
|
+
* return attempt < 5; // Retry rate limiting up to 5 times
|
|
195
|
+
* }
|
|
196
|
+
* return false;
|
|
197
|
+
* }
|
|
198
|
+
* });
|
|
199
|
+
*/
|
|
200
|
+
declare function requestWithRetry<T>(url: string, options?: RequestOptions & RetryOptions): Promise<HttpResponse<T>>;
|
|
201
|
+
/**
|
|
202
|
+
* Creates a retry wrapper for any async function.
|
|
203
|
+
*
|
|
204
|
+
* @example
|
|
205
|
+
* const fetchWithRetry = withRetry(async (id: string) => {
|
|
206
|
+
* const response = await fetch(`/api/users/${id}`);
|
|
207
|
+
* return response.json();
|
|
208
|
+
* }, { maxRetries: 3 });
|
|
209
|
+
*
|
|
210
|
+
* const user = await fetchWithRetry('123');
|
|
211
|
+
*/
|
|
212
|
+
declare function withRetry<T extends (...args: unknown[]) => Promise<unknown>>(fn: T, options?: RetryOptions): T;
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Common HTTP headers.
|
|
216
|
+
*/
|
|
217
|
+
declare const HttpHeaders: {
|
|
218
|
+
readonly ContentType: "Content-Type";
|
|
219
|
+
readonly Accept: "Accept";
|
|
220
|
+
readonly Authorization: "Authorization";
|
|
221
|
+
readonly CacheControl: "Cache-Control";
|
|
222
|
+
readonly UserAgent: "User-Agent";
|
|
223
|
+
readonly AcceptLanguage: "Accept-Language";
|
|
224
|
+
readonly ContentLength: "Content-Length";
|
|
225
|
+
readonly XRequestId: "X-Request-Id";
|
|
226
|
+
readonly XForwardedFor: "X-Forwarded-For";
|
|
227
|
+
readonly XRealIp: "X-Real-IP";
|
|
228
|
+
};
|
|
229
|
+
/**
|
|
230
|
+
* Common content types.
|
|
231
|
+
*/
|
|
232
|
+
declare const ContentTypes: {
|
|
233
|
+
readonly JSON: "application/json";
|
|
234
|
+
readonly FormData: "multipart/form-data";
|
|
235
|
+
readonly FormUrlEncoded: "application/x-www-form-urlencoded";
|
|
236
|
+
readonly Text: "text/plain";
|
|
237
|
+
readonly HTML: "text/html";
|
|
238
|
+
readonly XML: "application/xml";
|
|
239
|
+
readonly Binary: "application/octet-stream";
|
|
240
|
+
};
|
|
241
|
+
/**
|
|
242
|
+
* Parses authorization header.
|
|
243
|
+
*
|
|
244
|
+
* @example
|
|
245
|
+
* parseAuthHeader('Bearer token123');
|
|
246
|
+
* // => { type: 'Bearer', credentials: 'token123' }
|
|
247
|
+
*
|
|
248
|
+
* @example
|
|
249
|
+
* parseAuthHeader('Basic dXNlcjpwYXNz');
|
|
250
|
+
* // => { type: 'Basic', credentials: 'dXNlcjpwYXNz' }
|
|
251
|
+
*/
|
|
252
|
+
declare function parseAuthHeader(header: string | null): {
|
|
253
|
+
type: string;
|
|
254
|
+
credentials: string;
|
|
255
|
+
} | null;
|
|
256
|
+
/**
|
|
257
|
+
* Creates a Bearer authorization header.
|
|
258
|
+
*
|
|
259
|
+
* @example
|
|
260
|
+
* bearerAuth('token123');
|
|
261
|
+
* // => 'Bearer token123'
|
|
262
|
+
*/
|
|
263
|
+
declare function bearerAuth(token: string): string;
|
|
264
|
+
/**
|
|
265
|
+
* Creates a Basic authorization header.
|
|
266
|
+
*
|
|
267
|
+
* @example
|
|
268
|
+
* basicAuth('user', 'password');
|
|
269
|
+
* // => 'Basic dXNlcjpwYXNzd29yZA=='
|
|
270
|
+
*/
|
|
271
|
+
declare function basicAuth(username: string, password: string): string;
|
|
272
|
+
/**
|
|
273
|
+
* Parses cookies from a Cookie header string.
|
|
274
|
+
*
|
|
275
|
+
* @example
|
|
276
|
+
* parseCookieHeader('session=abc123; user=john');
|
|
277
|
+
* // => { session: 'abc123', user: 'john' }
|
|
278
|
+
*/
|
|
279
|
+
declare function parseCookieHeader(header: string): Record<string, string>;
|
|
280
|
+
/**
|
|
281
|
+
* Builds a Cookie header string from an object.
|
|
282
|
+
*
|
|
283
|
+
* @example
|
|
284
|
+
* buildCookieHeader({ session: 'abc123', user: 'john' });
|
|
285
|
+
* // => 'session=abc123; user=john'
|
|
286
|
+
*/
|
|
287
|
+
declare function buildCookieHeader(cookies: Record<string, string>): string;
|
|
288
|
+
/**
|
|
289
|
+
* Merges multiple header objects.
|
|
290
|
+
*
|
|
291
|
+
* @example
|
|
292
|
+
* mergeHeaders(
|
|
293
|
+
* { 'Content-Type': 'application/json' },
|
|
294
|
+
* { 'Authorization': 'Bearer token' },
|
|
295
|
+
* { 'X-Custom': 'value' }
|
|
296
|
+
* );
|
|
297
|
+
* // => { 'Content-Type': 'application/json', 'Authorization': 'Bearer token', 'X-Custom': 'value' }
|
|
298
|
+
*/
|
|
299
|
+
declare function mergeHeaders(...headerObjects: (Record<string, string> | undefined)[]): Record<string, string>;
|
|
300
|
+
|
|
301
|
+
export { ContentTypes, type HttpClient, type HttpClientConfig, HttpError, HttpHeaders, type HttpResponse, type RequestOptions, type RetryOptions, basicAuth, bearerAuth, buildCookieHeader, buildUrl, createHttpClient, del, get, mergeHeaders, parseAuthHeader, parseCookieHeader, patch, post, put, request, requestWithRetry, withRetry };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
'use strict';var chunkXKTDG6LH_js=require('../chunk-XKTDG6LH.js');require('../chunk-WOT6VMZA.js');Object.defineProperty(exports,"ContentTypes",{enumerable:true,get:function(){return chunkXKTDG6LH_js.m}});Object.defineProperty(exports,"HttpError",{enumerable:true,get:function(){return chunkXKTDG6LH_js.a}});Object.defineProperty(exports,"HttpHeaders",{enumerable:true,get:function(){return chunkXKTDG6LH_js.l}});Object.defineProperty(exports,"basicAuth",{enumerable:true,get:function(){return chunkXKTDG6LH_js.p}});Object.defineProperty(exports,"bearerAuth",{enumerable:true,get:function(){return chunkXKTDG6LH_js.o}});Object.defineProperty(exports,"buildCookieHeader",{enumerable:true,get:function(){return chunkXKTDG6LH_js.r}});Object.defineProperty(exports,"buildUrl",{enumerable:true,get:function(){return chunkXKTDG6LH_js.b}});Object.defineProperty(exports,"createHttpClient",{enumerable:true,get:function(){return chunkXKTDG6LH_js.i}});Object.defineProperty(exports,"del",{enumerable:true,get:function(){return chunkXKTDG6LH_js.h}});Object.defineProperty(exports,"get",{enumerable:true,get:function(){return chunkXKTDG6LH_js.d}});Object.defineProperty(exports,"mergeHeaders",{enumerable:true,get:function(){return chunkXKTDG6LH_js.s}});Object.defineProperty(exports,"parseAuthHeader",{enumerable:true,get:function(){return chunkXKTDG6LH_js.n}});Object.defineProperty(exports,"parseCookieHeader",{enumerable:true,get:function(){return chunkXKTDG6LH_js.q}});Object.defineProperty(exports,"patch",{enumerable:true,get:function(){return chunkXKTDG6LH_js.g}});Object.defineProperty(exports,"post",{enumerable:true,get:function(){return chunkXKTDG6LH_js.e}});Object.defineProperty(exports,"put",{enumerable:true,get:function(){return chunkXKTDG6LH_js.f}});Object.defineProperty(exports,"request",{enumerable:true,get:function(){return chunkXKTDG6LH_js.c}});Object.defineProperty(exports,"requestWithRetry",{enumerable:true,get:function(){return chunkXKTDG6LH_js.j}});Object.defineProperty(exports,"withRetry",{enumerable:true,get:function(){return chunkXKTDG6LH_js.k}});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export{m as ContentTypes,a as HttpError,l as HttpHeaders,p as basicAuth,o as bearerAuth,r as buildCookieHeader,b as buildUrl,i as createHttpClient,h as del,d as get,s as mergeHeaders,n as parseAuthHeader,q as parseCookieHeader,g as patch,e as post,f as put,c as request,j as requestWithRetry,k as withRetry}from'../chunk-MCQFAXUX.mjs';import'../chunk-JZXLCA2E.mjs';
|