@rechargeapps/storefront-client 0.0.16 → 0.0.17

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.
@@ -12,8 +12,7 @@ function getTimestampSecondsFromClient() {
12
12
  return Math.ceil(Date.now() / 1e3);
13
13
  }
14
14
  async function getTimestampSecondsFromServer(request2) {
15
- const opts = options.getOptions();
16
- const url = `https://${opts.storeIdentifier}${STORE_FRONT_MANAGER_URL}/t`;
15
+ const url = `${STORE_FRONT_MANAGER_URL}/t`;
17
16
  try {
18
17
  const { timestamp } = await request2("get", url, {
19
18
  headers: { "X-Recharge-App": "storefront-client" }
@@ -45,7 +44,7 @@ const getBundleId = async (bundle) => {
45
44
  })
46
45
  });
47
46
  try {
48
- const STORE_FRONT_BUNDLE_URL = `https://${opts.storeIdentifier}${STORE_FRONT_MANAGER_URL}/api/v1/bundles`;
47
+ const STORE_FRONT_BUNDLE_URL = `${STORE_FRONT_MANAGER_URL}/api/v1/bundles`;
49
48
  const payload = await request.request("post", STORE_FRONT_BUNDLE_URL, {
50
49
  data: {
51
50
  bundle: bundleData
@@ -1 +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 = '/tools/recurring/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(request: Request): Promise<number> {\n const opts = getOptions();\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 = `https://${opts.storeIdentifier}${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 = `https://${opts.storeIdentifier}${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":";;;;;;;;;AAIA,MAAM,uBAAuB,GAAG,8CAA8C,CAAC;AAC/E,SAAS,6BAA6B,GAAG;AACzC,EAAE,OAAO,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,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M,CAAC,iBAAiB,CAAC,CAAC;AAC7E,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;;;;;"}
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 = '/tools/recurring/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(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":";;;;;;;;;AAIA,MAAM,uBAAuB,GAAG,8CAA8C,CAAC;AAC/E,SAAS,6BAA6B,GAAG;AACzC,EAAE,OAAO,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,GAAG,CAAC,CAAC;AACrC,CAAC;AACD,eAAe,6BAA6B,CAAC,QAAQ,EAAE;AACvD,EAAE,MAAM,GAAG,GAAG,CAAC,EAAE,uBAAuB,CAAC,EAAE,CAAC,CAAC;AAC7C,EAAE,IAAI;AACN,IAAI,MAAM,EAAE,SAAS,EAAE,GAAG,MAAM,QAAQ,CAAC,KAAK,EAAE,GAAG,EAAE;AACrD,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,OA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AAC,CAAC;AACjD,QAAQ,gBAAgB,EAAE,mBAAmB;AAC7C,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,EAAE;AACU,MAAC,cAAc,GAAG,OAAO,MAAM,KAAK;AAChD,EAAE,IAAI;AACN,IAAI,MAAM,cAAc,GAAG,MAAME,qBAAiB,CAAC,MAAM,CAAC,iBAAiB,CAAC,CAAC;AAC7E,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;;;;;"}
@@ -8,8 +8,7 @@ function getTimestampSecondsFromClient() {
8
8
  return Math.ceil(Date.now() / 1e3);
9
9
  }
10
10
  async function getTimestampSecondsFromServer(request2) {
11
- const opts = getOptions();
12
- const url = `https://${opts.storeIdentifier}${STORE_FRONT_MANAGER_URL}/t`;
11
+ const url = `${STORE_FRONT_MANAGER_URL}/t`;
13
12
  try {
14
13
  const { timestamp } = await request2("get", url, {
15
14
  headers: { "X-Recharge-App": "storefront-client" }
@@ -41,7 +40,7 @@ const getBundleId = async (bundle) => {
41
40
  })
42
41
  });
43
42
  try {
44
- const STORE_FRONT_BUNDLE_URL = `https://${opts.storeIdentifier}${STORE_FRONT_MANAGER_URL}/api/v1/bundles`;
43
+ const STORE_FRONT_BUNDLE_URL = `${STORE_FRONT_MANAGER_URL}/api/v1/bundles`;
45
44
  const payload = await request("post", STORE_FRONT_BUNDLE_URL, {
46
45
  data: {
47
46
  bundle: bundleData
@@ -1 +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 = '/tools/recurring/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(request: Request): Promise<number> {\n const opts = getOptions();\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 = `https://${opts.storeIdentifier}${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 = `https://${opts.storeIdentifier}${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":[],"mappings":";;;;;AAIA,MAAM,uBAAuB,GAAG,8CAA8C,CAAC;AAC/E,SAAS,6BAA6B,GAAG;AACzC,EAAE,OAAO,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,GAAG,CAAC,CAAC;AACrC,CAAC;AACD,eAAe,6BAA6B,CAAC,QAAQ,EAAE;AACvD,EAAE,MAAM,IAAI,GAAG,UAAU,EAAE,CAAC;AAC5B,EAAE,MAAM,GAAG,GAAG,CAAC,QAAQ,EAAE,IAAI,CAAC,eAAe,CAAC,EAAE,uBAAuB,CAAC,EAAE,CAAC,CAAC;AAC5E,EAAE,IAAI;AACN,IAAI,MAAM,EAAE,SAAS,EAAE,GAAG,MAAM,QAAQ,CAAC,KAAK,EAAE,GAAG,EAAE;AACrD,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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,EAAE,kBAAkB;AAC1C,QAAQ,MAAM,EAAE,CAAC,QAAQ,EAAE,IAAI,CAAC,eAAe,CAAC,CAAC;AACjD,QAAQ,gBAAgB,EAAE,mBAAmB;AAC7C,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,EAAE;AACU,MAAC,cAAc,GAAG,OAAO,MAAM,KAAK;AAChD,EAAE,IAAI;AACN,IAAI,MAAM,cAAc,GAAG,MAAM,iBAAiB,CAAC,MAAM,CAAC,iBAAiB,CAAC,CAAC;AAC7E,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;;;;"}
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 = '/tools/recurring/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(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":[],"mappings":";;;;;AA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