@zonos/elements 1.3.44 → 1.3.46
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/dist/cjs/{cart-89b4f62d.js → cart-7a79bccc.js} +152 -49
- package/dist/cjs/{sendErrorLog-a966a8c1.js → createCustomEvent-2fe80338.js} +2 -22
- package/dist/cjs/index.cjs.js +12 -8
- package/dist/cjs/loader.cjs.js +1 -1
- package/dist/cjs/{paypal-1a1882c4.js → paypal-eb07e9f5.js} +3 -3
- package/dist/cjs/zonos-address_8.cjs.entry.js +2 -2
- package/dist/cjs/zonos-cart-items_12.cjs.entry.js +29 -21
- package/dist/cjs/zonos-checkout.cjs.entry.js +11 -11
- package/dist/cjs/zonos-controller.cjs.entry.js +6 -6
- package/dist/cjs/zonos-country-flag.cjs.entry.js +1 -1
- package/dist/cjs/zonos-country-select_4.cjs.entry.js +1 -1
- package/dist/cjs/zonos-elements.cjs.js +1 -1
- package/dist/cjs/zonos-hello-dialog.cjs.entry.js +1 -1
- package/dist/cjs/zonos-hello.cjs.entry.js +2 -3
- package/dist/cjs/zonos-language-select_2.cjs.entry.js +1 -1
- package/dist/cjs/zonos-notification_3.cjs.entry.js +2 -2
- package/dist/collection/components/checkout/zonos-checkout/zonos-checkout-finish/zonos-checkout-finish.css +7 -0
- package/dist/collection/components/checkout/zonos-checkout/zonos-checkout-finish/zonos-checkout-finish.js +16 -9
- package/dist/collection/components/checkout/zonos-checkout/zonos-checkout.css +4 -0
- package/dist/collection/components/common/zonos-dialog/zonos-dialog.css +1 -0
- package/dist/collection/components/main/zonos-controller.js +4 -3
- package/dist/collection/components/store/checkout/stripe.js +4 -7
- package/dist/collection/components/store/hello/localization.js +1 -1
- package/dist/collection/components/utils/api/calculateLandedCost.js +1 -2
- package/dist/collection/components/utils/api/checkItemRestrictions.js +1 -2
- package/dist/collection/components/utils/api/getCountries.js +1 -1
- package/dist/collection/components/utils/api/getCountryLandedCost.js +1 -1
- package/dist/collection/components/utils/api/getExchangeRate.js +6 -2
- package/dist/collection/components/utils/api/getInclusivePricing.js +1 -2
- package/dist/collection/components/utils/api/getLocaleMessages.js +7 -2
- package/dist/collection/components/utils/api/getLocalization.js +3 -6
- package/dist/collection/components/utils/api/getShippingZones.js +1 -2
- package/dist/collection/components/utils/api/getZonosSettings.js +1 -2
- package/dist/collection/components/utils/api/paypalCaptureOrder.js +1 -1
- package/dist/collection/components/utils/api/paypalCreateOrder.js +1 -1
- package/dist/collection/components/utils/api/trackConversionEvent.js +1 -2
- package/dist/collection/components/utils/api/triggerBuildLandedCost.js +3 -2
- package/dist/collection/components/utils/getNextFallbackApi.js +18 -0
- package/dist/collection/components/utils/handleFetch.js +75 -21
- package/dist/collection/components/utils/logging/sendErrorLog.js +19 -5
- package/dist/collection/components/utils/logging/sendLoadScriptLog.js +1 -1
- package/dist/collection/environment.js +13 -78
- package/dist/collection/scripts/_getZonosQaUrl.js +6 -2
- package/dist/collection/utils/formattedCloudflareApiUrl.js +43 -0
- package/dist/collection/utils/formattedUrl.js +80 -0
- package/dist/collection/utils/generateCloudflareDeployUrl.js +21 -0
- package/dist/collection/utils/getGraphQlShortId.js +1 -0
- package/dist/esm/{cart-9f40efd7.js → cart-2198741e.js} +153 -51
- package/dist/esm/{sendErrorLog-aea606c0.js → createCustomEvent-451d7879.js} +3 -22
- package/dist/esm/index.js +12 -8
- package/dist/esm/loader.js +1 -1
- package/dist/esm/{paypal-f4bb6ce7.js → paypal-2890968d.js} +3 -3
- package/dist/esm/zonos-address_8.entry.js +2 -2
- package/dist/esm/zonos-cart-items_12.entry.js +21 -13
- package/dist/esm/zonos-checkout.entry.js +4 -4
- package/dist/esm/zonos-controller.entry.js +6 -6
- package/dist/esm/zonos-country-flag.entry.js +1 -1
- package/dist/esm/zonos-country-select_4.entry.js +1 -1
- package/dist/esm/zonos-elements.js +1 -1
- package/dist/esm/zonos-hello-dialog.entry.js +1 -1
- package/dist/esm/zonos-hello.entry.js +2 -3
- package/dist/esm/zonos-language-select_2.entry.js +1 -1
- package/dist/esm/zonos-notification_3.entry.js +2 -2
- package/dist/esm-es5/{cart-9f40efd7.js → cart-2198741e.js} +1 -1
- package/dist/esm-es5/createCustomEvent-451d7879.js +1 -0
- package/dist/esm-es5/index.js +1 -1
- package/dist/esm-es5/loader.js +1 -1
- package/dist/esm-es5/paypal-2890968d.js +17 -0
- package/dist/esm-es5/zonos-address_8.entry.js +1 -1
- package/dist/esm-es5/zonos-cart-items_12.entry.js +1 -1
- package/dist/esm-es5/zonos-checkout.entry.js +1 -1
- package/dist/esm-es5/zonos-controller.entry.js +1 -1
- package/dist/esm-es5/zonos-country-flag.entry.js +1 -1
- package/dist/esm-es5/zonos-country-select_4.entry.js +1 -1
- package/dist/esm-es5/zonos-elements.js +1 -1
- package/dist/esm-es5/zonos-hello-dialog.entry.js +1 -1
- package/dist/esm-es5/zonos-hello.entry.js +1 -1
- package/dist/esm-es5/zonos-language-select_2.entry.js +1 -1
- package/dist/esm-es5/zonos-notification_3.entry.js +1 -1
- package/dist/scripts/index.d.ts +3 -0
- package/dist/scripts/index.js +1 -1
- package/dist/scripts/loadZonos.js +1 -1
- package/dist/scripts/zonos.js +1 -1
- package/dist/types/components/utils/getNextFallbackApi.d.ts +10 -0
- package/dist/types/components/utils/handleFetch.d.ts +10 -1
- package/dist/types/environment.d.ts +4 -9
- package/dist/types/scripts/_getNextFallbackUrl.d.ts +1 -1
- package/dist/types/utils/formattedCloudflareApiUrl.d.ts +20 -0
- package/dist/types/utils/formattedUrl.d.ts +9 -0
- package/dist/types/utils/generateCloudflareDeployUrl.d.ts +10 -0
- package/dist/types/utils/getGraphQlShortId.d.ts +1 -0
- package/dist/zonos-elements/assets/RemoveIcon.svg +1 -1
- package/dist/zonos-elements/index.esm.js +1 -1
- package/dist/zonos-elements/p-18f46c3f.entry.js +1 -0
- package/dist/zonos-elements/p-2b25ac5e.system.js +1 -0
- package/dist/zonos-elements/p-2f24a1f7.system.js +17 -0
- package/dist/zonos-elements/p-3b116246.system.entry.js +1 -0
- package/dist/zonos-elements/{p-578f42c1.entry.js → p-40e8f3bd.entry.js} +1 -1
- package/dist/zonos-elements/p-467cdce8.system.entry.js +1 -0
- package/dist/zonos-elements/p-52c2fb9d.entry.js +1 -0
- package/dist/zonos-elements/{p-5845029a.system.entry.js → p-65efc406.system.entry.js} +1 -1
- package/dist/zonos-elements/{p-8618e185.entry.js → p-6b868590.entry.js} +1 -1
- package/dist/zonos-elements/{p-21d07dda.entry.js → p-791d240f.entry.js} +1 -1
- package/dist/zonos-elements/{p-acf9a97e.system.entry.js → p-7b9ed9ed.system.entry.js} +1 -1
- package/dist/zonos-elements/{p-7b8099ed.js → p-84e5636c.js} +1 -1
- package/dist/zonos-elements/{p-8630168f.system.entry.js → p-85b297d3.system.entry.js} +1 -1
- package/dist/zonos-elements/p-8b2486b7.system.entry.js +1 -0
- package/dist/zonos-elements/{p-aee29731.system.entry.js → p-a28492ca.system.entry.js} +1 -1
- package/dist/zonos-elements/{p-43d3cafd.system.js → p-ac01a4fe.system.js} +1 -1
- package/dist/zonos-elements/{p-4204ccf9.system.entry.js → p-b066612a.system.entry.js} +1 -1
- package/dist/zonos-elements/p-b06704a9.js +17 -0
- package/dist/zonos-elements/{p-37c9fdd2.system.entry.js → p-bfe85ef2.system.entry.js} +1 -1
- package/dist/zonos-elements/p-c3bb224b.entry.js +1 -0
- package/dist/zonos-elements/p-c78dd680.system.js +1 -1
- package/dist/zonos-elements/{p-09988f34.entry.js → p-cfbcfd4b.entry.js} +1 -1
- package/dist/zonos-elements/{p-c7952400.entry.js → p-e43bbfe1.entry.js} +1 -1
- package/dist/zonos-elements/{p-567b457b.entry.js → p-e69e429d.entry.js} +1 -1
- package/dist/zonos-elements/p-f12c159e.js +1 -0
- package/dist/zonos-elements/p-f9aa9bd8.system.js +1 -0
- package/dist/zonos-elements/{p-8bf1af28.entry.js → p-fb6c529a.entry.js} +1 -1
- package/dist/zonos-elements/p-ff10609a.system.entry.js +1 -0
- package/dist/zonos-elements/zonos-elements.esm.js +1 -1
- package/package.json +1 -1
- package/dist/esm-es5/paypal-f4bb6ce7.js +0 -17
- package/dist/esm-es5/sendErrorLog-aea606c0.js +0 -1
- package/dist/zonos-elements/p-0641784b.system.js +0 -1
- package/dist/zonos-elements/p-0bfe455c.system.entry.js +0 -1
- package/dist/zonos-elements/p-29f4cce5.system.js +0 -17
- package/dist/zonos-elements/p-2d01b8e6.system.entry.js +0 -1
- package/dist/zonos-elements/p-2e12d178.system.entry.js +0 -1
- package/dist/zonos-elements/p-3490d40f.js +0 -17
- package/dist/zonos-elements/p-6f759c76.system.js +0 -1
- package/dist/zonos-elements/p-744180f1.entry.js +0 -1
- package/dist/zonos-elements/p-76291114.entry.js +0 -1
- package/dist/zonos-elements/p-9aef3a35.entry.js +0 -1
- package/dist/zonos-elements/p-c10099bd.system.entry.js +0 -1
- package/dist/zonos-elements/p-d99e58f2.js +0 -1
|
@@ -358,13 +358,11 @@ const stripeStoreInitCheckoutSession = async () => {
|
|
|
358
358
|
return false;
|
|
359
359
|
}
|
|
360
360
|
stripeStore.isFetchingSession = true;
|
|
361
|
-
const { errors, json } = await handleFetch(
|
|
361
|
+
const { errors, json } = await handleFetch('/api/zonos-elements/create-checkout-session', {
|
|
362
362
|
body: {
|
|
363
363
|
currency: ((_a = cartStore.items.find(Boolean)) === null || _a === void 0 ? void 0 : _a.currencyCode) || '',
|
|
364
364
|
itemsAmount: total,
|
|
365
|
-
metadata: [
|
|
366
|
-
{ key: 'language', value: localizationStore.selectedLocale },
|
|
367
|
-
],
|
|
365
|
+
metadata: [{ key: 'language', value: localizationStore.selectedLocale }],
|
|
368
366
|
presentmentCountryCode: localizationStore.selectedCountryCode || null,
|
|
369
367
|
},
|
|
370
368
|
method: 'POST',
|
|
@@ -389,7 +387,7 @@ const stripeStoreInitCheckoutSession = async () => {
|
|
|
389
387
|
const stripeStoreUpdateCheckoutSession = async ({ currency, id, itemsAmount, landedCostId, presentmentCountryCode, }) => {
|
|
390
388
|
// No need to set loading state here because this function will be called under the hood to update
|
|
391
389
|
// landed cost id for the section.
|
|
392
|
-
const { errors, json } = await handleFetch(
|
|
390
|
+
const { errors, json } = await handleFetch('/api/zonos-elements/update-checkout-session', {
|
|
393
391
|
body: {
|
|
394
392
|
currency,
|
|
395
393
|
id,
|
|
@@ -488,8 +486,7 @@ const stripeStoreTranslateAddressToLatin = async ({ addressInvalidCharactersList
|
|
|
488
486
|
if (!isAddressOutdated) {
|
|
489
487
|
return;
|
|
490
488
|
}
|
|
491
|
-
const
|
|
492
|
-
const { errors, json } = await handleFetch(`${routeUrl}/api/zonos-elements/validate-address`, {
|
|
489
|
+
const { errors, json } = await handleFetch('/api/zonos-elements/validate-address', {
|
|
493
490
|
body: requestBody,
|
|
494
491
|
method: 'POST',
|
|
495
492
|
});
|
|
@@ -201,7 +201,7 @@ const updateCheckoutSessionLanguage = async () => {
|
|
|
201
201
|
if (!cartStore.checkoutSessionId) {
|
|
202
202
|
return null;
|
|
203
203
|
}
|
|
204
|
-
const { errors, json } = await handleFetch(
|
|
204
|
+
const { errors, json } = await handleFetch('/api/zonos-elements/update-checkout-session-metadata', {
|
|
205
205
|
body: {
|
|
206
206
|
id: cartStore.checkoutSessionId,
|
|
207
207
|
metadata: [
|
|
@@ -11,8 +11,7 @@ export const calculateLandedCost = async (abortController) => {
|
|
|
11
11
|
return json;
|
|
12
12
|
}
|
|
13
13
|
// request to get landed cost info
|
|
14
|
-
const
|
|
15
|
-
const { errors, json } = await handleFetch(url, {
|
|
14
|
+
const { errors, json } = await handleFetch('/api/zonos-elements/calculate-landed-cost', {
|
|
16
15
|
body: prepCalculateLandedCost(),
|
|
17
16
|
method: 'POST',
|
|
18
17
|
signal: abortController === null || abortController === void 0 ? void 0 : abortController.signal,
|
|
@@ -2,13 +2,12 @@ import { handleFetch } from "../handleFetch";
|
|
|
2
2
|
import { logError } from "../logging/logError";
|
|
3
3
|
export const checkItemRestrictions = async ({ items, shipFrom, shipTo, }) => {
|
|
4
4
|
var _a;
|
|
5
|
-
const url = `${process.env.NEXT_PUBLIC_ZONOS_ROUTE_URL}/api/zonos-elements/check-item-restrictions`;
|
|
6
5
|
const body = {
|
|
7
6
|
items,
|
|
8
7
|
shipFrom,
|
|
9
8
|
shipTo,
|
|
10
9
|
};
|
|
11
|
-
const { errors, json } = await handleFetch(
|
|
10
|
+
const { errors, json } = await handleFetch('/api/zonos-elements/check-item-restrictions', { body, method: 'POST' });
|
|
12
11
|
if (errors.length || !json) {
|
|
13
12
|
logError(`Error fetching item restrictions: ${(_a = errors.find(Boolean)) === null || _a === void 0 ? void 0 : _a.message}`);
|
|
14
13
|
return [];
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { handleFetch } from "../handleFetch";
|
|
2
2
|
export const getCountries = async () => {
|
|
3
3
|
try {
|
|
4
|
-
const { errors, json } = await handleFetch(
|
|
4
|
+
const { errors, json } = await handleFetch('/api/zonos-elements/get-countries', {
|
|
5
5
|
method: 'GET',
|
|
6
6
|
});
|
|
7
7
|
if (errors.length || !json) {
|
|
@@ -5,7 +5,7 @@ export const getCountryLandedCost = async (countryCode) => {
|
|
|
5
5
|
if (!countryCode) {
|
|
6
6
|
return null;
|
|
7
7
|
}
|
|
8
|
-
const url =
|
|
8
|
+
const url = `/api/zonos-elements/country-landed-cost/${countryCode}`;
|
|
9
9
|
const { errors, json } = await handleFetch(url);
|
|
10
10
|
if (errors.length || !json) {
|
|
11
11
|
logError(`Error fetching country landed cost: ${(_a = errors.find(Boolean)) === null || _a === void 0 ? void 0 : _a.message}`);
|
|
@@ -20,8 +20,12 @@ export const getExchangeRate = async ({ country, isGuaranteed, sourceCurrency, t
|
|
|
20
20
|
if (sourceCurrency === targetCurrency && !isGuaranteed) {
|
|
21
21
|
return defaultUsExchangeRate;
|
|
22
22
|
}
|
|
23
|
-
const
|
|
24
|
-
|
|
23
|
+
const searchParams = new URLSearchParams();
|
|
24
|
+
searchParams.set('sourceCurrency', sourceCurrency);
|
|
25
|
+
searchParams.set('targetCurrency', targetCurrency);
|
|
26
|
+
searchParams.set('isGuaranteed', String(isGuaranteed));
|
|
27
|
+
searchParams.set('country', country || '');
|
|
28
|
+
const { errors, json } = await handleFetch(`/api/zonos-elements/exchange-rates?${searchParams.toString()}`);
|
|
25
29
|
if (errors.length || !json) {
|
|
26
30
|
logError(`Error fetching country landed cost: ${(_a = errors.find(Boolean)) === null || _a === void 0 ? void 0 : _a.message}`);
|
|
27
31
|
return defaultUsExchangeRate;
|
|
@@ -5,8 +5,7 @@ export const getInclusivePricing = async (countryCode) => {
|
|
|
5
5
|
if (!countryCode) {
|
|
6
6
|
return null;
|
|
7
7
|
}
|
|
8
|
-
const
|
|
9
|
-
const { errors, json } = await handleFetch(url);
|
|
8
|
+
const { errors, json } = await handleFetch(`/api/zonos-elements/inclusive-prices/${countryCode}`);
|
|
10
9
|
if (errors.length || !json) {
|
|
11
10
|
logError(`Error fetching country inclusive price setting: ${(_a = errors.find(Boolean)) === null || _a === void 0 ? void 0 : _a.message}`);
|
|
12
11
|
return null;
|
|
@@ -2,8 +2,13 @@ import { handleFetch } from "../handleFetch";
|
|
|
2
2
|
import { logError } from "../logging/logError";
|
|
3
3
|
export const getLocaleMessages = async ({ countryCode, localeTo, storeId, }) => {
|
|
4
4
|
var _a;
|
|
5
|
-
const
|
|
6
|
-
|
|
5
|
+
const searchParams = new URLSearchParams();
|
|
6
|
+
searchParams.set('countryCode', countryCode);
|
|
7
|
+
searchParams.set('storeId', storeId.toString());
|
|
8
|
+
if (localeTo) {
|
|
9
|
+
searchParams.set('localeTo', localeTo);
|
|
10
|
+
}
|
|
11
|
+
const { errors, json } = await handleFetch(`/api/zonos-elements/locale-messages?${searchParams.toString()}`);
|
|
7
12
|
if (errors.length || !json) {
|
|
8
13
|
logError('Error fetching exchange rate', (_a = errors.find(Boolean)) === null || _a === void 0 ? void 0 : _a.message);
|
|
9
14
|
return [];
|
|
@@ -6,14 +6,11 @@ export const getLocalization = async () => {
|
|
|
6
6
|
// load and use country from session storage when zCountry query param is not present
|
|
7
7
|
const countryLoadedSession = window.sessionStorage.getItem('zonos--country');
|
|
8
8
|
const zCountry = getZCountryUrlParam(currentUrl.searchParams) || countryLoadedSession;
|
|
9
|
-
const
|
|
9
|
+
const searchParams = new URLSearchParams();
|
|
10
10
|
if (zCountry) {
|
|
11
|
-
|
|
11
|
+
searchParams.set('zCountry', zCountry);
|
|
12
12
|
}
|
|
13
|
-
const
|
|
14
|
-
const { errors, json } = await handleFetch(fetchUrl, {
|
|
15
|
-
method: 'GET',
|
|
16
|
-
});
|
|
13
|
+
const { errors, json } = await handleFetch(`/api/zonos-elements/localization?${searchParams.toString()}`);
|
|
17
14
|
if (errors.length || !json) {
|
|
18
15
|
return { country: null, locale: null };
|
|
19
16
|
}
|
|
@@ -2,8 +2,7 @@ import { handleFetch } from "../handleFetch";
|
|
|
2
2
|
import { logError } from "../logging/logError";
|
|
3
3
|
export const getShippingZones = async () => {
|
|
4
4
|
var _a, _b;
|
|
5
|
-
const
|
|
6
|
-
const { errors, json } = await handleFetch(url);
|
|
5
|
+
const { errors, json } = await handleFetch('/api/zonos-elements/shipping-zones');
|
|
7
6
|
if (errors.length || !json) {
|
|
8
7
|
logError(`Error fetching shipping zones: ${(_a = errors.find(Boolean)) === null || _a === void 0 ? void 0 : _a.message}`);
|
|
9
8
|
return null;
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { handleFetch } from "../handleFetch";
|
|
2
2
|
import { logError } from "../logging/logError";
|
|
3
3
|
export const getZonosSettings = async () => {
|
|
4
|
-
const
|
|
5
|
-
const { errors, json, zonosMode } = await handleFetch(url);
|
|
4
|
+
const { errors, json, zonosMode } = await handleFetch('/api/zonos-elements/zonos-settings');
|
|
6
5
|
if (errors.length) {
|
|
7
6
|
logError(`Error occurred when getting zonos settings. If this is not an unauthorized error, it will use user settings or default settings`);
|
|
8
7
|
return { errors, json: null, zonosMode };
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { handleFetch } from "../handleFetch";
|
|
2
2
|
export const paypalCaptureOrder = async ({ checkoutSessionId, paypalMockResponse, }) => {
|
|
3
|
-
const { errors, json } = await handleFetch(
|
|
3
|
+
const { errors, json } = await handleFetch('/api/zonos-elements/capture-paypal-order', {
|
|
4
4
|
body: { checkoutSessionId, paypalMockResponse },
|
|
5
5
|
method: 'POST',
|
|
6
6
|
});
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { handleFetch } from "../handleFetch";
|
|
2
2
|
export const paypalCreateOrder = async ({ checkoutSessionId, }) => {
|
|
3
|
-
const { errors, json } = await handleFetch(
|
|
3
|
+
const { errors, json } = await handleFetch('/api/zonos-elements/create-paypal-order', {
|
|
4
4
|
body: { checkoutSessionId },
|
|
5
5
|
method: 'POST',
|
|
6
6
|
});
|
|
@@ -14,7 +14,6 @@ export const trackConversionEvent = async (eventName, metadata) => {
|
|
|
14
14
|
!window.Zonos.zonosConversionTest) {
|
|
15
15
|
return;
|
|
16
16
|
}
|
|
17
|
-
const url = `${process.env.NEXT_PUBLIC_ZONOS_ROUTE_URL}/api/zonos-elements/track-conversion-event`;
|
|
18
17
|
const countryCode = localizationStore.selectedCountryCode;
|
|
19
18
|
const organizationId = zonosStore.organizationId;
|
|
20
19
|
const checkoutSessionId = cartStore.checkoutSessionId;
|
|
@@ -29,7 +28,7 @@ export const trackConversionEvent = async (eventName, metadata) => {
|
|
|
29
28
|
metadata,
|
|
30
29
|
organizationId,
|
|
31
30
|
};
|
|
32
|
-
await handleFetch(
|
|
31
|
+
await handleFetch('/api/zonos-elements/track-conversion-event', {
|
|
33
32
|
body,
|
|
34
33
|
method: 'POST',
|
|
35
34
|
});
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { prepCalculateLandedCost } from "./prepCalculateLandedCost";
|
|
2
|
+
import { MAIN_BASE_API } from "../getNextFallbackApi";
|
|
2
3
|
import { handleFetch } from "../handleFetch";
|
|
3
4
|
export const triggerBuildLandedCost = async (buildLandedCost) => {
|
|
4
5
|
var _a, _b, _c, _d;
|
|
@@ -9,13 +10,13 @@ export const triggerBuildLandedCost = async (buildLandedCost) => {
|
|
|
9
10
|
checkoutSessionId: data.checkoutSessionId,
|
|
10
11
|
contact: data.contact,
|
|
11
12
|
shippingAddress: data.shippingAddress,
|
|
12
|
-
zonosApiRoute:
|
|
13
|
+
zonosApiRoute: MAIN_BASE_API,
|
|
13
14
|
});
|
|
14
15
|
if (!landedCost || ((_a = landedCost.errors) === null || _a === void 0 ? void 0 : _a.length)) {
|
|
15
16
|
throw new Error(`Failed to build landed cost: ${((_c = (_b = landedCost === null || landedCost === void 0 ? void 0 : landedCost.errors) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.message) || 'Unknown error'}`);
|
|
16
17
|
}
|
|
17
18
|
// Validate the landed cost object to make sure the returned value from buildLandedCost has a valid schema
|
|
18
|
-
const { errors, json } = await handleFetch(
|
|
19
|
+
const { errors, json } = await handleFetch('/api/zonos-elements/validate-landed-cost', {
|
|
19
20
|
body: landedCost,
|
|
20
21
|
headers: {
|
|
21
22
|
'Content-Type': 'application/json',
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
const fallbackApiType = ['vercel', 'cloudflare'];
|
|
2
|
+
export const MAIN_BASE_API_TYPE = fallbackApiType[0];
|
|
3
|
+
export const fallbackApiList = {
|
|
4
|
+
cloudflare: process.env.NEXT_PUBLIC_ZONOS_CLOUDFLARE_URL,
|
|
5
|
+
vercel: process.env.NEXT_PUBLIC_ZONOS_ROUTE_URL,
|
|
6
|
+
};
|
|
7
|
+
export const MAIN_BASE_API = fallbackApiList[MAIN_BASE_API_TYPE];
|
|
8
|
+
/**
|
|
9
|
+
* Next available fallback api type
|
|
10
|
+
*/
|
|
11
|
+
export const getNextFallbackApiType = (currentApiType = MAIN_BASE_API_TYPE) => {
|
|
12
|
+
const currentIndex = fallbackApiType.indexOf(currentApiType);
|
|
13
|
+
const nextFallbackApiType = fallbackApiType[currentIndex + 1];
|
|
14
|
+
if (!nextFallbackApiType) {
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
return nextFallbackApiType;
|
|
18
|
+
};
|
|
@@ -10,6 +10,9 @@ var __rest = (this && this.__rest) || function (s, e) {
|
|
|
10
10
|
}
|
|
11
11
|
return t;
|
|
12
12
|
};
|
|
13
|
+
import { fallbackApiList, getNextFallbackApiType, MAIN_BASE_API_TYPE, } from "./getNextFallbackApi";
|
|
14
|
+
import { log } from "./logging/log";
|
|
15
|
+
import { sendErrorLog } from "./logging/sendErrorLog";
|
|
13
16
|
const getResponseBody = async ({ response, shouldLog = true, shouldThrow = false, }) => {
|
|
14
17
|
try {
|
|
15
18
|
const json = await response.json();
|
|
@@ -26,14 +29,36 @@ const getResponseBody = async ({ response, shouldLog = true, shouldThrow = false
|
|
|
26
29
|
return null;
|
|
27
30
|
}
|
|
28
31
|
};
|
|
29
|
-
const
|
|
32
|
+
const encodeString = (text) => {
|
|
33
|
+
try {
|
|
34
|
+
// encode the string, remove the "=" at the end of the encoded string
|
|
35
|
+
const encodedString = btoa(text).replace(/=/g, '');
|
|
36
|
+
return encodedString;
|
|
37
|
+
}
|
|
38
|
+
catch (error) {
|
|
39
|
+
// return the reverse string if the encoding failed
|
|
40
|
+
return text
|
|
41
|
+
.replace(/[^A-Za-z0-9]/g, '')
|
|
42
|
+
.split('')
|
|
43
|
+
.reverse()
|
|
44
|
+
.join('');
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
const handleRequest = async ({ basePathType, options, path, }) => {
|
|
30
48
|
let response;
|
|
49
|
+
// fallback to the main base API if the passed in base path is not available
|
|
50
|
+
const baseUrl = fallbackApiList[basePathType] || MAIN_BASE_API_TYPE;
|
|
51
|
+
const url = baseUrl + path;
|
|
52
|
+
// adding random encoded token to the url so that the request will be cached differently for each store
|
|
53
|
+
const modifiedUrl = new URL(url);
|
|
54
|
+
modifiedUrl.searchParams.set('zToken', encodeString(window.location.hostname));
|
|
31
55
|
try {
|
|
32
|
-
response = await fetch(
|
|
56
|
+
response = await fetch(modifiedUrl.toString(), options);
|
|
33
57
|
}
|
|
34
58
|
catch (e) {
|
|
35
59
|
if (e instanceof Error) {
|
|
36
60
|
return {
|
|
61
|
+
baseUrl,
|
|
37
62
|
errors: [{ message: e.message, type: 'unknown' }],
|
|
38
63
|
json: null,
|
|
39
64
|
response: null,
|
|
@@ -42,6 +67,7 @@ const handleRequest = async (url, options) => {
|
|
|
42
67
|
}
|
|
43
68
|
const message = 'Fail to fetch';
|
|
44
69
|
return {
|
|
70
|
+
baseUrl,
|
|
45
71
|
errors: [{ message, type: 'unknown' }],
|
|
46
72
|
json: null,
|
|
47
73
|
response: null,
|
|
@@ -57,6 +83,7 @@ const handleRequest = async (url, options) => {
|
|
|
57
83
|
if (response.ok) {
|
|
58
84
|
const data = await getResponseBody({ response });
|
|
59
85
|
return {
|
|
86
|
+
baseUrl,
|
|
60
87
|
errors: [],
|
|
61
88
|
json: data,
|
|
62
89
|
response,
|
|
@@ -71,6 +98,7 @@ const handleRequest = async (url, options) => {
|
|
|
71
98
|
});
|
|
72
99
|
const message = JSON.stringify(data);
|
|
73
100
|
return {
|
|
101
|
+
baseUrl,
|
|
74
102
|
errors: [{ message, type: status }],
|
|
75
103
|
json: null,
|
|
76
104
|
response,
|
|
@@ -80,6 +108,7 @@ const handleRequest = async (url, options) => {
|
|
|
80
108
|
catch (e) {
|
|
81
109
|
const message = 'Unknown error';
|
|
82
110
|
return {
|
|
111
|
+
baseUrl,
|
|
83
112
|
errors: [{ message: `${status}: ${message}`, type: status }],
|
|
84
113
|
json: null,
|
|
85
114
|
response: null,
|
|
@@ -87,31 +116,56 @@ const handleRequest = async (url, options) => {
|
|
|
87
116
|
};
|
|
88
117
|
}
|
|
89
118
|
};
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
119
|
+
const handleRequestWithFallback = async ({ basePathType, options, path, }) => {
|
|
120
|
+
const _a = await handleRequest({
|
|
121
|
+
basePathType,
|
|
122
|
+
options,
|
|
123
|
+
path,
|
|
124
|
+
}), { errors, response } = _a, rest = __rest(_a, ["errors", "response"]);
|
|
125
|
+
const nextFallbackApiType = getNextFallbackApiType(basePathType);
|
|
126
|
+
const unknownError = errors.find(({ type }) => type === 'unknown');
|
|
127
|
+
const shouldCallFallback =
|
|
128
|
+
// If next fallback is available, and the error is unknown (fail to fetch) or 404
|
|
129
|
+
nextFallbackApiType && (!!unknownError || (response === null || response === void 0 ? void 0 : response.status) === 404);
|
|
130
|
+
if (shouldCallFallback) {
|
|
131
|
+
void sendErrorLog({
|
|
132
|
+
endpoint: fallbackApiList[basePathType] + path,
|
|
133
|
+
errors: errors.map(({ message }) => message),
|
|
134
|
+
metadata: {
|
|
135
|
+
currentBasePath: basePathType,
|
|
136
|
+
nextFallbackApiType,
|
|
137
|
+
status: response === null || response === void 0 ? void 0 : response.status,
|
|
138
|
+
statusText: response === null || response === void 0 ? void 0 : response.statusText,
|
|
139
|
+
},
|
|
140
|
+
});
|
|
141
|
+
log(`=== "${path}" failed to fetch, trying next fallback ${nextFallbackApiType}`);
|
|
142
|
+
return handleRequestWithFallback({
|
|
143
|
+
basePathType: nextFallbackApiType,
|
|
144
|
+
options,
|
|
145
|
+
path,
|
|
146
|
+
});
|
|
103
147
|
}
|
|
104
|
-
|
|
105
|
-
|
|
148
|
+
// No more fallbacks, just return the result
|
|
149
|
+
return Object.assign({ errors, response }, rest);
|
|
150
|
+
};
|
|
151
|
+
/**
|
|
152
|
+
* Fetch request and will request to fallback API when the first one fails
|
|
153
|
+
* @param path Path name of the request (e.g. /api/something/com).
|
|
154
|
+
* @param _opts fetch options
|
|
155
|
+
*/
|
|
156
|
+
export const handleFetch = async (path, _opts) => {
|
|
106
157
|
const opts = __rest(_opts || {}, []);
|
|
107
158
|
const body = opts.body ? JSON.stringify(opts.body) : undefined;
|
|
108
159
|
const method = opts.method || 'GET';
|
|
109
|
-
// adding random encoded token to the url so that the request will be cached differently for each store
|
|
110
|
-
const modifiedUrl = new URL(url);
|
|
111
|
-
modifiedUrl.searchParams.set('zToken', encodeString(window.location.hostname));
|
|
112
160
|
// headers content type
|
|
113
161
|
const headers = Object.assign({ 'Content-Type': 'application/json', 'Zonos-Is-BigCommerce': window.Zonos.isBigCommerce.toString(), 'Zonos-Referer': window.location.hostname, 'Zonos-Version': window.Zonos.version, credentialtoken: window.Zonos.zonosApiKey || 'Empty credential token from zonos-elements' }, opts.headers);
|
|
114
162
|
const options = Object.assign(Object.assign({}, opts), { body: method === 'GET' ? undefined : body, headers,
|
|
115
163
|
method });
|
|
116
|
-
|
|
164
|
+
// This is for testing fallback API, set fallback API type to start with
|
|
165
|
+
const useFallbackApi = new URLSearchParams(window.location.search).get('useFallbackApi');
|
|
166
|
+
return handleRequestWithFallback({
|
|
167
|
+
basePathType: useFallbackApi || MAIN_BASE_API_TYPE,
|
|
168
|
+
options,
|
|
169
|
+
path,
|
|
170
|
+
});
|
|
117
171
|
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { fallbackApiList, getNextFallbackApiType, MAIN_BASE_API, } from "../getNextFallbackApi";
|
|
2
2
|
export const sendErrorLog = async ({ endpoint, errors, metadata }) => {
|
|
3
3
|
const userAgentData = 'userAgentData' in window.navigator
|
|
4
4
|
? // userAgentData is not yet supported in all browsers
|
|
@@ -11,8 +11,22 @@ export const sendErrorLog = async ({ endpoint, errors, metadata }) => {
|
|
|
11
11
|
? userAgentData
|
|
12
12
|
: window.navigator.userAgent }, metadata),
|
|
13
13
|
};
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
14
|
+
try {
|
|
15
|
+
// use native fetch because in handleFetch also calls sendErrorLog and it will possibly cause circular dependencies issue
|
|
16
|
+
void fetch(`${MAIN_BASE_API}/api/zonos-elements/log-error`, {
|
|
17
|
+
body: JSON.stringify(errorObject),
|
|
18
|
+
method: 'POST',
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
catch (_err) {
|
|
22
|
+
const nextFallbackApi = getNextFallbackApiType();
|
|
23
|
+
if (!nextFallbackApi) {
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
// request to the fallback API if the first one fails
|
|
27
|
+
void fetch(`${fallbackApiList[nextFallbackApi]}/api/zonos-elements/log-error`, {
|
|
28
|
+
body: JSON.stringify(errorObject),
|
|
29
|
+
method: 'POST',
|
|
30
|
+
});
|
|
31
|
+
}
|
|
18
32
|
};
|
|
@@ -14,7 +14,7 @@ export const sendLoadScriptLog = async (params) => {
|
|
|
14
14
|
const scriptLoadLogBody = Object.assign(Object.assign({ href: window.location.href }, params), { metadata: Object.assign({ connectionDownlink: baseConnection === null || baseConnection === void 0 ? void 0 : baseConnection.downlink, connectionEffectiveType: baseConnection === null || baseConnection === void 0 ? void 0 : baseConnection.effectiveType, connectionRtt: baseConnection === null || baseConnection === void 0 ? void 0 : baseConnection.rtt, connectionSaveData: baseConnection === null || baseConnection === void 0 ? void 0 : baseConnection.saveData, userAgent: userAgentData && userAgentData.brands.length > 0
|
|
15
15
|
? userAgentData
|
|
16
16
|
: window.navigator.userAgent }, params.metadata) });
|
|
17
|
-
void handleFetch(
|
|
17
|
+
void handleFetch('/api/zonos-elements/log-script-load', {
|
|
18
18
|
body: scriptLoadLogBody,
|
|
19
19
|
method: 'POST',
|
|
20
20
|
});
|
|
@@ -5,7 +5,10 @@ import { config } from "dotenv";
|
|
|
5
5
|
import { z } from "zod";
|
|
6
6
|
// rollup and ts somehow can't compile this with absolute path
|
|
7
7
|
// eslint-disable-next-line no-relative-import-paths/no-relative-import-paths
|
|
8
|
-
import {
|
|
8
|
+
import { formattedCloudflareApiUrl } from "./utils/formattedCloudflareApiUrl";
|
|
9
|
+
// rollup and ts somehow can't compile this with absolute path
|
|
10
|
+
// eslint-disable-next-line no-relative-import-paths/no-relative-import-paths
|
|
11
|
+
import { formattedUrl } from "./utils/formattedUrl";
|
|
9
12
|
export const isClientSide = typeof window !== 'undefined';
|
|
10
13
|
export const inProdEnvironment = process.env.NODE_ENV === 'production';
|
|
11
14
|
export const inTestEnvironment = process.env.NODE_ENV === 'test';
|
|
@@ -26,83 +29,6 @@ const UNPKG_ZONOS_URL = `https://unpkg.com/@zonos/elements@${packageVersion}`;
|
|
|
26
29
|
*/
|
|
27
30
|
const CDNJS_ZONOS_URL = `https://cdnjs.cloudflare.com/ajax/libs/zonos-elements/${packageVersion}`;
|
|
28
31
|
const currentBranch = process.env.VERCEL_GIT_COMMIT_REF || process.env.BRANCH_NAME;
|
|
29
|
-
export const formattedUrl = ({ branchName, inCI: _inCI, inVercel: _inVercel, isVercelPreview: _isVercelPreview, packageVersion: _packageVersion, type = 'full', url, }) => {
|
|
30
|
-
if (!url) {
|
|
31
|
-
// return origin value so t3 can throw error when it's empty
|
|
32
|
-
return url;
|
|
33
|
-
}
|
|
34
|
-
const trimmedUrl = (_url) => _url === null || _url === void 0 ? void 0 : _url.replace('https://', '');
|
|
35
|
-
const domainOnlyUrl = trimmedUrl(url);
|
|
36
|
-
if (!_inCI && !_inVercel) {
|
|
37
|
-
// local host environment, no need to format url
|
|
38
|
-
return `https://${domainOnlyUrl}`;
|
|
39
|
-
}
|
|
40
|
-
const isProdBranch = branchName && ['main'].includes(branchName);
|
|
41
|
-
const isServerless =
|
|
42
|
-
// url: route.js.zonos.com
|
|
43
|
-
domainOnlyUrl.includes('route.js') ||
|
|
44
|
-
// url: zonos-elements-serverless-git-something
|
|
45
|
-
domainOnlyUrl.includes('serverless-git');
|
|
46
|
-
const isTest =
|
|
47
|
-
// url: test.js.zonos.com
|
|
48
|
-
domainOnlyUrl.includes('test.') ||
|
|
49
|
-
// url: zonos-elements-test-git-something
|
|
50
|
-
domainOnlyUrl.includes('test-git') ||
|
|
51
|
-
// url: something.preview.js.zonos.com (this is only applicable on preview deployment of `zonos-elements` and `zonos-elements-test`)
|
|
52
|
-
domainOnlyUrl.includes('preview.js.zonos.com');
|
|
53
|
-
if (isTest && isProdBranch) {
|
|
54
|
-
return `https://${domainOnlyUrl}`;
|
|
55
|
-
}
|
|
56
|
-
// always use vercel url if in vercel env, non-production branch (not qa or main branch) or the domain using is a test url
|
|
57
|
-
if ((_isVercelPreview && !isProdBranch) ||
|
|
58
|
-
isTest ||
|
|
59
|
-
(_inVercel && type === 'vercel-git')) {
|
|
60
|
-
// use vercel branch_url instead of the production url
|
|
61
|
-
if (!branchName) {
|
|
62
|
-
throw new Error('Fail to get vercel commit ref or branch name');
|
|
63
|
-
}
|
|
64
|
-
// get project name based on the `${url}`
|
|
65
|
-
const isStorybook =
|
|
66
|
-
// url: storybook.js.zonos.com
|
|
67
|
-
domainOnlyUrl.includes('storybook.js') ||
|
|
68
|
-
// url: zonos-elements-storybook-git-something
|
|
69
|
-
domainOnlyUrl.includes('storybook-git');
|
|
70
|
-
const projectName = isStorybook
|
|
71
|
-
? 'zonos-elements-storybook'
|
|
72
|
-
: isServerless
|
|
73
|
-
? 'zonos-elements-serverless'
|
|
74
|
-
: 'zonos-elements';
|
|
75
|
-
// add -test suffix for test environment
|
|
76
|
-
const fullProjectName = isTest ? `${projectName}-test` : projectName;
|
|
77
|
-
// generate generated deploy url based on the branch name
|
|
78
|
-
const vercelGitBranchSuffix = generateVercelDeployUrl({
|
|
79
|
-
branchName,
|
|
80
|
-
projectName: fullProjectName,
|
|
81
|
-
});
|
|
82
|
-
return `https://${vercelGitBranchSuffix}`;
|
|
83
|
-
}
|
|
84
|
-
// only require package version in production and in CI, so that we still can use dev url (no version prefix).
|
|
85
|
-
if (!_packageVersion && (isProdBranch || _inCI)) {
|
|
86
|
-
throw new Error('package version is required');
|
|
87
|
-
}
|
|
88
|
-
const version = type === 'major' ? `${_packageVersion === null || _packageVersion === void 0 ? void 0 : _packageVersion.split('.')[0]}` : _packageVersion;
|
|
89
|
-
const isVersionedUrl = domainOnlyUrl === null || domainOnlyUrl === void 0 ? void 0 : domainOnlyUrl.startsWith('v');
|
|
90
|
-
const shouldAddVersion = (isProdBranch || _inCI) && !isVersionedUrl;
|
|
91
|
-
/**
|
|
92
|
-
* Trimming the `qa` from the domain url only when we add version prefix
|
|
93
|
-
* @reason We've now introduced alpha versioning for the QA branch deployment.
|
|
94
|
-
* However, we've generated the domain as `v1.2.4-alpha.1.js.zonos.com`, while the
|
|
95
|
-
* current CDN URL env uses `qa.js.zonos.com` on qa branch. This discrepancy causes
|
|
96
|
-
* this function to return an invalid alpha domain (v1.2.4-alpha.1.qa.js.zonos.com).
|
|
97
|
-
*/
|
|
98
|
-
const trimmedQaUrl = shouldAddVersion
|
|
99
|
-
? domainOnlyUrl === null || domainOnlyUrl === void 0 ? void 0 : domainOnlyUrl.replace('qa.js.zonos.com', 'js.zonos.com')
|
|
100
|
-
: domainOnlyUrl;
|
|
101
|
-
// only set version for the url once
|
|
102
|
-
return shouldAddVersion
|
|
103
|
-
? `https://v${version}.${trimmedQaUrl}`
|
|
104
|
-
: `https://${trimmedQaUrl}`;
|
|
105
|
-
};
|
|
106
32
|
if (typeof window === 'undefined') {
|
|
107
33
|
// storybook can't access to any "node" modules
|
|
108
34
|
config();
|
|
@@ -127,6 +53,7 @@ const envClient = {
|
|
|
127
53
|
NEXT_PUBLIC_ZONOS_API_KEY: z.string(),
|
|
128
54
|
NEXT_PUBLIC_ZONOS_CDNJS_CDN_URL: z.string(),
|
|
129
55
|
NEXT_PUBLIC_ZONOS_CDN_URL: z.string(),
|
|
56
|
+
NEXT_PUBLIC_ZONOS_CLOUDFLARE_URL: z.string(),
|
|
130
57
|
NEXT_PUBLIC_ZONOS_JS_DELIVR_CDN_URL: z.string(),
|
|
131
58
|
NEXT_PUBLIC_ZONOS_PACKAGE_VERSION: z.string(),
|
|
132
59
|
NEXT_PUBLIC_ZONOS_ROUTE_URL: z.string(),
|
|
@@ -164,6 +91,14 @@ export const env = createEnv({
|
|
|
164
91
|
type: 'full',
|
|
165
92
|
url: process.env.NEXT_PUBLIC_ZONOS_CDN_URL,
|
|
166
93
|
}),
|
|
94
|
+
NEXT_PUBLIC_ZONOS_CLOUDFLARE_URL: formattedCloudflareApiUrl({
|
|
95
|
+
branchName: currentBranch,
|
|
96
|
+
inCI,
|
|
97
|
+
inVercel,
|
|
98
|
+
isVercelPreview,
|
|
99
|
+
projectName: 'zonos-elements',
|
|
100
|
+
url: process.env.NEXT_PUBLIC_ZONOS_CLOUDFLARE_URL,
|
|
101
|
+
}),
|
|
167
102
|
NEXT_PUBLIC_ZONOS_JS_DELIVR_CDN_URL: JS_DELIVR_ZONOS_URL,
|
|
168
103
|
NEXT_PUBLIC_ZONOS_PACKAGE_VERSION: packageVersion,
|
|
169
104
|
/**
|
|
@@ -16,11 +16,15 @@ export const getZonosQaUrl = () => {
|
|
|
16
16
|
const qaUrl = extractZonosQaUrl(window.location.href);
|
|
17
17
|
const cdnUrl = process.env.NEXT_PUBLIC_ZONOS_CDN_URL;
|
|
18
18
|
// when the qaUrl is not set and the built URL is from the test domain, use the test domain for testing
|
|
19
|
-
if (!qaUrl &&
|
|
19
|
+
if (!qaUrl &&
|
|
20
|
+
(cdnUrl.includes('test.') ||
|
|
21
|
+
// Vercel git domain
|
|
22
|
+
cdnUrl.includes('test-'))) {
|
|
20
23
|
return cdnUrl;
|
|
21
24
|
}
|
|
22
25
|
// when local demo app is running, it needs to use the local dev script unless the qaUrl is set
|
|
23
|
-
if (!qaUrl &&
|
|
26
|
+
if (!qaUrl &&
|
|
27
|
+
['dev.demo.elements.zonos.com'].includes(window.location.hostname)) {
|
|
24
28
|
return 'https://dev.elements.zonos.com';
|
|
25
29
|
}
|
|
26
30
|
if (!qaUrl) {
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// rollup and ts somehow can't compile this with absolute path
|
|
2
|
+
// eslint-disable-next-line no-relative-import-paths/no-relative-import-paths
|
|
3
|
+
import { generateCloudflareDeployUrl } from "./generateCloudflareDeployUrl";
|
|
4
|
+
/**
|
|
5
|
+
* This is to generate cloudflare url based on the environment. This won't
|
|
6
|
+
* - Get the cloudflare alias to build if the environment is Preview
|
|
7
|
+
* - Get the cloudflare production url (not preview alias) if it's production
|
|
8
|
+
* @note
|
|
9
|
+
* This is mainly used for get correct Cloudflare API to be used in scripts
|
|
10
|
+
* @note
|
|
11
|
+
* "url" is the API url in Cloudflare
|
|
12
|
+
* @returns
|
|
13
|
+
* - `https://<branch_name>.<project_name>.pages.dev` if is Preview
|
|
14
|
+
* - `https://route.elements.zonos.com` if is Production
|
|
15
|
+
*/
|
|
16
|
+
export const formattedCloudflareApiUrl = ({ branchName, inCI: _inCI, inVercel: _inVercel, isVercelPreview: _isVercelPreview, projectName, url, }) => {
|
|
17
|
+
if (!url) {
|
|
18
|
+
// return origin value so t3 can throw error when it's empty
|
|
19
|
+
return url;
|
|
20
|
+
}
|
|
21
|
+
const trimmedUrl = (_url) => _url === null || _url === void 0 ? void 0 : _url.replace('https://', '');
|
|
22
|
+
const domainOnlyUrl = trimmedUrl(url);
|
|
23
|
+
if (!_inCI && !_inVercel) {
|
|
24
|
+
// local host environment, no need to format url
|
|
25
|
+
return `https://${domainOnlyUrl}`;
|
|
26
|
+
}
|
|
27
|
+
const isProdBranch = branchName && ['main'].includes(branchName);
|
|
28
|
+
// always use Cloudflare preview alias if in not production environment
|
|
29
|
+
if (_isVercelPreview && !isProdBranch) {
|
|
30
|
+
// use Cloudflare branch_url instead of the production url
|
|
31
|
+
if (!branchName) {
|
|
32
|
+
throw new Error('Fail to get Cloudflare commit ref or branch name');
|
|
33
|
+
}
|
|
34
|
+
// generate generated deploy url based on the branch name
|
|
35
|
+
const cloudflareUrl = generateCloudflareDeployUrl({
|
|
36
|
+
branchName,
|
|
37
|
+
projectName,
|
|
38
|
+
});
|
|
39
|
+
return `https://${cloudflareUrl}`;
|
|
40
|
+
}
|
|
41
|
+
// use cloudflare production url
|
|
42
|
+
return `https://${domainOnlyUrl}`;
|
|
43
|
+
};
|