✨ Improve code readability and formatting in API client utility
This commit is contained in:
@@ -1,13 +1,13 @@
|
|||||||
/**
|
/**
|
||||||
* API Client Utility
|
* API Client Utility
|
||||||
*
|
*
|
||||||
* Centralized fetch wrapper with:
|
* Centralized fetch wrapper with:
|
||||||
* - Automatic error handling
|
* - Automatic error handling
|
||||||
* - Retry logic with exponential backoff
|
* - Retry logic with exponential backoff
|
||||||
* - Request/response interceptors
|
* - Request/response interceptors
|
||||||
* - Request deduplication
|
* - Request deduplication
|
||||||
* - Timeout support
|
* - Timeout support
|
||||||
*
|
*
|
||||||
* @example
|
* @example
|
||||||
* const client = createApiClient({ baseURL: '/api' });
|
* const client = createApiClient({ baseURL: '/api' });
|
||||||
* const data = await client.get('/users');
|
* const data = await client.get('/users');
|
||||||
@@ -66,10 +66,7 @@ export function createApiClient(config = {}) {
|
|||||||
* Make the actual HTTP request with retries
|
* Make the actual HTTP request with retries
|
||||||
*/
|
*/
|
||||||
async function makeRequest(url, options) {
|
async function makeRequest(url, options) {
|
||||||
const {
|
const { retries = maxRetries, ...fetchOptions } = options;
|
||||||
retries = maxRetries,
|
|
||||||
...fetchOptions
|
|
||||||
} = options;
|
|
||||||
|
|
||||||
// Merge headers
|
// Merge headers
|
||||||
const headers = {
|
const headers = {
|
||||||
@@ -88,9 +85,14 @@ export function createApiClient(config = {}) {
|
|||||||
while (attempt <= retries) {
|
while (attempt <= retries) {
|
||||||
try {
|
try {
|
||||||
// Call request interceptor
|
// Call request interceptor
|
||||||
let requestOptions = { ...fetchOptions, headers, signal: controller.signal };
|
let requestOptions = {
|
||||||
|
...fetchOptions,
|
||||||
|
headers,
|
||||||
|
signal: controller.signal
|
||||||
|
};
|
||||||
if (onRequest) {
|
if (onRequest) {
|
||||||
requestOptions = await onRequest(url, requestOptions) || requestOptions;
|
requestOptions =
|
||||||
|
(await onRequest(url, requestOptions)) || requestOptions;
|
||||||
}
|
}
|
||||||
|
|
||||||
const response = await fetch(url, requestOptions);
|
const response = await fetch(url, requestOptions);
|
||||||
@@ -99,15 +101,17 @@ export function createApiClient(config = {}) {
|
|||||||
// Call response interceptor
|
// Call response interceptor
|
||||||
let processedResponse = response;
|
let processedResponse = response;
|
||||||
if (onResponse) {
|
if (onResponse) {
|
||||||
processedResponse = await onResponse(response.clone()) || response;
|
processedResponse = (await onResponse(response.clone())) || response;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle HTTP errors
|
// Handle HTTP errors
|
||||||
if (!processedResponse.ok) {
|
if (!processedResponse.ok) {
|
||||||
const error = new Error(`HTTP ${processedResponse.status}: ${processedResponse.statusText}`);
|
const error = new Error(
|
||||||
|
`HTTP ${processedResponse.status}: ${processedResponse.statusText}`
|
||||||
|
);
|
||||||
error.status = processedResponse.status;
|
error.status = processedResponse.status;
|
||||||
error.response = processedResponse;
|
error.response = processedResponse;
|
||||||
|
|
||||||
// Try to parse error body
|
// Try to parse error body
|
||||||
try {
|
try {
|
||||||
const contentType = processedResponse.headers.get('content-type');
|
const contentType = processedResponse.headers.get('content-type');
|
||||||
@@ -129,7 +133,6 @@ export function createApiClient(config = {}) {
|
|||||||
return await processedResponse.json();
|
return await processedResponse.json();
|
||||||
}
|
}
|
||||||
return await processedResponse.text();
|
return await processedResponse.text();
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
clearTimeout(timeoutId);
|
clearTimeout(timeoutId);
|
||||||
lastError = error;
|
lastError = error;
|
||||||
@@ -150,7 +153,9 @@ export function createApiClient(config = {}) {
|
|||||||
|
|
||||||
// If more retries remaining, wait before retrying
|
// If more retries remaining, wait before retrying
|
||||||
if (attempt <= retries) {
|
if (attempt <= retries) {
|
||||||
await new Promise(resolve => setTimeout(resolve, retryDelay * attempt));
|
await new Promise(resolve =>
|
||||||
|
setTimeout(resolve, retryDelay * attempt)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -167,22 +172,26 @@ export function createApiClient(config = {}) {
|
|||||||
return {
|
return {
|
||||||
request,
|
request,
|
||||||
get: (url, options = {}) => request(url, { ...options, method: 'GET' }),
|
get: (url, options = {}) => request(url, { ...options, method: 'GET' }),
|
||||||
post: (url, data, options = {}) => request(url, {
|
post: (url, data, options = {}) =>
|
||||||
...options,
|
request(url, {
|
||||||
method: 'POST',
|
...options,
|
||||||
body: JSON.stringify(data)
|
method: 'POST',
|
||||||
}),
|
body: JSON.stringify(data)
|
||||||
put: (url, data, options = {}) => request(url, {
|
}),
|
||||||
...options,
|
put: (url, data, options = {}) =>
|
||||||
method: 'PUT',
|
request(url, {
|
||||||
body: JSON.stringify(data)
|
...options,
|
||||||
}),
|
method: 'PUT',
|
||||||
patch: (url, data, options = {}) => request(url, {
|
body: JSON.stringify(data)
|
||||||
...options,
|
}),
|
||||||
method: 'PATCH',
|
patch: (url, data, options = {}) =>
|
||||||
body: JSON.stringify(data)
|
request(url, {
|
||||||
}),
|
...options,
|
||||||
delete: (url, options = {}) => request(url, { ...options, method: 'DELETE' })
|
method: 'PATCH',
|
||||||
|
body: JSON.stringify(data)
|
||||||
|
}),
|
||||||
|
delete: (url, options = {}) =>
|
||||||
|
request(url, { ...options, method: 'DELETE' })
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user