@shop-on/theme-server 0.9.0 → 0.11.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/dist/account-state.js +1 -2
- package/dist/account.d.ts +1 -3
- package/dist/account.js +1 -3
- package/dist/activity.d.ts +1 -3
- package/dist/activity.js +1 -2
- package/dist/analytics-proxy.d.ts +8 -14
- package/dist/analytics-proxy.js +111 -28
- package/dist/analytics-script-proxy.d.ts +1 -5
- package/dist/analytics-script-proxy.js +4 -14
- package/dist/api-contract/src/addresses.js +32 -0
- package/dist/api-contract/src/areas.js +49 -0
- package/dist/api-contract/src/custom-data.d.ts +31 -0
- package/dist/api-contract/src/custom-data.js +303 -0
- package/dist/api-contract/src/ids.js +110 -0
- package/dist/api-contract/src/image.js +65 -0
- package/dist/api-contract/src/limits.js +44 -0
- package/dist/api-contract/src/media.js +13 -0
- package/dist/api-contract/src/pickup.js +11 -0
- package/dist/api-contract/src/recommendations.js +4 -0
- package/dist/api-contract/src/shops.js +27 -0
- package/dist/api-contract/src/storefront/analytics.d.ts +100 -0
- package/dist/api-contract/src/storefront/analytics.js +138 -0
- package/dist/api-contract/src/storefront/areas.d.ts +2 -0
- package/dist/api-contract/src/storefront/cart.d.ts +132 -0
- package/dist/api-contract/src/storefront/cart.js +248 -0
- package/dist/api-contract/src/storefront/catalog.d.ts +3 -0
- package/dist/api-contract/src/storefront/checkout-analytics.d.ts +2 -0
- package/dist/api-contract/src/storefront/checkout.d.ts +2 -0
- package/dist/api-contract/src/storefront/checkout.js +624 -0
- package/dist/api-contract/src/storefront/client.d.ts +3 -0
- package/dist/api-contract/src/storefront/client.js +6 -0
- package/dist/api-contract/src/storefront/content.d.ts +3 -0
- package/dist/api-contract/src/storefront/custom-data.d.ts +2 -0
- package/dist/api-contract/src/storefront/custom-data.js +27 -0
- package/dist/api-contract/src/storefront/customer.d.ts +2 -0
- package/dist/api-contract/src/storefront/customer.js +261 -0
- package/dist/api-contract/src/storefront/downloads.d.ts +2 -0
- package/dist/api-contract/src/storefront/draft-order-invoice.d.ts +2 -0
- package/dist/api-contract/src/storefront/forms.d.ts +2 -0
- package/dist/api-contract/src/storefront/forms.js +82 -0
- package/dist/api-contract/src/storefront/index.d.ts +20956 -0
- package/dist/api-contract/src/storefront/orders.d.ts +2 -0
- package/dist/api-contract/src/storefront/policies.d.ts +2 -0
- package/dist/api-contract/src/storefront/procedure.d.ts +2 -0
- package/dist/api-contract/src/storefront/procedure.js +29 -0
- package/dist/api-contract/src/storefront/reviews.d.ts +20 -0
- package/dist/api-contract/src/storefront/search.d.ts +3 -0
- package/dist/api-contract/src/storefront/seo.d.ts +2 -0
- package/dist/api-contract/src/storefront/seo.js +132 -0
- package/dist/api-contract/src/storefront/shop.d.ts +2 -0
- package/dist/api-contract/src/storefront/shop.js +101 -0
- package/dist/api-contract/src/taxonomy.js +5 -0
- package/dist/api-contract/src/text.js +15 -0
- package/dist/api-contract/src/theme-documents.js +13 -0
- package/dist/api-contract/src/theme-translations.d.ts +14 -0
- package/dist/api-contract/src/theme-translations.js +30 -0
- package/dist/api-contract/src/video.js +19 -0
- package/dist/availability.d.ts +6 -3
- package/dist/availability.js +11 -5
- package/dist/blog.d.ts +53 -59
- package/dist/blog.js +197 -497
- package/dist/cache.d.ts +5 -6
- package/dist/cache.js +1 -2
- package/dist/canonical-host.d.ts +4 -6
- package/dist/canonical-host.js +5 -6
- package/dist/card.js +159 -0
- package/dist/cart-customer.d.ts +1 -3
- package/dist/cart-customer.js +64 -105
- package/dist/cart-mutations.d.ts +43 -77
- package/dist/cart-mutations.js +106 -232
- package/dist/cart-recommendations.d.ts +1 -3
- package/dist/cart-recommendations.js +1 -2
- package/dist/cart-session.d.ts +1 -3
- package/dist/cart-session.js +1 -2
- package/dist/cart.d.ts +63 -224
- package/dist/cart.js +70 -197
- package/dist/checkout.d.ts +13 -17
- package/dist/checkout.js +75 -101
- package/dist/coalesce.js +57 -0
- package/dist/collection.d.ts +89 -298
- package/dist/collection.js +210 -656
- package/dist/contact.d.ts +20 -10
- package/dist/contact.js +36 -30
- package/dist/content-version.d.ts +37 -21
- package/dist/content-version.js +64 -42
- package/dist/contract-kit/src/client.d.ts +12 -0
- package/dist/contract-kit/src/client.js +156 -0
- package/dist/contract-kit/src/errors.d.ts +1 -0
- package/dist/contract-kit/src/errors.js +81 -0
- package/dist/contract-kit/src/ids.d.ts +1 -0
- package/dist/contract-kit/src/ids.js +118 -0
- package/dist/contract-kit/src/index.d.ts +7 -0
- package/dist/contract-kit/src/money.d.ts +1 -0
- package/dist/contract-kit/src/money.js +10 -0
- package/dist/contract-kit/src/pagination.d.ts +1 -0
- package/dist/contract-kit/src/procedure.d.ts +69 -0
- package/dist/contract-kit/src/procedure.js +16 -0
- package/dist/contract-kit/src/realtime.d.ts +1 -0
- package/dist/cookie-secret.js +1 -2
- package/dist/csrf.js +1 -2
- package/dist/customer-handoff.d.ts +1 -3
- package/dist/customer-handoff.js +10 -25
- package/dist/customer-session.d.ts +1 -3
- package/dist/customer-session.js +1 -2
- package/dist/customer.d.ts +2 -3
- package/dist/customer.js +27 -44
- package/dist/files.d.ts +49 -10
- package/dist/files.js +106 -89
- package/dist/fonts.d.ts +3 -5
- package/dist/fonts.js +16 -35
- package/dist/html.d.ts +1 -3
- package/dist/html.js +1 -2
- package/dist/liquid.d.ts +4 -10
- package/dist/liquid.js +25 -32
- package/dist/listing.js +79 -0
- package/dist/marketing-cookies.d.ts +1 -3
- package/dist/marketing-cookies.js +1 -2
- package/dist/menu.d.ts +46 -155
- package/dist/menu.js +51 -61
- package/dist/metafields.d.ts +9 -5
- package/dist/metafields.js +10 -38
- package/dist/metaobject.d.ts +10 -5
- package/dist/metaobject.js +43 -66
- package/dist/money.d.ts +7 -0
- package/dist/money.js +18 -0
- package/dist/newsletter.d.ts +19 -10
- package/dist/newsletter.js +31 -25
- package/dist/page.d.ts +13 -17
- package/dist/page.js +49 -105
- package/dist/password-session.d.ts +1 -3
- package/dist/password-session.js +1 -2
- package/dist/password.d.ts +25 -18
- package/dist/password.js +41 -59
- package/dist/policy.d.ts +7 -7
- package/dist/policy.js +19 -55
- package/dist/product.d.ts +42 -67
- package/dist/product.js +144 -644
- package/dist/read-ahead.d.ts +18 -0
- package/dist/read-ahead.js +32 -0
- package/dist/recommendations.d.ts +6 -12
- package/dist/recommendations.js +36 -60
- package/dist/review.d.ts +134 -94
- package/dist/review.js +257 -469
- package/dist/search.d.ts +59 -64
- package/dist/search.js +170 -469
- package/dist/seo-context.d.ts +1 -3
- package/dist/seo-context.js +4 -5
- package/dist/seo-documents.d.ts +50 -0
- package/dist/seo-documents.js +149 -0
- package/dist/shop-seo.d.ts +3 -5
- package/dist/shop-seo.js +7 -26
- package/dist/shop.d.ts +38 -42
- package/dist/shop.js +37 -51
- package/dist/store-availability.d.ts +12 -14
- package/dist/store-availability.js +18 -56
- package/dist/storefront.js +199 -0
- package/dist/structured-data.d.ts +3 -28
- package/dist/structured-data.js +19 -49
- package/dist/trace.js +1 -2
- package/dist/translations.d.ts +26 -31
- package/dist/translations.js +55 -28
- package/package.json +16 -14
- package/dist/graphql-env.d.ts +0 -22778
- package/dist/graphql.d.ts +0 -64
- package/dist/graphql.js +0 -344
- package/dist/schema.storefront.graphql +0 -10483
- package/dist/seo-proxy.d.ts +0 -11
- package/dist/seo-proxy.js +0 -26
package/dist/account-state.js
CHANGED
|
@@ -2,7 +2,7 @@ import { derivedCookieSecret } from "./cookie-secret.js";
|
|
|
2
2
|
import { createServerOnlyFn } from "@tanstack/react-start";
|
|
3
3
|
import { createShopBoundCookieConfig } from "@shop-on/customer-runtime/session-cookie";
|
|
4
4
|
import { ACCOUNT_STATE_FORMAT } from "@shop-on/customer-runtime/handoff-state";
|
|
5
|
-
|
|
5
|
+
// src/account-state.ts
|
|
6
6
|
const TTL_SECONDS = 1800;
|
|
7
7
|
const DOMAIN_LABEL = "account-state";
|
|
8
8
|
const sessionConfig = createServerOnlyFn(async () => {
|
|
@@ -51,5 +51,4 @@ const matchesAccountState = createServerOnlyFn(async (state) => {
|
|
|
51
51
|
const b = Buffer.from(state);
|
|
52
52
|
return a.length === b.length && timingSafeEqual(a, b);
|
|
53
53
|
});
|
|
54
|
-
//#endregion
|
|
55
54
|
export { issueAccountState, matchesAccountState };
|
package/dist/account.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
//#region src/account.d.ts
|
|
2
1
|
export declare function normalizeReturnToPath(raw: unknown): string;
|
|
3
2
|
export type AccountRedirectSubpath = "" | `/${string}`;
|
|
4
3
|
export declare function getAccountRedirectUrl(data: {
|
|
@@ -8,5 +7,4 @@ export declare function getAccountRedirectUrl(data: {
|
|
|
8
7
|
url: string;
|
|
9
8
|
}>;
|
|
10
9
|
export declare function handleCustomerLogoutPost(request: Request): Promise<Response>;
|
|
11
|
-
export declare function renderLogoutForm(accountLogoutUrl: string, returnTo: string): string;
|
|
12
|
-
//#endregion
|
|
10
|
+
export declare function renderLogoutForm(accountLogoutUrl: string, returnTo: string): string;
|
package/dist/account.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { fetchShopInfo } from "./shop.js";
|
|
2
2
|
import { checkOrigin } from "./csrf.js";
|
|
3
3
|
import { escapeHtmlAttr } from "./html.js";
|
|
4
|
-
|
|
4
|
+
// src/account.ts
|
|
5
5
|
const RETURN_TO_BASE = "http://return-to.invalid";
|
|
6
6
|
const FORBIDDEN_RETURN_TO = /^(?:\/[^/]+)?\/(?:account|customer_logout)(?:\/|$)/;
|
|
7
7
|
function normalizeReturnToPath(raw) {
|
|
@@ -15,7 +15,6 @@ function normalizeReturnToPath(raw) {
|
|
|
15
15
|
}
|
|
16
16
|
async function getAccountRedirectUrl(data) {
|
|
17
17
|
const shop = await fetchShopInfo();
|
|
18
|
-
if (!shop) throw new Error("[theme/account] shop info unavailable from Rails");
|
|
19
18
|
const subpath = data.subpath ?? "";
|
|
20
19
|
const target = new URL(`${shop.accountUrl}${subpath}`);
|
|
21
20
|
if (data.returnToPath !== void 0) {
|
|
@@ -76,5 +75,4 @@ function renderLogoutForm(accountLogoutUrl, returnTo) {
|
|
|
76
75
|
<script>document.forms[0].submit()<\/script>
|
|
77
76
|
</body></html>`;
|
|
78
77
|
}
|
|
79
|
-
//#endregion
|
|
80
78
|
export { getAccountRedirectUrl, handleCustomerLogoutPost, normalizeReturnToPath, renderLogoutForm };
|
package/dist/activity.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
//#region src/activity.d.ts
|
|
2
1
|
/**
|
|
3
2
|
* In-memory last-request tracker for a running storefront isolate. The
|
|
4
3
|
* orchestrator probes `/health` to prove an install renders before it publishes
|
|
@@ -21,5 +20,4 @@ export declare function createActivityTracker({ now }?: {
|
|
|
21
20
|
* must not count as activity, which the short-circuit guarantees by returning
|
|
22
21
|
* before the record.
|
|
23
22
|
*/
|
|
24
|
-
export declare const storefrontActivityMiddleware: import("@tanstack/react-start").RequestMiddlewareAfterServer<{}, undefined, undefined>;
|
|
25
|
-
//#endregion
|
|
23
|
+
export declare const storefrontActivityMiddleware: import("@tanstack/react-start").RequestMiddlewareAfterServer<{}, undefined, undefined>;
|
package/dist/activity.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { createMiddleware } from "@tanstack/react-start";
|
|
2
|
-
|
|
2
|
+
// src/activity.ts
|
|
3
3
|
/**
|
|
4
4
|
* In-memory last-request tracker for a running storefront isolate. The
|
|
5
5
|
* orchestrator probes `/health` to prove an install renders before it publishes
|
|
@@ -31,5 +31,4 @@ const storefrontActivityMiddleware = createMiddleware().server(async ({ next, re
|
|
|
31
31
|
tracker.record();
|
|
32
32
|
return next();
|
|
33
33
|
});
|
|
34
|
-
//#endregion
|
|
35
34
|
export { createActivityTracker, storefrontActivityMiddleware };
|
|
@@ -1,23 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
export
|
|
7
|
-
export declare function proxyAnalytics({ endpoint, body, visitToken, visitorToken, userAgent, clientIp, fbp, fbc, cookies, eventSourceUrl }: {
|
|
1
|
+
import { StorefrontEvent } from "./api-contract/src/storefront/analytics.js";
|
|
2
|
+
import "./api-contract/src/storefront/index.js";
|
|
3
|
+
export type AnalyticsEndpoint = "visits" | "events";
|
|
4
|
+
/** The beacon's events the contract takes, at most a call's worth. */
|
|
5
|
+
export declare function storefrontEvents(body: unknown): StorefrontEvent[];
|
|
6
|
+
export declare function proxyAnalytics({ endpoint, body, visitToken, visitorToken, clientIp, fbp, fbc, cookies, eventSourceUrl }: {
|
|
8
7
|
endpoint: AnalyticsEndpoint;
|
|
9
8
|
body: unknown;
|
|
10
9
|
visitToken?: string | null;
|
|
11
10
|
visitorToken?: string | null;
|
|
12
|
-
userAgent?: string | null;
|
|
13
11
|
clientIp?: string | null;
|
|
14
12
|
fbp?: string | null;
|
|
15
13
|
fbc?: string | null;
|
|
16
14
|
cookies?: string | null;
|
|
17
15
|
eventSourceUrl?: string | null;
|
|
18
|
-
}): Promise<
|
|
19
|
-
|
|
20
|
-
}>;
|
|
21
|
-
export declare function extractClientIp(headers: Headers): string | null;
|
|
22
|
-
//#endregion
|
|
23
|
-
export {};
|
|
16
|
+
}): Promise<void>;
|
|
17
|
+
export declare function extractClientIp(headers: Headers): string | null;
|
package/dist/analytics-proxy.js
CHANGED
|
@@ -1,32 +1,116 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import { analyticsToken, storefrontEvent } from "./api-contract/src/storefront/analytics.js";
|
|
2
|
+
import { toMnt } from "./money.js";
|
|
3
|
+
import { storefront } from "./storefront.js";
|
|
4
4
|
import { isIP } from "node:net";
|
|
5
|
-
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
// src/analytics-proxy.ts
|
|
6
|
+
const record = (value) => typeof value === "object" && value !== null && !Array.isArray(value) ? value : {};
|
|
7
|
+
const text = (value) => typeof value === "string" && value ? value : void 0;
|
|
8
|
+
const money = (value) => typeof value === "number" ? toMnt(value) ?? void 0 : void 0;
|
|
9
|
+
const list = (p) => p.list_type === void 0 ? void 0 : {
|
|
10
|
+
kind: p.list_type,
|
|
11
|
+
collectionId: text(p.list_id)
|
|
9
12
|
};
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
"
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
/** An event the theme sent, as the contract's: the Rails properties renamed. */
|
|
14
|
+
function contractEvent(name, p) {
|
|
15
|
+
switch (name) {
|
|
16
|
+
case "search_submitted": return {
|
|
17
|
+
query: p.query,
|
|
18
|
+
resultCount: p.total_count
|
|
19
|
+
};
|
|
20
|
+
case "collection_viewed": return {
|
|
21
|
+
collectionId: p.collection_id,
|
|
22
|
+
collectionHandle: text(p.collection_handle)
|
|
23
|
+
};
|
|
24
|
+
case "product_list_viewed": return {
|
|
25
|
+
list: list(p),
|
|
26
|
+
resultCount: p.total_count,
|
|
27
|
+
items: Array.isArray(p.items) ? p.items.map((item) => ({
|
|
28
|
+
productId: record(item).product_id,
|
|
29
|
+
index: record(item).position
|
|
30
|
+
})) : void 0
|
|
31
|
+
};
|
|
32
|
+
case "product_clicked": return {
|
|
33
|
+
productId: p.product_id,
|
|
34
|
+
productHandle: text(p.product_handle),
|
|
35
|
+
list: list(p),
|
|
36
|
+
index: p.position
|
|
37
|
+
};
|
|
38
|
+
case "product_viewed": return {
|
|
39
|
+
productId: p.product_id,
|
|
40
|
+
productHandle: text(p.product_handle)
|
|
41
|
+
};
|
|
42
|
+
case "recommendation_shown":
|
|
43
|
+
case "recommendation_clicked": return {
|
|
44
|
+
productId: p.recommended_product_id,
|
|
45
|
+
productHandle: text(p.recommended_product_handle),
|
|
46
|
+
referenceProductId: p.reference_product_id,
|
|
47
|
+
referenceProductHandle: text(p.reference_product_handle),
|
|
48
|
+
intent: typeof p.intent === "string" ? p.intent.toLowerCase() : void 0,
|
|
49
|
+
index: p.position
|
|
50
|
+
};
|
|
51
|
+
case "product_added_to_cart":
|
|
52
|
+
case "product_removed_from_cart": return {
|
|
53
|
+
productId: p.product_id,
|
|
54
|
+
variantId: p.variant_id,
|
|
55
|
+
quantity: p.quantity
|
|
56
|
+
};
|
|
57
|
+
case "cart_viewed": return {
|
|
58
|
+
lineCount: p.line_count,
|
|
59
|
+
totalQuantity: p.total_quantity,
|
|
60
|
+
subtotal: money(p.subtotal_amount)
|
|
61
|
+
};
|
|
62
|
+
case "checkout_started": return {
|
|
63
|
+
totalQuantity: p.total_quantity,
|
|
64
|
+
subtotal: money(p.subtotal_amount)
|
|
65
|
+
};
|
|
66
|
+
default: return {};
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
/** The beacon's events the contract takes, at most a call's worth. */
|
|
70
|
+
function storefrontEvents(body) {
|
|
71
|
+
const events = record(body).events;
|
|
72
|
+
if (!Array.isArray(events)) return [];
|
|
73
|
+
return events.flatMap((raw) => {
|
|
74
|
+
const event = record(raw);
|
|
75
|
+
const parsed = storefrontEvent.safeParse({
|
|
76
|
+
id: event.id,
|
|
77
|
+
name: event.name,
|
|
78
|
+
...contractEvent(event.name, record(event.properties))
|
|
79
|
+
});
|
|
80
|
+
return parsed.success ? [parsed.data] : [];
|
|
81
|
+
}).slice(0, 20);
|
|
82
|
+
}
|
|
83
|
+
async function proxyAnalytics({ endpoint, body, visitToken, visitorToken, clientIp, fbp, fbc, cookies, eventSourceUrl }) {
|
|
84
|
+
const visitId = analyticsToken.safeParse(visitToken);
|
|
85
|
+
const visitorId = analyticsToken.safeParse(visitorToken);
|
|
86
|
+
if (!visitId.success || !visitorId.success) return;
|
|
87
|
+
const api = storefront(void 0, clientIp ? { "x-forwarded-for": clientIp } : {});
|
|
88
|
+
const visit = {
|
|
89
|
+
visitId: visitId.data,
|
|
90
|
+
visitorId: visitorId.data
|
|
91
|
+
};
|
|
92
|
+
if (endpoint === "visits") {
|
|
93
|
+
const { landing_page, referrer } = record(body);
|
|
94
|
+
await api.analytics.visits.record({
|
|
95
|
+
...visit,
|
|
96
|
+
landingUrl: text(landing_page),
|
|
97
|
+
referrerUrl: text(referrer)
|
|
98
|
+
});
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
const events = storefrontEvents(body);
|
|
102
|
+
if (events.length === 0) return;
|
|
103
|
+
const marketing = {
|
|
104
|
+
fbp: fbp ?? void 0,
|
|
105
|
+
fbc: fbc ?? void 0,
|
|
106
|
+
cookies: cookies ?? void 0,
|
|
107
|
+
eventSourceUrl: eventSourceUrl ?? void 0
|
|
16
108
|
};
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
if (fbc) headers["X-Marketing-Fbc"] = fbc;
|
|
23
|
-
if (cookies) headers["X-Marketing-Cookies"] = cookies;
|
|
24
|
-
if (eventSourceUrl) headers["X-Event-Source-Url"] = eventSourceUrl;
|
|
25
|
-
return { status: (await fetch(`${storefrontApiUrl()}${PATHS[endpoint]}`, {
|
|
26
|
-
method: "POST",
|
|
27
|
-
headers,
|
|
28
|
-
body: JSON.stringify(body ?? {})
|
|
29
|
-
})).status };
|
|
109
|
+
await api.analytics.events.record({
|
|
110
|
+
...visit,
|
|
111
|
+
events,
|
|
112
|
+
...Object.values(marketing).some(Boolean) ? { marketing } : {}
|
|
113
|
+
});
|
|
30
114
|
}
|
|
31
115
|
function extractClientIp(headers) {
|
|
32
116
|
const realIp = headers.get("x-real-ip");
|
|
@@ -38,5 +122,4 @@ function extractClientIp(headers) {
|
|
|
38
122
|
}
|
|
39
123
|
return null;
|
|
40
124
|
}
|
|
41
|
-
|
|
42
|
-
export { extractClientIp, proxyAnalytics };
|
|
125
|
+
export { extractClientIp, proxyAnalytics, storefrontEvents };
|
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
//#region src/analytics-script-proxy.d.ts
|
|
2
1
|
export interface AnalyticsScriptDocument {
|
|
3
2
|
body: string;
|
|
4
|
-
/** Rails' own `Cache-Control`, so the browser TTL stays a server decision. */
|
|
5
|
-
cacheControl: string | null;
|
|
6
3
|
}
|
|
7
4
|
/**
|
|
8
5
|
* The script body, memoized, under the hard `DEADLINE_MS` ceiling. Rejects when
|
|
@@ -14,5 +11,4 @@ export interface AnalyticsScriptDocument {
|
|
|
14
11
|
* `Promise.race` has already attached a handler to the memoized call, so a
|
|
15
12
|
* later failure can't surface as an unhandled rejection.
|
|
16
13
|
*/
|
|
17
|
-
export declare function getAnalyticsScript(): Promise<AnalyticsScriptDocument>;
|
|
18
|
-
//#endregion
|
|
14
|
+
export declare function getAnalyticsScript(): Promise<AnalyticsScriptDocument>;
|
|
@@ -1,21 +1,12 @@
|
|
|
1
1
|
import { memoTTL } from "./cache.js";
|
|
2
|
-
import { getStorefrontTokenWithin, storefrontApiUrl } from "./graphql.js";
|
|
3
2
|
import { getContentVersion } from "./content-version.js";
|
|
4
|
-
|
|
3
|
+
import { storefront } from "./storefront.js";
|
|
4
|
+
// src/analytics-script-proxy.ts
|
|
5
5
|
const TIMEOUT_MS = 2e3;
|
|
6
6
|
const DEADLINE_MS = 2500;
|
|
7
7
|
async function fetchAnalyticsScript() {
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
const res = await fetch(`${storefrontApiUrl()}/storefront/analytics/script`, {
|
|
11
|
-
headers: { Authorization: `Bearer ${token}` },
|
|
12
|
-
signal
|
|
13
|
-
});
|
|
14
|
-
if (!res.ok) throw new Error(`[analytics-script-proxy] upstream ${res.status}`);
|
|
15
|
-
return {
|
|
16
|
-
body: await res.text(),
|
|
17
|
-
cacheControl: res.headers.get("cache-control")
|
|
18
|
-
};
|
|
8
|
+
const { script } = await storefront().marketing.script({}, { context: { timeoutMs: TIMEOUT_MS } });
|
|
9
|
+
return { body: script };
|
|
19
10
|
}
|
|
20
11
|
const memoizedPixelScript = memoTTL(fetchAnalyticsScript, {
|
|
21
12
|
key: () => "analytics-script",
|
|
@@ -42,5 +33,4 @@ async function getAnalyticsScript() {
|
|
|
42
33
|
clearTimeout(timer);
|
|
43
34
|
}
|
|
44
35
|
}
|
|
45
|
-
//#endregion
|
|
46
36
|
export { getAnalyticsScript };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { liteId } from "./ids.js";
|
|
2
|
+
import { e164Phone } from "./text.js";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
const personName = z.string().trim().max(255);
|
|
5
|
+
const businessAddressInput = z.object({
|
|
6
|
+
/** The most specific area chosen (khoroo, sum or district). */
|
|
7
|
+
areaId: liteId("area"),
|
|
8
|
+
address1: z.string().trim().min(1).max(255),
|
|
9
|
+
address2: z.string().trim().max(255).nullable().optional(),
|
|
10
|
+
postalCode: z.string().regex(/^[0-9A-Za-z -]{3,12}$/).nullable().optional(),
|
|
11
|
+
/** Directions to the door. */
|
|
12
|
+
instructions: z.string().trim().max(500).nullable().optional(),
|
|
13
|
+
coordinates: z.object({
|
|
14
|
+
latitude: z.number().min(-90).max(90),
|
|
15
|
+
longitude: z.number().min(-180).max(180)
|
|
16
|
+
}).nullable().optional()
|
|
17
|
+
});
|
|
18
|
+
/**
|
|
19
|
+
* A customer address as the address dialog sends it. Its area names the
|
|
20
|
+
* levels its country asks for (`areas.list`'s `addressRules`), else
|
|
21
|
+
* VALIDATION_FAILED `area_incomplete` `{ missing }` on `areaId`. A blank
|
|
22
|
+
* `city` is the area's aimag's (or the capital's) name, as the forms that
|
|
23
|
+
* hide the city for Mongolia send it.
|
|
24
|
+
*/
|
|
25
|
+
const customerAddressInput = businessAddressInput.safeExtend({
|
|
26
|
+
firstName: personName.nullable().optional(),
|
|
27
|
+
lastName: personName.nullable().optional(),
|
|
28
|
+
company: personName.nullable().optional(),
|
|
29
|
+
phone: e164Phone.nullable().optional(),
|
|
30
|
+
city: z.string().trim().max(255).nullable().optional()
|
|
31
|
+
});
|
|
32
|
+
export { businessAddressInput, customerAddressInput, personName };
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { liteId } from "./ids.js";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
// ../api-contract/src/areas.ts
|
|
4
|
+
/** Depth in the tree: country; province (an aimag or the capital); district (a sum or düüreg); khoroo (a bag or khoroo). */
|
|
5
|
+
const AREA_KINDS = [
|
|
6
|
+
"country",
|
|
7
|
+
"province",
|
|
8
|
+
"district",
|
|
9
|
+
"khoroo"
|
|
10
|
+
];
|
|
11
|
+
/** An area's names: Mongolian always, English where the source has one (else null: show `mn`). */
|
|
12
|
+
const areaName = z.object({
|
|
13
|
+
mn: z.string(),
|
|
14
|
+
en: z.string().nullable()
|
|
15
|
+
});
|
|
16
|
+
/** One area as an address prints it. */
|
|
17
|
+
const areaRef = z.object({
|
|
18
|
+
id: liteId("area"),
|
|
19
|
+
kind: z.enum(AREA_KINDS),
|
|
20
|
+
name: areaName
|
|
21
|
+
});
|
|
22
|
+
z.object({
|
|
23
|
+
south: z.number(),
|
|
24
|
+
west: z.number(),
|
|
25
|
+
north: z.number(),
|
|
26
|
+
east: z.number()
|
|
27
|
+
});
|
|
28
|
+
areaRef.extend({
|
|
29
|
+
/** Whether the area has live children: whether the picker goes a level deeper. */
|
|
30
|
+
hasChildren: z.boolean(),
|
|
31
|
+
/**
|
|
32
|
+
* A country's and a province's ISO key (`MN`; `1` is Ulaanbaatar), a
|
|
33
|
+
* district's or khoroo's administrative code (`1107`); a picker finds an
|
|
34
|
+
* area by it, never by its name.
|
|
35
|
+
*/
|
|
36
|
+
code: z.string().nullable(),
|
|
37
|
+
/**
|
|
38
|
+
* A country's address rules, null below a country: an address there names
|
|
39
|
+
* a province (`provinceRequired`), the full chain down to the khoroo
|
|
40
|
+
* (`fullChainRequired`; an area with no children ends it), a postal code
|
|
41
|
+
* (`postalCodeRequired`). The api refuses an address short of them.
|
|
42
|
+
*/
|
|
43
|
+
addressRules: z.object({
|
|
44
|
+
provinceRequired: z.boolean(),
|
|
45
|
+
fullChainRequired: z.boolean(),
|
|
46
|
+
postalCodeRequired: z.boolean()
|
|
47
|
+
}).nullable()
|
|
48
|
+
});
|
|
49
|
+
export { AREA_KINDS, areaName, areaRef };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import "zod";
|
|
2
|
+
export type RichTextNode = {
|
|
3
|
+
type: "text";
|
|
4
|
+
value: string;
|
|
5
|
+
bold?: boolean;
|
|
6
|
+
italic?: boolean;
|
|
7
|
+
} | {
|
|
8
|
+
type: "link";
|
|
9
|
+
url: string;
|
|
10
|
+
title?: string;
|
|
11
|
+
target?: "_blank" | "_self";
|
|
12
|
+
children: RichTextNode[];
|
|
13
|
+
} | {
|
|
14
|
+
type: "image";
|
|
15
|
+
url: string;
|
|
16
|
+
alt?: string;
|
|
17
|
+
} | {
|
|
18
|
+
type: "paragraph";
|
|
19
|
+
children: RichTextNode[];
|
|
20
|
+
} | {
|
|
21
|
+
type: "heading";
|
|
22
|
+
level: number;
|
|
23
|
+
children: RichTextNode[];
|
|
24
|
+
} | {
|
|
25
|
+
type: "list";
|
|
26
|
+
listType: "ordered" | "unordered";
|
|
27
|
+
children: RichTextNode[];
|
|
28
|
+
} | {
|
|
29
|
+
type: "list-item";
|
|
30
|
+
children: RichTextNode[];
|
|
31
|
+
};
|