@relyon/kit 0.0.0-stage → 1.0.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/AGENTS.md +75 -0
- package/CHANGELOG.md +1053 -0
- package/LICENSE +21 -0
- package/QUICKSTART.md +239 -0
- package/README.md +117 -2
- package/dist/adapters/express.d.ts +39 -0
- package/dist/adapters/express.js +106 -0
- package/dist/adapters/hono.d.ts +16 -0
- package/dist/adapters/hono.js +15 -0
- package/dist/adapters/next.d.ts +29 -0
- package/dist/adapters/next.js +38 -0
- package/dist/agent-card.d.ts +14 -0
- package/dist/agent-card.js +58 -0
- package/dist/agent-docs.d.ts +36 -0
- package/dist/agent-docs.js +78 -0
- package/dist/agent-hooks.d.ts +66 -0
- package/dist/agent-hooks.js +280 -0
- package/dist/agents.d.ts +24 -0
- package/dist/agents.js +120 -0
- package/dist/api-key.d.ts +6 -0
- package/dist/api-key.js +33 -0
- package/dist/cache.d.ts +46 -0
- package/dist/cache.js +86 -0
- package/dist/cart-tokens.d.ts +31 -0
- package/dist/cart-tokens.js +58 -0
- package/dist/cart-transfer.d.ts +95 -0
- package/dist/cart-transfer.js +387 -0
- package/dist/catalog-enrichment.d.ts +6 -0
- package/dist/catalog-enrichment.js +71 -0
- package/dist/catalog-reads.d.ts +126 -0
- package/dist/catalog-reads.js +104 -0
- package/dist/catalog.d.ts +78 -0
- package/dist/catalog.js +288 -0
- package/dist/charge-claims.d.ts +11 -0
- package/dist/charge-claims.js +47 -0
- package/dist/check.d.ts +44 -0
- package/dist/check.js +152 -0
- package/dist/checklist-manifest.d.ts +72 -0
- package/dist/checklist-manifest.js +341 -0
- package/dist/checkout.d.ts +222 -0
- package/dist/checkout.js +353 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +727 -0
- package/dist/confirmation.d.ts +78 -0
- package/dist/confirmation.js +124 -0
- package/dist/context.d.ts +81 -0
- package/dist/context.js +10 -0
- package/dist/discovery.d.ts +151 -0
- package/dist/discovery.js +346 -0
- package/dist/doctor-cart-transfer.d.ts +33 -0
- package/dist/doctor-cart-transfer.js +186 -0
- package/dist/doctor-catalog.d.ts +8 -0
- package/dist/doctor-catalog.js +47 -0
- package/dist/doctor-evidence.d.ts +22 -0
- package/dist/doctor-evidence.js +57 -0
- package/dist/doctor-merchant.d.ts +48 -0
- package/dist/doctor-merchant.js +74 -0
- package/dist/doctor-source.d.ts +5 -0
- package/dist/doctor-source.js +142 -0
- package/dist/doctor-variants.d.ts +70 -0
- package/dist/doctor-variants.js +102 -0
- package/dist/doctor.d.ts +215 -0
- package/dist/doctor.js +1689 -0
- package/dist/events.d.ts +95 -0
- package/dist/events.js +200 -0
- package/dist/feed-mapping.d.ts +114 -0
- package/dist/feed-mapping.js +152 -0
- package/dist/feed-values.d.ts +13 -0
- package/dist/feed-values.js +40 -0
- package/dist/feeds.d.ts +104 -0
- package/dist/feeds.js +366 -0
- package/dist/identify.d.ts +102 -0
- package/dist/identify.js +124 -0
- package/dist/identity.d.ts +37 -0
- package/dist/identity.js +54 -0
- package/dist/index.d.ts +42 -0
- package/dist/index.js +41 -0
- package/dist/init.d.ts +157 -0
- package/dist/init.js +912 -0
- package/dist/kit-config.d.ts +19 -0
- package/dist/kit-config.js +43 -0
- package/dist/kit-environment.d.ts +20 -0
- package/dist/kit-environment.js +32 -0
- package/dist/kit-updates.d.ts +47 -0
- package/dist/kit-updates.js +77 -0
- package/dist/kit-version-project.d.ts +18 -0
- package/dist/kit-version-project.js +214 -0
- package/dist/kit-version.d.ts +37 -0
- package/dist/kit-version.js +162 -0
- package/dist/kit.d.ts +516 -0
- package/dist/kit.js +1309 -0
- package/dist/layers/pages.d.ts +3 -0
- package/dist/layers/pages.js +5 -0
- package/dist/layers/payments.d.ts +7 -0
- package/dist/layers/payments.js +9 -0
- package/dist/layers/products.d.ts +10 -0
- package/dist/layers/products.js +13 -0
- package/dist/live.d.ts +13 -0
- package/dist/live.js +52 -0
- package/dist/mcp.d.ts +270 -0
- package/dist/mcp.js +312 -0
- package/dist/next-steps.d.ts +101 -0
- package/dist/next-steps.js +257 -0
- package/dist/order-pending.d.ts +57 -0
- package/dist/order-pending.js +127 -0
- package/dist/orders.d.ts +12 -0
- package/dist/orders.js +21 -0
- package/dist/payments-init.d.ts +19 -0
- package/dist/payments-init.js +70 -0
- package/dist/payments-routes.d.ts +54 -0
- package/dist/payments-routes.js +123 -0
- package/dist/payments.d.ts +165 -0
- package/dist/payments.js +192 -0
- package/dist/pnpm-workspace.d.ts +58 -0
- package/dist/pnpm-workspace.js +250 -0
- package/dist/policies.d.ts +111 -0
- package/dist/policies.js +317 -0
- package/dist/preview-hosts.d.ts +64 -0
- package/dist/preview-hosts.js +175 -0
- package/dist/private-config.d.ts +169 -0
- package/dist/private-config.js +820 -0
- package/dist/progress.d.ts +54 -0
- package/dist/progress.js +153 -0
- package/dist/protocol-inputs.d.ts +22 -0
- package/dist/protocol-inputs.js +151 -0
- package/dist/public-origin.d.ts +50 -0
- package/dist/public-origin.js +180 -0
- package/dist/refusals.d.ts +86 -0
- package/dist/refusals.js +157 -0
- package/dist/relay.d.ts +107 -0
- package/dist/relay.js +655 -0
- package/dist/release-notes.d.ts +2 -0
- package/dist/release-notes.js +290 -0
- package/dist/relyon-cart-tokens.d.ts +101 -0
- package/dist/relyon-cart-tokens.js +521 -0
- package/dist/relyon.d.ts +141 -0
- package/dist/relyon.js +238 -0
- package/dist/robots.d.ts +27 -0
- package/dist/robots.js +127 -0
- package/dist/rules.d.ts +144 -0
- package/dist/rules.js +347 -0
- package/dist/sdk/generated/public-api.d.ts +4816 -0
- package/dist/sdk/generated/public-api.js +1 -0
- package/dist/sdk/index.d.ts +24 -0
- package/dist/sdk/index.js +34 -0
- package/dist/sdk/signatures.d.ts +17 -0
- package/dist/sdk/signatures.js +33 -0
- package/dist/setup.d.ts +37 -0
- package/dist/setup.js +94 -0
- package/dist/signatures.d.ts +112 -0
- package/dist/signatures.js +443 -0
- package/dist/state.d.ts +185 -0
- package/dist/state.js +96 -0
- package/dist/store-payments.d.ts +48 -0
- package/dist/store-payments.js +143 -0
- package/dist/structured-fields.d.ts +26 -0
- package/dist/structured-fields.js +200 -0
- package/dist/templates.d.ts +19 -0
- package/dist/templates.js +193 -0
- package/dist/test-checkout-command.d.ts +32 -0
- package/dist/test-checkout-command.js +161 -0
- package/dist/test-checkout.d.ts +88 -0
- package/dist/test-checkout.js +292 -0
- package/dist/types.d.ts +103 -0
- package/dist/types.js +18 -0
- package/dist/ucp-versions.d.ts +57 -0
- package/dist/ucp-versions.js +129 -0
- package/dist/util.d.ts +23 -0
- package/dist/util.js +68 -0
- package/dist/variants.d.ts +67 -0
- package/dist/variants.js +146 -0
- package/dist/version.d.ts +9 -0
- package/dist/version.js +10 -0
- package/dist/waiting.d.ts +43 -0
- package/dist/waiting.js +57 -0
- package/dist/webhook-command.d.ts +48 -0
- package/dist/webhook-command.js +367 -0
- package/dist/webhook-registration.d.ts +47 -0
- package/dist/webhook-registration.js +69 -0
- package/dist/webmcp-schema.d.ts +14 -0
- package/dist/webmcp-schema.js +140 -0
- package/dist/webmcp-script.d.ts +3 -0
- package/dist/webmcp-script.js +176 -0
- package/dist/webmcp-store.d.ts +102 -0
- package/dist/webmcp-store.js +116 -0
- package/dist/webmcp.d.ts +242 -0
- package/dist/webmcp.js +866 -0
- package/dist/wire-names.d.ts +85 -0
- package/dist/wire-names.js +109 -0
- package/package.json +118 -4
- package/release-notes.json +290 -0
- package/scripts/install-notice.cjs +7 -0
- package/skills/relyon-agents/SKILL.md +327 -0
- package/skills/relyon-catalog/SKILL.md +499 -0
- package/skills/relyon-catalog/references/example-store.md +218 -0
- package/skills/relyon-checkout/SKILL.md +603 -0
- package/skills/relyon-checkout/references/checkout-flow.md +257 -0
- package/skills/relyon-checkout/references/checkout-page.md +315 -0
- package/skills/relyon-go-live/SKILL.md +344 -0
- package/skills/relyon-orders/SKILL.md +191 -0
- package/skills/relyon-orders/references/webhook-receiver.md +113 -0
- package/skills/relyon-payments/SKILL.md +117 -0
- package/skills/relyon-setup/SKILL.md +1061 -0
- package/skills/relyon-setup/references/QUICKSTART.md +239 -0
- package/skills/relyon-setup/references/durable-state.md +209 -0
- package/skills/relyon-setup/references/express-server.md +388 -0
- package/skills/relyon-setup/references/next-app-router.md +251 -0
- package/templates/common/STORE-README.md +67 -0
- package/templates/common/gitignore +8 -0
- package/templates/common/scripts/check-pricing.ts +22 -0
- package/templates/common/scripts/serve.mjs +13 -0
- package/templates/common/server/catalog.ts +79 -0
- package/templates/common/server/checkout-flow.d.ts +34 -0
- package/templates/common/server/pages.ts +184 -0
- package/templates/common/server/storage.ts +114 -0
- package/templates/common/server/store.ts +67 -0
- package/templates/express/server/express.ts +20 -0
- package/templates/express/tsconfig.build.json +6 -0
- package/templates/express/tsconfig.json +9 -0
- package/templates/next/app/[[...path]]/route.ts +24 -0
- package/templates/next/next.config.mjs +19 -0
- package/templates/next/tsconfig.json +12 -0
- package/templates/payments/express.ts +39 -0
- package/templates/payments/next.ts +38 -0
- package/templates/payments/node.ts +39 -0
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
import { kitIdentity } from "./identity.js";
|
|
2
|
+
import { lstat, opendir, readFile, realpath } from "node:fs/promises";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
// pnpm workspaces, for `init`: the kit belongs in the package that runs the
|
|
5
|
+
// store's server, never the workspace root (pnpm refuses a root add without
|
|
6
|
+
// -w, and the root isn't what the server imports from). Only files are read
|
|
7
|
+
// here; nothing is followed through a symbolic link and nothing is written.
|
|
8
|
+
/** The workspace file pnpm looks for, at or above the project. */
|
|
9
|
+
export const PNPM_WORKSPACE_FILE = "pnpm-workspace.yaml";
|
|
10
|
+
/** The server frameworks the kit has adapters for, by package name. */
|
|
11
|
+
export const SERVER_FRAMEWORKS = {
|
|
12
|
+
express: "Express", next: "Next.js", hono: "Hono", "@hono/node-server": "Hono",
|
|
13
|
+
};
|
|
14
|
+
const MAX_FILE_BYTES = 1024 * 1024;
|
|
15
|
+
const MAX_DEPTH = 4;
|
|
16
|
+
const MAX_DIRECTORIES = 5000;
|
|
17
|
+
/** Directory entries of any kind read in one scan, so a huge flat directory can't exhaust memory. */
|
|
18
|
+
const MAX_ENTRIES = 50_000;
|
|
19
|
+
/** The workspace is too large to scan within init's limits; init then picks no package. */
|
|
20
|
+
export class WorkspaceScanLimitError extends Error {
|
|
21
|
+
constructor() { super("The workspace is too large for init to scan."); }
|
|
22
|
+
}
|
|
23
|
+
async function lstatOrNull(file) {
|
|
24
|
+
try {
|
|
25
|
+
return await lstat(file);
|
|
26
|
+
}
|
|
27
|
+
catch {
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
/** Reads a small regular file, never through a link; null when it isn't one. */
|
|
32
|
+
async function readRegularFile(file) {
|
|
33
|
+
const stat = await lstatOrNull(file);
|
|
34
|
+
if (!stat || !stat.isFile() || stat.size > MAX_FILE_BYTES)
|
|
35
|
+
return null;
|
|
36
|
+
try {
|
|
37
|
+
return await readFile(file, "utf8");
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
function unquote(value) {
|
|
44
|
+
const trimmed = value.trim();
|
|
45
|
+
const quoted = /^(["'])(.*)\1$/.exec(trimmed);
|
|
46
|
+
return quoted ? quoted[2] : trimmed.replace(/\s+#.*$/, "").trim();
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* The `packages` globs of a pnpm-workspace.yaml: the block list and the flow
|
|
50
|
+
* list forms pnpm documents. [] when the file has no `packages` key; null when
|
|
51
|
+
* the key is there in a form init doesn't read.
|
|
52
|
+
*/
|
|
53
|
+
export function workspacePatterns(text) {
|
|
54
|
+
const lines = text.replace(/\r/g, "").split("\n");
|
|
55
|
+
const at = lines.findIndex((line) => /^packages\s*:/.test(line));
|
|
56
|
+
if (at < 0)
|
|
57
|
+
return [];
|
|
58
|
+
const rest = lines[at].replace(/^packages\s*:/, "").replace(/\s+#.*$/, "").trim();
|
|
59
|
+
if (rest.startsWith("[")) {
|
|
60
|
+
if (!rest.endsWith("]"))
|
|
61
|
+
return null;
|
|
62
|
+
return rest.slice(1, -1).split(",").map(unquote).filter(Boolean);
|
|
63
|
+
}
|
|
64
|
+
if (rest)
|
|
65
|
+
return null;
|
|
66
|
+
const patterns = [];
|
|
67
|
+
for (const line of lines.slice(at + 1)) {
|
|
68
|
+
if (!line.trim() || /^\s*#/.test(line))
|
|
69
|
+
continue;
|
|
70
|
+
const item = /^\s+-\s*(.+)$/.exec(line);
|
|
71
|
+
if (!item) {
|
|
72
|
+
if (/^\S/.test(line))
|
|
73
|
+
break;
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
const value = unquote(item[1]);
|
|
77
|
+
if (value)
|
|
78
|
+
patterns.push(value);
|
|
79
|
+
}
|
|
80
|
+
return patterns;
|
|
81
|
+
}
|
|
82
|
+
/** A pnpm workspace glob as a regular expression over "/"-separated relative paths. */
|
|
83
|
+
function globExpression(glob) {
|
|
84
|
+
const pattern = glob.replace(/^\.\//, "").replace(/\/+$/, "");
|
|
85
|
+
let out = "";
|
|
86
|
+
for (let i = 0; i < pattern.length; i++) {
|
|
87
|
+
const char = pattern[i];
|
|
88
|
+
if (char === "*" && pattern[i + 1] === "*") {
|
|
89
|
+
// "**/" matches any number of directories, including none.
|
|
90
|
+
if (pattern[i + 2] === "/") {
|
|
91
|
+
out += "(?:[^/]+/)*";
|
|
92
|
+
i += 2;
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
out += ".*";
|
|
96
|
+
i += 1;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
else if (char === "*")
|
|
100
|
+
out += "[^/]*";
|
|
101
|
+
else if (char === "?")
|
|
102
|
+
out += "[^/]";
|
|
103
|
+
else
|
|
104
|
+
out += char.replace(/[.+^${}()|[\]\\]/g, "\\$&");
|
|
105
|
+
}
|
|
106
|
+
return new RegExp(`^${out}$`);
|
|
107
|
+
}
|
|
108
|
+
/** True when a workspace-relative directory is one of the workspace's packages. */
|
|
109
|
+
export function matchesWorkspace(rel, patterns) {
|
|
110
|
+
let included = false;
|
|
111
|
+
for (const pattern of patterns) {
|
|
112
|
+
if (pattern.startsWith("!")) {
|
|
113
|
+
if (globExpression(pattern.slice(1)).test(rel))
|
|
114
|
+
return false;
|
|
115
|
+
}
|
|
116
|
+
else if (globExpression(pattern).test(rel))
|
|
117
|
+
included = true;
|
|
118
|
+
}
|
|
119
|
+
return included;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* The nearest pnpm-workspace.yaml at or above the project (already a real
|
|
123
|
+
* path). A link, or a file too large or unreadable, reads as `patterns: null`.
|
|
124
|
+
*/
|
|
125
|
+
export async function findPnpmWorkspace(project) {
|
|
126
|
+
let dir = project;
|
|
127
|
+
for (;;) {
|
|
128
|
+
const file = path.join(dir, PNPM_WORKSPACE_FILE);
|
|
129
|
+
const stat = await lstatOrNull(file);
|
|
130
|
+
if (stat) {
|
|
131
|
+
const text = await readRegularFile(file);
|
|
132
|
+
return {
|
|
133
|
+
root: dir,
|
|
134
|
+
patterns: text === null ? null : workspacePatterns(text),
|
|
135
|
+
minimumReleaseAge: text !== null && /^minimumReleaseAge\s*:/m.test(text),
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
const parent = path.dirname(dir);
|
|
139
|
+
if (parent === dir)
|
|
140
|
+
return null;
|
|
141
|
+
dir = parent;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
const PACKAGE_NAME = /^(?:@[a-z0-9][a-z0-9._~-]*\/)?[a-z0-9][a-z0-9._~-]*$/;
|
|
145
|
+
async function describePackage(root, rel) {
|
|
146
|
+
const dir = path.join(root, ...rel.split("/"));
|
|
147
|
+
const text = await readRegularFile(path.join(dir, "package.json"));
|
|
148
|
+
if (text === null)
|
|
149
|
+
return null;
|
|
150
|
+
let manifest;
|
|
151
|
+
try {
|
|
152
|
+
manifest = JSON.parse(text);
|
|
153
|
+
}
|
|
154
|
+
catch {
|
|
155
|
+
return null;
|
|
156
|
+
}
|
|
157
|
+
if (typeof manifest !== "object" || manifest === null)
|
|
158
|
+
return null;
|
|
159
|
+
const record = manifest;
|
|
160
|
+
const deps = new Set();
|
|
161
|
+
for (const field of ["dependencies", "devDependencies"]) {
|
|
162
|
+
const value = record[field];
|
|
163
|
+
if (typeof value === "object" && value !== null)
|
|
164
|
+
for (const key of Object.keys(value))
|
|
165
|
+
deps.add(key);
|
|
166
|
+
}
|
|
167
|
+
const name = typeof record.name === "string" && record.name.length <= 214 && PACKAGE_NAME.test(record.name) ? record.name : null;
|
|
168
|
+
const frameworks = [...new Set(Object.keys(SERVER_FRAMEWORKS).filter((dep) => deps.has(dep)).map((dep) => SERVER_FRAMEWORKS[dep]))];
|
|
169
|
+
return { dir, rel, name, frameworks, hasKit: deps.has(kitIdentity().name) };
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* The workspace's packages under its root, found by walking real directories
|
|
173
|
+
* only (links, node_modules and dot-directories are skipped) a few levels
|
|
174
|
+
* deep. The root itself is never one of them.
|
|
175
|
+
*/
|
|
176
|
+
export async function workspacePackages(root, patterns) {
|
|
177
|
+
const found = [];
|
|
178
|
+
let visited = 0;
|
|
179
|
+
let read = 0;
|
|
180
|
+
const walk = async (rel, depth) => {
|
|
181
|
+
if (depth > MAX_DEPTH)
|
|
182
|
+
return;
|
|
183
|
+
// Streamed, with a budget on every entry read: a scan that would exceed
|
|
184
|
+
// it fails closed instead of choosing from a partial view.
|
|
185
|
+
const names = [];
|
|
186
|
+
let dir;
|
|
187
|
+
try {
|
|
188
|
+
dir = await opendir(rel ? path.join(root, ...rel.split("/")) : root);
|
|
189
|
+
}
|
|
190
|
+
catch {
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
try {
|
|
194
|
+
for await (const entry of dir) {
|
|
195
|
+
if (++read > MAX_ENTRIES)
|
|
196
|
+
throw new WorkspaceScanLimitError();
|
|
197
|
+
// Dirent types come from lstat: a link is never a directory here.
|
|
198
|
+
if (!entry.isDirectory() || entry.name === "node_modules" || entry.name.startsWith("."))
|
|
199
|
+
continue;
|
|
200
|
+
if (++visited > MAX_DIRECTORIES)
|
|
201
|
+
throw new WorkspaceScanLimitError();
|
|
202
|
+
names.push(entry.name);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
catch (error) {
|
|
206
|
+
if (error instanceof WorkspaceScanLimitError)
|
|
207
|
+
throw error;
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
names.sort((a, b) => a < b ? -1 : a > b ? 1 : 0);
|
|
211
|
+
for (const name of names) {
|
|
212
|
+
const child = rel ? `${rel}/${name}` : name;
|
|
213
|
+
if (matchesWorkspace(child, patterns)) {
|
|
214
|
+
const described = await describePackage(root, child);
|
|
215
|
+
if (described)
|
|
216
|
+
found.push(described);
|
|
217
|
+
}
|
|
218
|
+
await walk(child, depth + 1);
|
|
219
|
+
}
|
|
220
|
+
};
|
|
221
|
+
await walk("", 1);
|
|
222
|
+
return found;
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* The one package the kit belongs in: the only one that already depends on
|
|
226
|
+
* the kit, else the only one that runs a supported server framework. null
|
|
227
|
+
* when there is none or more than one (init then names the command instead).
|
|
228
|
+
*/
|
|
229
|
+
export function serverPackage(packages) {
|
|
230
|
+
const withKit = packages.filter((pkg) => pkg.hasKit);
|
|
231
|
+
if (withKit.length)
|
|
232
|
+
return withKit.length === 1 ? withKit[0] : null;
|
|
233
|
+
const servers = packages.filter((pkg) => pkg.frameworks.length);
|
|
234
|
+
return servers.length === 1 ? servers[0] : null;
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* True when `dir` is a real directory strictly inside `root` with no link on
|
|
238
|
+
* the way: its real path is exactly the path init built.
|
|
239
|
+
*/
|
|
240
|
+
export async function insideRoot(root, dir) {
|
|
241
|
+
if (!dir.startsWith(`${root}${path.sep}`))
|
|
242
|
+
return false;
|
|
243
|
+
try {
|
|
244
|
+
const stat = await lstat(dir);
|
|
245
|
+
return stat.isDirectory() && (await realpath(dir)) === dir;
|
|
246
|
+
}
|
|
247
|
+
catch {
|
|
248
|
+
return false;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import type { StorePoliciesInput } from "./setup.js";
|
|
2
|
+
export type StoreReturnScope = "all" | "defective_only" | "none";
|
|
3
|
+
export interface StoreReturnsPolicy {
|
|
4
|
+
/**
|
|
5
|
+
* Days after delivery the store accepts a return (1–365 for all). With
|
|
6
|
+
* `scope: "defective_only"` it is the window for reporting a defective or
|
|
7
|
+
* damaged item (0–365; zero promises no reporting period). none requires zero.
|
|
8
|
+
*/
|
|
9
|
+
windowDays: number;
|
|
10
|
+
/** `all`: any item. `defective_only`: only defective/damaged items. `none`: no returns. */
|
|
11
|
+
scope: StoreReturnScope;
|
|
12
|
+
/** The store's returns page (absolute https URL). */
|
|
13
|
+
policyUrl?: string;
|
|
14
|
+
/** Plain-text conditions, at most 2000 characters. */
|
|
15
|
+
conditions?: string;
|
|
16
|
+
}
|
|
17
|
+
export interface StorePolicyOptions {
|
|
18
|
+
/**
|
|
19
|
+
* Absolute https URLs of the store's own policy pages. In Test, plain http on
|
|
20
|
+
* localhost, 127.0.0.1 or [::1] is accepted too (Live requires https).
|
|
21
|
+
*/
|
|
22
|
+
shippingUrl?: string;
|
|
23
|
+
privacyUrl?: string;
|
|
24
|
+
termsUrl?: string;
|
|
25
|
+
contactUrl?: string;
|
|
26
|
+
returns?: StoreReturnsPolicy;
|
|
27
|
+
}
|
|
28
|
+
/** True for an http URL on localhost, 127.0.0.1 or [::1] (Test-only policy pages). */
|
|
29
|
+
export declare function isLoopbackPolicyUrl(value: string): boolean;
|
|
30
|
+
/** Validates the `policies` option once, at kit creation. Returns a frozen, normalized copy. */
|
|
31
|
+
export declare function normalizeStorePolicies(input: unknown, options?: {
|
|
32
|
+
live?: boolean;
|
|
33
|
+
}): Readonly<StorePolicyOptions> | undefined;
|
|
34
|
+
/** One sentence describing the returns policy. Never mentions cost; defective-only never reads as a general window. */
|
|
35
|
+
export declare function returnsSentence(returns: StoreReturnsPolicy): string;
|
|
36
|
+
/** The `## Policies` section of `llms.txt`, or no lines when no policy is configured. */
|
|
37
|
+
export declare function policyLlmsLines(policies: StorePolicyOptions | undefined): string[];
|
|
38
|
+
/**
|
|
39
|
+
* schema.org `MerchantReturnPolicy` for an Offer. `all` is a finite return
|
|
40
|
+
* window. `defective_only` is `MerchantReturnNotPermitted` (ordinary returns
|
|
41
|
+
* aren't accepted) with the defect window stated in its description. No
|
|
42
|
+
* `returnFees` is ever set: the kit isn't told the cost.
|
|
43
|
+
*/
|
|
44
|
+
export declare function merchantReturnPolicy(returns: StoreReturnsPolicy | undefined): Record<string, unknown> | undefined;
|
|
45
|
+
/**
|
|
46
|
+
* Explicit replacement only. Caller fields win; unrelated support, notices and
|
|
47
|
+
* fees survive. The local option contains no cost: require the caller to supply
|
|
48
|
+
* it rather than inventing free returns or fetching/merging hidden state.
|
|
49
|
+
*/
|
|
50
|
+
export declare function policyReplacement(input: StorePoliciesInput, policies: StorePolicyOptions | undefined): StorePoliciesInput;
|
|
51
|
+
/** Checkout `links` each protocol already defines, from the configured policies. */
|
|
52
|
+
export declare function policyCheckoutLinks(protocol: string, policies: StorePolicyOptions | undefined, nativeAcp?: boolean): {
|
|
53
|
+
type: string;
|
|
54
|
+
url: string;
|
|
55
|
+
}[];
|
|
56
|
+
/**
|
|
57
|
+
* Adds the store's policy links to a successful UCP or ACP checkout body that
|
|
58
|
+
* already carries a `links` array. Links Relyon already sent for a type are
|
|
59
|
+
* kept; nothing else in the body changes. Returns null when nothing is added.
|
|
60
|
+
*/
|
|
61
|
+
export declare function withPolicyLinks(protocol: string, body: string, policies: StorePolicyOptions | undefined): string | null;
|
|
62
|
+
/** One field a policy sync would change in Relyon's store policies. */
|
|
63
|
+
export interface StorePolicySyncChange {
|
|
64
|
+
/** The field's path in the PUT /v1/project_space_policies body, such as `returns.policy_url`. */
|
|
65
|
+
field: string;
|
|
66
|
+
from: unknown;
|
|
67
|
+
to: unknown;
|
|
68
|
+
}
|
|
69
|
+
/** What the merchant confirms for a sync: facts the kit never infers. */
|
|
70
|
+
export interface StorePolicySyncConfirmed {
|
|
71
|
+
/**
|
|
72
|
+
* The merchant's own return cost (and window, when it differs from the
|
|
73
|
+
* kit's `policies.returns.windowDays`). Never defaulted: without it (and
|
|
74
|
+
* with no cost already stored in Relyon) the returns terms are left as
|
|
75
|
+
* they are and the plan lists `returns.cost` under `needs`.
|
|
76
|
+
*/
|
|
77
|
+
returns?: {
|
|
78
|
+
cost: "free" | "buyer_pays" | "fixed";
|
|
79
|
+
cost_amount?: {
|
|
80
|
+
amount: number;
|
|
81
|
+
currency: string;
|
|
82
|
+
};
|
|
83
|
+
window_days?: number;
|
|
84
|
+
};
|
|
85
|
+
/** The merchant's own support contact, when the store has no contact page (`policies.contactUrl`). */
|
|
86
|
+
support?: {
|
|
87
|
+
email?: string;
|
|
88
|
+
phone?: string;
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
export interface StorePolicySyncPlan {
|
|
92
|
+
/** The full replacement document (PUT replaces every field). */
|
|
93
|
+
input: StorePoliciesInput;
|
|
94
|
+
changes: StorePolicySyncChange[];
|
|
95
|
+
/** Fields the merchant must still confirm or supply, each with what to do. */
|
|
96
|
+
needs: {
|
|
97
|
+
field: string;
|
|
98
|
+
fix: string;
|
|
99
|
+
}[];
|
|
100
|
+
}
|
|
101
|
+
declare const POLICY_DOCUMENT_FIELDS: readonly ["returns", "support", "consumer_notices", "shipping_policy_url", "privacy_policy_url", "terms_url"];
|
|
102
|
+
/**
|
|
103
|
+
* Plans keeping Relyon's store policies in step with the store's own policy
|
|
104
|
+
* pages (the kit `policies` option). Starts from what Relyon has, so support
|
|
105
|
+
* contacts, consumer notices and other fields survive; the store's page URLs,
|
|
106
|
+
* returns scope, window and conditions replace Relyon's. A return cost comes
|
|
107
|
+
* only from `confirmed.returns` or a cost the merchant already stored in
|
|
108
|
+
* Relyon: the kit never infers free returns or a return cost.
|
|
109
|
+
*/
|
|
110
|
+
export declare function planStorePolicySync(current: Partial<Record<(typeof POLICY_DOCUMENT_FIELDS)[number], unknown>> | undefined, local: StorePolicyOptions | undefined, confirmed?: StorePolicySyncConfirmed): StorePolicySyncPlan;
|
|
111
|
+
export {};
|
package/dist/policies.js
ADDED
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
// The store's own policies (the kit `policies` option): where shipping,
|
|
2
|
+
// privacy, terms and contact pages live, and the returns window and scope.
|
|
3
|
+
// The kit publishes them only where the protocols already carry policies:
|
|
4
|
+
// UCP and ACP checkout `links`, `llms.txt`, and schema.org
|
|
5
|
+
// `MerchantReturnPolicy` in product JSON-LD. It never states a return cost it
|
|
6
|
+
// wasn't given, so nothing here reads as "free returns", and a defective-only
|
|
7
|
+
// policy is never phrased as a general N-day return window.
|
|
8
|
+
import { isRecord } from "./util.js";
|
|
9
|
+
const URL_FIELDS = ["shippingUrl", "privacyUrl", "termsUrl", "contactUrl"];
|
|
10
|
+
/** The local development hosts a Test store may link policy pages on over plain http. */
|
|
11
|
+
const LOOPBACK_POLICY_HOSTS = new Set(["localhost", "127.0.0.1", "[::1]"]);
|
|
12
|
+
/** True for an http URL on localhost, 127.0.0.1 or [::1] (Test-only policy pages). */
|
|
13
|
+
export function isLoopbackPolicyUrl(value) {
|
|
14
|
+
try {
|
|
15
|
+
const url = new URL(value);
|
|
16
|
+
return url.protocol === "http:" && LOOPBACK_POLICY_HOSTS.has(url.hostname);
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
const LIVE_HTTPS = "Live mode requires https policy URLs on the store's published domain; plain http on localhost, 127.0.0.1 or [::1] is accepted in Test only.";
|
|
23
|
+
function httpsUrl(value, name, live) {
|
|
24
|
+
const expected = live ? "an absolute https URL" : "an absolute https URL (or, in Test, http on localhost, 127.0.0.1 or [::1])";
|
|
25
|
+
if (typeof value !== "string" || value.length > 4096)
|
|
26
|
+
throw new TypeError(`policies.${name} must be ${expected}.`);
|
|
27
|
+
let url;
|
|
28
|
+
try {
|
|
29
|
+
url = new URL(value);
|
|
30
|
+
}
|
|
31
|
+
catch {
|
|
32
|
+
throw new TypeError(`policies.${name} must be ${expected}.`);
|
|
33
|
+
}
|
|
34
|
+
if (url.username || url.password || !url.hostname)
|
|
35
|
+
throw new TypeError(`policies.${name} must be ${expected} without credentials.`);
|
|
36
|
+
if (url.protocol === "https:")
|
|
37
|
+
return url.href;
|
|
38
|
+
if (url.protocol === "http:" && LOOPBACK_POLICY_HOSTS.has(url.hostname)) {
|
|
39
|
+
if (live)
|
|
40
|
+
throw new TypeError(`policies.${name} is a local http address. ${LIVE_HTTPS}`);
|
|
41
|
+
return url.href;
|
|
42
|
+
}
|
|
43
|
+
throw new TypeError(`policies.${name} must be ${expected} without credentials.${live ? ` ${LIVE_HTTPS}` : ""}`);
|
|
44
|
+
}
|
|
45
|
+
/** Validates the `policies` option once, at kit creation. Returns a frozen, normalized copy. */
|
|
46
|
+
export function normalizeStorePolicies(input, options = {}) {
|
|
47
|
+
const live = options.live === true;
|
|
48
|
+
if (input === undefined)
|
|
49
|
+
return undefined;
|
|
50
|
+
if (!isRecord(input))
|
|
51
|
+
throw new TypeError("policies must be an object.");
|
|
52
|
+
for (const key of Object.keys(input)) {
|
|
53
|
+
if (![...URL_FIELDS, "returns"].includes(key))
|
|
54
|
+
throw new TypeError(`policies.${key} is not a known policy field.`);
|
|
55
|
+
}
|
|
56
|
+
const out = {};
|
|
57
|
+
for (const field of URL_FIELDS)
|
|
58
|
+
if (input[field] !== undefined)
|
|
59
|
+
out[field] = httpsUrl(input[field], field, live);
|
|
60
|
+
if (input.returns !== undefined) {
|
|
61
|
+
const returns = input.returns;
|
|
62
|
+
if (!isRecord(returns))
|
|
63
|
+
throw new TypeError("policies.returns must be an object.");
|
|
64
|
+
for (const key of Object.keys(returns)) {
|
|
65
|
+
if (!["windowDays", "scope", "policyUrl", "conditions"].includes(key))
|
|
66
|
+
throw new TypeError(`policies.returns.${key} is not a known returns field.`);
|
|
67
|
+
}
|
|
68
|
+
const days = returns.windowDays;
|
|
69
|
+
if (typeof days !== "number" || !Number.isInteger(days) || days < 0 || days > 365 || (days === 0 && returns.scope === "all")) {
|
|
70
|
+
throw new TypeError("policies.returns.windowDays must be 1 to 365 for all, or 0 to 365 for defective_only; none requires zero.");
|
|
71
|
+
}
|
|
72
|
+
if (!["all", "defective_only", "none"].includes(returns.scope) || (returns.scope === "none" && days !== 0)) {
|
|
73
|
+
throw new TypeError('policies.returns.scope must be "all", "defective_only", or "none" with zero days.');
|
|
74
|
+
}
|
|
75
|
+
if (returns.conditions !== undefined && (typeof returns.conditions !== "string" || returns.conditions.length > 2000
|
|
76
|
+
|| /[<>\u0000-\u0008\u000b\u000c\u000e-\u001f\u007f]|&(?:lt|gt|#)/iu.test(returns.conditions.normalize("NFKC")))) {
|
|
77
|
+
throw new TypeError("policies.returns.conditions must be plain text of at most 2000 characters.");
|
|
78
|
+
}
|
|
79
|
+
out.returns = Object.freeze({
|
|
80
|
+
windowDays: days, scope: returns.scope,
|
|
81
|
+
...(returns.policyUrl !== undefined ? { policyUrl: httpsUrl(returns.policyUrl, "returns.policyUrl", live) } : {}),
|
|
82
|
+
...(returns.conditions !== undefined ? { conditions: returns.conditions } : {}),
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
return Object.freeze(out);
|
|
86
|
+
}
|
|
87
|
+
const days = (n) => `${n} day${n === 1 ? "" : "s"}`;
|
|
88
|
+
/** One sentence describing the returns policy. Never mentions cost; defective-only never reads as a general window. */
|
|
89
|
+
export function returnsSentence(returns) {
|
|
90
|
+
const sentence = returns.scope === "none" ? "Items cannot be returned."
|
|
91
|
+
: returns.scope === "defective_only"
|
|
92
|
+
? `Returns are accepted only for items that arrive defective or damaged${returns.windowDays > 0 ? `, reported within ${days(returns.windowDays)} of delivery` : "; no reporting period is specified"}; other items can't be returned.`
|
|
93
|
+
: `Items can be returned within ${days(returns.windowDays)} of delivery.`;
|
|
94
|
+
return sentence + (returns.conditions ? ` ${returns.conditions}` : "");
|
|
95
|
+
}
|
|
96
|
+
/** The `## Policies` section of `llms.txt`, or no lines when no policy is configured. */
|
|
97
|
+
export function policyLlmsLines(policies) {
|
|
98
|
+
if (!policies)
|
|
99
|
+
return [];
|
|
100
|
+
const lines = [];
|
|
101
|
+
if (policies.returns) {
|
|
102
|
+
lines.push(`- Returns: ${returnsSentence(policies.returns)}${policies.returns.policyUrl ? ` Policy: ${policies.returns.policyUrl}` : ""}`);
|
|
103
|
+
}
|
|
104
|
+
if (policies.shippingUrl)
|
|
105
|
+
lines.push(`- Shipping policy: ${policies.shippingUrl}`);
|
|
106
|
+
if (policies.privacyUrl)
|
|
107
|
+
lines.push(`- Privacy policy: ${policies.privacyUrl}`);
|
|
108
|
+
if (policies.termsUrl)
|
|
109
|
+
lines.push(`- Terms of service: ${policies.termsUrl}`);
|
|
110
|
+
if (policies.contactUrl)
|
|
111
|
+
lines.push(`- Contact: ${policies.contactUrl}`);
|
|
112
|
+
return lines.length ? ["", "## Policies", "", ...lines] : [];
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* schema.org `MerchantReturnPolicy` for an Offer. `all` is a finite return
|
|
116
|
+
* window. `defective_only` is `MerchantReturnNotPermitted` (ordinary returns
|
|
117
|
+
* aren't accepted) with the defect window stated in its description. No
|
|
118
|
+
* `returnFees` is ever set: the kit isn't told the cost.
|
|
119
|
+
*/
|
|
120
|
+
export function merchantReturnPolicy(returns) {
|
|
121
|
+
if (!returns)
|
|
122
|
+
return undefined;
|
|
123
|
+
const link = returns.policyUrl ? { merchantReturnLink: returns.policyUrl } : {};
|
|
124
|
+
return returns.scope !== "all"
|
|
125
|
+
? {
|
|
126
|
+
"@type": "MerchantReturnPolicy",
|
|
127
|
+
returnPolicyCategory: "https://schema.org/MerchantReturnNotPermitted",
|
|
128
|
+
description: returnsSentence(returns),
|
|
129
|
+
...link,
|
|
130
|
+
}
|
|
131
|
+
: {
|
|
132
|
+
"@type": "MerchantReturnPolicy",
|
|
133
|
+
returnPolicyCategory: "https://schema.org/MerchantReturnFiniteReturnWindow",
|
|
134
|
+
merchantReturnDays: returns.windowDays,
|
|
135
|
+
...(returns.conditions ? { description: returnsSentence(returns) } : {}),
|
|
136
|
+
...link,
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Explicit replacement only. Caller fields win; unrelated support, notices and
|
|
141
|
+
* fees survive. The local option contains no cost: require the caller to supply
|
|
142
|
+
* it rather than inventing free returns or fetching/merging hidden state.
|
|
143
|
+
*/
|
|
144
|
+
export function policyReplacement(input, policies) {
|
|
145
|
+
if (!policies)
|
|
146
|
+
return input;
|
|
147
|
+
const mapped = {
|
|
148
|
+
...(policies.shippingUrl ? { shipping_policy_url: policies.shippingUrl } : {}),
|
|
149
|
+
...(policies.privacyUrl ? { privacy_policy_url: policies.privacyUrl } : {}),
|
|
150
|
+
...(policies.termsUrl ? { terms_url: policies.termsUrl } : {}),
|
|
151
|
+
...input,
|
|
152
|
+
};
|
|
153
|
+
if (policies.contactUrl)
|
|
154
|
+
mapped.support = { url: policies.contactUrl, ...input.support };
|
|
155
|
+
if (policies.returns) {
|
|
156
|
+
if (!input.returns?.cost)
|
|
157
|
+
throw new TypeError("Explicit policy replacement with local returns requires caller-supplied returns.cost and window_days.");
|
|
158
|
+
mapped.returns = {
|
|
159
|
+
scope: policies.returns.scope,
|
|
160
|
+
...(policies.returns.policyUrl ? { policy_url: policies.returns.policyUrl } : {}),
|
|
161
|
+
...(policies.returns.conditions !== undefined ? { conditions: policies.returns.conditions } : {}),
|
|
162
|
+
...input.returns,
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
return mapped;
|
|
166
|
+
}
|
|
167
|
+
/** Checkout `links` each protocol already defines, from the configured policies. */
|
|
168
|
+
export function policyCheckoutLinks(protocol, policies, nativeAcp = false) {
|
|
169
|
+
if (!policies)
|
|
170
|
+
return [];
|
|
171
|
+
const links = [];
|
|
172
|
+
if (protocol === "ucp") {
|
|
173
|
+
if (policies.privacyUrl)
|
|
174
|
+
links.push({ type: "privacy_policy", url: policies.privacyUrl });
|
|
175
|
+
if (policies.termsUrl)
|
|
176
|
+
links.push({ type: "terms_of_service", url: policies.termsUrl });
|
|
177
|
+
if (policies.returns?.policyUrl)
|
|
178
|
+
links.push({ type: "refund_policy", url: policies.returns.policyUrl });
|
|
179
|
+
if (policies.shippingUrl)
|
|
180
|
+
links.push({ type: "shipping_policy", url: policies.shippingUrl });
|
|
181
|
+
}
|
|
182
|
+
else if (protocol === "acp") {
|
|
183
|
+
if (policies.termsUrl)
|
|
184
|
+
links.push({ type: "terms_of_use", url: policies.termsUrl });
|
|
185
|
+
if (policies.privacyUrl)
|
|
186
|
+
links.push({ type: "privacy_policy", url: policies.privacyUrl });
|
|
187
|
+
if (nativeAcp) {
|
|
188
|
+
if (policies.returns?.policyUrl)
|
|
189
|
+
links.push({ type: "return_policy", url: policies.returns.policyUrl });
|
|
190
|
+
if (policies.shippingUrl)
|
|
191
|
+
links.push({ type: "shipping_policy", url: policies.shippingUrl });
|
|
192
|
+
if (policies.contactUrl)
|
|
193
|
+
links.push({ type: "contact_us", url: policies.contactUrl });
|
|
194
|
+
}
|
|
195
|
+
else {
|
|
196
|
+
// Preserve existing callers of the legacy relay dialect. Native ACP
|
|
197
|
+
// identifies itself with capabilities.payment and has a closed link enum.
|
|
198
|
+
const shop = policies.returns?.policyUrl ?? policies.shippingUrl;
|
|
199
|
+
if (shop)
|
|
200
|
+
links.push({ type: "seller_shop_policies", url: shop });
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
return links;
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Adds the store's policy links to a successful UCP or ACP checkout body that
|
|
207
|
+
* already carries a `links` array. Links Relyon already sent for a type are
|
|
208
|
+
* kept; nothing else in the body changes. Returns null when nothing is added.
|
|
209
|
+
*/
|
|
210
|
+
export function withPolicyLinks(protocol, body, policies) {
|
|
211
|
+
let parsed;
|
|
212
|
+
try {
|
|
213
|
+
parsed = JSON.parse(body);
|
|
214
|
+
}
|
|
215
|
+
catch {
|
|
216
|
+
return null;
|
|
217
|
+
}
|
|
218
|
+
if (!isRecord(parsed) || !Array.isArray(parsed.links))
|
|
219
|
+
return null;
|
|
220
|
+
const nativeAcp = protocol === "acp" && isRecord(parsed.capabilities) && isRecord(parsed.capabilities.payment);
|
|
221
|
+
const extra = policyCheckoutLinks(protocol, policies, nativeAcp);
|
|
222
|
+
if (!extra.length)
|
|
223
|
+
return null;
|
|
224
|
+
const present = new Set(parsed.links.filter(isRecord).map((link) => link.type));
|
|
225
|
+
const added = extra.filter((link) => !present.has(link.type));
|
|
226
|
+
if (!added.length)
|
|
227
|
+
return null;
|
|
228
|
+
return JSON.stringify({ ...parsed, links: [...parsed.links, ...added] });
|
|
229
|
+
}
|
|
230
|
+
const POLICY_DOCUMENT_FIELDS = ["returns", "support", "consumer_notices", "shipping_policy_url", "privacy_policy_url", "terms_url"];
|
|
231
|
+
/**
|
|
232
|
+
* Plans keeping Relyon's store policies in step with the store's own policy
|
|
233
|
+
* pages (the kit `policies` option). Starts from what Relyon has, so support
|
|
234
|
+
* contacts, consumer notices and other fields survive; the store's page URLs,
|
|
235
|
+
* returns scope, window and conditions replace Relyon's. A return cost comes
|
|
236
|
+
* only from `confirmed.returns` or a cost the merchant already stored in
|
|
237
|
+
* Relyon: the kit never infers free returns or a return cost.
|
|
238
|
+
*/
|
|
239
|
+
export function planStorePolicySync(current, local, confirmed = {}) {
|
|
240
|
+
const input = {};
|
|
241
|
+
for (const field of POLICY_DOCUMENT_FIELDS) {
|
|
242
|
+
if (current?.[field] !== undefined && current[field] !== null)
|
|
243
|
+
input[field] = structuredClone(current[field]);
|
|
244
|
+
}
|
|
245
|
+
const needs = [];
|
|
246
|
+
if (local?.shippingUrl)
|
|
247
|
+
input.shipping_policy_url = local.shippingUrl;
|
|
248
|
+
if (local?.privacyUrl)
|
|
249
|
+
input.privacy_policy_url = local.privacyUrl;
|
|
250
|
+
if (local?.termsUrl)
|
|
251
|
+
input.terms_url = local.termsUrl;
|
|
252
|
+
if (local?.contactUrl) {
|
|
253
|
+
const support = isRecord(input.support) ? input.support : {};
|
|
254
|
+
input.support = { ...support, url: local.contactUrl };
|
|
255
|
+
}
|
|
256
|
+
if (confirmed.support?.email || confirmed.support?.phone) {
|
|
257
|
+
const support = isRecord(input.support) ? input.support : {};
|
|
258
|
+
input.support = {
|
|
259
|
+
...support,
|
|
260
|
+
...(confirmed.support.email ? { email: confirmed.support.email } : {}),
|
|
261
|
+
...(confirmed.support.phone ? { phone: confirmed.support.phone } : {}),
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
const stored = isRecord(input.returns) ? input.returns : undefined;
|
|
265
|
+
if (local?.returns || confirmed.returns) {
|
|
266
|
+
const cost = confirmed.returns?.cost ?? (typeof stored?.cost === "string" ? stored.cost : undefined);
|
|
267
|
+
const costAmount = confirmed.returns ? confirmed.returns.cost_amount : stored?.cost_amount;
|
|
268
|
+
const windowDays = confirmed.returns?.window_days ?? local?.returns?.windowDays ?? (typeof stored?.window_days === "number" ? stored.window_days : undefined);
|
|
269
|
+
if (cost === undefined) {
|
|
270
|
+
needs.push({ field: "returns.cost", fix: "Ask the merchant what a return costs the shopper (free, buyer_pays, or fixed with cost_amount), then pass it as returns.cost. The kit never assumes free returns." });
|
|
271
|
+
}
|
|
272
|
+
else if (cost === "fixed" && !isRecord(costAmount)) {
|
|
273
|
+
needs.push({ field: "returns.cost_amount", fix: "A fixed return cost needs returns.cost_amount (amount in minor units and currency) from the merchant." });
|
|
274
|
+
}
|
|
275
|
+
else if (windowDays === undefined) {
|
|
276
|
+
needs.push({ field: "returns.window_days", fix: "Ask the merchant for the return window in days and set policies.returns.windowDays." });
|
|
277
|
+
}
|
|
278
|
+
else {
|
|
279
|
+
const returns = { ...(stored ?? {}), window_days: windowDays, cost };
|
|
280
|
+
if (cost === "fixed")
|
|
281
|
+
returns.cost_amount = costAmount;
|
|
282
|
+
else
|
|
283
|
+
delete returns.cost_amount;
|
|
284
|
+
if (local?.returns) {
|
|
285
|
+
returns.scope = local.returns.scope;
|
|
286
|
+
if (local.returns.policyUrl)
|
|
287
|
+
returns.policy_url = local.returns.policyUrl;
|
|
288
|
+
if (local.returns.conditions !== undefined)
|
|
289
|
+
returns.conditions = local.returns.conditions;
|
|
290
|
+
else
|
|
291
|
+
delete returns.conditions;
|
|
292
|
+
}
|
|
293
|
+
input.returns = returns;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
const finalReturns = isRecord(input.returns) ? input.returns : undefined;
|
|
297
|
+
if (finalReturns && !finalReturns.policy_url) {
|
|
298
|
+
needs.push({ field: "returns.policy_url", fix: "Publish the store's returns page, then set policies.returns.policyUrl to its https:// address." });
|
|
299
|
+
}
|
|
300
|
+
const finalSupport = isRecord(input.support) ? input.support : undefined;
|
|
301
|
+
if (!finalSupport?.email && !finalSupport?.url) {
|
|
302
|
+
needs.push({ field: "support", fix: "Set policies.contactUrl to the store's https:// contact page, or ask the merchant for a support email and pass it as support.email." });
|
|
303
|
+
}
|
|
304
|
+
const changes = [];
|
|
305
|
+
const diff = (path, from, to) => {
|
|
306
|
+
if (isRecord(from) && isRecord(to)) {
|
|
307
|
+
for (const key of new Set([...Object.keys(from), ...Object.keys(to)]))
|
|
308
|
+
diff(`${path}.${key}`, from[key], to[key]);
|
|
309
|
+
}
|
|
310
|
+
else if (JSON.stringify(from) !== JSON.stringify(to)) {
|
|
311
|
+
changes.push({ field: path, from: from ?? null, to: to ?? null });
|
|
312
|
+
}
|
|
313
|
+
};
|
|
314
|
+
for (const field of POLICY_DOCUMENT_FIELDS)
|
|
315
|
+
diff(field, current?.[field] ?? undefined, input[field]);
|
|
316
|
+
return { input: input, changes, needs };
|
|
317
|
+
}
|