@cloudcommerce/api
Advanced tools
Comparing version 0.0.5 to 0.0.6
@@ -7,3 +7,3 @@ import type { Endpoint, Config, ResponseBody } from './types'; | ||
declare const api: { | ||
<T extends Config>(config: T): Promise<Response & { | ||
<T extends Config>(config: T, retries?: number): Promise<Response & { | ||
config: Config; | ||
@@ -10,0 +10,0 @@ data: ResponseBody<T>; |
@@ -32,5 +32,7 @@ // @ts-ignore | ||
}; | ||
const api = async (config) => { | ||
const api = async (config, retries = 0) => { | ||
const url = def.middleware(config); | ||
const { method, headers, timeout = 20000 } = config; | ||
const { | ||
method, headers, timeout = 20000, maxRetries = 3, | ||
} = config; | ||
const abortController = new AbortController(); | ||
@@ -51,6 +53,15 @@ const timer = setTimeout(() => abortController.abort(), timeout); | ||
} | ||
const { status } = response; | ||
if (maxRetries < retries && (status === 429 || status >= 500)) { | ||
const retryAfter = response.headers.get('retry-after'); | ||
return new Promise((resolve, reject) => { | ||
setTimeout(() => { | ||
api(config, retries + 1).then(resolve).catch(reject); | ||
}, (retryAfter && parseInt(retryAfter, 10)) || 5000); | ||
}); | ||
} | ||
const error = new Error(response.statusText); | ||
error.config = config; | ||
error.response = response; | ||
error.statusCode = response.status; | ||
error.statusCode = status; | ||
throw error; | ||
@@ -57,0 +68,0 @@ }; |
@@ -27,2 +27,3 @@ import type { Products } from './types/products'; | ||
timeout?: number; | ||
maxRetries?: number; | ||
}; | ||
@@ -29,0 +30,0 @@ declare type ReadConfig<endpoint> = Config & { |
{ | ||
"name": "@cloudcommerce/api", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"description": "E-Com Plus Cloud Commerce APIs client/adapter", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -37,3 +37,3 @@ import type { Endpoint, Config, ResponseBody } from './types'; | ||
const api = async <T extends Config>(config: T): Promise<Response & { | ||
const api = async <T extends Config>(config: T, retries = 0): Promise<Response & { | ||
config: Config, | ||
@@ -43,3 +43,8 @@ data: ResponseBody<T>, | ||
const url = def.middleware(config); | ||
const { method, headers, timeout = 20000 } = config; | ||
const { | ||
method, | ||
headers, | ||
timeout = 20000, | ||
maxRetries = 3, | ||
} = config; | ||
const abortController = new AbortController(); | ||
@@ -60,6 +65,15 @@ const timer = setTimeout(() => abortController.abort(), timeout); | ||
} | ||
const { status } = response; | ||
if (maxRetries < retries && (status === 429 || status >= 500)) { | ||
const retryAfter = response.headers.get('retry-after'); | ||
return new Promise((resolve, reject) => { | ||
setTimeout(() => { | ||
api(config, retries + 1).then(resolve).catch(reject); | ||
}, (retryAfter && parseInt(retryAfter, 10)) || 5000); | ||
}); | ||
} | ||
const error: any = new Error(response.statusText); | ||
error.config = config; | ||
error.response = response; | ||
error.statusCode = response.status; | ||
error.statusCode = status; | ||
throw error; | ||
@@ -66,0 +80,0 @@ }; |
@@ -40,2 +40,3 @@ import type { Products } from './types/products'; | ||
timeout?: number, | ||
maxRetries?: number, | ||
}; | ||
@@ -42,0 +43,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
239601
9934
2