@rechargeapps/storefront-client 0.0.14 → 0.0.16
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.
- package/README.md +2 -2
- package/dist/cjs/api/bundle.js +22 -43
- package/dist/cjs/api/bundle.js.map +1 -1
- package/dist/cjs/api/cdn.js +21 -41
- package/dist/cjs/api/cdn.js.map +1 -1
- package/dist/cjs/utils/request.js +30 -52
- package/dist/cjs/utils/request.js.map +1 -1
- package/dist/esm/api/bundle.js +22 -43
- package/dist/esm/api/bundle.js.map +1 -1
- package/dist/esm/api/cdn.js +22 -42
- package/dist/esm/api/cdn.js.map +1 -1
- package/dist/esm/utils/request.js +30 -52
- package/dist/esm/utils/request.js.map +1 -1
- package/dist/umd/recharge-storefront-client.min.js +8 -8
- package/dist/umd/recharge-storefront-client.min.js.map +1 -1
- package/package.json +23 -9
package/dist/esm/api/cdn.js
CHANGED
|
@@ -1,28 +1,8 @@
|
|
|
1
1
|
import { RECHARGE_CDN_URL } from '../constants/api.js';
|
|
2
|
-
import { widgetSettingsMapper, productArrayMapper
|
|
2
|
+
import { productMapper, widgetSettingsMapper, productArrayMapper } from '../mappers/cdn.js';
|
|
3
3
|
import { request } from '../utils/request.js';
|
|
4
4
|
import { getOptions } from '../utils/options.js';
|
|
5
5
|
|
|
6
|
-
var __async = (__this, __arguments, generator) => {
|
|
7
|
-
return new Promise((resolve, reject) => {
|
|
8
|
-
var fulfilled = (value) => {
|
|
9
|
-
try {
|
|
10
|
-
step(generator.next(value));
|
|
11
|
-
} catch (e) {
|
|
12
|
-
reject(e);
|
|
13
|
-
}
|
|
14
|
-
};
|
|
15
|
-
var rejected = (value) => {
|
|
16
|
-
try {
|
|
17
|
-
step(generator.throw(value));
|
|
18
|
-
} catch (e) {
|
|
19
|
-
reject(e);
|
|
20
|
-
}
|
|
21
|
-
};
|
|
22
|
-
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
23
|
-
step((generator = generator.apply(__this, __arguments)).next());
|
|
24
|
-
});
|
|
25
|
-
};
|
|
26
6
|
const CDN_VERSION = "2020-12";
|
|
27
7
|
const DEFAULT_STORE_SETTINGS = {
|
|
28
8
|
store_currency: {
|
|
@@ -40,27 +20,27 @@ function cacheRequest(cacheKey, request2) {
|
|
|
40
20
|
}
|
|
41
21
|
return promiseCache.get(cacheKey);
|
|
42
22
|
}
|
|
43
|
-
const getProduct = (externalProductId) =>
|
|
23
|
+
const getProduct = async (externalProductId) => {
|
|
44
24
|
const opts = getOptions();
|
|
45
|
-
const { product } =
|
|
25
|
+
const { product } = await cacheRequest(`product.${externalProductId}`, () => request("get", `${RECHARGE_CDN_URL(opts.environment)}/store/${opts.storeIdentifier}/product/${CDN_VERSION}/${externalProductId}.json`));
|
|
46
26
|
return productMapper(product);
|
|
47
|
-
}
|
|
48
|
-
const getStoreSettings = () =>
|
|
27
|
+
};
|
|
28
|
+
const getStoreSettings = async () => {
|
|
49
29
|
const opts = getOptions();
|
|
50
|
-
const storeSettings =
|
|
30
|
+
const storeSettings = await cacheRequest("storeSettings", () => request("get", `${RECHARGE_CDN_URL(opts.environment)}/store/${opts.storeIdentifier}/${CDN_VERSION}/store_settings.json`).catch(() => {
|
|
51
31
|
return DEFAULT_STORE_SETTINGS;
|
|
52
32
|
}));
|
|
53
33
|
return storeSettings;
|
|
54
|
-
}
|
|
55
|
-
const getWidgetSettings = () =>
|
|
34
|
+
};
|
|
35
|
+
const getWidgetSettings = async () => {
|
|
56
36
|
const opts = getOptions();
|
|
57
|
-
const { widget_settings } =
|
|
37
|
+
const { widget_settings } = await cacheRequest("widgetSettings", () => request("get", `${RECHARGE_CDN_URL(opts.environment)}/store/${opts.storeIdentifier}/${CDN_VERSION}/widget_settings.json`));
|
|
58
38
|
const widgetSettings = widgetSettingsMapper(widget_settings);
|
|
59
39
|
return widgetSettings;
|
|
60
|
-
}
|
|
61
|
-
const getProductsAndSettings = () =>
|
|
40
|
+
};
|
|
41
|
+
const getProductsAndSettings = async () => {
|
|
62
42
|
const opts = getOptions();
|
|
63
|
-
const { products, widget_settings, store_settings, meta } =
|
|
43
|
+
const { products, widget_settings, store_settings, meta } = await cacheRequest("productsAndSettings", () => request("get", `${RECHARGE_CDN_URL(opts.environment)}/store/${opts.storeIdentifier}/product/${CDN_VERSION}/products.json`));
|
|
64
44
|
if ((meta == null ? void 0 : meta.status) === "error") {
|
|
65
45
|
return Promise.reject(meta.message);
|
|
66
46
|
}
|
|
@@ -69,13 +49,13 @@ const getProductsAndSettings = () => __async(void 0, null, function* () {
|
|
|
69
49
|
widget_settings: widgetSettingsMapper(widget_settings),
|
|
70
50
|
store_settings
|
|
71
51
|
};
|
|
72
|
-
}
|
|
73
|
-
const getProducts = () =>
|
|
74
|
-
const { products } =
|
|
52
|
+
};
|
|
53
|
+
const getProducts = async () => {
|
|
54
|
+
const { products } = await getProductsAndSettings();
|
|
75
55
|
return products;
|
|
76
|
-
}
|
|
77
|
-
const getProductAndSettings = (externalProductId) =>
|
|
78
|
-
const [product, store_settings, widget_settings] =
|
|
56
|
+
};
|
|
57
|
+
const getProductAndSettings = async (externalProductId) => {
|
|
58
|
+
const [product, store_settings, widget_settings] = await Promise.all([
|
|
79
59
|
getProduct(externalProductId),
|
|
80
60
|
getStoreSettings(),
|
|
81
61
|
getWidgetSettings()
|
|
@@ -87,11 +67,11 @@ const getProductAndSettings = (externalProductId) => __async(void 0, null, funct
|
|
|
87
67
|
storeSettings: store_settings,
|
|
88
68
|
widgetSettings: widget_settings
|
|
89
69
|
};
|
|
90
|
-
}
|
|
91
|
-
const getBundleSettings = (externalProductId) =>
|
|
92
|
-
const { bundle_product } =
|
|
70
|
+
};
|
|
71
|
+
const getBundleSettings = async (externalProductId) => {
|
|
72
|
+
const { bundle_product } = await getProduct(externalProductId);
|
|
93
73
|
return bundle_product;
|
|
94
|
-
}
|
|
74
|
+
};
|
|
95
75
|
const resetCache = () => Array.from(promiseCache.keys()).forEach((key) => promiseCache.delete(key));
|
|
96
76
|
|
|
97
77
|
export { getBundleSettings, getProduct, getProductAndSettings, getProducts, getProductsAndSettings, getStoreSettings, getWidgetSettings, resetCache };
|
package/dist/esm/api/cdn.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cdn.js","sources":["../../../src/api/cdn.ts"],"sourcesContent":["import { RECHARGE_CDN_URL } from '../constants/api';\nimport {\n CDNWidgetSettings,\n CDNStoreSettings,\n CDNWidgetSettingsResource,\n CDNProductsAndSettings,\n CDNProductResource,\n CDNProduct,\n CDNProductAndSettings,\n CDNProductsAndSettingsResource,\n CDNBundleSettings,\n CDNProductKeyObject,\n} from '../types';\nimport { productArrayMapper, productMapper, widgetSettingsMapper } from '../mappers/cdn';\nimport { request } from '../utils/request';\nimport { getOptions } from '../utils/options';\n\nconst CDN_VERSION = '2020-12';\n\n// Default store settings if none are provided from the cdn\nconst DEFAULT_STORE_SETTINGS: CDNStoreSettings = {\n store_currency: {\n currency_code: 'USD',\n currency_symbol: '$',\n decimal_separator: '.',\n thousands_separator: ',',\n currency_symbol_location: 'left',\n },\n};\n\nconst promiseCache = new Map();\n\n// This will cache the request and use the same promise anywhere we call this function. This will never make multiple calls and always return the first request\nfunction cacheRequest<T>(cacheKey: string, request: () => T): T {\n if (!promiseCache.has(cacheKey)) {\n promiseCache.set(cacheKey, request());\n }\n return promiseCache.get(cacheKey);\n}\n\nexport const getProduct = async (externalProductId: string | number): Promise<CDNProduct> => {\n const opts = getOptions();\n const { product } = await cacheRequest(`product.${externalProductId}`, () =>\n request<CDNProductResource>(\n 'get',\n `${RECHARGE_CDN_URL(opts.environment)}/store/${\n opts.storeIdentifier\n }/product/${CDN_VERSION}/${externalProductId}.json`\n )\n );\n\n return productMapper(product);\n};\n\nexport const getStoreSettings = async (): Promise<CDNStoreSettings> => {\n const opts = getOptions();\n const storeSettings = await cacheRequest('storeSettings', () =>\n request<CDNStoreSettings>(\n 'get',\n `${RECHARGE_CDN_URL(opts.environment)}/store/${opts.storeIdentifier}/${CDN_VERSION}/store_settings.json`\n ).catch(() => {\n return DEFAULT_STORE_SETTINGS;\n })\n );\n return storeSettings;\n};\n\nexport const getWidgetSettings = async (): Promise<CDNWidgetSettings> => {\n const opts = getOptions();\n const { widget_settings } = await cacheRequest('widgetSettings', () =>\n request<CDNWidgetSettingsResource>(\n 'get',\n `${RECHARGE_CDN_URL(opts.environment)}/store/${opts.storeIdentifier}/${CDN_VERSION}/widget_settings.json`\n )\n );\n\n const widgetSettings = widgetSettingsMapper(widget_settings);\n\n return widgetSettings;\n};\n\nexport const getProductsAndSettings = async (): Promise<CDNProductsAndSettings> => {\n const opts = getOptions();\n const { products, widget_settings, store_settings, meta } = await cacheRequest('productsAndSettings', () =>\n request<CDNProductsAndSettingsResource>(\n 'get',\n `${RECHARGE_CDN_URL(opts.environment)}/store/${opts.storeIdentifier}/product/${CDN_VERSION}/products.json`\n )\n );\n\n if (meta?.status === 'error') {\n return Promise.reject(meta.message);\n }\n\n return {\n products: productArrayMapper(products),\n widget_settings: widgetSettingsMapper(widget_settings),\n store_settings,\n };\n};\n\nexport const getProducts = async (): Promise<CDNProductKeyObject[]> => {\n const { products } = await getProductsAndSettings();\n return products;\n};\n\nexport const getProductAndSettings = async (externalProductId: string | number): Promise<CDNProductAndSettings> => {\n const [product, store_settings, widget_settings] = await Promise.all([\n getProduct(externalProductId),\n getStoreSettings(),\n getWidgetSettings(),\n ]);\n\n return {\n product,\n store_settings,\n widget_settings,\n storeSettings: store_settings,\n widgetSettings: widget_settings,\n };\n};\n\nexport const getBundleSettings = async (\n externalProductId: string | number\n): Promise<CDNBundleSettings | null | undefined> => {\n const { bundle_product } = await getProduct(externalProductId);\n\n return bundle_product;\n};\n\nexport const resetCache = () => Array.from(promiseCache.keys()).forEach(key => promiseCache.delete(key));\n"],"names":[],"mappings":";;;;;
|
|
1
|
+
{"version":3,"file":"cdn.js","sources":["../../../src/api/cdn.ts"],"sourcesContent":["import { RECHARGE_CDN_URL } from '../constants/api';\nimport {\n CDNWidgetSettings,\n CDNStoreSettings,\n CDNWidgetSettingsResource,\n CDNProductsAndSettings,\n CDNProductResource,\n CDNProduct,\n CDNProductAndSettings,\n CDNProductsAndSettingsResource,\n CDNBundleSettings,\n CDNProductKeyObject,\n} from '../types';\nimport { productArrayMapper, productMapper, widgetSettingsMapper } from '../mappers/cdn';\nimport { request } from '../utils/request';\nimport { getOptions } from '../utils/options';\n\nconst CDN_VERSION = '2020-12';\n\n// Default store settings if none are provided from the cdn\nconst DEFAULT_STORE_SETTINGS: CDNStoreSettings = {\n store_currency: {\n currency_code: 'USD',\n currency_symbol: '$',\n decimal_separator: '.',\n thousands_separator: ',',\n currency_symbol_location: 'left',\n },\n};\n\nconst promiseCache = new Map();\n\n// This will cache the request and use the same promise anywhere we call this function. This will never make multiple calls and always return the first request\nfunction cacheRequest<T>(cacheKey: string, request: () => T): T {\n if (!promiseCache.has(cacheKey)) {\n promiseCache.set(cacheKey, request());\n }\n return promiseCache.get(cacheKey);\n}\n\nexport const getProduct = async (externalProductId: string | number): Promise<CDNProduct> => {\n const opts = getOptions();\n const { product } = await cacheRequest(`product.${externalProductId}`, () =>\n request<CDNProductResource>(\n 'get',\n `${RECHARGE_CDN_URL(opts.environment)}/store/${\n opts.storeIdentifier\n }/product/${CDN_VERSION}/${externalProductId}.json`\n )\n );\n\n return productMapper(product);\n};\n\nexport const getStoreSettings = async (): Promise<CDNStoreSettings> => {\n const opts = getOptions();\n const storeSettings = await cacheRequest('storeSettings', () =>\n request<CDNStoreSettings>(\n 'get',\n `${RECHARGE_CDN_URL(opts.environment)}/store/${opts.storeIdentifier}/${CDN_VERSION}/store_settings.json`\n ).catch(() => {\n return DEFAULT_STORE_SETTINGS;\n })\n );\n return storeSettings;\n};\n\nexport const getWidgetSettings = async (): Promise<CDNWidgetSettings> => {\n const opts = getOptions();\n const { widget_settings } = await cacheRequest('widgetSettings', () =>\n request<CDNWidgetSettingsResource>(\n 'get',\n `${RECHARGE_CDN_URL(opts.environment)}/store/${opts.storeIdentifier}/${CDN_VERSION}/widget_settings.json`\n )\n );\n\n const widgetSettings = widgetSettingsMapper(widget_settings);\n\n return widgetSettings;\n};\n\nexport const getProductsAndSettings = async (): Promise<CDNProductsAndSettings> => {\n const opts = getOptions();\n const { products, widget_settings, store_settings, meta } = await cacheRequest('productsAndSettings', () =>\n request<CDNProductsAndSettingsResource>(\n 'get',\n `${RECHARGE_CDN_URL(opts.environment)}/store/${opts.storeIdentifier}/product/${CDN_VERSION}/products.json`\n )\n );\n\n if (meta?.status === 'error') {\n return Promise.reject(meta.message);\n }\n\n return {\n products: productArrayMapper(products),\n widget_settings: widgetSettingsMapper(widget_settings),\n store_settings,\n };\n};\n\nexport const getProducts = async (): Promise<CDNProductKeyObject[]> => {\n const { products } = await getProductsAndSettings();\n return products;\n};\n\nexport const getProductAndSettings = async (externalProductId: string | number): Promise<CDNProductAndSettings> => {\n const [product, store_settings, widget_settings] = await Promise.all([\n getProduct(externalProductId),\n getStoreSettings(),\n getWidgetSettings(),\n ]);\n\n return {\n product,\n store_settings,\n widget_settings,\n storeSettings: store_settings,\n widgetSettings: widget_settings,\n };\n};\n\nexport const getBundleSettings = async (\n externalProductId: string | number\n): Promise<CDNBundleSettings | null | undefined> => {\n const { bundle_product } = await getProduct(externalProductId);\n\n return bundle_product;\n};\n\nexport const resetCache = () => Array.from(promiseCache.keys()).forEach(key => promiseCache.delete(key));\n"],"names":[],"mappings":";;;;;AAIA,MAAM,WAAW,GAAG,SAAS,CAAC;AAC9B,MAAM,sBAAsB,GAAG;AAC/B,EAAE,cAAc,EAAE;AAClB,IAAI,aAAa,EAAE,KAAK;AACxB,IAAI,eAAe,EAAE,GAAG;AACxB,IAAI,iBAAiB,EAAE,GAAG;AAC1B,IAAI,mBAAmB,EAAE,GAAG;AAC5B,IAAI,wBAAwB,EAAE,MAAM;AACpC,GAAG;AACH,CAAC,CAAC;AACF,MAAM,YAAY,mBAAmB,IAAI,GAAG,EAAE,CAA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O,QAAQ,CAAC;AAClB,EAAE;AACU,MAAC,qBAAqB,GAAG,OAAO,iBAAiB,KAAK;AAClE,EAAE,MAAM,CAAC,OAAO,EAAE,cAAc,EAAE,eAAe,CAAC,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC;AACvE,IAAI,UAAU,CAAC,iBAAiB,CAAC;AACjC,IAAI,gBAAgB,EAAE;AACtB,IAAI,iBAAiB,EAAE;AACvB,GAAG,CAAC,CAAC;AACL,EAAE,OAAO;AACT,IAAI,OAAO;AACX,IAAI,cAAc;AAClB,IAAI,eAAe;AACnB,IAAI,aAAa,EAAE,cAAc;AACjC,IAAI,cAAc,EAAE,eAAe;AACnC,GAAG,CAAC;AACJ,EAAE;AACU,MAAC,iBAAiB,GAAG,OAAO,iBAAiB,KAAK;AAC9D,EAAE,MAAM,EAAE,cAAc,EAAE,GAAG,MAAM,UAAU,CAAC,iBAAiB,CAAC,CAAC;AACjE,EAAE,OAAO,cAAc,CAAC;AACxB,EAAE;AACU,MAAC,UAAU,GAAG,MAAM,KAAK,CAAC,IAAI,CAAC,YAAY,CAAC,IAAI,EAAE,CAAC,CAAC,OAAO,CAAC,CAAC,GAAG,KAAK,YAAY,CAAC,MAAM,CAAC,GAAG,CAAC;;;;"}
|
|
@@ -19,26 +19,6 @@ var __spreadValues = (a, b) => {
|
|
|
19
19
|
}
|
|
20
20
|
return a;
|
|
21
21
|
};
|
|
22
|
-
var __async = (__this, __arguments, generator) => {
|
|
23
|
-
return new Promise((resolve, reject) => {
|
|
24
|
-
var fulfilled = (value) => {
|
|
25
|
-
try {
|
|
26
|
-
step(generator.next(value));
|
|
27
|
-
} catch (e) {
|
|
28
|
-
reject(e);
|
|
29
|
-
}
|
|
30
|
-
};
|
|
31
|
-
var rejected = (value) => {
|
|
32
|
-
try {
|
|
33
|
-
step(generator.throw(value));
|
|
34
|
-
} catch (e) {
|
|
35
|
-
reject(e);
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
|
-
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
39
|
-
step((generator = generator.apply(__this, __arguments)).next());
|
|
40
|
-
});
|
|
41
|
-
};
|
|
42
22
|
function stringifyQuery(str) {
|
|
43
23
|
return stringify(str, {
|
|
44
24
|
encode: false,
|
|
@@ -46,40 +26,38 @@ function stringifyQuery(str) {
|
|
|
46
26
|
arrayFormat: "comma"
|
|
47
27
|
});
|
|
48
28
|
}
|
|
49
|
-
function request(
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
29
|
+
async function request(method, url, { id, query, data, headers } = {}) {
|
|
30
|
+
let reqUrl = url;
|
|
31
|
+
if (id) {
|
|
32
|
+
reqUrl = [trimEnd(url), trimStart(id)].join("/");
|
|
33
|
+
}
|
|
34
|
+
let reqBody;
|
|
35
|
+
if (method === "get") {
|
|
36
|
+
let exQuery;
|
|
37
|
+
[reqUrl, exQuery] = reqUrl.split("?");
|
|
38
|
+
const fullQuery = [exQuery, stringifyQuery(query)].join("&").replace(/^&/, "");
|
|
39
|
+
reqUrl = `${reqUrl}${fullQuery ? `?${fullQuery}` : ""}`;
|
|
40
|
+
} else {
|
|
41
|
+
reqBody = JSON.stringify(data);
|
|
42
|
+
}
|
|
43
|
+
const reqHeaders = __spreadValues({
|
|
44
|
+
Accept: "application/json"
|
|
45
|
+
}, headers ? headers : {});
|
|
46
|
+
const response = await fetch(reqUrl, {
|
|
47
|
+
method,
|
|
48
|
+
headers: reqHeaders,
|
|
49
|
+
body: reqBody,
|
|
50
|
+
mode: "cors"
|
|
51
|
+
});
|
|
52
|
+
const result = await response.json();
|
|
53
|
+
if (!response.ok) {
|
|
54
|
+
if (result && result.error) {
|
|
55
|
+
throw new Error(`${response.status}: ${result.error}`);
|
|
61
56
|
} else {
|
|
62
|
-
|
|
63
|
-
}
|
|
64
|
-
const reqHeaders = __spreadValues({
|
|
65
|
-
Accept: "application/json"
|
|
66
|
-
}, headers ? headers : {});
|
|
67
|
-
const response = yield fetch(reqUrl, {
|
|
68
|
-
method,
|
|
69
|
-
headers: reqHeaders,
|
|
70
|
-
body: reqBody,
|
|
71
|
-
mode: "cors"
|
|
72
|
-
});
|
|
73
|
-
const result = yield response.json();
|
|
74
|
-
if (!response.ok) {
|
|
75
|
-
if (result && result.error) {
|
|
76
|
-
throw new Error(`${response.status}: ${result.error}`);
|
|
77
|
-
} else {
|
|
78
|
-
throw new Error("A connection error occurred while making the request");
|
|
79
|
-
}
|
|
57
|
+
throw new Error("A connection error occurred while making the request");
|
|
80
58
|
}
|
|
81
|
-
|
|
82
|
-
|
|
59
|
+
}
|
|
60
|
+
return result;
|
|
83
61
|
}
|
|
84
62
|
|
|
85
63
|
export { request };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"request.js","sources":["../../../src/utils/request.ts"],"sourcesContent":["import 'isomorphic-fetch';\n\nimport { stringify } from 'qs';\nimport trimStart from 'lodash/trimStart';\nimport trimEnd from 'lodash/trimEnd';\n\nimport { Method, RequestHeaders, RequestOptions } from '../types';\n\nfunction stringifyQuery(str: unknown): string {\n return stringify(str, {\n encode: false,\n indices: false,\n arrayFormat: 'comma',\n });\n}\n\nexport async function request<T>(\n method: Method,\n url: string,\n { id, query, data, headers }: RequestOptions = {}\n): Promise<T> {\n let reqUrl = url;\n\n if (id) {\n reqUrl = [trimEnd(url), trimStart(id as string)].join('/');\n }\n\n let reqBody;\n if (method === 'get') {\n let exQuery;\n [reqUrl, exQuery] = reqUrl.split('?');\n const fullQuery = [exQuery, stringifyQuery(query)].join('&').replace(/^&/, '');\n reqUrl = `${reqUrl}${fullQuery ? `?${fullQuery}` : ''}`;\n } else {\n reqBody = JSON.stringify(data);\n }\n\n const reqHeaders: RequestHeaders = {\n Accept: 'application/json',\n // 'Content-Type': 'application/json',\n // 'X-Recharge-App': 'storefront-client',\n ...(headers ? headers : {}),\n };\n\n const response = await fetch(reqUrl, {\n method,\n headers: reqHeaders,\n body: reqBody,\n mode: 'cors',\n });\n\n const result = await response.json();\n\n if (!response.ok) {\n if (result && result.error) {\n throw new Error(`${response.status}: ${result.error}`);\n } else {\n throw new Error('A connection error occurred while making the request');\n }\n }\n\n return result as T;\n}\n"],"names":[],"mappings":";;;;;AAAA,IAAI,SAAS,GAAG,MAAM,CAAC,cAAc,CAAC;AACtC,IAAI,mBAAmB,GAAG,MAAM,CAAC,qBAAqB,CAAC;AACvD,IAAI,YAAY,GAAG,MAAM,CAAC,SAAS,CAAC,cAAc,CAAC;AACnD,IAAI,YAAY,GAAG,MAAM,CAAC,SAAS,CAAC,oBAAoB,CAAC;AACzD,IAAI,eAAe,GAAG,CAAC,GAAG,EAAE,GAAG,EAAE,KAAK,KAAK,GAAG,IAAI,GAAG,GAAG,SAAS,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE,UAAU,EAAE,IAAI,EAAE,YAAY,EAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,GAAG,GAAG,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;AAChK,IAAI,cAAc,GAAG,CAAC,CAAC,EAAE,CAAC,KAAK;AAC/B,EAAE,KAAK,IAAI,IAAI,IAAI,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC;AAChC,IAAI,IAAI,YAAY,CAAC,IAAI,CAAC,CAAC,EAAE,IAAI,CAAC;AAClC,MAAM,eAAe,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC;AACxC,EAAE,IAAI,mBAAmB;AACzB,IAAI,KAAK,IAAI,IAAI,IAAI,mBAAmB,CAAC,CAAC,CAAC,EAAE;AAC7C,MAAM,IAAI,YAAY,CAAC,IAAI,CAAC,CAAC,EAAE,IAAI,CAAC;AACpC,QAAQ,eAAe,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC;AAC1C,KAAK;AACL,EAAE,OAAO,CAAC,CAAC;AACX,CAAC,CAAC;
|
|
1
|
+
{"version":3,"file":"request.js","sources":["../../../src/utils/request.ts"],"sourcesContent":["import 'isomorphic-fetch';\n\nimport { stringify } from 'qs';\nimport trimStart from 'lodash/trimStart';\nimport trimEnd from 'lodash/trimEnd';\n\nimport { Method, RequestHeaders, RequestOptions } from '../types';\n\nfunction stringifyQuery(str: unknown): string {\n return stringify(str, {\n encode: false,\n indices: false,\n arrayFormat: 'comma',\n });\n}\n\nexport async function request<T>(\n method: Method,\n url: string,\n { id, query, data, headers }: RequestOptions = {}\n): Promise<T> {\n let reqUrl = url;\n\n if (id) {\n reqUrl = [trimEnd(url), trimStart(id as string)].join('/');\n }\n\n let reqBody;\n if (method === 'get') {\n let exQuery;\n [reqUrl, exQuery] = reqUrl.split('?');\n const fullQuery = [exQuery, stringifyQuery(query)].join('&').replace(/^&/, '');\n reqUrl = `${reqUrl}${fullQuery ? `?${fullQuery}` : ''}`;\n } else {\n reqBody = JSON.stringify(data);\n }\n\n const reqHeaders: RequestHeaders = {\n Accept: 'application/json',\n // 'Content-Type': 'application/json',\n // 'X-Recharge-App': 'storefront-client',\n ...(headers ? headers : {}),\n };\n\n const response = await fetch(reqUrl, {\n method,\n headers: reqHeaders,\n body: reqBody,\n mode: 'cors',\n });\n\n const result = await response.json();\n\n if (!response.ok) {\n if (result && result.error) {\n throw new Error(`${response.status}: ${result.error}`);\n } else {\n throw new Error('A connection error occurred while making the request');\n }\n }\n\n return result as T;\n}\n"],"names":[],"mappings":";;;;;AAAA,IAAI,SAAS,GAAG,MAAM,CAAC,cAAc,CAAC;AACtC,IAAI,mBAAmB,GAAG,MAAM,CAAC,qBAAqB,CAAC;AACvD,IAAI,YAAY,GAAG,MAAM,CAAC,SAAS,CAAC,cAAc,CAAC;AACnD,IAAI,YAAY,GAAG,MAAM,CAAC,SAAS,CAAC,oBAAoB,CAAC;AACzD,IAAI,eA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