@rechargeapps/storefront-client 0.0.14
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 +47 -0
- package/dist/cjs/api/bundle.js +100 -0
- package/dist/cjs/api/bundle.js.map +1 -0
- package/dist/cjs/api/cdn.js +109 -0
- package/dist/cjs/api/cdn.js.map +1 -0
- package/dist/cjs/constants/api.js +8 -0
- package/dist/cjs/constants/api.js.map +1 -0
- package/dist/cjs/index.js +23 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/mappers/cdn.js +67 -0
- package/dist/cjs/mappers/cdn.js.map +1 -0
- package/dist/cjs/mappers/utils.js +38 -0
- package/dist/cjs/mappers/utils.js.map +1 -0
- package/dist/cjs/utils/init.js +33 -0
- package/dist/cjs/utils/init.js.map +1 -0
- package/dist/cjs/utils/options.js +18 -0
- package/dist/cjs/utils/options.js.map +1 -0
- package/dist/cjs/utils/request.js +95 -0
- package/dist/cjs/utils/request.js.map +1 -0
- package/dist/esm/api/bundle.js +95 -0
- package/dist/esm/api/bundle.js.map +1 -0
- package/dist/esm/api/cdn.js +98 -0
- package/dist/esm/api/cdn.js.map +1 -0
- package/dist/esm/constants/api.js +4 -0
- package/dist/esm/constants/api.js.map +1 -0
- package/dist/esm/index.js +4 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/mappers/cdn.js +57 -0
- package/dist/esm/mappers/cdn.js.map +1 -0
- package/dist/esm/mappers/utils.js +34 -0
- package/dist/esm/mappers/utils.js.map +1 -0
- package/dist/esm/utils/init.js +28 -0
- package/dist/esm/utils/init.js.map +1 -0
- package/dist/esm/utils/options.js +13 -0
- package/dist/esm/utils/options.js.map +1 -0
- package/dist/esm/utils/request.js +86 -0
- package/dist/esm/utils/request.js.map +1 -0
- package/dist/index.d.ts +419 -0
- package/dist/umd/recharge-storefront-client.min.js +24 -0
- package/dist/umd/recharge-storefront-client.min.js.map +1 -0
- package/package.json +66 -0
package/README.md
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# Storefront client
|
|
2
|
+
|
|
3
|
+
Recharge Storefront client
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
Script:
|
|
8
|
+
|
|
9
|
+
```html
|
|
10
|
+
<head>
|
|
11
|
+
...
|
|
12
|
+
<script src="https://static.rechargecdn.com/scripts/recharge-storefront-client.0.0.14.min.js"></script>
|
|
13
|
+
...
|
|
14
|
+
</head>
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
NPM/Yarn
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npm install @recharge-packages/storefront-client
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
yarn add @recharge-packages/storefront-client
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Init
|
|
28
|
+
|
|
29
|
+
```js
|
|
30
|
+
recharge.init({ storeIdentifier: 'storeIdentifier' });
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## CDN
|
|
34
|
+
|
|
35
|
+
- `recharge.cdn.getStoreSettings()`
|
|
36
|
+
- `recharge.cdn.getWidgetSettings()`
|
|
37
|
+
- `recharge.cdn.getProduct(externalProductId)`
|
|
38
|
+
- `recharge.cdn.getProductAndSettings(externalProductId)`
|
|
39
|
+
- `recharge.cdn.getProductsAndSettings()`
|
|
40
|
+
- `recharge.cdn.getProducts()`
|
|
41
|
+
- `recharge.cdn.getBundleSettings(externalProductId)`
|
|
42
|
+
- `recharge.cdn.resetCache()`
|
|
43
|
+
|
|
44
|
+
## Bundle
|
|
45
|
+
|
|
46
|
+
- `recharge.bundle.getBundleId(bundle)`
|
|
47
|
+
- `recharge.bundle.validate(bundle)`
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var bundlingData = require('@recharge-packages/bundling-data');
|
|
6
|
+
var request = require('../utils/request.js');
|
|
7
|
+
var options = require('../utils/options.js');
|
|
8
|
+
var cdn = require('./cdn.js');
|
|
9
|
+
|
|
10
|
+
var __async = (__this, __arguments, generator) => {
|
|
11
|
+
return new Promise((resolve, reject) => {
|
|
12
|
+
var fulfilled = (value) => {
|
|
13
|
+
try {
|
|
14
|
+
step(generator.next(value));
|
|
15
|
+
} catch (e) {
|
|
16
|
+
reject(e);
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
var rejected = (value) => {
|
|
20
|
+
try {
|
|
21
|
+
step(generator.throw(value));
|
|
22
|
+
} catch (e) {
|
|
23
|
+
reject(e);
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
27
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
28
|
+
});
|
|
29
|
+
};
|
|
30
|
+
const STORE_FRONT_MANAGER_URL = "https://us-east4-recharge-prod-services-f818.cloudfunctions.net/bundling-storefront-manager";
|
|
31
|
+
function getTimestampSecondsFromClient() {
|
|
32
|
+
return Math.ceil(Date.now() / 1e3);
|
|
33
|
+
}
|
|
34
|
+
function getTimestampSecondsFromServer(request2) {
|
|
35
|
+
return __async(this, null, function* () {
|
|
36
|
+
const url = `${STORE_FRONT_MANAGER_URL}/t`;
|
|
37
|
+
try {
|
|
38
|
+
const { timestamp } = yield request2("get", url, {
|
|
39
|
+
headers: { "X-Recharge-App": "storefront-client" }
|
|
40
|
+
});
|
|
41
|
+
return timestamp;
|
|
42
|
+
} catch (ex) {
|
|
43
|
+
console.error(`Fetch failed: ${ex}. Using client-side date.`);
|
|
44
|
+
return getTimestampSecondsFromClient();
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
const getBundleId = (bundle) => __async(void 0, null, function* () {
|
|
49
|
+
const opts = options.getOptions();
|
|
50
|
+
const isValid = yield validateBundle(bundle);
|
|
51
|
+
if (!isValid) {
|
|
52
|
+
throw new Error("Bundle selection is invalid.");
|
|
53
|
+
}
|
|
54
|
+
const timestampSeconds = yield getTimestampSecondsFromServer(request.request);
|
|
55
|
+
const bundleData = bundlingData.toLineItemProperty({
|
|
56
|
+
variantId: bundle.externalVariantId,
|
|
57
|
+
version: timestampSeconds,
|
|
58
|
+
items: bundle.selections.map((item) => {
|
|
59
|
+
return {
|
|
60
|
+
collectionId: item.collectionId,
|
|
61
|
+
productId: item.externalProductId,
|
|
62
|
+
variantId: item.externalVariantId,
|
|
63
|
+
quantity: item.quantity,
|
|
64
|
+
sku: ""
|
|
65
|
+
};
|
|
66
|
+
})
|
|
67
|
+
});
|
|
68
|
+
try {
|
|
69
|
+
const STORE_FRONT_BUNDLE_URL = `${STORE_FRONT_MANAGER_URL}/api/v1/bundles`;
|
|
70
|
+
const payload = yield request.request("post", STORE_FRONT_BUNDLE_URL, {
|
|
71
|
+
data: {
|
|
72
|
+
bundle: bundleData
|
|
73
|
+
},
|
|
74
|
+
headers: {
|
|
75
|
+
"Content-Type": "application/json",
|
|
76
|
+
Origin: `https://${opts.storeIdentifier}`,
|
|
77
|
+
"X-Recharge-App": "storefront-client"
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
if (!payload.id || payload.code !== 200) {
|
|
81
|
+
throw new Error(`1: failed generating rb_id: ${JSON.stringify(payload)}`);
|
|
82
|
+
}
|
|
83
|
+
return payload.id;
|
|
84
|
+
} catch (e) {
|
|
85
|
+
throw new Error(`2: failed generating rb_id ${e}`);
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
const validateBundle = (bundle) => __async(void 0, null, function* () {
|
|
89
|
+
try {
|
|
90
|
+
const bundleSettings = yield cdn.getBundleSettings(bundle.externalProductId);
|
|
91
|
+
return !!bundle && !!bundleSettings;
|
|
92
|
+
} catch (e) {
|
|
93
|
+
console.error("Error fetching bundle settings");
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
exports.getBundleId = getBundleId;
|
|
99
|
+
exports.validateBundle = validateBundle;
|
|
100
|
+
//# sourceMappingURL=bundle.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bundle.js","sources":["../../../src/api/bundle.ts"],"sourcesContent":["import { toLineItemProperty } from '@recharge-packages/bundling-data';\nimport { Request, Bundle } from '../types';\nimport { request } from '../utils/request';\nimport { getOptions } from '../utils/options';\nimport { getBundleSettings } from './cdn';\n\nconst STORE_FRONT_MANAGER_URL = process.env.BUNDLING_STOREFRONT_URL;\n\nfunction getTimestampSecondsFromClient(): number {\n /**\n * Get the current unix epoch in seconds from the client-side.\n */\n return Math.ceil(Date.now() / 1000);\n}\n\nasync function getTimestampSecondsFromServer(request: Request): Promise<number> {\n /**\n * Get the unix epoch from the server instead of using it directly from the\n * client. This must reduce even more the number of invalid Bundles.\n */\n const url = `${STORE_FRONT_MANAGER_URL}/t`;\n\n try {\n const { timestamp } = await request<{ timestamp: number }>('get', url, {\n headers: { 'X-Recharge-App': 'storefront-client' },\n });\n return timestamp;\n } catch (ex) {\n console.error(`Fetch failed: ${ex}. Using client-side date.`);\n return getTimestampSecondsFromClient();\n }\n}\n\nexport const getBundleId = async (bundle: Bundle): Promise<string> => {\n const opts = getOptions();\n const isValid = await validateBundle(bundle);\n if (!isValid) {\n throw new Error('Bundle selection is invalid.');\n }\n const timestampSeconds = await getTimestampSecondsFromServer(request);\n const bundleData = toLineItemProperty({\n variantId: bundle.externalVariantId,\n version: timestampSeconds,\n items: bundle.selections.map(item => {\n return {\n collectionId: item.collectionId,\n productId: item.externalProductId,\n variantId: item.externalVariantId,\n quantity: item.quantity,\n sku: '',\n };\n }),\n });\n\n try {\n const STORE_FRONT_BUNDLE_URL = `${STORE_FRONT_MANAGER_URL}/api/v1/bundles`;\n const payload = await request<{ id: string; code: number; message: string }>('post', STORE_FRONT_BUNDLE_URL, {\n data: {\n bundle: bundleData,\n },\n headers: {\n 'Content-Type': 'application/json',\n Origin: `https://${opts.storeIdentifier}`,\n 'X-Recharge-App': 'storefront-client',\n },\n });\n\n if (!payload.id || payload.code !== 200) {\n throw new Error(`1: failed generating rb_id: ${JSON.stringify(payload)}`);\n }\n\n return payload.id;\n } catch (e) {\n // Handle NetworkError exceptions\n throw new Error(`2: failed generating rb_id ${e}`);\n }\n};\n\nexport const validateBundle = async (bundle: Bundle): Promise<boolean> => {\n try {\n const bundleSettings = await getBundleSettings(bundle.externalProductId);\n // once we implement this function, we can make it raise an exception\n // we could also have a local store relative to this function so we don't have to pass bundleProduct\n return !!bundle && !!bundleSettings;\n } catch (e) {\n console.error('Error fetching bundle settings');\n return false;\n }\n};\n"],"names":["getOptions","request","toLineItemProperty","getBundleSettings"],"mappings":";;;;;;;;;AAAA,IAAI,OAAO,GAAG,CAAC,MAAM,EAAE,WAAW,EAAE,SAAS,KAAK;AAClD,EAAE,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,KAAK;AAC1C,IAAI,IAAI,SAAS,GAAG,CAAC,KAAK,KAAK;AAC/B,MAAM,IAAI;AACV,QAAQ,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC;AACpC,OAAA,CAAA,OAAA,CAAA,EAAA;AACA,QAAQ,MAAM,CAAC,CAAC,CAAC,CAAC;AAClB,OAAA;AACA,KAAK,CAAC;AACN,IAAI,IAAC,QAAA,GAAA,CAAA,KAAA,KAAA;AACL,MAAM,IAAI;AACV,QAAK,IAAA,CAAA,SAAA,CAAA,KAAA,CAAA,KAAA,CAAA,CAAA,CAAA;AACL,OAAO,CAAC,OAAO,CAAC,EAAE;AAClB,QAAC,MAAA,CAAA,CAAA,CAAA,CAAA;AACD,OAAA;AACA,KAAK,CAAC;AACN,IAAI,IAAC,IAAA,GAAA,CAAA,CAAA,KAAA,CAAA,CAAA,IAAA,GAAA,OAAA,CAAA,CAAA,CAAA,KAAA,CAAA,GAAA,OAAA,CAAA,OA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,CAAA,CAAA,CAAA;AACH,KAAE;AACF,IAAA,OAAA,OAAA,CAAA,EAAA,CAAA;AACA,GAAG,CAAC,OAAO,CAAC,EAAE;AACd,IAAI,MAAG,IAAA,KAAA,CAAA,CAAA,2BAAA,EAAA,CAAA,CAAA,CAAA,CAAA,CAAA;AACP,GAAG;AACH,CAAC,EAAE;AACS,MAAC,cAAc,GAAG,CAAC,MAAM,KAAK,OAAO,CAAC,KAAK,CAAC,EAAE,IAAI,EAAE,aAAa;AAC7E,EAAE,IAAI;AACN,IAAI,MAAM,cAAK,GAAA,MAAAE,qBAAA,CAAA,MAAA,CAAA,iBAAA,CAAA,CAAA;AACf,IAAI,OAAO,CAAC,CAAC,MAAM,IAAI,CAAC,CAAC,cAAc,CAAC;AACxC,GAAG,CAAC,OAAO,CAAC,EAAE;AACd,IAAG,OAAA,CAAA,KAAA,CAAA,gCAAA,CAAA,CAAA;AACH,IAAE,OAAA,KAAA,CAAA;;;;;;;"}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var api = require('../constants/api.js');
|
|
6
|
+
var cdn = require('../mappers/cdn.js');
|
|
7
|
+
var request = require('../utils/request.js');
|
|
8
|
+
var options = require('../utils/options.js');
|
|
9
|
+
|
|
10
|
+
var __async = (__this, __arguments, generator) => {
|
|
11
|
+
return new Promise((resolve, reject) => {
|
|
12
|
+
var fulfilled = (value) => {
|
|
13
|
+
try {
|
|
14
|
+
step(generator.next(value));
|
|
15
|
+
} catch (e) {
|
|
16
|
+
reject(e);
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
var rejected = (value) => {
|
|
20
|
+
try {
|
|
21
|
+
step(generator.throw(value));
|
|
22
|
+
} catch (e) {
|
|
23
|
+
reject(e);
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
27
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
28
|
+
});
|
|
29
|
+
};
|
|
30
|
+
const CDN_VERSION = "2020-12";
|
|
31
|
+
const DEFAULT_STORE_SETTINGS = {
|
|
32
|
+
store_currency: {
|
|
33
|
+
currency_code: "USD",
|
|
34
|
+
currency_symbol: "$",
|
|
35
|
+
decimal_separator: ".",
|
|
36
|
+
thousands_separator: ",",
|
|
37
|
+
currency_symbol_location: "left"
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
const promiseCache = /* @__PURE__ */ new Map();
|
|
41
|
+
function cacheRequest(cacheKey, request2) {
|
|
42
|
+
if (!promiseCache.has(cacheKey)) {
|
|
43
|
+
promiseCache.set(cacheKey, request2());
|
|
44
|
+
}
|
|
45
|
+
return promiseCache.get(cacheKey);
|
|
46
|
+
}
|
|
47
|
+
const getProduct = (externalProductId) => __async(void 0, null, function* () {
|
|
48
|
+
const opts = options.getOptions();
|
|
49
|
+
const { product } = yield cacheRequest(`product.${externalProductId}`, () => request.request("get", `${api.RECHARGE_CDN_URL(opts.environment)}/store/${opts.storeIdentifier}/product/${CDN_VERSION}/${externalProductId}.json`));
|
|
50
|
+
return cdn.productMapper(product);
|
|
51
|
+
});
|
|
52
|
+
const getStoreSettings = () => __async(void 0, null, function* () {
|
|
53
|
+
const opts = options.getOptions();
|
|
54
|
+
const storeSettings = yield cacheRequest("storeSettings", () => request.request("get", `${api.RECHARGE_CDN_URL(opts.environment)}/store/${opts.storeIdentifier}/${CDN_VERSION}/store_settings.json`).catch(() => {
|
|
55
|
+
return DEFAULT_STORE_SETTINGS;
|
|
56
|
+
}));
|
|
57
|
+
return storeSettings;
|
|
58
|
+
});
|
|
59
|
+
const getWidgetSettings = () => __async(void 0, null, function* () {
|
|
60
|
+
const opts = options.getOptions();
|
|
61
|
+
const { widget_settings } = yield cacheRequest("widgetSettings", () => request.request("get", `${api.RECHARGE_CDN_URL(opts.environment)}/store/${opts.storeIdentifier}/${CDN_VERSION}/widget_settings.json`));
|
|
62
|
+
const widgetSettings = cdn.widgetSettingsMapper(widget_settings);
|
|
63
|
+
return widgetSettings;
|
|
64
|
+
});
|
|
65
|
+
const getProductsAndSettings = () => __async(void 0, null, function* () {
|
|
66
|
+
const opts = options.getOptions();
|
|
67
|
+
const { products, widget_settings, store_settings, meta } = yield cacheRequest("productsAndSettings", () => request.request("get", `${api.RECHARGE_CDN_URL(opts.environment)}/store/${opts.storeIdentifier}/product/${CDN_VERSION}/products.json`));
|
|
68
|
+
if ((meta == null ? void 0 : meta.status) === "error") {
|
|
69
|
+
return Promise.reject(meta.message);
|
|
70
|
+
}
|
|
71
|
+
return {
|
|
72
|
+
products: cdn.productArrayMapper(products),
|
|
73
|
+
widget_settings: cdn.widgetSettingsMapper(widget_settings),
|
|
74
|
+
store_settings
|
|
75
|
+
};
|
|
76
|
+
});
|
|
77
|
+
const getProducts = () => __async(void 0, null, function* () {
|
|
78
|
+
const { products } = yield getProductsAndSettings();
|
|
79
|
+
return products;
|
|
80
|
+
});
|
|
81
|
+
const getProductAndSettings = (externalProductId) => __async(void 0, null, function* () {
|
|
82
|
+
const [product, store_settings, widget_settings] = yield Promise.all([
|
|
83
|
+
getProduct(externalProductId),
|
|
84
|
+
getStoreSettings(),
|
|
85
|
+
getWidgetSettings()
|
|
86
|
+
]);
|
|
87
|
+
return {
|
|
88
|
+
product,
|
|
89
|
+
store_settings,
|
|
90
|
+
widget_settings,
|
|
91
|
+
storeSettings: store_settings,
|
|
92
|
+
widgetSettings: widget_settings
|
|
93
|
+
};
|
|
94
|
+
});
|
|
95
|
+
const getBundleSettings = (externalProductId) => __async(void 0, null, function* () {
|
|
96
|
+
const { bundle_product } = yield getProduct(externalProductId);
|
|
97
|
+
return bundle_product;
|
|
98
|
+
});
|
|
99
|
+
const resetCache = () => Array.from(promiseCache.keys()).forEach((key) => promiseCache.delete(key));
|
|
100
|
+
|
|
101
|
+
exports.getBundleSettings = getBundleSettings;
|
|
102
|
+
exports.getProduct = getProduct;
|
|
103
|
+
exports.getProductAndSettings = getProductAndSettings;
|
|
104
|
+
exports.getProducts = getProducts;
|
|
105
|
+
exports.getProductsAndSettings = getProductsAndSettings;
|
|
106
|
+
exports.getStoreSettings = getStoreSettings;
|
|
107
|
+
exports.getWidgetSettings = getWidgetSettings;
|
|
108
|
+
exports.resetCache = resetCache;
|
|
109
|
+
//# sourceMappingURL=cdn.js.map
|
|
@@ -0,0 +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":["getOptions","request","RECHARGE_CDN_URL","productMapper","widgetSettingsMapper","productArrayMapper"],"mappings":";;;;;;;;;AAAA,IAAI,OAAO,GAAG,CAAC,MAAM,EAAE,WAAW,EAAE,SAAS,KAAK;AAClD,EAAE,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,KAAK;AAC1C,IAAI,IAAI,SAAS,GAAG,CAAC,KAAK,KAAK;AAC/B,MAAM,IAAI;AACV,QAAQ,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC;AACpC,OAAO,CAAC,OAAO,CAAC,EAAE;AAClB,QAAQ,MAAM,CAAC,CAAC,CAAC,CAAC;AAClB,OAAO;AACP,KAAK,CAAC;AACN,IAAI,IAAI,QAAQ,GAAG,CAAC,KAAK,KAAK;AAC9B,MAAM,IAAI;AACV,QAAQ,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC;AACrC,OAAO,CAAC,OAAO,CAAC,EAAE;AAClB,QAAQ,MAAM,CAAC,CAAC,CAAC,CAAC;AAClB,OAAO;AACP,KAAK,CAAC;AACN,IAAI,IAAI,IAAI,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,IAAI,GAAG,OAAO,CAAC,CAAC,CAAC,KAAK,CAAC,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAC;AACrG,IAAI,IAAI,CAAC,CAAC,SAAS,GAAG,SAAS,CAAC,KAAK,CAAC,MAAM,EAAE,WAAW,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC;AACpE,GAAG,CAAC,CAAC;AACL,CAAC,CAAC;AAKF,MAAM,WAAW,GAAG,SAAS,CAAC;AAC9B,MAAM,sBAAsB,GAAG;AAC/B,EAAE,cAAc,EAAE;AAClB,IAAI,aAAa,EAAE,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ACS,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;;;;;;;;;;;"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
const RECHARGE_CDN_URL = (environment) => environment === "stage" ? "https://static.stage.rechargecdn.com" : "https://static.rechargecdn.com";
|
|
6
|
+
|
|
7
|
+
exports.RECHARGE_CDN_URL = RECHARGE_CDN_URL;
|
|
8
|
+
//# sourceMappingURL=api.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"api.js","sources":["../../../src/constants/api.ts"],"sourcesContent":["export const RECHARGE_API_URL = (environment: 'stage' | 'prod'): string =>\n environment === 'stage' ? 'https://api.stage.rechargeapps.com' : 'https://api.rechargeapps.com';\nexport const RECHARGE_CDN_URL = (environment: 'stage' | 'prod'): string =>\n environment === 'stage' ? 'https://static.stage.rechargecdn.com' : 'https://static.rechargecdn.com';\n"],"names":[],"mappings":";;;;AACY,MAAC,gBAAgB,GAAG,CAAC,WAAW,KAAK,WAAW,KAAK,OAAO,GAAG,sCAAsC,GAAG;;;;"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var cdn = require('./api/cdn.js');
|
|
6
|
+
var bundle = require('./api/bundle.js');
|
|
7
|
+
var init = require('./utils/init.js');
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
exports.getBundleSettings = cdn.getBundleSettings;
|
|
12
|
+
exports.getProduct = cdn.getProduct;
|
|
13
|
+
exports.getProductAndSettings = cdn.getProductAndSettings;
|
|
14
|
+
exports.getProducts = cdn.getProducts;
|
|
15
|
+
exports.getProductsAndSettings = cdn.getProductsAndSettings;
|
|
16
|
+
exports.getStoreSettings = cdn.getStoreSettings;
|
|
17
|
+
exports.getWidgetSettings = cdn.getWidgetSettings;
|
|
18
|
+
exports.resetCache = cdn.resetCache;
|
|
19
|
+
exports.getBundleId = bundle.getBundleId;
|
|
20
|
+
exports.validateBundle = bundle.validateBundle;
|
|
21
|
+
exports.api = init.api;
|
|
22
|
+
exports.initRecharge = init.initRecharge;
|
|
23
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var omit = require('lodash/omit');
|
|
6
|
+
var utils = require('./utils.js');
|
|
7
|
+
|
|
8
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
9
|
+
|
|
10
|
+
var omit__default = /*#__PURE__*/_interopDefaultLegacy(omit);
|
|
11
|
+
|
|
12
|
+
var __defProp = Object.defineProperty;
|
|
13
|
+
var __defProps = Object.defineProperties;
|
|
14
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
15
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
16
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
17
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
18
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
19
|
+
var __spreadValues = (a, b) => {
|
|
20
|
+
for (var prop in b || (b = {}))
|
|
21
|
+
if (__hasOwnProp.call(b, prop))
|
|
22
|
+
__defNormalProp(a, prop, b[prop]);
|
|
23
|
+
if (__getOwnPropSymbols)
|
|
24
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
25
|
+
if (__propIsEnum.call(b, prop))
|
|
26
|
+
__defNormalProp(a, prop, b[prop]);
|
|
27
|
+
}
|
|
28
|
+
return a;
|
|
29
|
+
};
|
|
30
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
31
|
+
function widgetSettingsMapper(widgetSettingsRaw) {
|
|
32
|
+
var _a;
|
|
33
|
+
const parsedSettings = utils.parseValues(widgetSettingsRaw);
|
|
34
|
+
const auto_inject = parsedSettings.auto_inject === void 0 ? true : parsedSettings.auto_inject;
|
|
35
|
+
const valid_pages = (_a = parsedSettings.display_on) != null ? _a : [];
|
|
36
|
+
const is_subscription_first = parsedSettings.first_option === "autodeliver";
|
|
37
|
+
return __spreadProps(__spreadValues({}, omit__default["default"](parsedSettings, ["display_on", "first_option"])), {
|
|
38
|
+
auto_inject,
|
|
39
|
+
valid_pages,
|
|
40
|
+
is_subscription_first,
|
|
41
|
+
autoInject: auto_inject,
|
|
42
|
+
validPages: valid_pages,
|
|
43
|
+
isSubscriptionFirst: is_subscription_first
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
function productMapper(productRaw) {
|
|
47
|
+
var _a;
|
|
48
|
+
const is_subscription_only = ((_a = productRaw.subscription_options) == null ? void 0 : _a.storefront_purchase_options) === "subscription_only";
|
|
49
|
+
return __spreadProps(__spreadValues({}, productRaw), {
|
|
50
|
+
is_subscription_only,
|
|
51
|
+
isSubscriptionOnly: is_subscription_only
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
function productArrayMapper(productsArray) {
|
|
55
|
+
return productsArray.map((productObjRaw) => {
|
|
56
|
+
const productObj = {};
|
|
57
|
+
Object.entries(productObjRaw).forEach(([key, value]) => {
|
|
58
|
+
productObj[key] = productMapper(value);
|
|
59
|
+
});
|
|
60
|
+
return productObj;
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
exports.productArrayMapper = productArrayMapper;
|
|
65
|
+
exports.productMapper = productMapper;
|
|
66
|
+
exports.widgetSettingsMapper = widgetSettingsMapper;
|
|
67
|
+
//# sourceMappingURL=cdn.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cdn.js","sources":["../../../src/mappers/cdn.ts"],"sourcesContent":["import omit from 'lodash/omit';\nimport {\n CDNBaseWidgetSettings,\n CDNProductRaw,\n CDNWidgetSettings,\n CDNWidgetSettingsRaw,\n CDNProduct,\n CDNProductKeyObject,\n} from '../types';\nimport { parseValues } from './utils';\n\nexport function widgetSettingsMapper(widgetSettingsRaw: CDNWidgetSettingsRaw): CDNWidgetSettings {\n const parsedSettings = parseValues<CDNBaseWidgetSettings>(widgetSettingsRaw);\n\n const auto_inject = parsedSettings.auto_inject === undefined ? true : parsedSettings.auto_inject;\n const valid_pages = parsedSettings.display_on ?? [];\n const is_subscription_first = parsedSettings.first_option === 'autodeliver';\n\n return {\n ...omit(parsedSettings, ['display_on', 'first_option']),\n auto_inject,\n valid_pages,\n is_subscription_first,\n autoInject: auto_inject,\n validPages: valid_pages,\n isSubscriptionFirst: is_subscription_first,\n };\n}\n\nexport function productMapper(productRaw: CDNProductRaw): CDNProduct {\n const is_subscription_only = productRaw.subscription_options?.storefront_purchase_options === 'subscription_only';\n return {\n ...productRaw,\n is_subscription_only,\n isSubscriptionOnly: is_subscription_only,\n };\n}\n\nexport function productArrayMapper(\n productsArray: {\n [key: string]: CDNProductRaw;\n }[]\n): CDNProductKeyObject[] {\n return productsArray.map(productObjRaw => {\n const productObj: CDNProductKeyObject = {};\n Object.entries(productObjRaw).forEach(([key, value]) => {\n productObj[key] = productMapper(value);\n });\n return productObj;\n });\n}\n"],"names":["parseValues","omit"],"mappings":";;;;;;;;;;;AAAA,IAAI,SAAS,GAAG,MAAM,CAAC,cAAc,CAAC;AACtC,IAAI,UAAU,GAAG,MAAM,CAAC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AG,EAAE,CAAC;AAC1B,IAAI,MAAM,CAAC,OAAO,CAAC,aAAa,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,EAAE,KAAK,CAAC,KAAK;AAC5D,MAAM,UAAU,CAAC,GAAG,CAAC,GAAG,aAAa,CAAC,KAAK,CAAC,CAAC;AAC7C,KAAK,CAAC,CAAC;AACP,IAAI,OAAO,UAAU,CAAC;AACtB,GAAG,CAAC,CAAC;AACL;;;;;;"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var __defProp = Object.defineProperty;
|
|
6
|
+
var __defProps = Object.defineProperties;
|
|
7
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
8
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
9
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
10
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
11
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
12
|
+
var __spreadValues = (a, b) => {
|
|
13
|
+
for (var prop in b || (b = {}))
|
|
14
|
+
if (__hasOwnProp.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
if (__getOwnPropSymbols)
|
|
17
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
18
|
+
if (__propIsEnum.call(b, prop))
|
|
19
|
+
__defNormalProp(a, prop, b[prop]);
|
|
20
|
+
}
|
|
21
|
+
return a;
|
|
22
|
+
};
|
|
23
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
24
|
+
function parseValue(value) {
|
|
25
|
+
try {
|
|
26
|
+
return JSON.parse(value);
|
|
27
|
+
} catch (e) {
|
|
28
|
+
return value;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
function parseValues(json) {
|
|
32
|
+
return Object.entries(json).reduce((memo, [key, value]) => {
|
|
33
|
+
return __spreadProps(__spreadValues({}, memo), { [key]: parseValue(value) });
|
|
34
|
+
}, {});
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
exports.parseValues = parseValues;
|
|
38
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sources":["../../../src/mappers/utils.ts"],"sourcesContent":["/** Trys to parse passed in value. If it doesn't work returns the same passed in value */\nfunction parseValue(value: unknown): any {\n try {\n return JSON.parse(value as string);\n } catch {\n return value;\n }\n}\n\n/** Returns a new object that updates \"string\" values into their correct data type. */\nexport function parseValues<T = any>(json: Record<string, unknown>): T {\n return Object.entries(json).reduce((memo, [key, value]) => {\n return { ...memo, [key]: parseValue(value) };\n }, {}) as T;\n}\n"],"names":[],"mappings":";;;;AAAA,IAAI,SAAS,GAAG,MAAM,CAAC,cAAc,CAAC;AACtC,IAAI,UAAU,GAAG,MAAM,CAAC,gBAAgB,CAAC;AACzC,IAAI,iBAAiB,GAAG,MAAM,CAAC,yBAAyB,CAAC;AACzD,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,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@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var cdn = require('../api/cdn.js');
|
|
6
|
+
var options = require('./options.js');
|
|
7
|
+
var request = require('./request.js');
|
|
8
|
+
|
|
9
|
+
const api = {
|
|
10
|
+
get(url, requestOptions) {
|
|
11
|
+
return request.request("get", url, requestOptions);
|
|
12
|
+
},
|
|
13
|
+
post(url, requestOptions) {
|
|
14
|
+
return request.request("post", url, requestOptions);
|
|
15
|
+
},
|
|
16
|
+
put(url, requestOptions) {
|
|
17
|
+
return request.request("put", url, requestOptions);
|
|
18
|
+
},
|
|
19
|
+
delete(url, requestOptions) {
|
|
20
|
+
return request.request("delete", url, requestOptions);
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
function initRecharge(opt) {
|
|
24
|
+
options.setOptions({
|
|
25
|
+
storeIdentifier: opt.storeIdentifier,
|
|
26
|
+
environment: opt.environment ? opt.environment : "prod"
|
|
27
|
+
});
|
|
28
|
+
cdn.resetCache();
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
exports.api = api;
|
|
32
|
+
exports.initRecharge = initRecharge;
|
|
33
|
+
//# sourceMappingURL=init.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"init.js","sources":["../../../src/utils/init.ts"],"sourcesContent":["import { resetCache } from '../api/cdn';\nimport { StorefrontOptions, CRUDRequestOptions, GetRequestOptions } from '../types';\nimport { setOptions } from './options';\nimport { request } from './request';\n\nexport interface InitOptions extends Omit<StorefrontOptions, 'environment'> {\n /** This is only for internal use. Sets the environment where data is fetched from. */\n environment?: StorefrontOptions['environment'];\n}\n\nexport const api = {\n get<T>(url: string, requestOptions?: GetRequestOptions): Promise<T> {\n return request<T>('get', url, requestOptions);\n },\n post<T>(url: string, requestOptions?: CRUDRequestOptions): Promise<T> {\n return request<T>('post', url, requestOptions);\n },\n put<T>(url: string, requestOptions?: CRUDRequestOptions): Promise<T> {\n return request<T>('put', url, requestOptions);\n },\n delete<T>(url: string, requestOptions?: CRUDRequestOptions): Promise<T> {\n return request<T>('delete', url, requestOptions);\n },\n};\n\nexport function initRecharge(opt: InitOptions) {\n setOptions({\n storeIdentifier: opt.storeIdentifier,\n environment: opt.environment ? opt.environment : 'prod',\n });\n\n // When init is called again, reset the cache to make sure we don't have the old cache around\n resetCache();\n}\n"],"names":["request","setOptions","resetCache"],"mappings":";;;;;;;;AAGY,MAAC,GAAG,GAAG;AACnB,EAAE,GAAG,CAAC,GAAG,EAAE,cAAc,EAAE;AAC3B,IAAI,OAAOA,eAAO,CAAC,KAAK,EAAE,GAAG,EAAE,cAAc,CAAC,CAAC;AAC/C,GAAG;AACH,EAAE,IAAI,CAAC,GAAG,EAAE,cAAc,EAAE;AAC5B,IAAI,OAAOA,eAAO,CAAC,MAAM,EAAE,GAAG,EAAE,cAAc,CAAC,CAAC;AAChD,GAAG;AACH,EAAE,GAAG,CAAC,GAAG,EAAE,cAAc,EAAE;AAC3B,IAAI,OAAOA,eAAO,CAAC,KAAK,EAAE,GAAG,EAAE,cAAc,CAAC,CAAC;AAC/C,GAAG;AACH,EAAE,MAAM,CAAC,GAAG,EAAE,cAAc,EAAE;AAC9B,IAAI,OAAOA,eAAO,CAAC,QAAQ,EAAE,GAAG,EAAE,cAAc,CAAC,CAAC;AAClD,GAAG;AACH,EAAE;AACK,SAAS,YAAY,CAAC,GAAG,EAAE;AAClC,EAAEC,kBAAU,CAAC;AACb,IAAI,eAAe,EAAE,GAAG,CAAC,eAAe;AACxC,IAAI,WAAW,EAAE,GAAG,CAAC,WAAW,GAAG,GAAG,CAAC,WAAW,GAAG,MAAM;AAC3D,GAAG,CAAC,CAAC;AACL,EAAEC,cAAU,EAAE,CAAC;AACf;;;;;"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
let options = {
|
|
6
|
+
storeIdentifier: "",
|
|
7
|
+
environment: "prod"
|
|
8
|
+
};
|
|
9
|
+
function setOptions(opts) {
|
|
10
|
+
options = opts;
|
|
11
|
+
}
|
|
12
|
+
function getOptions() {
|
|
13
|
+
return options;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
exports.getOptions = getOptions;
|
|
17
|
+
exports.setOptions = setOptions;
|
|
18
|
+
//# sourceMappingURL=options.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"options.js","sources":["../../../src/utils/options.ts"],"sourcesContent":["import { StorefrontOptions } from '../types';\n\nlet options: StorefrontOptions = {\n storeIdentifier: '',\n environment: 'prod',\n};\n\nexport function setOptions(opts: StorefrontOptions) {\n options = opts;\n}\n\nexport function getOptions() {\n return options;\n}\n"],"names":[],"mappings":";;;;AAAA,IAAI,OAAO,GAAG;AACd,EAAE,eAAe,EAAE,EAAE;AACrB,EAAE,WAAW,EAAE,MAAM;AACrB,CAAC,CAAC;AACK,SAAS,UAAU,CAAC,IAAI,EAAE;AACjC,EAAE,OAAO,GAAG,IAAI,CAAC;AACjB,CAAC;AACM,SAAS,UAAU,GAAG;AAC7B,EAAE,OAAO,OAAO,CAAC;AACjB;;;;;"}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
require('isomorphic-fetch');
|
|
6
|
+
var qs = require('qs');
|
|
7
|
+
var trimStart = require('lodash/trimStart');
|
|
8
|
+
var trimEnd = require('lodash/trimEnd');
|
|
9
|
+
|
|
10
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
11
|
+
|
|
12
|
+
var trimStart__default = /*#__PURE__*/_interopDefaultLegacy(trimStart);
|
|
13
|
+
var trimEnd__default = /*#__PURE__*/_interopDefaultLegacy(trimEnd);
|
|
14
|
+
|
|
15
|
+
var __defProp = Object.defineProperty;
|
|
16
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
17
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
18
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
19
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
20
|
+
var __spreadValues = (a, b) => {
|
|
21
|
+
for (var prop in b || (b = {}))
|
|
22
|
+
if (__hasOwnProp.call(b, prop))
|
|
23
|
+
__defNormalProp(a, prop, b[prop]);
|
|
24
|
+
if (__getOwnPropSymbols)
|
|
25
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
26
|
+
if (__propIsEnum.call(b, prop))
|
|
27
|
+
__defNormalProp(a, prop, b[prop]);
|
|
28
|
+
}
|
|
29
|
+
return a;
|
|
30
|
+
};
|
|
31
|
+
var __async = (__this, __arguments, generator) => {
|
|
32
|
+
return new Promise((resolve, reject) => {
|
|
33
|
+
var fulfilled = (value) => {
|
|
34
|
+
try {
|
|
35
|
+
step(generator.next(value));
|
|
36
|
+
} catch (e) {
|
|
37
|
+
reject(e);
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
var rejected = (value) => {
|
|
41
|
+
try {
|
|
42
|
+
step(generator.throw(value));
|
|
43
|
+
} catch (e) {
|
|
44
|
+
reject(e);
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
48
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
49
|
+
});
|
|
50
|
+
};
|
|
51
|
+
function stringifyQuery(str) {
|
|
52
|
+
return qs.stringify(str, {
|
|
53
|
+
encode: false,
|
|
54
|
+
indices: false,
|
|
55
|
+
arrayFormat: "comma"
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
function request(_0, _1) {
|
|
59
|
+
return __async(this, arguments, function* (method, url, { id, query, data, headers } = {}) {
|
|
60
|
+
let reqUrl = url;
|
|
61
|
+
if (id) {
|
|
62
|
+
reqUrl = [trimEnd__default["default"](url), trimStart__default["default"](id)].join("/");
|
|
63
|
+
}
|
|
64
|
+
let reqBody;
|
|
65
|
+
if (method === "get") {
|
|
66
|
+
let exQuery;
|
|
67
|
+
[reqUrl, exQuery] = reqUrl.split("?");
|
|
68
|
+
const fullQuery = [exQuery, stringifyQuery(query)].join("&").replace(/^&/, "");
|
|
69
|
+
reqUrl = `${reqUrl}${fullQuery ? `?${fullQuery}` : ""}`;
|
|
70
|
+
} else {
|
|
71
|
+
reqBody = JSON.stringify(data);
|
|
72
|
+
}
|
|
73
|
+
const reqHeaders = __spreadValues({
|
|
74
|
+
Accept: "application/json"
|
|
75
|
+
}, headers ? headers : {});
|
|
76
|
+
const response = yield fetch(reqUrl, {
|
|
77
|
+
method,
|
|
78
|
+
headers: reqHeaders,
|
|
79
|
+
body: reqBody,
|
|
80
|
+
mode: "cors"
|
|
81
|
+
});
|
|
82
|
+
const result = yield response.json();
|
|
83
|
+
if (!response.ok) {
|
|
84
|
+
if (result && result.error) {
|
|
85
|
+
throw new Error(`${response.status}: ${result.error}`);
|
|
86
|
+
} else {
|
|
87
|
+
throw new Error("A connection error occurred while making the request");
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return result;
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
exports.request = request;
|
|
95
|
+
//# sourceMappingURL=request.js.map
|
|
@@ -0,0 +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":["stringify","trimEnd","trimStart"],"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,C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