@rechargeapps/storefront-client 0.8.1 → 0.10.0
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 +0 -16
- package/dist/cjs/api/auth.js +10 -5
- package/dist/cjs/api/auth.js.map +1 -1
- package/dist/cjs/api/bundle.js +1 -1
- package/dist/cjs/api/bundle.js.map +1 -1
- package/dist/cjs/api/cdn.js +21 -21
- package/dist/cjs/api/cdn.js.map +1 -1
- package/dist/cjs/api/charge.js +0 -5
- package/dist/cjs/api/charge.js.map +1 -1
- package/dist/cjs/api/{memberships.js → membership.js} +1 -1
- package/dist/cjs/api/membership.js.map +1 -0
- package/dist/cjs/api/plan.js +19 -0
- package/dist/cjs/api/plan.js.map +1 -0
- package/dist/cjs/index.js +16 -14
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/utils/init.js +2 -1
- package/dist/cjs/utils/init.js.map +1 -1
- package/dist/cjs/utils/request.js +3 -3
- package/dist/cjs/utils/request.js.map +1 -1
- package/dist/esm/api/auth.js +10 -5
- package/dist/esm/api/auth.js.map +1 -1
- package/dist/esm/api/bundle.js +2 -2
- package/dist/esm/api/bundle.js.map +1 -1
- package/dist/esm/api/cdn.js +14 -14
- package/dist/esm/api/cdn.js.map +1 -1
- package/dist/esm/api/charge.js +1 -5
- package/dist/esm/api/charge.js.map +1 -1
- package/dist/esm/api/{memberships.js → membership.js} +1 -1
- package/dist/esm/api/membership.js.map +1 -0
- package/dist/esm/api/plan.js +14 -0
- package/dist/esm/api/plan.js.map +1 -0
- package/dist/esm/index.js +4 -3
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/utils/init.js +3 -2
- package/dist/esm/utils/init.js.map +1 -1
- package/dist/esm/utils/request.js +4 -4
- package/dist/esm/utils/request.js.map +1 -1
- package/dist/index.d.ts +184 -85
- package/dist/umd/recharge-storefront-client.min.js +6 -6
- package/dist/umd/recharge-storefront-client.min.js.map +1 -1
- package/package.json +1 -1
- package/dist/cjs/api/memberships.js.map +0 -1
- package/dist/esm/api/memberships.js.map +0 -1
package/README.md
CHANGED
|
@@ -30,22 +30,6 @@ yarn add @rechargeapps/storefront-client
|
|
|
30
30
|
recharge.init({ storeIdentifier: 'storeIdentifier' });
|
|
31
31
|
```
|
|
32
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)`
|
|
48
|
-
|
|
49
33
|
## Testing with Shopify App Proxy
|
|
50
34
|
|
|
51
35
|
- Build UMD locally
|
package/dist/cjs/api/auth.js
CHANGED
|
@@ -7,17 +7,22 @@ var options = require('../utils/options.js');
|
|
|
7
7
|
var api = require('../constants/api.js');
|
|
8
8
|
|
|
9
9
|
async function loginShopifyAppProxy() {
|
|
10
|
-
const
|
|
11
|
-
|
|
10
|
+
const { storefrontAccessToken } = options.getOptions();
|
|
11
|
+
const headers = {};
|
|
12
|
+
if (storefrontAccessToken) {
|
|
13
|
+
headers["X-Recharge-Storefront-Access-Token"] = storefrontAccessToken;
|
|
14
|
+
}
|
|
15
|
+
const response = await request.shopifyAppProxyRequest("get", "/access", { headers });
|
|
16
|
+
return { apiToken: response.api_token, customerId: response.customer_id };
|
|
12
17
|
}
|
|
13
18
|
async function loginShopifyApi(customerAccessToken, storefrontAccessToken) {
|
|
14
|
-
const
|
|
15
|
-
const rechargeBaseUrl = api.RECHARGE_ADMIN_URL(
|
|
19
|
+
const { environment, storeIdentifier } = options.getOptions();
|
|
20
|
+
const rechargeBaseUrl = api.RECHARGE_ADMIN_URL(environment);
|
|
16
21
|
const response = await request.request("post", `${rechargeBaseUrl}/hydrogen_login_poc`, {
|
|
17
22
|
data: {
|
|
18
23
|
customer_token: customerAccessToken,
|
|
19
24
|
storefront_token: storefrontAccessToken,
|
|
20
|
-
shop_url:
|
|
25
|
+
shop_url: storeIdentifier
|
|
21
26
|
},
|
|
22
27
|
headers: {
|
|
23
28
|
"X-Recharge-App": "storefront-client"
|
package/dist/cjs/api/auth.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auth.js","sources":["../../../src/api/auth.ts"],"sourcesContent":["import { request as baseRequest, shopifyAppProxyRequest } from '../utils/request';\nimport { LoginResponse } from '../types/auth';\nimport { getOptions } from '../utils/options';\nimport { RECHARGE_ADMIN_URL } from '../constants/api';\nimport { Session } from '../types/session';\n\nexport async function loginShopifyAppProxy(): Promise<Session
|
|
1
|
+
{"version":3,"file":"auth.js","sources":["../../../src/api/auth.ts"],"sourcesContent":["import { request as baseRequest, shopifyAppProxyRequest } from '../utils/request';\nimport { LoginResponse } from '../types/auth';\nimport { getOptions } from '../utils/options';\nimport { RECHARGE_ADMIN_URL } from '../constants/api';\nimport { Session } from '../types/session';\nimport { RequestHeaders } from '../types';\n\nexport async function loginShopifyAppProxy(): Promise<Session> {\n const { storefrontAccessToken } = getOptions();\n const headers: RequestHeaders = {};\n if (storefrontAccessToken) {\n headers['X-Recharge-Storefront-Access-Token'] = storefrontAccessToken;\n }\n const response = await shopifyAppProxyRequest<LoginResponse>('get', '/access', { headers });\n\n return { apiToken: response.api_token, customerId: response.customer_id };\n}\n\nexport async function loginShopifyApi(\n customerAccessToken: string,\n storefrontAccessToken: string\n): Promise<Session | null> {\n const { environment, storeIdentifier } = getOptions();\n const rechargeBaseUrl = RECHARGE_ADMIN_URL(environment);\n const response = await baseRequest<LoginResponse>('post', `${rechargeBaseUrl}/hydrogen_login_poc`, {\n data: {\n customer_token: customerAccessToken,\n storefront_token: storefrontAccessToken,\n shop_url: storeIdentifier,\n },\n headers: {\n 'X-Recharge-App': 'storefront-client',\n },\n });\n\n return response.api_token ? { apiToken: response.api_token, customerId: response.customer_id } : null;\n}\n"],"names":["getOptions","shopifyAppProxyRequest","RECHARGE_ADMIN_URL","baseRequest"],"mappings":";;;;;;;;AAGO,eAAe,oBAAoB,GAAG;AAC7C,EAAE,MAAM,EAAE,qBAAqB,EAAE,GAAGA,kBAAU,EAAE,CAAC;AACjD,EAAE,MAAM,OAAO,GAAG,EAAE,CAAC;AACrB,EAAE,IAAI,qBAAqB,EAAE;AAC7B,IAAI,OAAO,CAAC,oCAAoC,CAAC,GAAG,qBAAqB,CAAC;AAC1E,GAAG;AACH,EAAE,MAAM,QAAQ,GAAG,MAAMC,8BAAsB,CAAC,KAAK,EAAE,SAAS,EAAE,EAAE,OAAO,EAAE,CAAC,CAAC;AAC/E,EAAE,OAAO,EAAE,QAAQ,EAAE,QAAQ,CAAC,SAAS,EAAE,UAAU,EAAE,QAAQ,CAAC,WAAW,EAAE,CAAC;AAC5E,CAAC;AACM,eAAe,eAAe,CAAC,mBAAmB,EAAE,qBAAqB,EAAE;AAClF,EAAE,MAAM,EAAE,WAAW,EAAE,eAAe,EAAE,GAAGD,kBAAU,EAAE,CAAC;AACxD,EAAE,MAAM,eAAe,GAAGE,sBAAkB,CAAC,WAAW,CAAC,CAAC;AAC1D,EAAE,MAAM,QAAQ,GAAG,MAAMC,eAAW,CAAC,MAAM,EAAE,CAAC,EAAE,eAAe,CAAC,mBAAmB,CAAC,EAAE;AACtF,IAAI,IAAI,EAAE;AACV,MAAM,cAAc,EAAE,mBAAmB;AACzC,MAAM,gBAAgB,EAAE,qBAAqB;AAC7C,MAAM,QAAQ,EAAE,eAAe;AAC/B,KAAK;AACL,IAAI,OAAO,EAAE;AACb,MAAM,gBAAgB,EAAE,mBAAmB;AAC3C,KAAK;AACL,GAAG,CAAC,CAAC;AACL,EAAE,OAAO,QAAQ,CAAC,SAAS,GAAG,EAAE,QAAQ,EAAE,QAAQ,CAAC,SAAS,EAAE,UAAU,EAAE,QAAQ,CAAC,WAAW,EAAE,GAAG,IAAI,CAAC;AACxG;;;;;"}
|
package/dist/cjs/api/bundle.js
CHANGED
|
@@ -61,7 +61,7 @@ async function getBundleId(bundle) {
|
|
|
61
61
|
}
|
|
62
62
|
async function validateBundle(bundle) {
|
|
63
63
|
try {
|
|
64
|
-
const bundleSettings = await cdn.
|
|
64
|
+
const bundleSettings = await cdn.getCDNBundleSettings(bundle.externalProductId);
|
|
65
65
|
return !!bundle && !!bundleSettings;
|
|
66
66
|
} catch (e) {
|
|
67
67
|
console.error("Error fetching bundle settings");
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bundle.js","sources":["../../../src/api/bundle.ts"],"sourcesContent":["import { toLineItemProperty } from '@recharge-packages/bundling-data';\nimport { Bundle } from '../types';\nimport { shopifyAppProxyRequest } from '../utils/request';\nimport { getOptions } from '../utils/options';\nimport {
|
|
1
|
+
{"version":3,"file":"bundle.js","sources":["../../../src/api/bundle.ts"],"sourcesContent":["import { toLineItemProperty } from '@recharge-packages/bundling-data';\nimport { Bundle } from '../types';\nimport { shopifyAppProxyRequest } from '../utils/request';\nimport { getOptions } from '../utils/options';\nimport { getCDNBundleSettings } from './cdn';\n\nconst STORE_FRONT_MANAGER_URL = '/bundling-storefront-manager';\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(): 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 try {\n const { timestamp } = await shopifyAppProxyRequest<{ timestamp: number }>('get', `${STORE_FRONT_MANAGER_URL}/t`, {\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 async function getBundleId(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();\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 payload = await shopifyAppProxyRequest<{ id: string; code: number; message: string }>(\n 'post',\n `${STORE_FRONT_MANAGER_URL}/api/v1/bundles`,\n {\n data: {\n bundle: bundleData,\n },\n headers: {\n Origin: `https://${opts.storeIdentifier}`,\n },\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 async function validateBundle(bundle: Bundle): Promise<boolean> {\n try {\n const bundleSettings = await getCDNBundleSettings(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":["shopifyAppProxyRequest","getOptions","toLineItemProperty","getCDNBundleSettings"],"mappings":";;;;;;;;;AAIA,MAAM,uBAAuB,GAAG,8BAA8B,CAAC;AAC/D,SAAS,6BAA6B,GAAG;AACzC,EAAE,OAAO,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,GAAG,CAAC,CAAC;AACrC,CAAC;AACD,eAAe,6BAA6B,GAAG;AAC/C,EAAE,IAAI;AACN,IAAI,MAAM,EAAE,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I,MAAM,OAAO,GAAG,MAAMF,8BAAsB,CAAC,MAAM,EAAE,CAAC,EAAE,uBAAuB,CAAC,eAAe,CAAC,EAAE;AACtG,MAAM,IAAI,EAAE;AACZ,QAAQ,MAAM,EAAE,UAAU;AAC1B,OAAO;AACP,MAAM,OAAO,EAAE;AACf,QAAQ,MAAM,EAAE,CAAC,QAAQ,EAAE,IAAI,CAAC,eAAe,CAAC,CAAC;AACjD,OAAO;AACP,KAAK,CAAC,CAAC;AACP,IAAI,IAAI,CAAC,OAAO,CAAC,EAAE,IAAI,OAAO,CAAC,IAAI,KAAK,GAAG,EAAE;AAC7C,MAAM,MAAM,IAAI,KAAK,CAAC,CAAC,4BAA4B,EAAE,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC;AAChF,KAAK;AACL,IAAI,OAAO,OAAO,CAAC,EAAE,CAAC;AACtB,GAAG,CAAC,OAAO,CAAC,EAAE;AACd,IAAI,MAAM,IAAI,KAAK,CAAC,CAAC,2BAA2B,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;AACvD,GAAG;AACH,CAAC;AACM,eAAe,cAAc,CAAC,MAAM,EAAE;AAC7C,EAAE,IAAI;AACN,IAAI,MAAM,cAAc,GAAG,MAAMG,wBAAoB,CAAC,MAAM,CAAC,iBAAiB,CAAC,CAAC;AAChF,IAAI,OAAO,CAAC,CAAC,MAAM,IAAI,CAAC,CAAC,cAAc,CAAC;AACxC,GAAG,CAAC,OAAO,CAAC,EAAE;AACd,IAAI,OAAO,CAAC,KAAK,CAAC,gCAAgC,CAAC,CAAC;AACpD,IAAI,OAAO,KAAK,CAAC;AACjB,GAAG;AACH;;;;;"}
|
package/dist/cjs/api/cdn.js
CHANGED
|
@@ -22,22 +22,22 @@ function cacheRequest(cacheKey, request) {
|
|
|
22
22
|
}
|
|
23
23
|
return promiseCache.get(cacheKey);
|
|
24
24
|
}
|
|
25
|
-
async function
|
|
25
|
+
async function getCDNProduct(externalProductId) {
|
|
26
26
|
const { product } = await cacheRequest(`product.${externalProductId}`, () => request.cdnRequest("get", `/product/${CDN_VERSION}/${externalProductId}.json`));
|
|
27
27
|
return cdn.productMapper(product);
|
|
28
28
|
}
|
|
29
|
-
async function
|
|
29
|
+
async function getCDNStoreSettings() {
|
|
30
30
|
const storeSettings = await cacheRequest("storeSettings", () => request.cdnRequest("get", `/${CDN_VERSION}/store_settings.json`).catch(() => {
|
|
31
31
|
return DEFAULT_STORE_SETTINGS;
|
|
32
32
|
}));
|
|
33
33
|
return storeSettings;
|
|
34
34
|
}
|
|
35
|
-
async function
|
|
35
|
+
async function getCDNWidgetSettings() {
|
|
36
36
|
const { widget_settings } = await cacheRequest("widgetSettings", () => request.cdnRequest("get", `/${CDN_VERSION}/widget_settings.json`));
|
|
37
37
|
const widgetSettings = cdn.widgetSettingsMapper(widget_settings);
|
|
38
38
|
return widgetSettings;
|
|
39
39
|
}
|
|
40
|
-
async function
|
|
40
|
+
async function getCDNProductsAndSettings() {
|
|
41
41
|
const { products, widget_settings, store_settings, meta } = await cacheRequest("productsAndSettings", () => request.cdnRequest("get", `/product/${CDN_VERSION}/products.json`));
|
|
42
42
|
if ((meta == null ? void 0 : meta.status) === "error") {
|
|
43
43
|
return Promise.reject(meta.message);
|
|
@@ -48,15 +48,15 @@ async function getProductsAndSettings() {
|
|
|
48
48
|
store_settings: store_settings != null ? store_settings : {}
|
|
49
49
|
};
|
|
50
50
|
}
|
|
51
|
-
async function
|
|
52
|
-
const { products } = await
|
|
51
|
+
async function getCDNProducts() {
|
|
52
|
+
const { products } = await getCDNProductsAndSettings();
|
|
53
53
|
return products;
|
|
54
54
|
}
|
|
55
|
-
async function
|
|
55
|
+
async function getCDNProductAndSettings(externalProductId) {
|
|
56
56
|
const [product, store_settings, widget_settings] = await Promise.all([
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
57
|
+
getCDNProduct(externalProductId),
|
|
58
|
+
getCDNStoreSettings(),
|
|
59
|
+
getCDNWidgetSettings()
|
|
60
60
|
]);
|
|
61
61
|
return {
|
|
62
62
|
product,
|
|
@@ -66,20 +66,20 @@ async function getProductAndSettings(externalProductId) {
|
|
|
66
66
|
widgetSettings: widget_settings
|
|
67
67
|
};
|
|
68
68
|
}
|
|
69
|
-
async function
|
|
70
|
-
const { bundle_product } = await
|
|
69
|
+
async function getCDNBundleSettings(externalProductId) {
|
|
70
|
+
const { bundle_product } = await getCDNProduct(externalProductId);
|
|
71
71
|
return bundle_product;
|
|
72
72
|
}
|
|
73
|
-
async function
|
|
73
|
+
async function resetCDNCache() {
|
|
74
74
|
return Array.from(promiseCache.keys()).forEach((key) => promiseCache.delete(key));
|
|
75
75
|
}
|
|
76
76
|
|
|
77
|
-
exports.
|
|
78
|
-
exports.
|
|
79
|
-
exports.
|
|
80
|
-
exports.
|
|
81
|
-
exports.
|
|
82
|
-
exports.
|
|
83
|
-
exports.
|
|
84
|
-
exports.
|
|
77
|
+
exports.getCDNBundleSettings = getCDNBundleSettings;
|
|
78
|
+
exports.getCDNProduct = getCDNProduct;
|
|
79
|
+
exports.getCDNProductAndSettings = getCDNProductAndSettings;
|
|
80
|
+
exports.getCDNProducts = getCDNProducts;
|
|
81
|
+
exports.getCDNProductsAndSettings = getCDNProductsAndSettings;
|
|
82
|
+
exports.getCDNStoreSettings = getCDNStoreSettings;
|
|
83
|
+
exports.getCDNWidgetSettings = getCDNWidgetSettings;
|
|
84
|
+
exports.resetCDNCache = resetCDNCache;
|
|
85
85
|
//# sourceMappingURL=cdn.js.map
|
package/dist/cjs/api/cdn.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cdn.js","sources":["../../../src/api/cdn.ts"],"sourcesContent":["import {\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 { cdnRequest } from '../utils/request';\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 async function
|
|
1
|
+
{"version":3,"file":"cdn.js","sources":["../../../src/api/cdn.ts"],"sourcesContent":["import {\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 { cdnRequest } from '../utils/request';\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 async function getCDNProduct(externalProductId: string | number): Promise<CDNProduct> {\n const { product } = await cacheRequest(`product.${externalProductId}`, () =>\n cdnRequest<CDNProductResource>('get', `/product/${CDN_VERSION}/${externalProductId}.json`)\n );\n\n return productMapper(product);\n}\n\nexport async function getCDNStoreSettings(): Promise<CDNStoreSettings> {\n const storeSettings = await cacheRequest('storeSettings', () =>\n cdnRequest<CDNStoreSettings>('get', `/${CDN_VERSION}/store_settings.json`).catch(() => {\n return DEFAULT_STORE_SETTINGS;\n })\n );\n return storeSettings;\n}\n\nexport async function getCDNWidgetSettings(): Promise<CDNWidgetSettings> {\n const { widget_settings } = await cacheRequest('widgetSettings', () =>\n cdnRequest<CDNWidgetSettingsResource>('get', `/${CDN_VERSION}/widget_settings.json`)\n );\n\n const widgetSettings = widgetSettingsMapper(widget_settings);\n\n return widgetSettings;\n}\n\nexport async function getCDNProductsAndSettings(): Promise<CDNProductsAndSettings> {\n const { products, widget_settings, store_settings, meta } = await cacheRequest('productsAndSettings', () =>\n cdnRequest<CDNProductsAndSettingsResource>('get', `/product/${CDN_VERSION}/products.json`)\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: store_settings ?? {},\n };\n}\n\nexport async function getCDNProducts(): Promise<CDNProductKeyObject[]> {\n const { products } = await getCDNProductsAndSettings();\n return products;\n}\n\nexport async function getCDNProductAndSettings(externalProductId: string | number): Promise<CDNProductAndSettings> {\n const [product, store_settings, widget_settings] = await Promise.all([\n getCDNProduct(externalProductId),\n getCDNStoreSettings(),\n getCDNWidgetSettings(),\n ]);\n\n return {\n product,\n store_settings,\n widget_settings,\n storeSettings: store_settings,\n widgetSettings: widget_settings,\n };\n}\n\nexport async function getCDNBundleSettings(\n externalProductId: string | number\n): Promise<CDNBundleSettings | null | undefined> {\n const { bundle_product } = await getCDNProduct(externalProductId);\n\n return bundle_product;\n}\n\nexport async function resetCDNCache() {\n return Array.from(promiseCache.keys()).forEach(key => promiseCache.delete(key));\n}\n"],"names":["cdnRequest","productMapper","widgetSettingsMapper","productArrayMapper"],"mappings":";;;;;;;AAEA,MAAM,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;AACpC,IAAI,mBAAmB,EAAE;AACzB,IAAI,oBAAoB,EAAE;AAC1B,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,CAAC;AACM,eAAe,oBAAoB,CAAC,iBAAiB,EAAE;AAC9D,EAAE,MAAM,EAAE,cAAc,EAAE,GAAG,MAAM,aAAa,CAAC,iBAAiB,CAAC,CAAC;AACpE,EAAE,OAAO,cAAc,CAAC;AACxB,CAAC;AACM,eAAe,aAAa,GAAG;AACtC,EAAE,OAAO,KAAK,CAAC,IAAI,CAAC,YAAY,CAAC,IAAI,EAAE,CAAC,CAAC,OAAO,CAAC,CAAC,GAAG,KAAK,YAAY,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC;AACpF;;;;;;;;;;;"}
|
package/dist/cjs/api/charge.js
CHANGED
|
@@ -29,15 +29,10 @@ async function unskipCharge(session, id) {
|
|
|
29
29
|
const { charge } = await request.rechargeApiRequest("post", `/charges/${id}/unskip`, {}, session);
|
|
30
30
|
return charge;
|
|
31
31
|
}
|
|
32
|
-
async function processCharge(session, id) {
|
|
33
|
-
const { charge } = await request.rechargeApiRequest("post", `/charges/${id}/process`, {}, session);
|
|
34
|
-
return charge;
|
|
35
|
-
}
|
|
36
32
|
|
|
37
33
|
exports.applyDiscount = applyDiscount;
|
|
38
34
|
exports.getCharge = getCharge;
|
|
39
35
|
exports.listCharges = listCharges;
|
|
40
|
-
exports.processCharge = processCharge;
|
|
41
36
|
exports.removeDiscount = removeDiscount;
|
|
42
37
|
exports.skipCharge = skipCharge;
|
|
43
38
|
exports.unskipCharge = unskipCharge;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"charge.js","sources":["../../../src/api/charge.ts"],"sourcesContent":["import { ApplyDiscountRequest, ChargeListParams, ChargeListResponse, ChargeResponse } from '../types/charge';\nimport { Session } from '../types/session';\nimport { rechargeApiRequest } from '../utils/request';\n\n/* Retrieve a Charge using the charge_id. */\nexport async function getCharge(session: Session, id: number | string) {\n const { charge } = await rechargeApiRequest<ChargeResponse>('get', `/charges`, { id }, session);\n return charge;\n}\n\n/** Lists charges */\nexport function listCharges(session: Session, query?: ChargeListParams) {\n return rechargeApiRequest<ChargeListResponse>('get', `/charges`, { query }, session);\n}\n\n/**\n * You cannot add a Discount to an existing queued Charge if the Charge or the associated Address already has one.\n * You can provide either discount_id or discount_code. If both parameters are passed, the value for discount_id will take precedence.\n * If a Charge has a Discount and it gets updated, or a regeneration occurs, the Discount will be lost. Regeneration is a process that refreshes the Charge JSON with new data in the case of the Subscription or Address being updated.\n */\nexport async function applyDiscount(session: Session, id: number | string, applyRequest: ApplyDiscountRequest) {\n const { charge } = await rechargeApiRequest<ChargeResponse>(\n 'post',\n `/charges/${id}/apply_discount`,\n {\n data: applyRequest,\n },\n session\n );\n return charge;\n}\n\n/**\n * Remove a Discount from a Charge without destroying the Discount.\n * In most cases the Discount should be removed from the Address. When the Discount is removed from the Address, the Discount is also removed from any future Charges.\n * If the Discount is on the parent Address, you cannot remove it using charge_id.\n * When removing your Discount, it is preferable to pass the address_id so that the Discount stays removed if the Charge is regenerated. Only pass charge_id in edge cases in which there are two or more Charges on a parent Address and you only want to remove the Discount from one Charge.\n * If you pass both parameters, it will remove the Discount from the Address.\n */\nexport async function removeDiscount(session: Session, id: number | string) {\n const { charge } = await rechargeApiRequest<ChargeResponse>('post', `/charges/${id}/remove_discount`, {}, session);\n return charge;\n}\n\n/* Skip a Charge. */\nexport async function skipCharge(session: Session, id: number | string) {\n const { charge } = await rechargeApiRequest<ChargeResponse>('post', `/charges/${id}/skip`, {}, session);\n return charge;\n}\n\n/* Unskip a Charge. */\nexport async function unskipCharge(session: Session, id: number | string) {\n const { charge } = await rechargeApiRequest<ChargeResponse>('post', `/charges/${id}/unskip`, {}, session);\n return charge;\n}\n
|
|
1
|
+
{"version":3,"file":"charge.js","sources":["../../../src/api/charge.ts"],"sourcesContent":["import { ApplyDiscountRequest, ChargeListParams, ChargeListResponse, ChargeResponse } from '../types/charge';\nimport { Session } from '../types/session';\nimport { rechargeApiRequest } from '../utils/request';\n\n/* Retrieve a Charge using the charge_id. */\nexport async function getCharge(session: Session, id: number | string) {\n const { charge } = await rechargeApiRequest<ChargeResponse>('get', `/charges`, { id }, session);\n return charge;\n}\n\n/** Lists charges */\nexport function listCharges(session: Session, query?: ChargeListParams) {\n return rechargeApiRequest<ChargeListResponse>('get', `/charges`, { query }, session);\n}\n\n/**\n * You cannot add a Discount to an existing queued Charge if the Charge or the associated Address already has one.\n * You can provide either discount_id or discount_code. If both parameters are passed, the value for discount_id will take precedence.\n * If a Charge has a Discount and it gets updated, or a regeneration occurs, the Discount will be lost. Regeneration is a process that refreshes the Charge JSON with new data in the case of the Subscription or Address being updated.\n */\nexport async function applyDiscount(session: Session, id: number | string, applyRequest: ApplyDiscountRequest) {\n const { charge } = await rechargeApiRequest<ChargeResponse>(\n 'post',\n `/charges/${id}/apply_discount`,\n {\n data: applyRequest,\n },\n session\n );\n return charge;\n}\n\n/**\n * Remove a Discount from a Charge without destroying the Discount.\n * In most cases the Discount should be removed from the Address. When the Discount is removed from the Address, the Discount is also removed from any future Charges.\n * If the Discount is on the parent Address, you cannot remove it using charge_id.\n * When removing your Discount, it is preferable to pass the address_id so that the Discount stays removed if the Charge is regenerated. Only pass charge_id in edge cases in which there are two or more Charges on a parent Address and you only want to remove the Discount from one Charge.\n * If you pass both parameters, it will remove the Discount from the Address.\n */\nexport async function removeDiscount(session: Session, id: number | string) {\n const { charge } = await rechargeApiRequest<ChargeResponse>('post', `/charges/${id}/remove_discount`, {}, session);\n return charge;\n}\n\n/* Skip a Charge. */\nexport async function skipCharge(session: Session, id: number | string) {\n const { charge } = await rechargeApiRequest<ChargeResponse>('post', `/charges/${id}/skip`, {}, session);\n return charge;\n}\n\n/* Unskip a Charge. */\nexport async function unskipCharge(session: Session, id: number | string) {\n const { charge } = await rechargeApiRequest<ChargeResponse>('post', `/charges/${id}/unskip`, {}, session);\n return charge;\n}\n"],"names":["rechargeApiRequest"],"mappings":";;;;;;AACO,eAAe,SAAS,CAAC,OAAO,EAAE,EAAE,EAAE;AAC7C,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,MAAMA,0BAAkB,CAAC,KAAK,EAAE,CAAC,QAAQ,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE,OAAO,CAAC,CAAC;AAClF,EAAE,OAAO,MAAM,CAAC;AAChB,CAAC;AACM,SAAS,WAAW,CAAC,OAAO,EAAE,KAAK,EAAE;AAC5C,EAAE,OAAOA,0BAAkB,CAAC,KAAK,EAAE,CAAC,QAAQ,CAAC,EAAE,EAAE,KAAK,EAAE,EAAE,OAAO,CAAC,CAAC;AACnE,CAAC;AACM,eAAe,aAAa,CAAC,OAAO,EAAE,EAAE,EAAE,YAAY,EAAE;AAC/D,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,MAAMA,0BAAkB,CAAC,MAAM,EAAE,CAAC,SAAS,EAAE,EAAE,CAAC,eAAe,CAAC,EAAE;AACvF,IAAI,IAAI,EAAE,YAAY;AACtB,GAAG,EAAE,OAAO,CAAC,CAAC;AACd,EAAE,OAAO,MAAM,CAAC;AAChB,CAAC;AACM,eAAe,cAAc,CAAC,OAAO,EAAE,EAAE,EAAE;AAClD,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,MAAMA,0BAAkB,CAAC,MAAM,EAAE,CAAC,SAAS,EAAE,EAAE,CAAC,gBAAgB,CAAC,EAAE,EAAE,EAAE,OAAO,CAAC,CAAC;AACrG,EAAE,OAAO,MAAM,CAAC;AAChB,CAAC;AACM,eAAe,UAAU,CAAC,OAAO,EAAE,EAAE,EAAE;AAC9C,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,MAAMA,0BAAkB,CAAC,MAAM,EAAE,CAAC,SAAS,EAAE,EAAE,CAAC,KAAK,CAAC,EAAE,EAAE,EAAE,OAAO,CAAC,CAAC;AAC1F,EAAE,OAAO,MAAM,CAAC;AAChB,CAAC;AACM,eAAe,YAAY,CAAC,OAAO,EAAE,EAAE,EAAE;AAChD,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,MAAMA,0BAAkB,CAAC,MAAM,EAAE,CAAC,SAAS,EAAE,EAAE,CAAC,OAAO,CAAC,EAAE,EAAE,EAAE,OAAO,CAAC,CAAC;AAC5F,EAAE,OAAO,MAAM,CAAC;AAChB;;;;;;;;;"}
|
|
@@ -28,4 +28,4 @@ exports.activateMembership = activateMembership;
|
|
|
28
28
|
exports.cancelMembership = cancelMembership;
|
|
29
29
|
exports.getMembership = getMembership;
|
|
30
30
|
exports.listMemberships = listMemberships;
|
|
31
|
-
//# sourceMappingURL=
|
|
31
|
+
//# sourceMappingURL=membership.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"membership.js","sources":["../../../src/api/membership.ts"],"sourcesContent":["import {\n ActivateMembershipRequest,\n CancelMembershipRequest,\n MembershipListParams,\n MembershipListResponse,\n MembershipResponse,\n} from '../types/membership';\nimport { Session } from '../types/session';\nimport { rechargeApiRequest } from '../utils/request';\n\n/** Retrieves membership information for passed in id */\nexport async function getMembership(session: Session, id: string | number) {\n const { membership } = await rechargeApiRequest<MembershipResponse>('get', `/memberships`, { id }, session);\n return membership;\n}\n\n/** Retrieves a list of memberships */\nexport function listMemberships(session: Session, query?: MembershipListParams) {\n return rechargeApiRequest<MembershipListResponse>('get', `/memberships`, { query }, session);\n}\n\n/** Cancels a membership */\nexport async function cancelMembership(session: Session, id: string | number, cancelRequest: CancelMembershipRequest) {\n const { membership } = await rechargeApiRequest<MembershipResponse>(\n 'post',\n `/memberships/${id}/cancel`,\n {\n data: cancelRequest,\n },\n session\n );\n return membership;\n}\n\n/** Activates a membership */\nexport async function activateMembership(\n session: Session,\n id: string | number,\n activateRequest: ActivateMembershipRequest\n) {\n const { membership } = await rechargeApiRequest<MembershipResponse>(\n 'post',\n `/memberships/${id}/activate`,\n {\n data: activateRequest,\n },\n session\n );\n return membership;\n}\n"],"names":["rechargeApiRequest"],"mappings":";;;;;;AACO,eAAe,aAAa,CAAC,OAAO,EAAE,EAAE,EAAE;AACjD,EAAE,MAAM,EAAE,UAAU,EAAE,GAAG,MAAMA,0BAAkB,CAAC,KAAK,EAAE,CAAC,YAAY,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE,OAAO,CAAC,CAAC;AAC1F,EAAE,OAAO,UAAU,CAAC;AACpB,CAAC;AACM,SAAS,eAAe,CAAC,OAAO,EAAE,KAAK,EAAE;AAChD,EAAE,OAAOA,0BAAkB,CAAC,KAAK,EAAE,CAAC,YAAY,CAAC,EAAE,EAAE,KAAK,EAAE,EAAE,OAAO,CAAC,CAAC;AACvE,CAAC;AACM,eAAe,gBAAgB,CAAC,OAAO,EAAE,EAAE,EAAE,aAAa,EAAE;AACnE,EAAE,MAAM,EAAE,UAAU,EAAE,GAAG,MAAMA,0BAAkB,CAAC,MAAM,EAAE,CAAC,aAAa,EAAE,EAAE,CAAC,OAAO,CAAC,EAAE;AACvF,IAAI,IAAI,EAAE,aAAa;AACvB,GAAG,EAAE,OAAO,CAAC,CAAC;AACd,EAAE,OAAO,UAAU,CAAC;AACpB,CAAC;AACM,eAAe,kBAAkB,CAAC,OAAO,EAAE,EAAE,EAAE,eAAe,EAAE;AACvE,EAAE,MAAM,EAAE,UAAU,EAAE,GAAG,MAAMA,0BAAkB,CAAC,MAAM,EAAE,CAAC,aAAa,EAAE,EAAE,CAAC,SAAS,CAAC,EAAE;AACzF,IAAI,IAAI,EAAE,eAAe;AACzB,GAAG,EAAE,OAAO,CAAC,CAAC;AACd,EAAE,OAAO,UAAU,CAAC;AACpB;;;;;;;"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var request = require('../utils/request.js');
|
|
6
|
+
|
|
7
|
+
async function getPlan(session, id) {
|
|
8
|
+
const { plan } = await request.rechargeApiRequest("get", `/plans`, {
|
|
9
|
+
id
|
|
10
|
+
}, session);
|
|
11
|
+
return plan;
|
|
12
|
+
}
|
|
13
|
+
function listPlans(session, query) {
|
|
14
|
+
return request.rechargeApiRequest("get", `/plans`, { query }, session);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
exports.getPlan = getPlan;
|
|
18
|
+
exports.listPlans = listPlans;
|
|
19
|
+
//# sourceMappingURL=plan.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plan.js","sources":["../../../src/api/plan.ts"],"sourcesContent":["import { Plan, PlanListParams, PlansResponse } from '../types/plan';\nimport { Session } from '../types/session';\nimport { rechargeApiRequest } from '../utils/request';\n\nexport async function getPlan(session: Session, id: string | number): Promise<Plan> {\n const { plan } = await rechargeApiRequest<{ plan: Plan }>(\n 'get',\n `/plans`,\n {\n id,\n },\n session\n );\n return plan;\n}\n\nexport function listPlans(session: Session, query?: PlanListParams): Promise<PlansResponse> {\n return rechargeApiRequest<PlansResponse>('get', `/plans`, { query }, session);\n}\n"],"names":["rechargeApiRequest"],"mappings":";;;;;;AACO,eAAe,OAAO,CAAC,OAAO,EAAE,EAAE,EAAE;AAC3C,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG,MAAMA,0BAAkB,CAAC,KAAK,EAAE,CAAC,MAAM,CAAC,EAAE;AAC7D,IAAI,EAAE;AACN,GAAG,EAAE,OAAO,CAAC,CAAC;AACd,EAAE,OAAO,IAAI,CAAC;AACd,CAAC;AACM,SAAS,SAAS,CAAC,OAAO,EAAE,KAAK,EAAE;AAC1C,EAAE,OAAOA,0BAAkB,CAAC,KAAK,EAAE,CAAC,MAAM,CAAC,EAAE,EAAE,KAAK,EAAE,EAAE,OAAO,CAAC,CAAC;AACjE;;;;;"}
|
package/dist/cjs/index.js
CHANGED
|
@@ -7,9 +7,10 @@ var auth = require('./api/auth.js');
|
|
|
7
7
|
var charge = require('./api/charge.js');
|
|
8
8
|
var cdn = require('./api/cdn.js');
|
|
9
9
|
var bundle = require('./api/bundle.js');
|
|
10
|
-
var
|
|
10
|
+
var membership = require('./api/membership.js');
|
|
11
11
|
var onetime = require('./api/onetime.js');
|
|
12
12
|
var order = require('./api/order.js');
|
|
13
|
+
var plan = require('./api/plan.js');
|
|
13
14
|
var subscription = require('./api/subscription.js');
|
|
14
15
|
var customer = require('./api/customer.js');
|
|
15
16
|
var init = require('./utils/init.js');
|
|
@@ -28,24 +29,23 @@ exports.loginShopifyAppProxy = auth.loginShopifyAppProxy;
|
|
|
28
29
|
exports.applyDiscount = charge.applyDiscount;
|
|
29
30
|
exports.getCharge = charge.getCharge;
|
|
30
31
|
exports.listCharges = charge.listCharges;
|
|
31
|
-
exports.processCharge = charge.processCharge;
|
|
32
32
|
exports.removeDiscount = charge.removeDiscount;
|
|
33
33
|
exports.skipCharge = charge.skipCharge;
|
|
34
34
|
exports.unskipCharge = charge.unskipCharge;
|
|
35
|
-
exports.
|
|
36
|
-
exports.
|
|
37
|
-
exports.
|
|
38
|
-
exports.
|
|
39
|
-
exports.
|
|
40
|
-
exports.
|
|
41
|
-
exports.
|
|
42
|
-
exports.
|
|
35
|
+
exports.getCDNBundleSettings = cdn.getCDNBundleSettings;
|
|
36
|
+
exports.getCDNProduct = cdn.getCDNProduct;
|
|
37
|
+
exports.getCDNProductAndSettings = cdn.getCDNProductAndSettings;
|
|
38
|
+
exports.getCDNProducts = cdn.getCDNProducts;
|
|
39
|
+
exports.getCDNProductsAndSettings = cdn.getCDNProductsAndSettings;
|
|
40
|
+
exports.getCDNStoreSettings = cdn.getCDNStoreSettings;
|
|
41
|
+
exports.getCDNWidgetSettings = cdn.getCDNWidgetSettings;
|
|
42
|
+
exports.resetCDNCache = cdn.resetCDNCache;
|
|
43
43
|
exports.getBundleId = bundle.getBundleId;
|
|
44
44
|
exports.validateBundle = bundle.validateBundle;
|
|
45
|
-
exports.activateMembership =
|
|
46
|
-
exports.cancelMembership =
|
|
47
|
-
exports.getMembership =
|
|
48
|
-
exports.listMemberships =
|
|
45
|
+
exports.activateMembership = membership.activateMembership;
|
|
46
|
+
exports.cancelMembership = membership.cancelMembership;
|
|
47
|
+
exports.getMembership = membership.getMembership;
|
|
48
|
+
exports.listMemberships = membership.listMemberships;
|
|
49
49
|
exports.createOnetime = onetime.createOnetime;
|
|
50
50
|
exports.deleteOnetime = onetime.deleteOnetime;
|
|
51
51
|
exports.getOnetime = onetime.getOnetime;
|
|
@@ -53,6 +53,8 @@ exports.listOnetimes = onetime.listOnetimes;
|
|
|
53
53
|
exports.updateOnetime = onetime.updateOnetime;
|
|
54
54
|
exports.getOrder = order.getOrder;
|
|
55
55
|
exports.listOrders = order.listOrders;
|
|
56
|
+
exports.getPlan = plan.getPlan;
|
|
57
|
+
exports.listPlans = plan.listPlans;
|
|
56
58
|
exports.activateSubscription = subscription.activateSubscription;
|
|
57
59
|
exports.cancelSubscription = subscription.cancelSubscription;
|
|
58
60
|
exports.createSubscription = subscription.createSubscription;
|
package/dist/cjs/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
package/dist/cjs/utils/init.js
CHANGED
|
@@ -44,9 +44,10 @@ function initRecharge(opt = {}) {
|
|
|
44
44
|
const hiddenOpts = opt;
|
|
45
45
|
options.setOptions({
|
|
46
46
|
storeIdentifier: getStoreIdentifier(opt.storeIdentifier),
|
|
47
|
+
storefrontAccessToken: opt.storefrontAccessToken,
|
|
47
48
|
environment: hiddenOpts.environment ? hiddenOpts.environment : "prod"
|
|
48
49
|
});
|
|
49
|
-
cdn.
|
|
50
|
+
cdn.resetCDNCache();
|
|
50
51
|
}
|
|
51
52
|
|
|
52
53
|
exports.api = api;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"init.js","sources":["../../../src/utils/init.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"file":"init.js","sources":["../../../src/utils/init.ts"],"sourcesContent":["import { resetCDNCache } from '../api/cdn';\nimport { StorefrontOptions, CRUDRequestOptions, GetRequestOptions, InitOptions } from '../types';\nimport { setOptions } from './options';\nimport { request } from './request';\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\n/**\n * Uses passed in storeIdentifier, but if it's not passed in will try to infer it.\n * Currently it will only infer if we are in the context of a Shopify store. This will not infer headless or hosted yet.\n */\nfunction getStoreIdentifier(storeIdentifier?: string): string {\n if (storeIdentifier) {\n return storeIdentifier;\n }\n\n // Infer's when on Shopify store (non headless)\n if (window?.Shopify?.shop) {\n return window.Shopify.shop;\n }\n\n // Domain exists on hosted themes. If it doesn't for some reason, get the subdomain and create the identifier\n let domain = window?.domain;\n if (!domain) {\n const subdomain = location?.href\n .match(/(?:http[s]*:\\/\\/)*(.*?)\\.(?=admin\\.rechargeapps\\.com)/i)?.[1]\n .replace(/-sp$/, '');\n if (subdomain) {\n domain = `${subdomain}.myshopify.com`;\n }\n }\n\n // Infer's when on Recharge hosted\n if (domain) {\n return domain;\n }\n\n throw new Error(`No storeIdentifier was passed into init.`);\n}\n\nexport function initRecharge(opt: InitOptions = {}) {\n const hiddenOpts = opt as StorefrontOptions;\n setOptions({\n storeIdentifier: getStoreIdentifier(opt.storeIdentifier),\n storefrontAccessToken: opt.storefrontAccessToken,\n environment: hiddenOpts.environment ? hiddenOpts.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 resetCDNCache();\n}\n"],"names":["request","setOptions","resetCDNCache"],"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;AACF,SAAS,kBAAkB,CAAC,eAAe,EAAE;AAC7C,EAAE,IAAI,EAAE,EAAE,EAAE,CAAC;AACb,EAAE,IAAI,eAAe,EAAE;AACvB,IAAI,OAAO,eAAe,CAAC;AAC3B,GAAG;AACH,EAAE,IAAI,CAAC,EAAE,GAAG,MAAM,IAAI,IAAI,GAAG,KAAK,CAAC,GAAG,MAAM,CAAC,OAAO,KAAK,IAAI,GAAG,KAAK,CAAC,GAAG,EAAE,CAAC,IAAI,EAAE;AAClF,IAAI,OAAO,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC;AAC/B,GAAG;AACH,EAAE,IAAI,MAAM,GAAG,MAAM,IAAI,IAAI,GAAG,KAAK,CAAC,GAAG,MAAM,CAAC,MAAM,CAAC;AACvD,EAAE,IAAI,CAAC,MAAM,EAAE;AACf,IAAI,MAAM,SAAS,GAAG,CAAC,EAAE,GAAG,QAAQ,IAAI,IAAI,GAAG,KAAK,CAAC,GAAG,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,wDAAwD,CAAC,KAAK,IAAI,GAAG,KAAK,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;AACpL,IAAI,IAAI,SAAS,EAAE;AACnB,MAAM,MAAM,GAAG,CAAC,EAAE,SAAS,CAAC,cAAc,CAAC,CAAC;AAC5C,KAAK;AACL,GAAG;AACH,EAAE,IAAI,MAAM,EAAE;AACd,IAAI,OAAO,MAAM,CAAC;AAClB,GAAG;AACH,EAAE,MAAM,IAAI,KAAK,CAAC,CAAC,wCAAwC,CAAC,CAAC,CAAC;AAC9D,CAAC;AACM,SAAS,YAAY,CAAC,GAAG,GAAG,EAAE,EAAE;AACvC,EAAE,MAAM,UAAU,GAAG,GAAG,CAAC;AACzB,EAAEC,kBAAU,CAAC;AACb,IAAI,eAAe,EAAE,kBAAkB,CAAC,GAAG,CAAC,eAAe,CAAC;AAC5D,IAAI,qBAAqB,EAAE,GAAG,CAAC,qBAAqB;AACpD,IAAI,WAAW,EAAE,UAAU,CAAC,WAAW,GAAG,UAAU,CAAC,WAAW,GAAG,MAAM;AACzE,GAAG,CAAC,CAAC;AACL,EAAEC,iBAAa,EAAE,CAAC;AAClB;;;;;"}
|
|
@@ -35,15 +35,15 @@ async function cdnRequest(method, url, requestOptions = {}) {
|
|
|
35
35
|
return request(method, `${api.RECHARGE_CDN_URL(opts.environment)}/store/${opts.storeIdentifier}${url}`, requestOptions);
|
|
36
36
|
}
|
|
37
37
|
async function rechargeApiRequest(method, url, { id, query, data, headers } = {}, session) {
|
|
38
|
-
const
|
|
38
|
+
const { environment, storeIdentifier } = options.getOptions();
|
|
39
39
|
const token = session.apiToken;
|
|
40
|
-
const rechargeBaseUrl = api.RECHARGE_API_URL(
|
|
40
|
+
const rechargeBaseUrl = api.RECHARGE_API_URL(environment);
|
|
41
41
|
const reqHeaders = __spreadValues({
|
|
42
42
|
"X-Recharge-Access-Token": token,
|
|
43
43
|
"X-Recharge-Version": "2021-11"
|
|
44
44
|
}, headers ? headers : {});
|
|
45
45
|
const localQuery = __spreadValues({
|
|
46
|
-
shop_url:
|
|
46
|
+
shop_url: storeIdentifier
|
|
47
47
|
}, query);
|
|
48
48
|
return request(method, `${rechargeBaseUrl}${url}`, { id, query: localQuery, data, headers: reqHeaders });
|
|
49
49
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"request.js","sources":["../../../src/utils/request.ts"],"sourcesContent":["import 'isomorphic-fetch';\n\nimport { stringify } from 'qs';\n\nimport { Method, RequestHeaders, RequestOptions } from '../types';\nimport { getOptions } from './options';\nimport { RECHARGE_API_URL, RECHARGE_CDN_URL, SHOPIFY_APP_PROXY_URL } from '../constants/api';\nimport { Session } from '../types/session';\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 cdnRequest<T>(method: Method, url: string, requestOptions: RequestOptions = {}): Promise<T> {\n const opts = getOptions();\n return request(method, `${RECHARGE_CDN_URL(opts.environment)}/store/${opts.storeIdentifier}${url}`, requestOptions);\n}\n\nexport async function rechargeApiRequest<T>(\n method: Method,\n url: string,\n { id, query, data, headers }: RequestOptions = {},\n session: Session\n): Promise<T> {\n const
|
|
1
|
+
{"version":3,"file":"request.js","sources":["../../../src/utils/request.ts"],"sourcesContent":["import 'isomorphic-fetch';\n\nimport { stringify } from 'qs';\n\nimport { Method, RequestHeaders, RequestOptions } from '../types';\nimport { getOptions } from './options';\nimport { RECHARGE_API_URL, RECHARGE_CDN_URL, SHOPIFY_APP_PROXY_URL } from '../constants/api';\nimport { Session } from '../types/session';\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 cdnRequest<T>(method: Method, url: string, requestOptions: RequestOptions = {}): Promise<T> {\n const opts = getOptions();\n return request(method, `${RECHARGE_CDN_URL(opts.environment)}/store/${opts.storeIdentifier}${url}`, requestOptions);\n}\n\nexport async function rechargeApiRequest<T>(\n method: Method,\n url: string,\n { id, query, data, headers }: RequestOptions = {},\n session: Session\n): Promise<T> {\n const { environment, storeIdentifier } = getOptions();\n const token = session.apiToken;\n const rechargeBaseUrl = RECHARGE_API_URL(environment);\n const reqHeaders: RequestHeaders = {\n 'X-Recharge-Access-Token': token,\n 'X-Recharge-Version': '2021-11',\n ...(headers ? headers : {}),\n };\n\n const localQuery = {\n shop_url: storeIdentifier,\n ...(query as any),\n };\n\n return request(method, `${rechargeBaseUrl}${url}`, { id, query: localQuery, data, headers: reqHeaders });\n}\n\nexport async function shopifyAppProxyRequest<T>(\n method: Method,\n url: string,\n requestOptions: RequestOptions = {}\n): Promise<T> {\n return request(method, `${SHOPIFY_APP_PROXY_URL}${url}`, requestOptions);\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.trim();\n\n if (id) {\n reqUrl = [reqUrl, `${id}`.trim()].join('/');\n }\n\n if (query) {\n let exQuery;\n [reqUrl, exQuery] = reqUrl.split('?');\n const fullQuery = [exQuery, stringifyQuery(query)].join('&').replace(/^&/, '');\n reqUrl = `${reqUrl}${fullQuery ? `?${fullQuery}` : ''}`;\n }\n\n let reqBody;\n if (data && method !== 'get') {\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 let result;\n try {\n result = await response.json();\n } catch (e) {\n // If we get here, it means we were a no content response.\n }\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","getOptions","RECHARGE_CDN_URL","RECHARGE_API_URL","SHOPIFY_APP_PROXY_URL"],"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,IAA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AI,IAAI,IAAI,MAAM,KAAK,KAAK,EAAE;AAChC,IAAI,OAAO,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC;AACnC,GAAG;AACH,EAAE,MAAM,UAAU,GAAG,cAAc,CAAC;AACpC,IAAI,MAAM,EAAE,kBAAkB;AAC9B,IAAI,cAAc,EAAE,kBAAkB;AACtC,IAAI,gBAAgB,EAAE,mBAAmB;AACzC,GAAG,EAAE,OAAO,GAAG,OAAO,GAAG,EAAE,CAAC,CAAC;AAC7B,EAAE,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,MAAM,EAAE;AACvC,IAAI,MAAM;AACV,IAAI,OAAO,EAAE,UAAU;AACvB,IAAI,IAAI,EAAE,OAAO;AACjB,IAAI,IAAI,EAAE,MAAM;AAChB,GAAG,CAAC,CAAC;AACL,EAAE,IAAI,MAAM,CAAC;AACb,EAAE,IAAI;AACN,IAAI,MAAM,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC;AACnC,GAAG,CAAC,OAAO,CAAC,EAAE;AACd,GAAG;AACH,EAAE,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE;AACpB,IAAI,IAAI,MAAM,IAAI,MAAM,CAAC,KAAK,EAAE;AAChC,MAAM,MAAM,IAAI,KAAK,CAAC,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,EAAE,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;AAC7D,KAAK,MAAM;AACX,MAAM,MAAM,IAAI,KAAK,CAAC,sDAAsD,CAAC,CAAC;AAC9E,KAAK;AACL,GAAG;AACH,EAAE,OAAO,MAAM,CAAC;AAChB;;;;;;;"}
|
package/dist/esm/api/auth.js
CHANGED
|
@@ -3,17 +3,22 @@ import { getOptions } from '../utils/options.js';
|
|
|
3
3
|
import { RECHARGE_ADMIN_URL } from '../constants/api.js';
|
|
4
4
|
|
|
5
5
|
async function loginShopifyAppProxy() {
|
|
6
|
-
const
|
|
7
|
-
|
|
6
|
+
const { storefrontAccessToken } = getOptions();
|
|
7
|
+
const headers = {};
|
|
8
|
+
if (storefrontAccessToken) {
|
|
9
|
+
headers["X-Recharge-Storefront-Access-Token"] = storefrontAccessToken;
|
|
10
|
+
}
|
|
11
|
+
const response = await shopifyAppProxyRequest("get", "/access", { headers });
|
|
12
|
+
return { apiToken: response.api_token, customerId: response.customer_id };
|
|
8
13
|
}
|
|
9
14
|
async function loginShopifyApi(customerAccessToken, storefrontAccessToken) {
|
|
10
|
-
const
|
|
11
|
-
const rechargeBaseUrl = RECHARGE_ADMIN_URL(
|
|
15
|
+
const { environment, storeIdentifier } = getOptions();
|
|
16
|
+
const rechargeBaseUrl = RECHARGE_ADMIN_URL(environment);
|
|
12
17
|
const response = await request("post", `${rechargeBaseUrl}/hydrogen_login_poc`, {
|
|
13
18
|
data: {
|
|
14
19
|
customer_token: customerAccessToken,
|
|
15
20
|
storefront_token: storefrontAccessToken,
|
|
16
|
-
shop_url:
|
|
21
|
+
shop_url: storeIdentifier
|
|
17
22
|
},
|
|
18
23
|
headers: {
|
|
19
24
|
"X-Recharge-App": "storefront-client"
|
package/dist/esm/api/auth.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auth.js","sources":["../../../src/api/auth.ts"],"sourcesContent":["import { request as baseRequest, shopifyAppProxyRequest } from '../utils/request';\nimport { LoginResponse } from '../types/auth';\nimport { getOptions } from '../utils/options';\nimport { RECHARGE_ADMIN_URL } from '../constants/api';\nimport { Session } from '../types/session';\n\nexport async function loginShopifyAppProxy(): Promise<Session
|
|
1
|
+
{"version":3,"file":"auth.js","sources":["../../../src/api/auth.ts"],"sourcesContent":["import { request as baseRequest, shopifyAppProxyRequest } from '../utils/request';\nimport { LoginResponse } from '../types/auth';\nimport { getOptions } from '../utils/options';\nimport { RECHARGE_ADMIN_URL } from '../constants/api';\nimport { Session } from '../types/session';\nimport { RequestHeaders } from '../types';\n\nexport async function loginShopifyAppProxy(): Promise<Session> {\n const { storefrontAccessToken } = getOptions();\n const headers: RequestHeaders = {};\n if (storefrontAccessToken) {\n headers['X-Recharge-Storefront-Access-Token'] = storefrontAccessToken;\n }\n const response = await shopifyAppProxyRequest<LoginResponse>('get', '/access', { headers });\n\n return { apiToken: response.api_token, customerId: response.customer_id };\n}\n\nexport async function loginShopifyApi(\n customerAccessToken: string,\n storefrontAccessToken: string\n): Promise<Session | null> {\n const { environment, storeIdentifier } = getOptions();\n const rechargeBaseUrl = RECHARGE_ADMIN_URL(environment);\n const response = await baseRequest<LoginResponse>('post', `${rechargeBaseUrl}/hydrogen_login_poc`, {\n data: {\n customer_token: customerAccessToken,\n storefront_token: storefrontAccessToken,\n shop_url: storeIdentifier,\n },\n headers: {\n 'X-Recharge-App': 'storefront-client',\n },\n });\n\n return response.api_token ? { apiToken: response.api_token, customerId: response.customer_id } : null;\n}\n"],"names":["baseRequest"],"mappings":";;;;AAGO,eAAe,oBAAoB,GAAG;AAC7C,EAAE,MAAM,EAAE,qBAAqB,EAAE,GAAG,UAAU,EAAE,CAAC;AACjD,EAAE,MAAM,OAAO,GAAG,EAAE,CAAC;AACrB,EAAE,IAAI,qBAAqB,EAAE;AAC7B,IAAI,OAAO,CAAC,oCAAoC,CAAC,GAAG,qBAAqB,CAAC;AAC1E,GAAG;AACH,EAAE,MAAM,QAAQ,GAAG,MAAM,sBAAsB,CAAC,KAAK,EAAE,SAAS,EAAE,EAAE,OAAO,EAAE,CAAC,CAAC;AAC/E,EAAE,OAAO,EAAE,QAAQ,EAAE,QAAQ,CAAC,SAAS,EAAE,UAAU,EAAE,QAAQ,CAAC,WAAW,EAAE,CAAC;AAC5E,CAAC;AACM,eAAe,eAAe,CAAC,mBAAmB,EAAE,qBAAqB,EAAE;AAClF,EAAE,MAAM,EAAE,WAAW,EAAE,eAAe,EAAE,GAAG,UAAU,EAAE,CAAC;AACxD,EAAE,MAAM,eAAe,GAAG,kBAAkB,CAAC,WAAW,CAAC,CAAC;AAC1D,EAAE,MAAM,QAAQ,GAAG,MAAMA,OAAW,CAAC,MAAM,EAAE,CAAC,EAAE,eAAe,CAAC,mBAAmB,CAAC,EAAE;AACtF,IAAI,IAAI,EAAE;AACV,MAAM,cAAc,EAAE,mBAAmB;AACzC,MAAM,gBAAgB,EAAE,qBAAqB;AAC7C,MAAM,QAAQ,EAAE,eAAe;AAC/B,KAAK;AACL,IAAI,OAAO,EAAE;AACb,MAAM,gBAAgB,EAAE,mBAAmB;AAC3C,KAAK;AACL,GAAG,CAAC,CAAC;AACL,EAAE,OAAO,QAAQ,CAAC,SAAS,GAAG,EAAE,QAAQ,EAAE,QAAQ,CAAC,SAAS,EAAE,UAAU,EAAE,QAAQ,CAAC,WAAW,EAAE,GAAG,IAAI,CAAC;AACxG;;;;"}
|
package/dist/esm/api/bundle.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { toLineItemProperty } from '@recharge-packages/bundling-data';
|
|
2
2
|
import { shopifyAppProxyRequest } from '../utils/request.js';
|
|
3
3
|
import { getOptions } from '../utils/options.js';
|
|
4
|
-
import {
|
|
4
|
+
import { getCDNBundleSettings } from './cdn.js';
|
|
5
5
|
|
|
6
6
|
const STORE_FRONT_MANAGER_URL = "/bundling-storefront-manager";
|
|
7
7
|
function getTimestampSecondsFromClient() {
|
|
@@ -57,7 +57,7 @@ async function getBundleId(bundle) {
|
|
|
57
57
|
}
|
|
58
58
|
async function validateBundle(bundle) {
|
|
59
59
|
try {
|
|
60
|
-
const bundleSettings = await
|
|
60
|
+
const bundleSettings = await getCDNBundleSettings(bundle.externalProductId);
|
|
61
61
|
return !!bundle && !!bundleSettings;
|
|
62
62
|
} catch (e) {
|
|
63
63
|
console.error("Error fetching bundle settings");
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bundle.js","sources":["../../../src/api/bundle.ts"],"sourcesContent":["import { toLineItemProperty } from '@recharge-packages/bundling-data';\nimport { Bundle } from '../types';\nimport { shopifyAppProxyRequest } from '../utils/request';\nimport { getOptions } from '../utils/options';\nimport {
|
|
1
|
+
{"version":3,"file":"bundle.js","sources":["../../../src/api/bundle.ts"],"sourcesContent":["import { toLineItemProperty } from '@recharge-packages/bundling-data';\nimport { Bundle } from '../types';\nimport { shopifyAppProxyRequest } from '../utils/request';\nimport { getOptions } from '../utils/options';\nimport { getCDNBundleSettings } from './cdn';\n\nconst STORE_FRONT_MANAGER_URL = '/bundling-storefront-manager';\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(): 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 try {\n const { timestamp } = await shopifyAppProxyRequest<{ timestamp: number }>('get', `${STORE_FRONT_MANAGER_URL}/t`, {\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 async function getBundleId(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();\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 payload = await shopifyAppProxyRequest<{ id: string; code: number; message: string }>(\n 'post',\n `${STORE_FRONT_MANAGER_URL}/api/v1/bundles`,\n {\n data: {\n bundle: bundleData,\n },\n headers: {\n Origin: `https://${opts.storeIdentifier}`,\n },\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 async function validateBundle(bundle: Bundle): Promise<boolean> {\n try {\n const bundleSettings = await getCDNBundleSettings(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":[],"mappings":";;;;;AAIA,MAAM,uBAAuB,GAAG,8BAA8B,CAAC;AAC/D,SAAS,6BAA6B,GAAG;AACzC,EAAE,OAAO,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,GAAG,CAAC,CAAC;AACrC,CAAC;AACD,eAAe,6BAA6B,GAAG;AAC/C,EAAE,IAAI;AACN,IAAI,MAAM,EAAE,SAAS,EAAE,GAAG,MAAM,sBAAsB,CAAC,KAAK,EAAE,CAAC,EAAE,uBAAuB,CAAC,EAAE,CAAC,EAAE;AAC9F,MAAM,OAAO,EAAE,EAAE,gBAAgB,EAAE,mBAAmB,EAAE;AACxD,KAAK,CAAC,CAAC;AACP,IAAI,OAAO,SAAS,CAAC;AACrB,GAAG,CAAC,OAAO,EAAE,EAAE;AACf,IAAI,OAAO,CAAC,KAAK,CAAC,CAAC,cAAc,EAAE,EAAE,CAAC,yBAAyB,CAAC,CAAC,CAAC;AAClE,IAAI,OAAO,6BAA6B,EAAE,CAAC;AAC3C,GAAG;AACH,CAAC;AACM,eAAe,WAAW,CAAC,MAAM,EAAE;AAC1C,EAAE,MAAM,IAAI,GAAG,UAAU,EAAE,CAAC;AAC5B,EAAE,MAAM,OAAO,GAAG,MAAM,cAAc,CAAC,MAAM,CAAC,CAAC;AAC/C,EAAE,IAAI,CAAC,OAAO,EAAE;AAChB,IAAI,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC,CAAC;AACpD,GAAG;AACH,EAAE,MAAM,gBAAgB,GAAG,MAAM,6BAA6B,EAAE,CAAC;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