@beyonk/http 12.1.1 → 12.1.2

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