@orunelle/theme-contract 0.0.0-stage → 2.0.0-alpha.14

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.
@@ -0,0 +1,168 @@
1
+ import { z } from "zod";
2
+ import { validateConfiguration } from "./merchant-validation.js";
3
+ export const PREVIEW_PROTOCOL = 1;
4
+ export const MAX_PREVIEW_MESSAGE_BYTES = 262144;
5
+ const id = z.string().min(1).max(100);
6
+ const uuid = z.string().uuid();
7
+ const digest = z.string().regex(/^[a-f0-9]{64}$/);
8
+ export const previewIdentitySchema = z
9
+ .object({
10
+ protocol: z.literal(1),
11
+ sessionId: uuid,
12
+ nonce: z.string().regex(/^[A-Za-z0-9_-]{43}$/),
13
+ storeId: uuid,
14
+ installationId: uuid,
15
+ releaseId: uuid,
16
+ artifactDigest: digest,
17
+ expiresAt: z.number().int().safe().positive(),
18
+ editorOrigin: z.string().url(),
19
+ })
20
+ .strict();
21
+ const viewSchema = z
22
+ .object({
23
+ template: id,
24
+ locale: z.enum(["en", "ar"]),
25
+ resourceId: uuid.optional(),
26
+ path: z
27
+ .string()
28
+ .max(500)
29
+ .regex(/^\/(?!\/)/),
30
+ })
31
+ .strict();
32
+ const channel = {
33
+ sessionId: uuid,
34
+ documentId: uuid,
35
+ generation: z.number().int().min(1).max(100000),
36
+ sequence: z.number().int().min(0).max(1000000),
37
+ };
38
+ export const previewConnectSchema = z
39
+ .object({
40
+ type: z.literal("orunelle.preview.connect"),
41
+ identity: previewIdentitySchema,
42
+ documentId: uuid,
43
+ generation: channel.generation,
44
+ })
45
+ .strict();
46
+ export const previewBootSchema = z
47
+ .object({
48
+ type: z.literal("orunelle.preview.boot"),
49
+ identity: previewIdentitySchema,
50
+ })
51
+ .strict();
52
+ const rect = z
53
+ .object({
54
+ x: z.number().finite().min(-1000000).max(1000000),
55
+ y: z.number().finite().min(-1000000).max(1000000),
56
+ width: z.number().finite().min(0).max(100000),
57
+ height: z.number().finite().min(0).max(100000),
58
+ })
59
+ .strict();
60
+ export const previewFrameMessageSchema = z.discriminatedUnion("type", [
61
+ z
62
+ .object({
63
+ ...channel,
64
+ type: z.literal("ready"),
65
+ capabilities: z
66
+ .array(z.enum(["inspect", "geometry", "snapshots", "drag", "navigate"]))
67
+ .max(5),
68
+ })
69
+ .strict(),
70
+ z.object({ ...channel, type: z.literal("ack") }).strict(),
71
+ z
72
+ .object({
73
+ ...channel,
74
+ type: z.literal("hover"),
75
+ nodeId: id.nullable(),
76
+ rect: rect.nullable(),
77
+ })
78
+ .strict(),
79
+ z
80
+ .object({ ...channel, type: z.literal("select"), nodeId: id.nullable() })
81
+ .strict(),
82
+ z
83
+ .object({
84
+ ...channel,
85
+ type: z.literal("geometry"),
86
+ nodes: z.array(z.object({ nodeId: id, rect }).strict()).max(500),
87
+ })
88
+ .strict(),
89
+ z
90
+ .object({
91
+ ...channel,
92
+ type: z.literal("navigate"),
93
+ path: viewSchema.shape.path,
94
+ })
95
+ .strict(),
96
+ z
97
+ .object({
98
+ ...channel,
99
+ type: z.literal("error"),
100
+ code: z.enum([
101
+ "invalid-snapshot",
102
+ "render-error",
103
+ "expired",
104
+ "unsupported-navigation",
105
+ "unsupported-template",
106
+ ]),
107
+ })
108
+ .strict(),
109
+ ]);
110
+ const configuration = z.custom();
111
+ export const previewHostMessageSchema = z.discriminatedUnion("type", [
112
+ z
113
+ .object({
114
+ ...channel,
115
+ type: z.literal("snapshot"),
116
+ configuration,
117
+ view: viewSchema,
118
+ inspect: z.boolean(),
119
+ })
120
+ .strict(),
121
+ z
122
+ .object({
123
+ ...channel,
124
+ type: z.literal("selection"),
125
+ nodeId: id.nullable(),
126
+ scroll: z.boolean(),
127
+ })
128
+ .strict(),
129
+ z
130
+ .object({
131
+ ...channel,
132
+ type: z.literal("drop-target"),
133
+ nodeId: id.nullable(),
134
+ valid: z.boolean(),
135
+ })
136
+ .strict(),
137
+ ]);
138
+ export function samePreviewIdentity(a, b) {
139
+ return Object.keys(previewIdentitySchema.shape).every((key) => a[key] === b[key]);
140
+ }
141
+ export function boundedPreviewMessage(value) {
142
+ try {
143
+ return (new TextEncoder().encode(JSON.stringify(value)).length <=
144
+ MAX_PREVIEW_MESSAGE_BYTES);
145
+ }
146
+ catch {
147
+ return false;
148
+ }
149
+ }
150
+ export function validPreviewSnapshot(manifest, value, installationId) {
151
+ return validateConfiguration(manifest, value, installationId) === undefined;
152
+ }
153
+ /** Matches Go's canonical JSON encoder for the finite JSON values in manifests. */
154
+ export function canonicalPreviewJson(value) {
155
+ const sort = (input) => Array.isArray(input)
156
+ ? input.map(sort)
157
+ : input && typeof input === "object"
158
+ ? Object.fromEntries(Object.keys(input)
159
+ .sort()
160
+ .map((key) => [
161
+ key,
162
+ sort(input[key]),
163
+ ]))
164
+ : input;
165
+ return JSON.stringify(sort(value))
166
+ .replace(/\u2028/g, "\\u2028")
167
+ .replace(/\u2029/g, "\\u2029");
168
+ }
@@ -0,0 +1,116 @@
1
+ import type { LocalizedText } from "./index.js";
2
+ import type { ConfigurationTemplate, MerchantManifest, StoreTextDefinition } from "./merchant-types.js";
3
+ export type StoreTextGroup = "header" | "home" | "product" | "cart" | "footer" | "general";
4
+ /**
5
+ * Storefront texts every theme can show, with platform defaults. Merchants
6
+ * override them per language in the theme editor; themes may declare more in
7
+ * `editor.strings`, which also replaces these defaults.
8
+ */
9
+ export declare const storeTextDefaults: {
10
+ readonly "header.skip": StoreTextDefinition & {
11
+ group: StoreTextGroup;
12
+ };
13
+ readonly "nav.products": StoreTextDefinition & {
14
+ group: StoreTextGroup;
15
+ };
16
+ readonly "nav.categories": StoreTextDefinition & {
17
+ group: StoreTextGroup;
18
+ };
19
+ readonly "nav.collections": StoreTextDefinition & {
20
+ group: StoreTextGroup;
21
+ };
22
+ readonly "nav.search": StoreTextDefinition & {
23
+ group: StoreTextGroup;
24
+ };
25
+ readonly "nav.cart": StoreTextDefinition & {
26
+ group: StoreTextGroup;
27
+ };
28
+ readonly "nav.account": StoreTextDefinition & {
29
+ group: StoreTextGroup;
30
+ };
31
+ readonly "home.title": StoreTextDefinition & {
32
+ group: StoreTextGroup;
33
+ };
34
+ readonly "home.intro": StoreTextDefinition & {
35
+ group: StoreTextGroup;
36
+ };
37
+ readonly "home.empty": StoreTextDefinition & {
38
+ group: StoreTextGroup;
39
+ };
40
+ readonly "product.back": StoreTextDefinition & {
41
+ group: StoreTextGroup;
42
+ };
43
+ readonly "product.details": StoreTextDefinition & {
44
+ group: StoreTextGroup;
45
+ };
46
+ readonly "product.choose_option": StoreTextDefinition & {
47
+ group: StoreTextGroup;
48
+ };
49
+ readonly "product.add_to_cart": StoreTextDefinition & {
50
+ group: StoreTextGroup;
51
+ };
52
+ readonly "product.added": StoreTextDefinition & {
53
+ group: StoreTextGroup;
54
+ };
55
+ readonly "product.out_of_stock": StoreTextDefinition & {
56
+ group: StoreTextGroup;
57
+ };
58
+ readonly "product.wishlist": StoreTextDefinition & {
59
+ group: StoreTextGroup;
60
+ };
61
+ readonly "product.buy_now": StoreTextDefinition & {
62
+ group: StoreTextGroup;
63
+ };
64
+ readonly "product.quantity": StoreTextDefinition & {
65
+ group: StoreTextGroup;
66
+ };
67
+ readonly "product.only_left": StoreTextDefinition & {
68
+ group: StoreTextGroup;
69
+ };
70
+ readonly "product.sale": StoreTextDefinition & {
71
+ group: StoreTextGroup;
72
+ };
73
+ readonly "product.description": StoreTextDefinition & {
74
+ group: StoreTextGroup;
75
+ };
76
+ readonly "product.specs": StoreTextDefinition & {
77
+ group: StoreTextGroup;
78
+ };
79
+ readonly "product.related": StoreTextDefinition & {
80
+ group: StoreTextGroup;
81
+ };
82
+ readonly "cart.title": StoreTextDefinition & {
83
+ group: StoreTextGroup;
84
+ };
85
+ readonly "cart.empty": StoreTextDefinition & {
86
+ group: StoreTextGroup;
87
+ };
88
+ readonly "cart.continue": StoreTextDefinition & {
89
+ group: StoreTextGroup;
90
+ };
91
+ readonly "cart.checkout": StoreTextDefinition & {
92
+ group: StoreTextGroup;
93
+ };
94
+ readonly "footer.powered_by": StoreTextDefinition & {
95
+ group: StoreTextGroup;
96
+ };
97
+ readonly "general.view_all": StoreTextDefinition & {
98
+ group: StoreTextGroup;
99
+ };
100
+ readonly "general.not_found": StoreTextDefinition & {
101
+ group: StoreTextGroup;
102
+ };
103
+ };
104
+ export type StoreTextKey = keyof typeof storeTextDefaults;
105
+ export declare const MAX_STORE_TEXT_LENGTH = 300;
106
+ /** Every text key the editor offers for a manifest, with its effective default. */
107
+ export declare function storeTextCatalog(manifest: MerchantManifest): Record<string, StoreTextDefinition & {
108
+ group?: StoreTextGroup;
109
+ }>;
110
+ /**
111
+ * Resolves one text: a non-empty merchant override, then the theme's default,
112
+ * then the platform default. Unknown keys return the key itself.
113
+ */
114
+ export declare function resolveStoreText(manifest: MerchantManifest, configuration: Pick<ConfigurationTemplate, "strings"> | undefined, key: string, locale: "en" | "ar"): string;
115
+ export type { LocalizedText };
116
+ //# sourceMappingURL=store-text.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"store-text.d.ts","sourceRoot":"","sources":["../src/store-text.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAChD,OAAO,KAAK,EACV,qBAAqB,EACrB,gBAAgB,EAChB,mBAAmB,EACpB,MAAM,qBAAqB,CAAC;AAE7B,MAAM,MAAM,cAAc,GACtB,QAAQ,GACR,MAAM,GACN,SAAS,GACT,MAAM,GACN,QAAQ,GACR,SAAS,CAAC;AAYd;;;;GAIG;AACH,eAAO,MAAM,iBAAiB;;eAXI,cAAc;;;eAAd,cAAc;;;eAAd,cAAc;;;eAAd,cAAc;;;eAAd,cAAc;;;eAAd,cAAc;;;eAAd,cAAc;;;eAAd,cAAc;;;eAAd,cAAc;;;eAAd,cAAc;;;eAAd,cAAc;;;eAAd,cAAc;;;eAAd,cAAc;;;eAAd,cAAc;;;eAAd,cAAc;;;eAAd,cAAc;;;eAAd,cAAc;;;eAAd,cAAc;;;eAAd,cAAc;;;eAAd,cAAc;;;eAAd,cAAc;;;eAAd,cAAc;;;eAAd,cAAc;;;eAAd,cAAc;;;eAAd,cAAc;;;eAAd,cAAc;;;eAAd,cAAc;;;eAAd,cAAc;;;eAAd,cAAc;;;eAAd,cAAc;;;eAAd,cAAc;;CAyKtC,CAAC;AACX,MAAM,MAAM,YAAY,GAAG,MAAM,OAAO,iBAAiB,CAAC;AAE1D,eAAO,MAAM,qBAAqB,MAAM,CAAC;AAEzC,mFAAmF;AACnF,wBAAgB,gBAAgB,CAC9B,QAAQ,EAAE,gBAAgB,GACzB,MAAM,CAAC,MAAM,EAAE,mBAAmB,GAAG;IAAE,KAAK,CAAC,EAAE,cAAc,CAAA;CAAE,CAAC,CAElE;AAED;;;GAGG;AACH,wBAAgB,gBAAgB,CAC9B,QAAQ,EAAE,gBAAgB,EAC1B,aAAa,EAAE,IAAI,CAAC,qBAAqB,EAAE,SAAS,CAAC,GAAG,SAAS,EACjE,GAAG,EAAE,MAAM,EACX,MAAM,EAAE,IAAI,GAAG,IAAI,GAClB,MAAM,CAUR;AACD,YAAY,EAAE,aAAa,EAAE,CAAC"}
@@ -0,0 +1,68 @@
1
+ const entry = (group, label, value) => ({
2
+ group,
3
+ label: { en: label[0], ar: label[1] },
4
+ default: { en: value[0], ar: value[1] },
5
+ });
6
+ /**
7
+ * Storefront texts every theme can show, with platform defaults. Merchants
8
+ * override them per language in the theme editor; themes may declare more in
9
+ * `editor.strings`, which also replaces these defaults.
10
+ */
11
+ export const storeTextDefaults = {
12
+ "header.skip": entry("header", ["Skip link", "رابط التخطي"], ["Skip to content", "انتقل إلى المحتوى"]),
13
+ "nav.products": entry("header", ["Products link", "رابط المنتجات"], ["Products", "المنتجات"]),
14
+ "nav.categories": entry("header", ["Categories link", "رابط الفئات"], ["Categories", "الفئات"]),
15
+ "nav.collections": entry("header", ["Collections link", "رابط المجموعات"], ["Collections", "المجموعات"]),
16
+ "nav.search": entry("header", ["Search link", "رابط البحث"], ["Search", "البحث"]),
17
+ "nav.cart": entry("header", ["Cart link", "رابط السلة"], ["Cart", "السلة"]),
18
+ "nav.account": entry("header", ["Account link", "رابط الحساب"], ["Account", "حسابك"]),
19
+ "home.title": entry("home", ["Home heading", "عنوان الرئيسية"], ["Discover our products", "اكتشف منتجاتنا"]),
20
+ "home.intro": entry("home", ["Home introduction", "مقدمة الرئيسية"], [
21
+ "Browse the collection and find what suits you.",
22
+ "تصفح المنتجات واختر ما يناسبك.",
23
+ ]),
24
+ "home.empty": entry("home", ["No products message", "رسالة عدم وجود منتجات"], ["There are no products to display.", "لا توجد منتجات لعرضها."]),
25
+ "product.back": entry("product", ["Back link", "رابط العودة"], ["Back to products", "العودة إلى المنتجات"]),
26
+ "product.details": entry("product", ["Details button", "زر التفاصيل"], ["Product details", "تفاصيل المنتج"]),
27
+ "product.choose_option": entry("product", ["Option prompt", "طلب اختيار الخيار"], ["Choose an option", "اختر خيارًا"]),
28
+ "product.add_to_cart": entry("product", ["Add to cart button", "زر الإضافة إلى السلة"], ["Add to cart", "أضف إلى السلة"]),
29
+ "product.added": entry("product", ["Added to cart message", "رسالة الإضافة إلى السلة"], ["Added to your cart.", "تمت الإضافة إلى السلة."]),
30
+ "product.out_of_stock": entry("product", ["Out of stock label", "عبارة نفاد المخزون"], ["Out of stock", "نفد المخزون"]),
31
+ "product.wishlist": entry("product", ["Wishlist button", "زر المفضلة"], ["Save to wishlist", "احفظ في المفضلة"]),
32
+ "product.buy_now": entry("product", ["Buy now button", "زر الشراء الآن"], ["Buy now", "اشترِ الآن"]),
33
+ "product.quantity": entry("product", ["Quantity label", "عنوان الكمية"], ["Quantity", "الكمية"]),
34
+ "product.only_left": entry("product", [
35
+ "Low stock message ({count} = items left)",
36
+ "رسالة المخزون المنخفض ({count} = المتبقي)",
37
+ ], ["Only {count} left", "متبقٍ {count} فقط"]),
38
+ "product.sale": entry("product", ["Sale badge", "شارة التخفيض"], ["Sale", "تخفيض"]),
39
+ "product.description": entry("product", ["Description heading", "عنوان الوصف"], ["Description", "الوصف"]),
40
+ "product.specs": entry("product", ["Specifications heading", "عنوان المواصفات"], ["Specifications", "المواصفات"]),
41
+ "product.related": entry("product", ["Related products heading", "عنوان المنتجات المشابهة"], ["You may also like", "قد يعجبك أيضًا"]),
42
+ "cart.title": entry("cart", ["Cart heading", "عنوان السلة"], ["Your cart", "سلة التسوق"]),
43
+ "cart.empty": entry("cart", ["Empty cart message", "رسالة السلة الفارغة"], ["Your cart is empty.", "السلة فارغة."]),
44
+ "cart.continue": entry("cart", ["Continue shopping link", "رابط متابعة التسوق"], ["Continue shopping", "متابعة التسوق"]),
45
+ "cart.checkout": entry("cart", ["Checkout button", "زر إتمام الطلب"], ["Checkout", "إتمام الطلب"]),
46
+ "footer.powered_by": entry("footer", ["Platform credit", "إشارة المنصة"], ["Powered by Orunelle", "مدعوم من أورونيل"]),
47
+ "general.view_all": entry("general", ["View all link", "رابط عرض الكل"], ["View all", "عرض الكل"]),
48
+ "general.not_found": entry("general", ["Page not found heading", "عنوان الصفحة غير الموجودة"], ["Page not found", "الصفحة غير موجودة"]),
49
+ };
50
+ export const MAX_STORE_TEXT_LENGTH = 300;
51
+ /** Every text key the editor offers for a manifest, with its effective default. */
52
+ export function storeTextCatalog(manifest) {
53
+ return { ...storeTextDefaults, ...manifest.editor?.strings };
54
+ }
55
+ /**
56
+ * Resolves one text: a non-empty merchant override, then the theme's default,
57
+ * then the platform default. Unknown keys return the key itself.
58
+ */
59
+ export function resolveStoreText(manifest, configuration, key, locale) {
60
+ // Own-property lookups only: keys such as "constructor" must not reach the prototype.
61
+ const own = (record) => record && Object.hasOwn(record, key) ? record[key] : undefined;
62
+ const override = own(configuration?.strings)?.[locale];
63
+ if (override)
64
+ return override;
65
+ const declared = own(manifest.editor?.strings) ??
66
+ own(storeTextDefaults);
67
+ return declared?.default[locale] || key;
68
+ }