@beyonk/http 12.1.1 → 12.2.0

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.
@@ -0,0 +1,310 @@
1
+ /**
2
+ * API Configuration options
3
+ */
4
+ type ApiOptions = {
5
+ /** Base URL for API requests */
6
+ baseUrl?: string;
7
+ /** Mock client for testing */
8
+ mock?: FetchClient;
9
+ /** Fetch client for HTTP requests */
10
+ fetch?: FetchClient;
11
+ /** Whether to retry failed requests */
12
+ retry?: RetryOptions | false;
13
+ /** Whether to parse error responses as JSON */
14
+ parseErrors?: boolean;
15
+ /** Global error handlers */
16
+ handlers?: ErrorHandlers;
17
+ };
18
+ /**
19
+ * Retry configuration
20
+ */
21
+ type RetryOptions = {
22
+ /** Number of retry attempts */
23
+ attempts: number;
24
+ /** Error codes to retry on */
25
+ errors: string[];
26
+ };
27
+ /**
28
+ * Context object for API requests
29
+ */
30
+ type ApiContext = {
31
+ /** Fetch client */
32
+ fetch?: FetchClient;
33
+ /** Additional context properties */
34
+ [key: string]: any;
35
+ };
36
+ /**
37
+ * Handler function for HTTP errors
38
+ */
39
+ type ErrorHandler = (error: HttpError, context?: ApiContext) => any;
40
+ /**
41
+ * Map of error handlers
42
+ */
43
+ type ErrorHandlers = {
44
+ accessDenied?: ErrorHandler;
45
+ paymentRequired?: ErrorHandler;
46
+ forbidden?: ErrorHandler;
47
+ notFound?: ErrorHandler;
48
+ notAcceptable?: ErrorHandler;
49
+ conflict?: ErrorHandler;
50
+ gone?: ErrorHandler;
51
+ preconditionFailed?: ErrorHandler;
52
+ expectationFailed?: ErrorHandler;
53
+ badData?: ErrorHandler;
54
+ tooManyRequests?: ErrorHandler;
55
+ [key: string]: ErrorHandler | undefined;
56
+ };
57
+ /**
58
+ * Function to transform API response
59
+ */
60
+ type ResponseTransformer<T = any> = (json: any, httpStatus: number) => T;
61
+ /**
62
+ * Fetch client interface
63
+ */
64
+ type FetchClient = (url: string, options: Record<string, any>) => Promise<Response>;
65
+ /**
66
+ * Fetch response interface
67
+ */
68
+ type Response = {
69
+ status: number;
70
+ statusText: string;
71
+ json(): Promise<any>;
72
+ text(): Promise<string>;
73
+ headers: Map<string, string> | {
74
+ get(name: string): string | null;
75
+ };
76
+ ok?: boolean;
77
+ };
78
+
79
+ /**
80
+ * @fileoverview Consolidated API client with error handling
81
+ * @import {
82
+ * ApiOptions,
83
+ * ApiContext,
84
+ * ErrorHandler,
85
+ * FetchClient,
86
+ * RequestConfig,
87
+ * ResponseTransformer,
88
+ * QueryResult
89
+ * } from './types.js'
90
+ */
91
+ /**
92
+ * Base HTTP error class
93
+ */
94
+ declare class HttpError extends Error {
95
+ /**
96
+ * Create a new HTTP error
97
+ * @param {string} message - Error message
98
+ * @param {any} body - Error response body
99
+ */
100
+ constructor(message: string, body: any);
101
+ body: any;
102
+ }
103
+ declare namespace _default {
104
+ export { create };
105
+ export { configure };
106
+ }
107
+
108
+ type Config = ApiOptions | undefined;
109
+ /**
110
+ * API client with chainable interface for making HTTP requests
111
+ */
112
+ declare class Api {
113
+ /**
114
+ * Creates a new API instance
115
+ * @param {ApiOptions} options - API configuration options
116
+ */
117
+ constructor(options: ApiOptions);
118
+ config: any;
119
+ /** @type {Record<string, ErrorHandler>} */
120
+ handlers: Record<string, ErrorHandler>;
121
+ /** @type {FetchClient|null} */
122
+ client: FetchClient | null;
123
+ /** @type {ApiContext|null} */
124
+ ctx: ApiContext | null;
125
+ /** @type {ErrorHandler|null} */
126
+ defaultHandler: ErrorHandler | null;
127
+ /** @type {ApiOptions} */
128
+ options: ApiOptions;
129
+ /**
130
+ * Reset the request configuration to defaults
131
+ */
132
+ resetRequest(): void;
133
+ /**
134
+ * Get the HTTP client to use for requests
135
+ * @returns {FetchClient} HTTP client
136
+ * @throws {Error} If no client is available
137
+ */
138
+ getClient(): FetchClient;
139
+ /**
140
+ * Handle an error
141
+ * @param {HttpError} e - Error instance
142
+ * @param {ApiContext} [ctx] - API context
143
+ * @returns {any} Result of error handler
144
+ */
145
+ handle(e: HttpError, ctx?: ApiContext): any;
146
+ /**
147
+ * Send the HTTP request
148
+ * @template T
149
+ * @param {ResponseTransformer<T>} [fn] - Function to transform the response
150
+ * @returns {Promise<T>} Response data
151
+ */
152
+ send<T>(fn?: ResponseTransformer<T>): Promise<T>;
153
+ /**
154
+ * Set the context for the request
155
+ * @param {ApiContext} ctx - Request context
156
+ * @returns {this} Current instance
157
+ */
158
+ context(ctx: ApiContext): this;
159
+ /**
160
+ * Set request overrides
161
+ * @param {Record<string, any>} override - Request overrides
162
+ * @returns {this} Current instance
163
+ */
164
+ override(override: Record<string, any>): this;
165
+ /**
166
+ * Set request headers
167
+ * @param {Record<string, string>} headers - Request headers
168
+ * @returns {this} Current instance
169
+ */
170
+ headers(headers: Record<string, string>): this;
171
+ /**
172
+ * Perform a GET request
173
+ * @template T
174
+ * @param {ResponseTransformer<T>} [fn] - Function to transform the response
175
+ * @returns {Promise<T>} Response data
176
+ */
177
+ get<T>(fn?: ResponseTransformer<T>): Promise<T>;
178
+ /**
179
+ * Perform a POST request
180
+ * @template T
181
+ * @param {ResponseTransformer<T>} [fn] - Function to transform the response
182
+ * @returns {Promise<T>} Response data
183
+ */
184
+ post<T>(fn?: ResponseTransformer<T>): Promise<T>;
185
+ /**
186
+ * Perform a PATCH request
187
+ * @template T
188
+ * @param {ResponseTransformer<T>} [fn] - Function to transform the response
189
+ * @returns {Promise<T>} Response data
190
+ */
191
+ patch<T>(fn?: ResponseTransformer<T>): Promise<T>;
192
+ /**
193
+ * Perform a PUT request
194
+ * @template T
195
+ * @param {ResponseTransformer<T>} [fn] - Function to transform the response
196
+ * @returns {Promise<T>} Response data
197
+ */
198
+ put<T>(fn?: ResponseTransformer<T>): Promise<T>;
199
+ /**
200
+ * Perform a DELETE request
201
+ * @template T
202
+ * @param {ResponseTransformer<T>} [fn] - Function to transform the response
203
+ * @returns {Promise<T>} Response data
204
+ */
205
+ del<T>(fn?: ResponseTransformer<T>): Promise<T>;
206
+ /**
207
+ * Set the API endpoint
208
+ * @param {string} endpoint - API endpoint
209
+ * @returns {this} Current instance
210
+ */
211
+ endpoint(endpoint: string): this;
212
+ /**
213
+ * Set query parameters
214
+ * @param {Record<string, any>} query - Query parameters
215
+ * @returns {this} Current instance
216
+ */
217
+ query(query: Record<string, any>): this;
218
+ /**
219
+ * Set request payload
220
+ * @param {any} payload - Request payload
221
+ * @returns {this} Current instance
222
+ */
223
+ payload(payload: any): this;
224
+ /**
225
+ * Register a default error handler
226
+ * @param {ErrorHandler} fn - Error handler function
227
+ * @returns {this} Current instance
228
+ */
229
+ default(fn: ErrorHandler): this;
230
+ /**
231
+ * Register a handler for AccessDenied (401) errors
232
+ * @param {ErrorHandler} fn - Error handler function
233
+ * @returns {this} Current instance
234
+ */
235
+ accessDenied(fn: ErrorHandler): this;
236
+ /**
237
+ * Register a handler for PaymentRequired (402) errors
238
+ * @param {ErrorHandler} fn - Error handler function
239
+ * @returns {this} Current instance
240
+ */
241
+ paymentRequired(fn: ErrorHandler): this;
242
+ /**
243
+ * Register a handler for Forbidden (403) errors
244
+ * @param {ErrorHandler} fn - Error handler function
245
+ * @returns {this} Current instance
246
+ */
247
+ forbidden(fn: ErrorHandler): this;
248
+ /**
249
+ * Register a handler for NotFound (404) errors
250
+ * @param {ErrorHandler} fn - Error handler function
251
+ * @returns {this} Current instance
252
+ */
253
+ notFound(fn: ErrorHandler): this;
254
+ /**
255
+ * Register a handler for NotAcceptable (406) errors
256
+ * @param {ErrorHandler} fn - Error handler function
257
+ * @returns {this} Current instance
258
+ */
259
+ notAcceptable(fn: ErrorHandler): this;
260
+ /**
261
+ * Register a handler for Conflict (409) errors
262
+ * @param {ErrorHandler} fn - Error handler function
263
+ * @returns {this} Current instance
264
+ */
265
+ conflict(fn: ErrorHandler): this;
266
+ /**
267
+ * Register a handler for Gone (410) errors
268
+ * @param {ErrorHandler} fn - Error handler function
269
+ * @returns {this} Current instance
270
+ */
271
+ gone(fn: ErrorHandler): this;
272
+ /**
273
+ * Register a handler for PreconditionFailed (412) errors
274
+ * @param {ErrorHandler} fn - Error handler function
275
+ * @returns {this} Current instance
276
+ */
277
+ preconditionFailed(fn: ErrorHandler): this;
278
+ /**
279
+ * Register a handler for ExpectationFailed (417) errors
280
+ * @param {ErrorHandler} fn - Error handler function
281
+ * @returns {this} Current instance
282
+ */
283
+ expectationFailed(fn: ErrorHandler): this;
284
+ /**
285
+ * Register a handler for BadData (422) errors
286
+ * @param {ErrorHandler} fn - Error handler function
287
+ * @returns {this} Current instance
288
+ */
289
+ badData(fn: ErrorHandler): this;
290
+ /**
291
+ * Register a handler for TooManyRequests (429) errors
292
+ * @param {ErrorHandler} fn - Error handler function
293
+ * @returns {this} Current instance
294
+ */
295
+ tooManyRequests(fn: ErrorHandler): this;
296
+ #private;
297
+ }
298
+ /**
299
+ * Create a new API client instance
300
+ * @returns {Api} API client instance
301
+ * @throws {Error} If API client is not configured
302
+ */
303
+ declare function create(): Api;
304
+ /**
305
+ * Configure the API client
306
+ * @param {ApiOptions} options - API configuration options
307
+ */
308
+ declare function configure(options: ApiOptions): void;
309
+
310
+ export { Api, type Config, HttpError, _default as default };
@@ -0,0 +1,310 @@
1
+ /**
2
+ * API Configuration options
3
+ */
4
+ type ApiOptions = {
5
+ /** Base URL for API requests */
6
+ baseUrl?: string;
7
+ /** Mock client for testing */
8
+ mock?: FetchClient;
9
+ /** Fetch client for HTTP requests */
10
+ fetch?: FetchClient;
11
+ /** Whether to retry failed requests */
12
+ retry?: RetryOptions | false;
13
+ /** Whether to parse error responses as JSON */
14
+ parseErrors?: boolean;
15
+ /** Global error handlers */
16
+ handlers?: ErrorHandlers;
17
+ };
18
+ /**
19
+ * Retry configuration
20
+ */
21
+ type RetryOptions = {
22
+ /** Number of retry attempts */
23
+ attempts: number;
24
+ /** Error codes to retry on */
25
+ errors: string[];
26
+ };
27
+ /**
28
+ * Context object for API requests
29
+ */
30
+ type ApiContext = {
31
+ /** Fetch client */
32
+ fetch?: FetchClient;
33
+ /** Additional context properties */
34
+ [key: string]: any;
35
+ };
36
+ /**
37
+ * Handler function for HTTP errors
38
+ */
39
+ type ErrorHandler = (error: HttpError, context?: ApiContext) => any;
40
+ /**
41
+ * Map of error handlers
42
+ */
43
+ type ErrorHandlers = {
44
+ accessDenied?: ErrorHandler;
45
+ paymentRequired?: ErrorHandler;
46
+ forbidden?: ErrorHandler;
47
+ notFound?: ErrorHandler;
48
+ notAcceptable?: ErrorHandler;
49
+ conflict?: ErrorHandler;
50
+ gone?: ErrorHandler;
51
+ preconditionFailed?: ErrorHandler;
52
+ expectationFailed?: ErrorHandler;
53
+ badData?: ErrorHandler;
54
+ tooManyRequests?: ErrorHandler;
55
+ [key: string]: ErrorHandler | undefined;
56
+ };
57
+ /**
58
+ * Function to transform API response
59
+ */
60
+ type ResponseTransformer<T = any> = (json: any, httpStatus: number) => T;
61
+ /**
62
+ * Fetch client interface
63
+ */
64
+ type FetchClient = (url: string, options: Record<string, any>) => Promise<Response>;
65
+ /**
66
+ * Fetch response interface
67
+ */
68
+ type Response = {
69
+ status: number;
70
+ statusText: string;
71
+ json(): Promise<any>;
72
+ text(): Promise<string>;
73
+ headers: Map<string, string> | {
74
+ get(name: string): string | null;
75
+ };
76
+ ok?: boolean;
77
+ };
78
+
79
+ /**
80
+ * @fileoverview Consolidated API client with error handling
81
+ * @import {
82
+ * ApiOptions,
83
+ * ApiContext,
84
+ * ErrorHandler,
85
+ * FetchClient,
86
+ * RequestConfig,
87
+ * ResponseTransformer,
88
+ * QueryResult
89
+ * } from './types.js'
90
+ */
91
+ /**
92
+ * Base HTTP error class
93
+ */
94
+ declare class HttpError extends Error {
95
+ /**
96
+ * Create a new HTTP error
97
+ * @param {string} message - Error message
98
+ * @param {any} body - Error response body
99
+ */
100
+ constructor(message: string, body: any);
101
+ body: any;
102
+ }
103
+ declare namespace _default {
104
+ export { create };
105
+ export { configure };
106
+ }
107
+
108
+ type Config = ApiOptions | undefined;
109
+ /**
110
+ * API client with chainable interface for making HTTP requests
111
+ */
112
+ declare class Api {
113
+ /**
114
+ * Creates a new API instance
115
+ * @param {ApiOptions} options - API configuration options
116
+ */
117
+ constructor(options: ApiOptions);
118
+ config: any;
119
+ /** @type {Record<string, ErrorHandler>} */
120
+ handlers: Record<string, ErrorHandler>;
121
+ /** @type {FetchClient|null} */
122
+ client: FetchClient | null;
123
+ /** @type {ApiContext|null} */
124
+ ctx: ApiContext | null;
125
+ /** @type {ErrorHandler|null} */
126
+ defaultHandler: ErrorHandler | null;
127
+ /** @type {ApiOptions} */
128
+ options: ApiOptions;
129
+ /**
130
+ * Reset the request configuration to defaults
131
+ */
132
+ resetRequest(): void;
133
+ /**
134
+ * Get the HTTP client to use for requests
135
+ * @returns {FetchClient} HTTP client
136
+ * @throws {Error} If no client is available
137
+ */
138
+ getClient(): FetchClient;
139
+ /**
140
+ * Handle an error
141
+ * @param {HttpError} e - Error instance
142
+ * @param {ApiContext} [ctx] - API context
143
+ * @returns {any} Result of error handler
144
+ */
145
+ handle(e: HttpError, ctx?: ApiContext): any;
146
+ /**
147
+ * Send the HTTP request
148
+ * @template T
149
+ * @param {ResponseTransformer<T>} [fn] - Function to transform the response
150
+ * @returns {Promise<T>} Response data
151
+ */
152
+ send<T>(fn?: ResponseTransformer<T>): Promise<T>;
153
+ /**
154
+ * Set the context for the request
155
+ * @param {ApiContext} ctx - Request context
156
+ * @returns {this} Current instance
157
+ */
158
+ context(ctx: ApiContext): this;
159
+ /**
160
+ * Set request overrides
161
+ * @param {Record<string, any>} override - Request overrides
162
+ * @returns {this} Current instance
163
+ */
164
+ override(override: Record<string, any>): this;
165
+ /**
166
+ * Set request headers
167
+ * @param {Record<string, string>} headers - Request headers
168
+ * @returns {this} Current instance
169
+ */
170
+ headers(headers: Record<string, string>): this;
171
+ /**
172
+ * Perform a GET request
173
+ * @template T
174
+ * @param {ResponseTransformer<T>} [fn] - Function to transform the response
175
+ * @returns {Promise<T>} Response data
176
+ */
177
+ get<T>(fn?: ResponseTransformer<T>): Promise<T>;
178
+ /**
179
+ * Perform a POST request
180
+ * @template T
181
+ * @param {ResponseTransformer<T>} [fn] - Function to transform the response
182
+ * @returns {Promise<T>} Response data
183
+ */
184
+ post<T>(fn?: ResponseTransformer<T>): Promise<T>;
185
+ /**
186
+ * Perform a PATCH request
187
+ * @template T
188
+ * @param {ResponseTransformer<T>} [fn] - Function to transform the response
189
+ * @returns {Promise<T>} Response data
190
+ */
191
+ patch<T>(fn?: ResponseTransformer<T>): Promise<T>;
192
+ /**
193
+ * Perform a PUT request
194
+ * @template T
195
+ * @param {ResponseTransformer<T>} [fn] - Function to transform the response
196
+ * @returns {Promise<T>} Response data
197
+ */
198
+ put<T>(fn?: ResponseTransformer<T>): Promise<T>;
199
+ /**
200
+ * Perform a DELETE request
201
+ * @template T
202
+ * @param {ResponseTransformer<T>} [fn] - Function to transform the response
203
+ * @returns {Promise<T>} Response data
204
+ */
205
+ del<T>(fn?: ResponseTransformer<T>): Promise<T>;
206
+ /**
207
+ * Set the API endpoint
208
+ * @param {string} endpoint - API endpoint
209
+ * @returns {this} Current instance
210
+ */
211
+ endpoint(endpoint: string): this;
212
+ /**
213
+ * Set query parameters
214
+ * @param {Record<string, any>} query - Query parameters
215
+ * @returns {this} Current instance
216
+ */
217
+ query(query: Record<string, any>): this;
218
+ /**
219
+ * Set request payload
220
+ * @param {any} payload - Request payload
221
+ * @returns {this} Current instance
222
+ */
223
+ payload(payload: any): this;
224
+ /**
225
+ * Register a default error handler
226
+ * @param {ErrorHandler} fn - Error handler function
227
+ * @returns {this} Current instance
228
+ */
229
+ default(fn: ErrorHandler): this;
230
+ /**
231
+ * Register a handler for AccessDenied (401) errors
232
+ * @param {ErrorHandler} fn - Error handler function
233
+ * @returns {this} Current instance
234
+ */
235
+ accessDenied(fn: ErrorHandler): this;
236
+ /**
237
+ * Register a handler for PaymentRequired (402) errors
238
+ * @param {ErrorHandler} fn - Error handler function
239
+ * @returns {this} Current instance
240
+ */
241
+ paymentRequired(fn: ErrorHandler): this;
242
+ /**
243
+ * Register a handler for Forbidden (403) errors
244
+ * @param {ErrorHandler} fn - Error handler function
245
+ * @returns {this} Current instance
246
+ */
247
+ forbidden(fn: ErrorHandler): this;
248
+ /**
249
+ * Register a handler for NotFound (404) errors
250
+ * @param {ErrorHandler} fn - Error handler function
251
+ * @returns {this} Current instance
252
+ */
253
+ notFound(fn: ErrorHandler): this;
254
+ /**
255
+ * Register a handler for NotAcceptable (406) errors
256
+ * @param {ErrorHandler} fn - Error handler function
257
+ * @returns {this} Current instance
258
+ */
259
+ notAcceptable(fn: ErrorHandler): this;
260
+ /**
261
+ * Register a handler for Conflict (409) errors
262
+ * @param {ErrorHandler} fn - Error handler function
263
+ * @returns {this} Current instance
264
+ */
265
+ conflict(fn: ErrorHandler): this;
266
+ /**
267
+ * Register a handler for Gone (410) errors
268
+ * @param {ErrorHandler} fn - Error handler function
269
+ * @returns {this} Current instance
270
+ */
271
+ gone(fn: ErrorHandler): this;
272
+ /**
273
+ * Register a handler for PreconditionFailed (412) errors
274
+ * @param {ErrorHandler} fn - Error handler function
275
+ * @returns {this} Current instance
276
+ */
277
+ preconditionFailed(fn: ErrorHandler): this;
278
+ /**
279
+ * Register a handler for ExpectationFailed (417) errors
280
+ * @param {ErrorHandler} fn - Error handler function
281
+ * @returns {this} Current instance
282
+ */
283
+ expectationFailed(fn: ErrorHandler): this;
284
+ /**
285
+ * Register a handler for BadData (422) errors
286
+ * @param {ErrorHandler} fn - Error handler function
287
+ * @returns {this} Current instance
288
+ */
289
+ badData(fn: ErrorHandler): this;
290
+ /**
291
+ * Register a handler for TooManyRequests (429) errors
292
+ * @param {ErrorHandler} fn - Error handler function
293
+ * @returns {this} Current instance
294
+ */
295
+ tooManyRequests(fn: ErrorHandler): this;
296
+ #private;
297
+ }
298
+ /**
299
+ * Create a new API client instance
300
+ * @returns {Api} API client instance
301
+ * @throws {Error} If API client is not configured
302
+ */
303
+ declare function create(): Api;
304
+ /**
305
+ * Configure the API client
306
+ * @param {ApiOptions} options - API configuration options
307
+ */
308
+ declare function configure(options: ApiOptions): void;
309
+
310
+ export { Api, type Config, HttpError, _default as default };