@shop-on/theme-server 0.10.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
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import { BUYER_SESSION_HEADER } from "./api-contract/src/storefront/customer.js";
|
|
2
|
+
import { CONTENT_VERSION_HEADER } from "./api-contract/src/storefront/shop.js";
|
|
3
|
+
import { createStorefrontClient } from "./api-contract/src/storefront/client.js";
|
|
4
|
+
import { currentRequest, requestSlot } from "./request-scope.js";
|
|
5
|
+
import { requestTraceHeaders } from "./trace.js";
|
|
6
|
+
import { shardBuyerHeaders } from "@shop-on/customer-runtime/relay";
|
|
7
|
+
// src/storefront.ts
|
|
8
|
+
/** How long one procedure call may take before it fails DEADLINE_EXCEEDED. */
|
|
9
|
+
const STOREFRONT_TIMEOUT_MS = 8e3;
|
|
10
|
+
/** A deploy bug: the storefront has no usable credential. Never degraded, always a 500. */
|
|
11
|
+
var StorefrontConfigError = class extends Error {
|
|
12
|
+
constructor(message) {
|
|
13
|
+
super(message);
|
|
14
|
+
this.name = "StorefrontConfigError";
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
function assertServerOnly() {
|
|
18
|
+
if (typeof window !== "undefined") throw new Error("the storefront api client is server-only and must not be imported into browser code");
|
|
19
|
+
}
|
|
20
|
+
/** The api's origin. Read at call time: a module-scope read would crash a client bundle that pulled this file in. */
|
|
21
|
+
function storefrontApiUrl() {
|
|
22
|
+
assertServerOnly();
|
|
23
|
+
return process.env.STOREFRONT_API_URL || "http://localhost:4000";
|
|
24
|
+
}
|
|
25
|
+
function storefrontKey() {
|
|
26
|
+
const key = process.env.STOREFRONT_KEY || "";
|
|
27
|
+
if (!key) throw new StorefrontConfigError("the storefront has no STOREFRONT_KEY");
|
|
28
|
+
return key;
|
|
29
|
+
}
|
|
30
|
+
/** The contract code a call failed with, if it failed with one. */
|
|
31
|
+
function errorCode(error) {
|
|
32
|
+
if (typeof error !== "object" || error === null) return void 0;
|
|
33
|
+
const { code } = error;
|
|
34
|
+
return typeof code === "string" ? code : void 0;
|
|
35
|
+
}
|
|
36
|
+
/** The input a VALIDATION_FAILED names: the head of an issue's path (the
|
|
37
|
+
* first issue's unless `issue` says), null when it names none. */
|
|
38
|
+
function invalidField(error, issue = 0) {
|
|
39
|
+
const { issues } = error.data ?? {};
|
|
40
|
+
const head = issues?.[issue]?.path[0];
|
|
41
|
+
return head === void 0 ? null : String(head);
|
|
42
|
+
}
|
|
43
|
+
/** A read whose NOT_FOUND is the caller's 404: null. Anything else is thrown. */
|
|
44
|
+
async function orNotFound(call) {
|
|
45
|
+
try {
|
|
46
|
+
return await call;
|
|
47
|
+
} catch (error) {
|
|
48
|
+
if (errorCode(error) === "NOT_FOUND") return null;
|
|
49
|
+
throw error;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* A read a page renders without (a footer's links): any failure but a
|
|
54
|
+
* misconfigured storefront is logged and answered with `fallback`.
|
|
55
|
+
*/
|
|
56
|
+
async function orFallback(call, fallback, label) {
|
|
57
|
+
try {
|
|
58
|
+
return await call;
|
|
59
|
+
} catch (error) {
|
|
60
|
+
if (error instanceof StorefrontConfigError) throw error;
|
|
61
|
+
console.warn(`[${label}] failed: ${errorCode(error) ?? String(error)}`);
|
|
62
|
+
return fallback;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
const SHOP_UNAVAILABLE = Symbol("storefront shop unavailable");
|
|
66
|
+
/** Whether a call this request made answered SHOP_UNAVAILABLE, and with which status. */
|
|
67
|
+
function shopUnavailableThisRequest() {
|
|
68
|
+
return requestSlot(SHOP_UNAVAILABLE, () => ({}))?.status;
|
|
69
|
+
}
|
|
70
|
+
function failed(error, buyer) {
|
|
71
|
+
const code = errorCode(error);
|
|
72
|
+
if (code === "UNAUTHENTICATED" && !buyer) {
|
|
73
|
+
console.error("[storefront] the api refused the storefront key (UNAUTHENTICATED): check STOREFRONT_KEY");
|
|
74
|
+
throw new StorefrontConfigError("the api refused the storefront key");
|
|
75
|
+
}
|
|
76
|
+
if (code === "SHOP_UNAVAILABLE") {
|
|
77
|
+
const { status } = error.data ?? {};
|
|
78
|
+
const mark = requestSlot(SHOP_UNAVAILABLE, () => ({}));
|
|
79
|
+
if (mark && status) mark.status = status;
|
|
80
|
+
}
|
|
81
|
+
throw error;
|
|
82
|
+
}
|
|
83
|
+
let seenContentVersion;
|
|
84
|
+
let callsSent = 0;
|
|
85
|
+
let seenFromCall = 0;
|
|
86
|
+
/** The content version of the latest-sent call whose answer carried one. */
|
|
87
|
+
function lastSeenContentVersion() {
|
|
88
|
+
return seenContentVersion;
|
|
89
|
+
}
|
|
90
|
+
async function sendRecordingContentVersion(request) {
|
|
91
|
+
const call = ++callsSent;
|
|
92
|
+
const sentAt = Date.now();
|
|
93
|
+
const response = await fetch(request);
|
|
94
|
+
const version = response.headers.get(CONTENT_VERSION_HEADER);
|
|
95
|
+
if (version && call > seenFromCall) {
|
|
96
|
+
seenFromCall = call;
|
|
97
|
+
seenContentVersion = {
|
|
98
|
+
version,
|
|
99
|
+
sentAt
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
return response;
|
|
103
|
+
}
|
|
104
|
+
const DEDUPE = Symbol("storefront calls");
|
|
105
|
+
/** The storefront procedures that are not reads, by path: each call goes out. */
|
|
106
|
+
const WRITES = /* @__PURE__ */ new Set([
|
|
107
|
+
"analytics.visits.record",
|
|
108
|
+
"analytics.events.record",
|
|
109
|
+
"passwordProtection.unlock",
|
|
110
|
+
"reviews.submit",
|
|
111
|
+
"reviews.vote",
|
|
112
|
+
"reviews.photos.create",
|
|
113
|
+
"contact.submit",
|
|
114
|
+
"newsletter.subscribe",
|
|
115
|
+
"cart.create",
|
|
116
|
+
"cart.lines.add",
|
|
117
|
+
"cart.lines.update",
|
|
118
|
+
"cart.lines.remove",
|
|
119
|
+
"cart.note.set",
|
|
120
|
+
"cart.discountCode.set",
|
|
121
|
+
"cart.claim",
|
|
122
|
+
"cart.attach",
|
|
123
|
+
"customer.signIn.requestCode",
|
|
124
|
+
"customer.signIn.verifyCode",
|
|
125
|
+
"customer.signOut",
|
|
126
|
+
"customer.handoff.create",
|
|
127
|
+
"customer.handoff.consume",
|
|
128
|
+
"checkout.start"
|
|
129
|
+
]);
|
|
130
|
+
/**
|
|
131
|
+
* Within one request, an identical read (same language, headers, procedure
|
|
132
|
+
* and input) shares the first one's answer; outside a request every call goes
|
|
133
|
+
* out, and so does every call to a procedure in `WRITES`. A procedure that is
|
|
134
|
+
* not a read joins `WRITES` when it joins the contract.
|
|
135
|
+
*/
|
|
136
|
+
function deduplicated(target, scope, path) {
|
|
137
|
+
return new Proxy(target, {
|
|
138
|
+
get(inner, key) {
|
|
139
|
+
const value = Reflect.get(inner, key);
|
|
140
|
+
if (typeof key !== "string" || typeof value !== "function") return value;
|
|
141
|
+
return deduplicated(value, scope, [...path, key]);
|
|
142
|
+
},
|
|
143
|
+
apply(inner, thisArg, args) {
|
|
144
|
+
const send = () => Reflect.apply(inner, thisArg, args).catch((error) => failed(error, BUYER_SESSION_HEADER in scope.headers));
|
|
145
|
+
if (args.length > 1 || WRITES.has(path.join("."))) return send();
|
|
146
|
+
const calls = requestSlot(DEDUPE, () => /* @__PURE__ */ new Map());
|
|
147
|
+
if (!calls) return send();
|
|
148
|
+
const key = `${scope.language}\n${JSON.stringify(scope.headers)}\n${path.join(".")}\n${JSON.stringify(args[0] ?? null)}`;
|
|
149
|
+
let call = calls.get(key);
|
|
150
|
+
if (!call) {
|
|
151
|
+
call = send();
|
|
152
|
+
calls.set(key, call);
|
|
153
|
+
}
|
|
154
|
+
return call;
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* The buyer's `User-Agent`, from the request this call runs under: the api
|
|
160
|
+
* records it on a review. Empty outside a request or when the buyer sent
|
|
161
|
+
* none, so the call carries the runtime's own.
|
|
162
|
+
*/
|
|
163
|
+
function buyerUserAgentHeaders() {
|
|
164
|
+
const userAgent = currentRequest()?.headers.get("user-agent");
|
|
165
|
+
return userAgent ? { "user-agent": userAgent } : {};
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* A storefront client whose calls are in `locale`, sent as `Accept-Language`.
|
|
169
|
+
* The api answers in the first of the shop's PUBLISHED languages that
|
|
170
|
+
* Accept-Language names, else the shop's primary; its own strings (policy
|
|
171
|
+
* titles) are mn or en, English for any other language. So `storefront()`
|
|
172
|
+
* with no locale sends none and gets the shop's primary language. `headers`
|
|
173
|
+
* go on every call after the others.
|
|
174
|
+
*/
|
|
175
|
+
function storefront(locale, headers = {}) {
|
|
176
|
+
assertServerOnly();
|
|
177
|
+
const language = locale ?? "";
|
|
178
|
+
return deduplicated(createStorefrontClient({
|
|
179
|
+
baseUrl: storefrontApiUrl(),
|
|
180
|
+
timeoutMs: STOREFRONT_TIMEOUT_MS,
|
|
181
|
+
fetch: sendRecordingContentVersion,
|
|
182
|
+
headers: () => ({
|
|
183
|
+
authorization: `Bearer ${storefrontKey()}`,
|
|
184
|
+
...language ? { "accept-language": language } : {},
|
|
185
|
+
...requestTraceHeaders(),
|
|
186
|
+
...shardBuyerHeaders(),
|
|
187
|
+
...buyerUserAgentHeaders(),
|
|
188
|
+
...headers
|
|
189
|
+
})
|
|
190
|
+
}), {
|
|
191
|
+
language,
|
|
192
|
+
headers
|
|
193
|
+
}, []);
|
|
194
|
+
}
|
|
195
|
+
/** A storefront client whose calls are made for the buyer `session` (a `buys_…` token) signed in. */
|
|
196
|
+
function buyerStorefront(session, locale) {
|
|
197
|
+
return storefront(locale, { [BUYER_SESSION_HEADER]: session });
|
|
198
|
+
}
|
|
199
|
+
export { STOREFRONT_TIMEOUT_MS, StorefrontConfigError, assertServerOnly, buyerStorefront, errorCode, invalidField, lastSeenContentVersion, orFallback, orNotFound, shopUnavailableThisRequest, storefront, storefrontApiUrl };
|
|
@@ -1,34 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { StorefrontClient } from "./api-contract/src/storefront/client.js";
|
|
2
2
|
import { OrganizationSchemaSource, ReturnPolicySchemaSource } from "@shop-on/theme-runtime/template-types";
|
|
3
|
-
|
|
4
|
-
declare const ShopStructuredDataQuery: import("gql.tada").TadaDocumentNode<{
|
|
5
|
-
shop: {
|
|
6
|
-
branding: {
|
|
7
|
-
logo: {
|
|
8
|
-
url: string | null;
|
|
9
|
-
} | null;
|
|
10
|
-
} | null;
|
|
11
|
-
countryCode: string | null;
|
|
12
|
-
refundPolicy: {
|
|
13
|
-
url: string;
|
|
14
|
-
} | null;
|
|
15
|
-
returnPolicy: {
|
|
16
|
-
configured: boolean;
|
|
17
|
-
flatRateAmount: {
|
|
18
|
-
amount: string;
|
|
19
|
-
currencyCode: "CNY" | "EUR" | "GBP" | "JPY" | "KRW" | "MNT" | "RUB" | "USD";
|
|
20
|
-
} | null;
|
|
21
|
-
restockingFeePercentage: number | null;
|
|
22
|
-
returnShippingCost: "CUSTOMER_PROVIDED" | "FLAT_RATE" | "FREE";
|
|
23
|
-
returnWindowDays: number | null;
|
|
24
|
-
};
|
|
25
|
-
} | null;
|
|
26
|
-
}, {}, void>;
|
|
27
|
-
type ShopRecord = NonNullable<ResultOf<typeof ShopStructuredDataQuery>["shop"]>;
|
|
3
|
+
type ShopRecord = Awaited<ReturnType<StorefrontClient["seo"]["structuredData"]>>;
|
|
28
4
|
/**
|
|
29
5
|
* The shop's return policy as schema.org reads it, for one product or (with
|
|
30
6
|
* `finalSale` false) the shop as a whole. Undefined until the merchant saves a
|
|
31
|
-
* policy: the defaults an unconfigured shop
|
|
7
|
+
* policy: the defaults an unconfigured shop prefills ("unlimited window,
|
|
32
8
|
* free returns") are no promise the merchant made, and publishing them would
|
|
33
9
|
* tell Google and every agent exactly that. Also undefined without a country,
|
|
34
10
|
* which Google requires.
|
|
@@ -43,5 +19,4 @@ export declare function fetchProductReturnPolicy(finalSale: boolean, base: strin
|
|
|
43
19
|
/** The homepage's Organization node. Undefined without a name or origin —
|
|
44
20
|
* schema.org needs both and the homepage then emits no node at all. */
|
|
45
21
|
export declare function fetchOrganizationSchema(name: string, base: string): Promise<OrganizationSchemaSource | undefined>;
|
|
46
|
-
//#endregion
|
|
47
22
|
export {};
|
package/dist/structured-data.js
CHANGED
|
@@ -1,61 +1,32 @@
|
|
|
1
1
|
import { memoTTL } from "./cache.js";
|
|
2
|
-
import { graphql, request } from "./graphql.js";
|
|
3
2
|
import { getContentVersion } from "./content-version.js";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
countryCode
|
|
9
|
-
branding {
|
|
10
|
-
logo {
|
|
11
|
-
url
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
refundPolicy {
|
|
15
|
-
url
|
|
16
|
-
}
|
|
17
|
-
returnPolicy {
|
|
18
|
-
configured
|
|
19
|
-
returnWindowDays
|
|
20
|
-
restockingFeePercentage
|
|
21
|
-
returnShippingCost
|
|
22
|
-
flatRateAmount {
|
|
23
|
-
amount
|
|
24
|
-
currencyCode
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
`);
|
|
30
|
-
const fetchShopRecord = memoTTL(async () => (await request(ShopStructuredDataQuery)).shop ?? null, {
|
|
3
|
+
import { toMoney } from "./money.js";
|
|
4
|
+
import { orFallback, storefront } from "./storefront.js";
|
|
5
|
+
// src/structured-data.ts
|
|
6
|
+
const fetchShopRecord = memoTTL(() => storefront().seo.structuredData({}), {
|
|
31
7
|
key: () => "",
|
|
32
8
|
version: getContentVersion
|
|
33
9
|
});
|
|
34
|
-
|
|
35
|
-
try {
|
|
36
|
-
return await fetchShopRecord();
|
|
37
|
-
} catch (err) {
|
|
38
|
-
console.warn("[structured-data] shop fetch failed:", err);
|
|
39
|
-
return null;
|
|
40
|
-
}
|
|
41
|
-
}
|
|
10
|
+
const safeShopRecord = () => orFallback(fetchShopRecord(), null, "structured-data");
|
|
42
11
|
const FEES = {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
12
|
+
free: "FreeReturn",
|
|
13
|
+
customerProvided: "ReturnFeesCustomerResponsibility",
|
|
14
|
+
flatRate: "ReturnShippingFees"
|
|
46
15
|
};
|
|
16
|
+
/** A rate's scale (contract-kit `RATE_SCALE`: 100 000 is 100 %) over a percent's. */
|
|
17
|
+
const RATE_PER_PERCENT = 1e3;
|
|
47
18
|
/**
|
|
48
19
|
* The shop's return policy as schema.org reads it, for one product or (with
|
|
49
20
|
* `finalSale` false) the shop as a whole. Undefined until the merchant saves a
|
|
50
|
-
* policy: the defaults an unconfigured shop
|
|
21
|
+
* policy: the defaults an unconfigured shop prefills ("unlimited window,
|
|
51
22
|
* free returns") are no promise the merchant made, and publishing them would
|
|
52
23
|
* tell Google and every agent exactly that. Also undefined without a country,
|
|
53
24
|
* which Google requires.
|
|
54
25
|
*/
|
|
55
26
|
function returnPolicySource(shop, { finalSale, base }) {
|
|
56
27
|
const policy = shop.returnPolicy;
|
|
57
|
-
if (!policy
|
|
58
|
-
const url = shop.
|
|
28
|
+
if (!policy || !shop.countryCode) return void 0;
|
|
29
|
+
const url = shop.refundPolicyUrl && base ? `${base}${shop.refundPolicyUrl}` : void 0;
|
|
59
30
|
const country = shop.countryCode;
|
|
60
31
|
if (finalSale) return {
|
|
61
32
|
country,
|
|
@@ -63,8 +34,8 @@ function returnPolicySource(shop, { finalSale, base }) {
|
|
|
63
34
|
...url ? { url } : {}
|
|
64
35
|
};
|
|
65
36
|
const days = policy.returnWindowDays;
|
|
66
|
-
const fee = policy.flatRateAmount;
|
|
67
|
-
const restocking = policy.
|
|
37
|
+
const fee = policy.returnShippingCost === "flatRate" && policy.flatRateAmount !== null ? toMoney(policy.flatRateAmount) : null;
|
|
38
|
+
const restocking = policy.restockingFeeRate;
|
|
68
39
|
return {
|
|
69
40
|
country,
|
|
70
41
|
...days ? {
|
|
@@ -72,11 +43,11 @@ function returnPolicySource(shop, { finalSale, base }) {
|
|
|
72
43
|
days
|
|
73
44
|
} : { category: "UnlimitedWindow" },
|
|
74
45
|
fees: FEES[policy.returnShippingCost],
|
|
75
|
-
...
|
|
76
|
-
amount:
|
|
46
|
+
...fee ? { shippingFee: {
|
|
47
|
+
amount: fee.amount,
|
|
77
48
|
currency: fee.currencyCode
|
|
78
49
|
} } : {},
|
|
79
|
-
...restocking ? { restockingFeePercent: restocking } : {},
|
|
50
|
+
...restocking ? { restockingFeePercent: restocking / RATE_PER_PERCENT } : {},
|
|
80
51
|
...url ? { url } : {}
|
|
81
52
|
};
|
|
82
53
|
}
|
|
@@ -94,7 +65,7 @@ async function fetchProductReturnPolicy(finalSale, base) {
|
|
|
94
65
|
async function fetchOrganizationSchema(name, base) {
|
|
95
66
|
if (!name || !base) return void 0;
|
|
96
67
|
const shop = await safeShopRecord();
|
|
97
|
-
const logo = shop?.
|
|
68
|
+
const logo = shop?.logo?.url;
|
|
98
69
|
const returnPolicy = shop ? returnPolicySource(shop, {
|
|
99
70
|
finalSale: false,
|
|
100
71
|
base
|
|
@@ -106,5 +77,4 @@ async function fetchOrganizationSchema(name, base) {
|
|
|
106
77
|
...returnPolicy ? { returnPolicy } : {}
|
|
107
78
|
};
|
|
108
79
|
}
|
|
109
|
-
//#endregion
|
|
110
80
|
export { fetchOrganizationSchema, fetchProductReturnPolicy, returnPolicySource };
|
package/dist/trace.js
CHANGED
package/dist/translations.d.ts
CHANGED
|
@@ -1,32 +1,28 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
}, {
|
|
10
|
-
container: {
|
|
11
|
-
handle?: string | null | undefined;
|
|
12
|
-
type: "FIELDS" | "JSON_TEMPLATE" | "LOCALE_CONTENT" | "SECTION_GROUP" | "SETTINGS_DATA";
|
|
13
|
-
};
|
|
14
|
-
installId: string;
|
|
15
|
-
locale: string;
|
|
16
|
-
}, void>;
|
|
17
|
-
export type ThemeContentRow = ResultOf<typeof ThemeContentQuery>["themeContent"][number];
|
|
18
|
-
export type ThemeContentContainer = VariablesOf<typeof ThemeContentQuery>["container"];
|
|
19
|
-
export declare const fetchThemeContent: (installId: string, locale: string, container: {
|
|
20
|
-
handle?: string | null | undefined;
|
|
21
|
-
type: "FIELDS" | "JSON_TEMPLATE" | "LOCALE_CONTENT" | "SECTION_GROUP" | "SETTINGS_DATA";
|
|
22
|
-
}) => Promise<readonly {
|
|
23
|
-
format: "HTML" | "PLAIN";
|
|
24
|
-
key: string;
|
|
25
|
-
value: string;
|
|
26
|
-
}[]>;
|
|
1
|
+
import { ThemeTranslationScope } from "./api-contract/src/theme-translations.js";
|
|
2
|
+
import "./api-contract/src/storefront/index.js";
|
|
3
|
+
import { StorefrontClient } from "./api-contract/src/storefront/client.js";
|
|
4
|
+
type ThemeContentGroup = Awaited<ReturnType<StorefrontClient["themeContent"]["get"]>>["scopes"][number];
|
|
5
|
+
/** One key of a scope (`themeContentRow`): a dot path into the document, its text and format. */
|
|
6
|
+
export type ThemeContentRow = ThemeContentGroup["items"][number];
|
|
7
|
+
/** Which of the install's texts: its strings, its settings, one template's or one section group's. */
|
|
8
|
+
export type ThemeContentScope = ThemeTranslationScope;
|
|
27
9
|
/**
|
|
28
|
-
*
|
|
29
|
-
*
|
|
10
|
+
* The merchant's rows for one scope of this storefront's install, in `locale`
|
|
11
|
+
* (undefined: the shop's primary language). None with no install, for a
|
|
12
|
+
* handle no document can have (one would fail the whole call it rides in),
|
|
13
|
+
* and on a failed read.
|
|
14
|
+
*/
|
|
15
|
+
export declare function fetchThemeContent(locale: string | undefined, scope: ThemeContentScope): Promise<readonly ThemeContentRow[]>;
|
|
16
|
+
/**
|
|
17
|
+
* The theme's strings for a page in `locale`: `base`, the theme's own
|
|
18
|
+
* catalogue for it, with the merchant's `strings` rows laid over it. Read in
|
|
19
|
+
* every language, the primary included: a primary-language row is the
|
|
20
|
+
* merchant rewording the theme ("Add to cart" → "Add to bag").
|
|
21
|
+
*/
|
|
22
|
+
export declare function themeStrings<T extends Record<string, unknown>>(base: T, locale: string | undefined): Promise<T>;
|
|
23
|
+
/**
|
|
24
|
+
* Deep-merge flat dot-path `strings` rows over a nested message catalog.
|
|
25
|
+
* Returns the base untouched when there is nothing to apply.
|
|
30
26
|
*
|
|
31
27
|
* Orphan-tolerant by design (theme upgrades): a row whose path runs through a
|
|
32
28
|
* string, or whose leaf is a subtree in the base, is skipped silently — the
|
|
@@ -35,8 +31,8 @@ export declare const fetchThemeContent: (installId: string, locale: string, cont
|
|
|
35
31
|
*/
|
|
36
32
|
export declare function applyCatalogOverrides<T extends Record<string, unknown>>(base: T, rows: readonly Pick<ThemeContentRow, "key" | "value">[]): T;
|
|
37
33
|
/**
|
|
38
|
-
* Strict in-place overlay of
|
|
39
|
-
*
|
|
34
|
+
* Strict in-place overlay of a template's, a section group's or the settings'
|
|
35
|
+
* rows onto that document.
|
|
40
36
|
*
|
|
41
37
|
* The caller must OWN `doc` — a document parsed fresh for this request. The
|
|
42
38
|
* overlay mutates in place, so applying it to a shared or cached object would
|
|
@@ -53,5 +49,4 @@ export declare function applyCatalogOverrides<T extends Record<string, unknown>>
|
|
|
53
49
|
* settings go inert instead of resurrecting paths.
|
|
54
50
|
*/
|
|
55
51
|
export declare function applyContentOverrides(doc: Record<string, unknown>, rows: readonly Pick<ThemeContentRow, "key" | "value">[]): void;
|
|
56
|
-
//#endregion
|
|
57
52
|
export {};
|
package/dist/translations.js
CHANGED
|
@@ -1,38 +1,66 @@
|
|
|
1
|
+
import { themeTranslationScope } from "./api-contract/src/theme-translations.js";
|
|
1
2
|
import { memoTTL } from "./cache.js";
|
|
2
|
-
import {
|
|
3
|
+
import { coalescedReads } from "./coalesce.js";
|
|
3
4
|
import { getContentVersion } from "./content-version.js";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
5
|
+
import { storefront } from "./storefront.js";
|
|
6
|
+
// src/translations.ts
|
|
7
|
+
/** A scope as one string: its type, and its handle when it has one. */
|
|
8
|
+
const scopeRef = (scope) => "handle" in scope ? `${scope.type}:${scope.handle}` : scope.type;
|
|
9
|
+
function scopeOf(ref) {
|
|
10
|
+
const at = ref.indexOf(":");
|
|
11
|
+
if (at < 0) return { type: ref };
|
|
12
|
+
return {
|
|
13
|
+
type: ref.slice(0, at),
|
|
14
|
+
handle: ref.slice(at + 1)
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
const coalescedScope = coalescedReads({
|
|
18
|
+
max: 20,
|
|
19
|
+
matches: (group, ref) => scopeRef(group.scope) === ref
|
|
20
|
+
});
|
|
21
|
+
const readScope = memoTTL(async (install, language, ref) => {
|
|
22
|
+
return (await coalescedScope(`${install}|${language}`, ref, async (refs) => {
|
|
23
|
+
return (await storefront(language || void 0).themeContent.get({
|
|
24
|
+
install,
|
|
25
|
+
scopes: refs.map(scopeOf)
|
|
26
|
+
})).scopes;
|
|
27
|
+
}))?.items ?? [];
|
|
24
28
|
}, {
|
|
25
|
-
key: (
|
|
29
|
+
key: (install, language, ref) => `${install}|${language}|${ref}`,
|
|
26
30
|
version: getContentVersion
|
|
27
31
|
});
|
|
32
|
+
/**
|
|
33
|
+
* The merchant's rows for one scope of this storefront's install, in `locale`
|
|
34
|
+
* (undefined: the shop's primary language). None with no install, for a
|
|
35
|
+
* handle no document can have (one would fail the whole call it rides in),
|
|
36
|
+
* and on a failed read.
|
|
37
|
+
*/
|
|
38
|
+
async function fetchThemeContent(locale, scope) {
|
|
39
|
+
const install = process.env.INSTALL_ID || "";
|
|
40
|
+
if (!install || !themeTranslationScope.safeParse(scope).success) return [];
|
|
41
|
+
try {
|
|
42
|
+
return await readScope(install, locale ?? "", scopeRef(scope));
|
|
43
|
+
} catch {
|
|
44
|
+
return [];
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* The theme's strings for a page in `locale`: `base`, the theme's own
|
|
49
|
+
* catalogue for it, with the merchant's `strings` rows laid over it. Read in
|
|
50
|
+
* every language, the primary included: a primary-language row is the
|
|
51
|
+
* merchant rewording the theme ("Add to cart" → "Add to bag").
|
|
52
|
+
*/
|
|
53
|
+
async function themeStrings(base, locale) {
|
|
54
|
+
return applyCatalogOverrides(base, await fetchThemeContent(locale, { type: "strings" }));
|
|
55
|
+
}
|
|
28
56
|
const UNSAFE_SEGMENTS = /* @__PURE__ */ new Set([
|
|
29
57
|
"__proto__",
|
|
30
58
|
"constructor",
|
|
31
59
|
"prototype"
|
|
32
60
|
]);
|
|
33
61
|
/**
|
|
34
|
-
* Deep-merge flat dot-path `
|
|
35
|
-
*
|
|
62
|
+
* Deep-merge flat dot-path `strings` rows over a nested message catalog.
|
|
63
|
+
* Returns the base untouched when there is nothing to apply.
|
|
36
64
|
*
|
|
37
65
|
* Orphan-tolerant by design (theme upgrades): a row whose path runs through a
|
|
38
66
|
* string, or whose leaf is a subtree in the base, is skipped silently — the
|
|
@@ -66,8 +94,8 @@ function applyCatalogOverrides(base, rows) {
|
|
|
66
94
|
return out;
|
|
67
95
|
}
|
|
68
96
|
/**
|
|
69
|
-
* Strict in-place overlay of
|
|
70
|
-
*
|
|
97
|
+
* Strict in-place overlay of a template's, a section group's or the settings'
|
|
98
|
+
* rows onto that document.
|
|
71
99
|
*
|
|
72
100
|
* The caller must OWN `doc` — a document parsed fresh for this request. The
|
|
73
101
|
* overlay mutates in place, so applying it to a shared or cached object would
|
|
@@ -101,5 +129,4 @@ function applyContentOverrides(doc, rows) {
|
|
|
101
129
|
if (ok && typeof cur[leaf] === "string") cur[leaf] = row.value;
|
|
102
130
|
}
|
|
103
131
|
}
|
|
104
|
-
|
|
105
|
-
export { applyCatalogOverrides, applyContentOverrides, fetchThemeContent };
|
|
132
|
+
export { applyCatalogOverrides, applyContentOverrides, fetchThemeContent, themeStrings };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shop-on/theme-server",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.11.0",
|
|
4
4
|
"license": "SEE LICENSE IN dist/LICENSE.md",
|
|
5
5
|
"files": [
|
|
6
6
|
"dist"
|
|
@@ -29,25 +29,26 @@
|
|
|
29
29
|
"./customer-session": "./dist/customer-session.js",
|
|
30
30
|
"./files": "./dist/files.js",
|
|
31
31
|
"./fonts": "./dist/fonts.js",
|
|
32
|
-
"./graphql": "./dist/graphql.js",
|
|
33
32
|
"./html": "./dist/html.js",
|
|
34
33
|
"./lease": "./dist/lease.js",
|
|
35
34
|
"./liquid": "./dist/liquid.js",
|
|
36
35
|
"./marketing-cookies": "./dist/marketing-cookies.js",
|
|
37
36
|
"./menu": "./dist/menu.js",
|
|
38
37
|
"./metaobject": "./dist/metaobject.js",
|
|
38
|
+
"./money": "./dist/money.js",
|
|
39
39
|
"./newsletter": "./dist/newsletter.js",
|
|
40
40
|
"./page": "./dist/page.js",
|
|
41
41
|
"./password": "./dist/password.js",
|
|
42
42
|
"./password-session": "./dist/password-session.js",
|
|
43
43
|
"./policy": "./dist/policy.js",
|
|
44
44
|
"./product": "./dist/product.js",
|
|
45
|
+
"./read-ahead": "./dist/read-ahead.js",
|
|
45
46
|
"./recommendations": "./dist/recommendations.js",
|
|
46
47
|
"./request-scope": "./dist/request-scope.js",
|
|
47
48
|
"./review": "./dist/review.js",
|
|
48
49
|
"./search": "./dist/search.js",
|
|
49
50
|
"./seo-context": "./dist/seo-context.js",
|
|
50
|
-
"./seo-
|
|
51
|
+
"./seo-documents": "./dist/seo-documents.js",
|
|
51
52
|
"./shop": "./dist/shop.js",
|
|
52
53
|
"./shop-seo": "./dist/shop-seo.js",
|
|
53
54
|
"./store-availability": "./dist/store-availability.js",
|
|
@@ -56,22 +57,23 @@
|
|
|
56
57
|
"./package.json": "./package.json"
|
|
57
58
|
},
|
|
58
59
|
"dependencies": {
|
|
59
|
-
"
|
|
60
|
-
"
|
|
61
|
-
"
|
|
62
|
-
"
|
|
63
|
-
"@shop-on/
|
|
64
|
-
"
|
|
65
|
-
"
|
|
60
|
+
"@orpc/client": "1.16.2",
|
|
61
|
+
"@orpc/contract": "1.16.2",
|
|
62
|
+
"@shop-on/customer-runtime": "0.11.0",
|
|
63
|
+
"@shop-on/theme-contract": "0.11.0",
|
|
64
|
+
"@shop-on/theme-runtime": "0.11.0",
|
|
65
|
+
"liquidjs": "10.30.1",
|
|
66
|
+
"zod": "4.6.7"
|
|
66
67
|
},
|
|
67
68
|
"devDependencies": {
|
|
68
|
-
"@
|
|
69
|
-
"@
|
|
69
|
+
"@shop-on/api-contract": "0.0.0",
|
|
70
|
+
"@tanstack/react-start": "1.169.5",
|
|
71
|
+
"@types/node": "26.6.6",
|
|
70
72
|
"typescript": "7.0.2",
|
|
71
|
-
"vitest": "5.0
|
|
73
|
+
"vitest": "5.1.0"
|
|
72
74
|
},
|
|
73
75
|
"peerDependencies": {
|
|
74
|
-
"@tanstack/react-start": "~1.169.
|
|
76
|
+
"@tanstack/react-start": "~1.169.5"
|
|
75
77
|
},
|
|
76
78
|
"scripts": {
|
|
77
79
|
"check-types": "tsc --noEmit",
|