@cloudcommerce/api
Advanced tools
Comparing version 0.0.2 to 0.0.3
@@ -6,11 +6,11 @@ // @ts-ignore | ||
const def = { | ||
middleware(options) { | ||
let url = options.baseUrl || env.API_BASE_URL || 'https://ecomplus.io/v2'; | ||
middleware(config) { | ||
let url = config.baseUrl || env.API_BASE_URL || 'https://ecomplus.io/v2'; | ||
if (!url) { | ||
const storeId = options.storeId || env.ECOM_STORE_ID; | ||
const storeId = config.storeId || env.ECOM_STORE_ID; | ||
if (!storeId) { | ||
throw new Error('`storeId` must be set in options or `ECOM_STORE_ID` env var'); | ||
throw new Error('`storeId` must be set in config or `ECOM_STORE_ID` env var'); | ||
} | ||
url += `/:${storeId}`; | ||
const lang = options.lang || env.ECOM_LANG; | ||
const lang = config.lang || env.ECOM_LANG; | ||
if (lang) { | ||
@@ -20,12 +20,12 @@ url += `,lang:${lang}`; | ||
} | ||
if (options.params) { | ||
if (typeof options.params === 'string') { | ||
url += `?${options.params}`; | ||
if (config.params) { | ||
if (typeof config.params === 'string') { | ||
url += `?${config.params}`; | ||
} | ||
else { | ||
// https://github.com/microsoft/TypeScript/issues/32951 | ||
url += `?${new URLSearchParams(options.params)}`; | ||
url += `?${new URLSearchParams(config.params)}`; | ||
} | ||
} | ||
return `${url}/${options.endpoint}`; | ||
return `${url}/${config.endpoint}`; | ||
}, | ||
@@ -37,29 +37,47 @@ }; | ||
}; | ||
const callApi = (options) => { | ||
const url = def.middleware(options); | ||
const { method, headers } = options; | ||
return fetch(url, { method, headers }); | ||
const callApi = async (config) => { | ||
const url = def.middleware(config); | ||
const { method, headers, timeout = 20000 } = config; | ||
const abortController = new AbortController(); | ||
const timer = setTimeout(() => abortController.abort(), timeout); | ||
const response = await fetch(url, { | ||
method, | ||
headers, | ||
signal: abortController.signal, | ||
}); | ||
clearTimeout(timer); | ||
if (response.ok) { | ||
return { | ||
...response, | ||
config, | ||
data: await response.json(), | ||
}; | ||
} | ||
const error = new Error(response.statusText); | ||
error.config = config; | ||
error.response = response; | ||
throw error; | ||
}; | ||
const get = (endpoint, options) => callApi({ | ||
...options, | ||
const get = (endpoint, config) => callApi({ | ||
...config, | ||
method: 'get', | ||
endpoint, | ||
}); | ||
const post = (endpoint, options) => callApi({ | ||
...options, | ||
const post = (endpoint, config) => callApi({ | ||
...config, | ||
method: 'post', | ||
endpoint, | ||
}); | ||
const put = (endpoint, options) => callApi({ | ||
...options, | ||
const put = (endpoint, config) => callApi({ | ||
...config, | ||
method: 'put', | ||
endpoint, | ||
}); | ||
const patch = (endpoint, options) => callApi({ | ||
...options, | ||
const patch = (endpoint, config) => callApi({ | ||
...config, | ||
method: 'patch', | ||
endpoint, | ||
}); | ||
const del = (endpoint, options) => callApi({ | ||
...options, | ||
const del = (endpoint, config) => callApi({ | ||
...config, | ||
method: 'delete', | ||
@@ -66,0 +84,0 @@ endpoint, |
{ | ||
"name": "@cloudcommerce/api", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "E-Com Plus Cloud Commerce APIs client/adapter", | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"repository": { | ||
@@ -18,4 +19,5 @@ "type": "git", | ||
"scripts": { | ||
"build": "tsc --build" | ||
"build": "zx scripts/build.mjs", | ||
"test": "tsc --noEmit --noImplicitAny" | ||
} | ||
} |
101
src/index.ts
@@ -1,26 +0,3 @@ | ||
type Resource = 'products' | ||
| 'categories' | ||
| 'brands' | ||
| 'collections' | ||
| 'grids' | ||
| 'carts' | ||
| 'orders' | ||
| 'customers' | ||
| 'stores' | ||
| 'applications'; | ||
import type { Endpoint, Config, ResponseBody } from './types'; | ||
type Endpoint = Resource | `${Resource}/${string}`; | ||
type Method = 'get' | 'post' | 'put' | 'patch' | 'delete'; | ||
type ReqOptions = { | ||
baseUrl?: string, | ||
storeId?: number, | ||
lang?: string, | ||
method: Method, | ||
endpoint: Endpoint, | ||
params?: Record<string, string | number>, | ||
headers?: Record<string, string>, | ||
}; | ||
// @ts-ignore | ||
@@ -32,11 +9,11 @@ const env: { [key: string]: string } = (typeof window === 'object' && window) | ||
const def = { | ||
middleware(options: ReqOptions) { | ||
let url = options.baseUrl || env.API_BASE_URL || 'https://ecomplus.io/v2'; | ||
middleware(config: Config) { | ||
let url = config.baseUrl || env.API_BASE_URL || 'https://ecomplus.io/v2'; | ||
if (!url) { | ||
const storeId = options.storeId || env.ECOM_STORE_ID; | ||
const storeId = config.storeId || env.ECOM_STORE_ID; | ||
if (!storeId) { | ||
throw new Error('`storeId` must be set in options or `ECOM_STORE_ID` env var'); | ||
throw new Error('`storeId` must be set in config or `ECOM_STORE_ID` env var'); | ||
} | ||
url += `/:${storeId}`; | ||
const lang = options.lang || env.ECOM_LANG; | ||
const lang = config.lang || env.ECOM_LANG; | ||
if (lang) { | ||
@@ -46,11 +23,11 @@ url += `,lang:${lang}`; | ||
} | ||
if (options.params) { | ||
if (typeof options.params === 'string') { | ||
url += `?${options.params}`; | ||
if (config.params) { | ||
if (typeof config.params === 'string') { | ||
url += `?${config.params}`; | ||
} else { | ||
// https://github.com/microsoft/TypeScript/issues/32951 | ||
url += `?${new URLSearchParams(options.params as Record<string, string>)}`; | ||
url += `?${new URLSearchParams(config.params as Record<string, string>)}`; | ||
} | ||
} | ||
return `${url}/${options.endpoint}`; | ||
return `${url}/${config.endpoint}`; | ||
}, | ||
@@ -64,10 +41,31 @@ }; | ||
const callApi = (options: ReqOptions) => { | ||
const url = def.middleware(options); | ||
const { method, headers } = options; | ||
return fetch(url, { method, headers }); | ||
const callApi = async <T extends Config>(config: T): Promise<Response & { | ||
config: Config, | ||
data: ResponseBody<T>, | ||
}> => { | ||
const url = def.middleware(config); | ||
const { method, headers, timeout = 20000 } = config; | ||
const abortController = new AbortController(); | ||
const timer = setTimeout(() => abortController.abort(), timeout); | ||
const response = await fetch(url, { | ||
method, | ||
headers, | ||
signal: abortController.signal, | ||
}); | ||
clearTimeout(timer); | ||
if (response.ok) { | ||
return { | ||
...response, | ||
config, | ||
data: await response.json(), | ||
}; | ||
} | ||
const error: any = new Error(response.statusText); | ||
error.config = config; | ||
error.response = response; | ||
throw error; | ||
}; | ||
const get = (endpoint: Endpoint, options: Exclude<ReqOptions, 'method'>) => callApi({ | ||
...options, | ||
const get = (endpoint: Endpoint, config: Exclude<Config, 'method'>) => callApi({ | ||
...config, | ||
method: 'get', | ||
@@ -77,4 +75,4 @@ endpoint, | ||
const post = (endpoint: Endpoint, options: Exclude<ReqOptions, 'method'>) => callApi({ | ||
...options, | ||
const post = (endpoint: Endpoint, config: Exclude<Config, 'method'>) => callApi({ | ||
...config, | ||
method: 'post', | ||
@@ -84,4 +82,4 @@ endpoint, | ||
const put = (endpoint: Endpoint, options: Exclude<ReqOptions, 'method'>) => callApi({ | ||
...options, | ||
const put = (endpoint: Endpoint, config: Exclude<Config, 'method'>) => callApi({ | ||
...config, | ||
method: 'put', | ||
@@ -91,4 +89,4 @@ endpoint, | ||
const patch = (endpoint: Endpoint, options: Exclude<ReqOptions, 'method'>) => callApi({ | ||
...options, | ||
const patch = (endpoint: Endpoint, config: Exclude<Config, 'method'>) => callApi({ | ||
...config, | ||
method: 'patch', | ||
@@ -98,4 +96,4 @@ endpoint, | ||
const del = (endpoint: Endpoint, options: Exclude<ReqOptions, 'method'>) => callApi({ | ||
...options, | ||
const del = (endpoint: Endpoint, config: Exclude<Config, 'method'>) => callApi({ | ||
...config, | ||
method: 'delete', | ||
@@ -122,8 +120,1 @@ endpoint, | ||
}; | ||
export type { | ||
Resource, | ||
Endpoint, | ||
Method, | ||
ReqOptions, | ||
}; |
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
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
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
244617
41
10257
2