@inferencebureau/apps-sdk 0.1.0 → 0.2.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/README.md +43 -3
- package/dist/index.d.ts +89 -1
- package/dist/index.js +4 -0
- package/dist/runtime.d.ts +9 -2
- package/dist/runtime.js +10 -5
- package/dist/test.d.ts +250 -0
- package/dist/test.js +1017 -0
- package/package.json +7 -2
package/dist/test.js
ADDED
|
@@ -0,0 +1,1017 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @inferencebureau/apps-sdk/test: an app's actions run in the test process against an in-memory copy of its records,
|
|
3
|
+
* so they can be tested without an environment. `loadApp()` reads the app's folder (its schemas, actions and
|
|
4
|
+
* settings) and gives back a TestApp: `run` an action as Inference Bureau would, read and write records through
|
|
5
|
+
* `api` as the action's own `api` does, deliver Shopify webhooks, install a store, run queued jobs, and answer the
|
|
6
|
+
* action's Shopify Admin calls with mocks.
|
|
7
|
+
*
|
|
8
|
+
* The records follow the records engine's rules (packages/apps/src/records.ts): field types and conversions,
|
|
9
|
+
* defaults, validations, uniqueness, belongsTo, filters, sorting and pages, and the errors the API answers with, as
|
|
10
|
+
* ApiError. An action's run goes through the same runner as a deployment (runtime.ts): its params and record arrive
|
|
11
|
+
* as JSON, its writes are applied when `run` returns, then `onSuccess` runs with the saved record. Not covered:
|
|
12
|
+
* permissions (actions run as the system), `timeoutMS`, webhook `filter`s (Shopify applies them), schedules' timing
|
|
13
|
+
* and computed or relationship fields other than belongsTo.
|
|
14
|
+
*/
|
|
15
|
+
import { ApiError, ShopifyError } from "./index.js";
|
|
16
|
+
import { runAction as runPhase } from "./runtime.js";
|
|
17
|
+
/** Kept in `data`; the rest are a vector, the other side of a relationship, or worked out (computed). */
|
|
18
|
+
const STORED = new Set(["string", "richText", "email", "url", "color", "number", "dateTime", "enum", "boolean", "roleList", "json", "belongsTo", "password", "encryptedString"]);
|
|
19
|
+
const SYSTEM = ["id", "createdAt", "updatedAt"];
|
|
20
|
+
const MAX_PAGE = 250;
|
|
21
|
+
const MAX_RECORD_BYTES = 256 * 1024;
|
|
22
|
+
const invalid = (problems) => new ApiError(problems.map((p) => `${p.field}: ${p.message}`).join("; "), 400, "invalid_request", problems);
|
|
23
|
+
const notFound = (message) => new ApiError(message, 404, "not_found");
|
|
24
|
+
const isObj = (v) => !!v && typeof v === "object" && !Array.isArray(v);
|
|
25
|
+
/** What crosses the wire between Inference Bureau and the app: JSON (a Date arrives as its ISO string). */
|
|
26
|
+
const wire = (v) => (v === undefined ? v : JSON.parse(JSON.stringify(v)));
|
|
27
|
+
function modelOf(name, schema) {
|
|
28
|
+
const fields = Object.entries(schema.fields ?? {}).map(([n, def]) => ({ name: n, key: typeof def.storageKey === "string" && def.storageKey ? def.storageKey : n, type: def.type, def }));
|
|
29
|
+
const dup = fields.find((f, i) => fields.findIndex((g) => g.key === f.key) !== i);
|
|
30
|
+
if (dup)
|
|
31
|
+
throw new Error(`${name}: two fields store their values under ${dup.key}; give ${dup.name} its own storageKey`);
|
|
32
|
+
return { name, fields, byName: new Map(fields.map((f) => [f.name, f])) };
|
|
33
|
+
}
|
|
34
|
+
const ok = (value) => ({ ok: true, value });
|
|
35
|
+
const bad = (message) => ({ ok: false, message });
|
|
36
|
+
const EMAIL = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
37
|
+
const COLOR = /^#[0-9a-fA-F]{6}$/;
|
|
38
|
+
const optionsOf = (f) => (Array.isArray(f.def.options) ? f.def.options.map(String) : []);
|
|
39
|
+
function convert(f, v, mode) {
|
|
40
|
+
if (v === null || v === undefined)
|
|
41
|
+
return ok(null);
|
|
42
|
+
const read = mode === "read";
|
|
43
|
+
switch (f.type) {
|
|
44
|
+
case "string":
|
|
45
|
+
case "richText":
|
|
46
|
+
case "email":
|
|
47
|
+
case "url":
|
|
48
|
+
case "color":
|
|
49
|
+
case "encryptedString": {
|
|
50
|
+
const s = typeof v === "string" ? v : read && (typeof v === "number" || typeof v === "boolean") ? String(v) : f.type === "richText" && isObj(v) && typeof v.markdown === "string" ? v.markdown : null;
|
|
51
|
+
if (s === null)
|
|
52
|
+
return read ? ok(null) : bad("must be text");
|
|
53
|
+
if (read)
|
|
54
|
+
return ok(s);
|
|
55
|
+
if (f.type === "email" && !EMAIL.test(s))
|
|
56
|
+
return bad("isn't an email address");
|
|
57
|
+
if (f.type === "url") {
|
|
58
|
+
try {
|
|
59
|
+
if (!/^https?:$/.test(new URL(s).protocol))
|
|
60
|
+
return bad("must be an http(s) URL");
|
|
61
|
+
}
|
|
62
|
+
catch {
|
|
63
|
+
return bad("isn't a URL");
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
if (f.type === "color" && !COLOR.test(s))
|
|
67
|
+
return bad("must be a hex color like #1a2b3c");
|
|
68
|
+
return ok(s);
|
|
69
|
+
}
|
|
70
|
+
case "number": {
|
|
71
|
+
const n = typeof v === "number" ? v : read && typeof v === "string" && v.trim() !== "" ? Number(v) : read && typeof v === "boolean" ? Number(v) : NaN;
|
|
72
|
+
return Number.isFinite(n) ? ok(n) : read ? ok(null) : bad("must be a number");
|
|
73
|
+
}
|
|
74
|
+
case "boolean": {
|
|
75
|
+
if (typeof v === "boolean")
|
|
76
|
+
return ok(v);
|
|
77
|
+
if (read && (v === "true" || v === 1))
|
|
78
|
+
return ok(true);
|
|
79
|
+
if (read && (v === "false" || v === 0))
|
|
80
|
+
return ok(false);
|
|
81
|
+
return read ? ok(null) : bad("must be true or false");
|
|
82
|
+
}
|
|
83
|
+
case "dateTime": {
|
|
84
|
+
const d = typeof v === "string" || (typeof v === "number" && read) ? new Date(v) : null;
|
|
85
|
+
return d && !Number.isNaN(d.getTime()) ? ok(d.toISOString()) : read ? ok(null) : bad("must be an ISO 8601 date and time");
|
|
86
|
+
}
|
|
87
|
+
case "enum": {
|
|
88
|
+
const opts = optionsOf(f);
|
|
89
|
+
const many = f.def.allowMultiple === true;
|
|
90
|
+
const list = Array.isArray(v) ? v : [v];
|
|
91
|
+
if (!list.every((x) => typeof x === "string"))
|
|
92
|
+
return read ? ok(null) : bad("must be one of its options");
|
|
93
|
+
const wrong = list.filter((x) => !opts.includes(x));
|
|
94
|
+
if (wrong.length && !read)
|
|
95
|
+
return bad(`${JSON.stringify(wrong[0])} isn't one of ${opts.map((o) => JSON.stringify(o)).join(", ") || "its options (it has none)"}`);
|
|
96
|
+
const kept = list.filter((x) => opts.includes(x));
|
|
97
|
+
if (many)
|
|
98
|
+
return ok(kept);
|
|
99
|
+
if (Array.isArray(v) && !read)
|
|
100
|
+
return bad("takes one option (allowMultiple is off)");
|
|
101
|
+
return ok(kept[0] ?? null);
|
|
102
|
+
}
|
|
103
|
+
case "roleList":
|
|
104
|
+
return Array.isArray(v) && v.every((x) => typeof x === "string") ? ok(v) : read ? ok(null) : bad("must be a list of role names");
|
|
105
|
+
case "json":
|
|
106
|
+
return ok(v);
|
|
107
|
+
case "belongsTo": {
|
|
108
|
+
const id = typeof v === "string" ? v : isObj(v) && typeof v._link === "string" ? v._link : null;
|
|
109
|
+
return id ? ok(id) : read ? ok(null) : bad("must be the id of a record");
|
|
110
|
+
}
|
|
111
|
+
case "password":
|
|
112
|
+
return typeof v === "string" ? ok(v) : read ? ok(null) : bad("must be text");
|
|
113
|
+
case "vector": {
|
|
114
|
+
if (!Array.isArray(v) || !v.length || !v.every((x) => typeof x === "number" && Number.isFinite(x)))
|
|
115
|
+
return read ? ok(null) : bad("must be a list of numbers");
|
|
116
|
+
if (v.length > 4000)
|
|
117
|
+
return bad("can have at most 4,000 dimensions");
|
|
118
|
+
const dims = typeof f.def.dimensions === "number" ? f.def.dimensions : null;
|
|
119
|
+
if (dims && v.length !== dims && !read)
|
|
120
|
+
return bad(`must have ${dims} dimensions, not ${v.length}`);
|
|
121
|
+
return ok(v);
|
|
122
|
+
}
|
|
123
|
+
default:
|
|
124
|
+
return read ? ok(null) : bad(`${f.type} fields can't be written yet`);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
function defaultOf(f) {
|
|
128
|
+
if (f.def.default === undefined)
|
|
129
|
+
return null;
|
|
130
|
+
const c = convert(f, f.def.default, "read");
|
|
131
|
+
return c.ok ? c.value : null;
|
|
132
|
+
}
|
|
133
|
+
/** Postgres's jsonb `@>`: objects contain each key's value, arrays contain each element, anything else is equal. */
|
|
134
|
+
function contains(a, b) {
|
|
135
|
+
if (Array.isArray(b))
|
|
136
|
+
return Array.isArray(a) && b.every((x) => a.some((y) => contains(y, x)));
|
|
137
|
+
if (isObj(b))
|
|
138
|
+
return isObj(a) && Object.entries(b).every(([k, v]) => k in a && contains(a[k], v));
|
|
139
|
+
return a === b;
|
|
140
|
+
}
|
|
141
|
+
const sameJson = (a, b) => JSON.stringify(a) === JSON.stringify(b);
|
|
142
|
+
/** `data->>key`: text, or null. */
|
|
143
|
+
const asText = (v) => (v === null || v === undefined ? null : typeof v === "string" ? v : JSON.stringify(v));
|
|
144
|
+
const DEFAULT_EMBEDDING_MODEL = "openai/text-embedding-3-small";
|
|
145
|
+
class Records {
|
|
146
|
+
models;
|
|
147
|
+
now;
|
|
148
|
+
embed;
|
|
149
|
+
rows = new Map();
|
|
150
|
+
seq = 0;
|
|
151
|
+
last = 0;
|
|
152
|
+
constructor(models, now, embed) {
|
|
153
|
+
this.models = models;
|
|
154
|
+
this.now = now;
|
|
155
|
+
this.embed = embed;
|
|
156
|
+
}
|
|
157
|
+
model(name) {
|
|
158
|
+
const m = this.models.get(name);
|
|
159
|
+
if (!m)
|
|
160
|
+
throw notFound(`There's no model named ${name}.`);
|
|
161
|
+
return m;
|
|
162
|
+
}
|
|
163
|
+
table(m) {
|
|
164
|
+
if (!this.rows.has(m.name))
|
|
165
|
+
this.rows.set(m.name, new Map());
|
|
166
|
+
return this.rows.get(m.name);
|
|
167
|
+
}
|
|
168
|
+
clear() { this.rows.clear(); }
|
|
169
|
+
present(m, row, vectors) {
|
|
170
|
+
const out = { id: row.id, createdAt: row.createdAt, updatedAt: row.updatedAt };
|
|
171
|
+
for (const f of m.fields) {
|
|
172
|
+
if (f.type === "password")
|
|
173
|
+
continue;
|
|
174
|
+
if (f.type === "vector") {
|
|
175
|
+
const v = row.vectors.get(f.key);
|
|
176
|
+
if (v && vectors)
|
|
177
|
+
out[f.name] = [...v];
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
180
|
+
if (!STORED.has(f.type))
|
|
181
|
+
continue;
|
|
182
|
+
const c = convert(f, Object.prototype.hasOwnProperty.call(row.data, f.key) ? row.data[f.key] : defaultOf(f), "read");
|
|
183
|
+
out[f.name] = c.ok ? wire(c.value) : null;
|
|
184
|
+
}
|
|
185
|
+
return out;
|
|
186
|
+
}
|
|
187
|
+
findOne(model, id) {
|
|
188
|
+
const m = this.model(model);
|
|
189
|
+
const row = this.table(m).get(id);
|
|
190
|
+
return row ? this.present(m, row, true) : null;
|
|
191
|
+
}
|
|
192
|
+
/** Every record, oldest first. */
|
|
193
|
+
all(model) {
|
|
194
|
+
const m = this.model(model);
|
|
195
|
+
return [...this.table(m).values()].map((r) => this.present(m, r, true));
|
|
196
|
+
}
|
|
197
|
+
// ---- filters and sorting ----
|
|
198
|
+
target(m, name) {
|
|
199
|
+
if (name === "id" || name === "createdAt" || name === "updatedAt")
|
|
200
|
+
return { column: name };
|
|
201
|
+
const f = m.byName.get(name);
|
|
202
|
+
if (!f)
|
|
203
|
+
throw invalid([{ field: name, message: `${m.name} has no field named ${name}` }]);
|
|
204
|
+
if (!STORED.has(f.type) || f.type === "password" || f.type === "encryptedString")
|
|
205
|
+
throw invalid([{ field: name, message: `${f.type} fields can't be filtered or sorted on` }]);
|
|
206
|
+
return { f };
|
|
207
|
+
}
|
|
208
|
+
/** The stored value as it compares: numbers and booleans as themselves, everything else as text. */
|
|
209
|
+
comparable(row, t) {
|
|
210
|
+
if ("column" in t)
|
|
211
|
+
return row[t.column];
|
|
212
|
+
const v = row.data[t.f.key];
|
|
213
|
+
if (t.f.type === "number")
|
|
214
|
+
return typeof v === "number" ? v : null;
|
|
215
|
+
if (t.f.type === "boolean")
|
|
216
|
+
return typeof v === "boolean" ? v : null;
|
|
217
|
+
return asText(v);
|
|
218
|
+
}
|
|
219
|
+
test(m, row, name, op, arg) {
|
|
220
|
+
const t = this.target(m, name);
|
|
221
|
+
const wrong = (message) => invalid([{ field: name, message }]);
|
|
222
|
+
const cmp = (a, b) => {
|
|
223
|
+
if (a === null || a === undefined || b === null || b === undefined)
|
|
224
|
+
return null;
|
|
225
|
+
const x = typeof a === "boolean" ? Number(a) : a, y = typeof b === "boolean" ? Number(b) : b;
|
|
226
|
+
return x < y ? -1 : x > y ? 1 : 0;
|
|
227
|
+
};
|
|
228
|
+
const compare = (a, b) => {
|
|
229
|
+
const c = cmp(a, b);
|
|
230
|
+
if (c === null)
|
|
231
|
+
return false;
|
|
232
|
+
return op === "lessThan" ? c < 0 : op === "lessThanOrEqual" ? c <= 0 : op === "greaterThan" ? c > 0 : c >= 0;
|
|
233
|
+
};
|
|
234
|
+
if ("column" in t) {
|
|
235
|
+
const col = row[t.column];
|
|
236
|
+
const v = (x) => { if (typeof x !== "string")
|
|
237
|
+
throw wrong(`${op} needs text`); return t.column === "id" ? x : new Date(x).toISOString(); };
|
|
238
|
+
switch (op) {
|
|
239
|
+
case "equals": return col === v(arg);
|
|
240
|
+
case "notEquals": return col !== v(arg);
|
|
241
|
+
case "in":
|
|
242
|
+
case "notIn": {
|
|
243
|
+
if (!Array.isArray(arg))
|
|
244
|
+
throw wrong(`${op} needs a list`);
|
|
245
|
+
const hit = arg.map(v).includes(col);
|
|
246
|
+
return op === "in" ? hit : !hit;
|
|
247
|
+
}
|
|
248
|
+
case "isSet": return arg !== false;
|
|
249
|
+
case "startsWith": return col.startsWith(String(v(arg)));
|
|
250
|
+
default: return compare(col, v(arg));
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
const f = t.f;
|
|
254
|
+
const def = defaultOf(f);
|
|
255
|
+
const has = Object.prototype.hasOwnProperty.call(row.data, f.key);
|
|
256
|
+
const val = (x) => { const c = convert(f, x, "write"); if (!c.ok)
|
|
257
|
+
throw wrong(c.message); return c.value; };
|
|
258
|
+
// A record that never stored the field matches as if it held the default.
|
|
259
|
+
const is = (x) => {
|
|
260
|
+
const value = val(x);
|
|
261
|
+
if (value === null)
|
|
262
|
+
return !has || row.data[f.key] === null;
|
|
263
|
+
return (has && contains(row.data[f.key], value)) || (!has && sameJson(value, def));
|
|
264
|
+
};
|
|
265
|
+
switch (op) {
|
|
266
|
+
case "equals": return is(arg);
|
|
267
|
+
case "notEquals": return !is(arg);
|
|
268
|
+
case "in":
|
|
269
|
+
case "notIn": {
|
|
270
|
+
if (!Array.isArray(arg))
|
|
271
|
+
throw wrong(`${op} needs a list`);
|
|
272
|
+
const hit = arg.some(is);
|
|
273
|
+
return op === "in" ? hit : !hit;
|
|
274
|
+
}
|
|
275
|
+
case "isSet": {
|
|
276
|
+
const set = def === null ? has && row.data[f.key] !== null : true;
|
|
277
|
+
return arg === false ? !set : set;
|
|
278
|
+
}
|
|
279
|
+
case "startsWith": {
|
|
280
|
+
if (typeof arg !== "string")
|
|
281
|
+
throw wrong("startsWith needs text");
|
|
282
|
+
return asText(row.data[f.key])?.startsWith(arg) ?? false;
|
|
283
|
+
}
|
|
284
|
+
default: {
|
|
285
|
+
const value = val(arg);
|
|
286
|
+
return compare(this.comparable(row, t), f.type === "number" || f.type === "boolean" ? value : asText(value));
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
matches(m, row, filter) {
|
|
291
|
+
if (filter === undefined || filter === null)
|
|
292
|
+
return true;
|
|
293
|
+
if (!isObj(filter))
|
|
294
|
+
throw invalid([{ field: "filter", message: "must be an object" }]);
|
|
295
|
+
for (const [k, v] of Object.entries(filter)) {
|
|
296
|
+
if (k === "AND" || k === "OR" || k === "NOT") {
|
|
297
|
+
const list = (Array.isArray(v) ? v : [v]).filter((x) => x !== undefined && x !== null);
|
|
298
|
+
if (!list.length)
|
|
299
|
+
continue;
|
|
300
|
+
const each = list.map((x) => this.matches(m, row, x));
|
|
301
|
+
if (k === "AND" && !each.every(Boolean))
|
|
302
|
+
return false;
|
|
303
|
+
if (k === "OR" && !each.some(Boolean))
|
|
304
|
+
return false;
|
|
305
|
+
if (k === "NOT" && each.some(Boolean))
|
|
306
|
+
return false;
|
|
307
|
+
continue;
|
|
308
|
+
}
|
|
309
|
+
if (!isObj(v))
|
|
310
|
+
throw invalid([{ field: k, message: "needs an operator, like { equals: … }" }]);
|
|
311
|
+
for (const [op, arg] of Object.entries(v)) {
|
|
312
|
+
if (!OPS.includes(op))
|
|
313
|
+
throw invalid([{ field: k, message: `${op} isn't a filter; use ${OPS.join(", ")}` }]);
|
|
314
|
+
if (!this.test(m, row, k, op, arg))
|
|
315
|
+
return false;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
return true;
|
|
319
|
+
}
|
|
320
|
+
sorted(m, rows, sort) {
|
|
321
|
+
const list = (Array.isArray(sort) ? sort : sort ? [sort] : []).flatMap((s) => Object.entries(s));
|
|
322
|
+
const keys = list.map(([name, dir]) => {
|
|
323
|
+
if (dir !== "Ascending" && dir !== "Descending")
|
|
324
|
+
throw invalid([{ field: name, message: "sorts Ascending or Descending" }]);
|
|
325
|
+
return { t: this.target(m, name), desc: dir === "Descending" };
|
|
326
|
+
});
|
|
327
|
+
if (!keys.length)
|
|
328
|
+
keys.push({ t: { column: "createdAt" }, desc: true });
|
|
329
|
+
keys.push({ t: { column: "id" }, desc: true });
|
|
330
|
+
return [...rows].sort((a, b) => {
|
|
331
|
+
for (const k of keys) {
|
|
332
|
+
const x = this.comparable(a, k.t), y = this.comparable(b, k.t);
|
|
333
|
+
if (x === y)
|
|
334
|
+
continue;
|
|
335
|
+
// Nulls last, either way.
|
|
336
|
+
if (x === null)
|
|
337
|
+
return 1;
|
|
338
|
+
if (y === null)
|
|
339
|
+
return -1;
|
|
340
|
+
const c = (typeof x === "boolean" ? Number(x) : x) < (typeof y === "boolean" ? Number(y) : y) ? -1 : 1;
|
|
341
|
+
return k.desc ? -c : c;
|
|
342
|
+
}
|
|
343
|
+
return 0;
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
async findMany(model, o = {}) {
|
|
347
|
+
const m = this.model(model);
|
|
348
|
+
const first = Math.min(Math.max(1, Math.floor(o.first ?? 50)), MAX_PAGE);
|
|
349
|
+
const offset = offsetOf(o.after);
|
|
350
|
+
let rows = [...this.table(m).values()].filter((r) => this.matches(m, r, o.filter));
|
|
351
|
+
let distances = null;
|
|
352
|
+
if (o.search) {
|
|
353
|
+
const f = m.byName.get(o.search.field);
|
|
354
|
+
if (!f || f.type !== "vector")
|
|
355
|
+
throw invalid([{ field: o.search.field, message: `${m.name} has no vector field named ${o.search.field}` }]);
|
|
356
|
+
let near = o.search.near;
|
|
357
|
+
if (typeof near === "string") {
|
|
358
|
+
if (!this.embed)
|
|
359
|
+
throw invalid([{ field: f.name, message: "searching by text needs an API key to embed it with; pass a vector instead" }]);
|
|
360
|
+
[near] = (await this.embed(embeddingModelOf(f), [near]));
|
|
361
|
+
}
|
|
362
|
+
const c = convert(f, near, "read");
|
|
363
|
+
if (!c.ok || !c.value)
|
|
364
|
+
throw invalid([{ field: f.name, message: "near must be text or a list of numbers" }]);
|
|
365
|
+
const q = c.value;
|
|
366
|
+
distances = new Map();
|
|
367
|
+
for (const r of rows) {
|
|
368
|
+
const v = r.vectors.get(f.key);
|
|
369
|
+
if (v && v.length === q.length)
|
|
370
|
+
distances.set(r.id, cosineDistance(v, q));
|
|
371
|
+
}
|
|
372
|
+
rows = rows.filter((r) => distances.has(r.id)).sort((a, b) => distances.get(a.id) - distances.get(b.id) || (a.id < b.id ? -1 : 1));
|
|
373
|
+
}
|
|
374
|
+
else
|
|
375
|
+
rows = this.sorted(m, rows, o.sort);
|
|
376
|
+
const page = rows.slice(offset, offset + first);
|
|
377
|
+
return {
|
|
378
|
+
records: page.map((r) => (distances ? { ...this.present(m, r, false), _distance: distances.get(r.id) } : this.present(m, r, false))),
|
|
379
|
+
hasNextPage: rows.length > offset + first,
|
|
380
|
+
endCursor: page.length ? cursorOf(offset + page.length) : null,
|
|
381
|
+
};
|
|
382
|
+
}
|
|
383
|
+
count(model, filter) {
|
|
384
|
+
const m = this.model(model);
|
|
385
|
+
return [...this.table(m).values()].filter((r) => this.matches(m, r, filter)).length;
|
|
386
|
+
}
|
|
387
|
+
// ---- writing ----
|
|
388
|
+
prepare(m, params, current) {
|
|
389
|
+
if (!isObj(params))
|
|
390
|
+
throw invalid([{ field: "record", message: "must be an object of field values" }]);
|
|
391
|
+
const problems = [];
|
|
392
|
+
const data = { ...(current?.data ?? {}) };
|
|
393
|
+
const vectors = new Map();
|
|
394
|
+
const changed = new Set();
|
|
395
|
+
for (const [name, value] of Object.entries(params)) {
|
|
396
|
+
if (SYSTEM.includes(name)) {
|
|
397
|
+
problems.push({ field: name, message: "is set by Inference Bureau" });
|
|
398
|
+
continue;
|
|
399
|
+
}
|
|
400
|
+
const f = m.byName.get(name);
|
|
401
|
+
if (!f) {
|
|
402
|
+
problems.push({ field: name, message: `${m.name} has no field named ${name}` });
|
|
403
|
+
continue;
|
|
404
|
+
}
|
|
405
|
+
if (!STORED.has(f.type) && f.type !== "vector") {
|
|
406
|
+
problems.push({ field: name, message: f.type === "file" ? "file fields can't be written yet" : `a ${f.type} field isn't stored on this record` });
|
|
407
|
+
continue;
|
|
408
|
+
}
|
|
409
|
+
if (f.type === "vector" && Array.isArray(f.def.source) && f.def.source.length) {
|
|
410
|
+
problems.push({ field: name, message: `is embedded from ${f.def.source.map(String).join(", ")}; it can't be written` });
|
|
411
|
+
continue;
|
|
412
|
+
}
|
|
413
|
+
const c = convert(f, value, "write");
|
|
414
|
+
if (!c.ok) {
|
|
415
|
+
problems.push({ field: name, message: c.message });
|
|
416
|
+
continue;
|
|
417
|
+
}
|
|
418
|
+
changed.add(name);
|
|
419
|
+
if (f.type === "vector") {
|
|
420
|
+
vectors.set(f.key, c.value ?? []);
|
|
421
|
+
continue;
|
|
422
|
+
}
|
|
423
|
+
data[f.key] = wire(c.value);
|
|
424
|
+
}
|
|
425
|
+
if (!current)
|
|
426
|
+
for (const f of m.fields)
|
|
427
|
+
if (STORED.has(f.type) && !(f.key in data) && f.def.default !== undefined)
|
|
428
|
+
data[f.key] = defaultOf(f);
|
|
429
|
+
for (const f of m.fields) {
|
|
430
|
+
if (!STORED.has(f.type))
|
|
431
|
+
continue;
|
|
432
|
+
const v = f.key in data ? data[f.key] : defaultOf(f);
|
|
433
|
+
const val = isObj(f.def.validations) ? f.def.validations : {};
|
|
434
|
+
const empty = v === null || v === undefined || v === "" || (Array.isArray(v) && !v.length);
|
|
435
|
+
if (val.required === true && empty && (changed.has(f.name) || !current)) {
|
|
436
|
+
problems.push({ field: f.name, message: "is required" });
|
|
437
|
+
continue;
|
|
438
|
+
}
|
|
439
|
+
if (empty || !changed.has(f.name))
|
|
440
|
+
continue;
|
|
441
|
+
const range = (r, n, what) => {
|
|
442
|
+
if (!isObj(r))
|
|
443
|
+
return;
|
|
444
|
+
if (typeof r.min === "number" && n < r.min)
|
|
445
|
+
problems.push({ field: f.name, message: `${what} at least ${r.min}` });
|
|
446
|
+
if (typeof r.max === "number" && n > r.max)
|
|
447
|
+
problems.push({ field: f.name, message: `${what} at most ${r.max}` });
|
|
448
|
+
};
|
|
449
|
+
if (f.type === "number")
|
|
450
|
+
range(val.range, v, "must be");
|
|
451
|
+
if (typeof v === "string" && f.type !== "password" && f.type !== "encryptedString") {
|
|
452
|
+
range(val.stringLength, v.length, "must have a length of");
|
|
453
|
+
const pattern = isObj(val.regex) ? val.regex.pattern : undefined;
|
|
454
|
+
if (typeof pattern === "string" && pattern) {
|
|
455
|
+
let re = null;
|
|
456
|
+
try {
|
|
457
|
+
re = new RegExp(pattern);
|
|
458
|
+
}
|
|
459
|
+
catch {
|
|
460
|
+
problems.push({ field: f.name, message: "has a pattern that isn't a valid RegExp" });
|
|
461
|
+
}
|
|
462
|
+
if (re && !re.test(v))
|
|
463
|
+
problems.push({ field: f.name, message: `doesn't match ${pattern}` });
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
if (f.type === "belongsTo") {
|
|
467
|
+
const parent = isObj(f.def.parent) ? String(f.def.parent.model ?? "") : "";
|
|
468
|
+
const pm = this.models.get(parent);
|
|
469
|
+
if (!pm) {
|
|
470
|
+
problems.push({ field: f.name, message: `points at ${parent || "no model"}, which doesn't exist` });
|
|
471
|
+
continue;
|
|
472
|
+
}
|
|
473
|
+
if (!this.table(pm).has(v))
|
|
474
|
+
problems.push({ field: f.name, message: `there's no ${pm.name} with id ${v}` });
|
|
475
|
+
}
|
|
476
|
+
if (val.unique === true || isObj(val.unique)) {
|
|
477
|
+
const dup = [...this.table(m).values()].some((r) => r.id !== current?.id && contains(r.data, { [f.key]: v }));
|
|
478
|
+
if (dup)
|
|
479
|
+
problems.push({ field: f.name, message: "is already taken by another record" });
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
if (problems.length)
|
|
483
|
+
throw invalid(problems);
|
|
484
|
+
if (JSON.stringify(data).length > MAX_RECORD_BYTES)
|
|
485
|
+
throw invalid([{ field: "record", message: `is larger than ${MAX_RECORD_BYTES / 1024} KiB` }]);
|
|
486
|
+
return { data, vectors, changed };
|
|
487
|
+
}
|
|
488
|
+
/** Given vectors, then sourced ones whose text changed: embedded with `embed` when the test app has one, else dropped. */
|
|
489
|
+
async vectorize(m, row, given, before) {
|
|
490
|
+
for (const [key, vec] of given) {
|
|
491
|
+
if (vec.length)
|
|
492
|
+
row.vectors.set(key, [...vec]);
|
|
493
|
+
else
|
|
494
|
+
row.vectors.delete(key);
|
|
495
|
+
}
|
|
496
|
+
for (const f of m.fields) {
|
|
497
|
+
if (f.type !== "vector" || !Array.isArray(f.def.source) || !f.def.source.length || given.has(f.key))
|
|
498
|
+
continue;
|
|
499
|
+
const text = sourceText(m, f, row.data);
|
|
500
|
+
if (before && text === sourceText(m, f, before) && row.vectors.has(f.key))
|
|
501
|
+
continue;
|
|
502
|
+
row.vectors.delete(f.key);
|
|
503
|
+
if (text && this.embed) {
|
|
504
|
+
const [v] = await this.embed(embeddingModelOf(f), [text]);
|
|
505
|
+
if (v)
|
|
506
|
+
row.vectors.set(f.key, v);
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
stamp() {
|
|
511
|
+
// Strictly later than the last write, so records made in the same millisecond still sort newest first.
|
|
512
|
+
const t = Math.max(this.now(), this.last + 1);
|
|
513
|
+
this.last = t;
|
|
514
|
+
return new Date(t).toISOString();
|
|
515
|
+
}
|
|
516
|
+
async create(model, values) {
|
|
517
|
+
const m = this.model(model);
|
|
518
|
+
const p = this.prepare(m, values, null);
|
|
519
|
+
const at = this.stamp();
|
|
520
|
+
const row = { id: `rec_${String(++this.seq).padStart(6, "0")}`, createdAt: at, updatedAt: at, data: p.data, vectors: new Map() };
|
|
521
|
+
await this.vectorize(m, row, p.vectors, null);
|
|
522
|
+
this.table(m).set(row.id, row);
|
|
523
|
+
return this.present(m, row, true);
|
|
524
|
+
}
|
|
525
|
+
async update(model, id, values) {
|
|
526
|
+
const m = this.model(model);
|
|
527
|
+
const row = this.table(m).get(id);
|
|
528
|
+
if (!row)
|
|
529
|
+
throw notFound(`There's no ${m.name} with id ${id}.`);
|
|
530
|
+
const p = this.prepare(m, values, row);
|
|
531
|
+
const before = row.data;
|
|
532
|
+
row.data = p.data;
|
|
533
|
+
row.updatedAt = this.stamp();
|
|
534
|
+
await this.vectorize(m, row, p.vectors, before);
|
|
535
|
+
return this.present(m, row, true);
|
|
536
|
+
}
|
|
537
|
+
delete(model, id) {
|
|
538
|
+
const m = this.model(model);
|
|
539
|
+
if (!this.table(m).delete(id))
|
|
540
|
+
throw notFound(`There's no ${m.name} with id ${id}.`);
|
|
541
|
+
}
|
|
542
|
+
checkPassword(model, id, password, field = "password") {
|
|
543
|
+
const m = this.model(model);
|
|
544
|
+
const f = m.byName.get(field);
|
|
545
|
+
if (!f || f.type !== "password")
|
|
546
|
+
throw invalid([{ field, message: `${m.name} has no password field named ${field}` }]);
|
|
547
|
+
const stored = this.table(m).get(id)?.data[f.key];
|
|
548
|
+
return typeof stored === "string" && stored === password;
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
const OPS = ["equals", "notEquals", "in", "notIn", "lessThan", "lessThanOrEqual", "greaterThan", "greaterThanOrEqual", "isSet", "startsWith"];
|
|
552
|
+
const embeddingModelOf = (f) => (typeof f.def.embeddingModel === "string" && f.def.embeddingModel ? f.def.embeddingModel : DEFAULT_EMBEDDING_MODEL);
|
|
553
|
+
function sourceText(m, f, data) {
|
|
554
|
+
const names = Array.isArray(f.def.source) ? f.def.source.map(String) : [];
|
|
555
|
+
return names.map((n) => m.byName.get(n)).filter((s) => !!s && STORED.has(s.type) && s.type !== "password" && s.type !== "encryptedString")
|
|
556
|
+
.map((s) => { const v = data[s.key]; return v === null || v === undefined ? "" : typeof v === "string" ? v : JSON.stringify(v); })
|
|
557
|
+
.filter(Boolean).join("\n");
|
|
558
|
+
}
|
|
559
|
+
function cosineDistance(a, b) {
|
|
560
|
+
let dot = 0, na = 0, nb = 0;
|
|
561
|
+
for (let i = 0; i < a.length; i++) {
|
|
562
|
+
dot += a[i] * b[i];
|
|
563
|
+
na += a[i] ** 2;
|
|
564
|
+
nb += b[i] ** 2;
|
|
565
|
+
}
|
|
566
|
+
return na && nb ? 1 - dot / Math.sqrt(na * nb) : 1;
|
|
567
|
+
}
|
|
568
|
+
const base64url = (s) => btoa(s).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
569
|
+
const cursorOf = (offset) => base64url(`o:${offset}`);
|
|
570
|
+
function offsetOf(cursor) {
|
|
571
|
+
if (!cursor)
|
|
572
|
+
return 0;
|
|
573
|
+
let text = "";
|
|
574
|
+
try {
|
|
575
|
+
text = atob(cursor.replace(/-/g, "+").replace(/_/g, "/"));
|
|
576
|
+
}
|
|
577
|
+
catch { /* reported below */ }
|
|
578
|
+
const m = /^o:(\d+)$/.exec(text);
|
|
579
|
+
if (!m)
|
|
580
|
+
throw invalid([{ field: "after", message: "isn't a cursor from this API" }]);
|
|
581
|
+
return Number(m[1]);
|
|
582
|
+
}
|
|
583
|
+
// ---- Shopify ----
|
|
584
|
+
/** The store a test app installs on and runs as, unless a test names another. */
|
|
585
|
+
export const TEST_SHOP = "test-shop.myshopify.com";
|
|
586
|
+
function shopifyMock() {
|
|
587
|
+
const handlers = [];
|
|
588
|
+
const mock = {
|
|
589
|
+
calls: [],
|
|
590
|
+
on(match, answer) { handlers.unshift({ match, answer }); return mock; },
|
|
591
|
+
client(shop) {
|
|
592
|
+
return {
|
|
593
|
+
shop,
|
|
594
|
+
async graphql(query, variables = {}) {
|
|
595
|
+
const call = { shop, query, variables: wire(variables) };
|
|
596
|
+
mock.calls.push(call);
|
|
597
|
+
const h = handlers.find((x) => (typeof x.match === "string" ? query.includes(x.match) : x.match.test(query)));
|
|
598
|
+
if (!h)
|
|
599
|
+
throw new ShopifyError(`No mock answers this Shopify query; add one with app.shopify.on(…): ${query.replace(/\s+/g, " ").trim().slice(0, 120)}`, []);
|
|
600
|
+
return wire(typeof h.answer === "function" ? await h.answer(call.variables, call) : h.answer);
|
|
601
|
+
},
|
|
602
|
+
};
|
|
603
|
+
},
|
|
604
|
+
};
|
|
605
|
+
return mock;
|
|
606
|
+
}
|
|
607
|
+
let fixtureSeq = 0;
|
|
608
|
+
const nextId = () => 1000 + ++fixtureSeq;
|
|
609
|
+
const iso = (t = Date.now()) => new Date(t).toISOString();
|
|
610
|
+
/**
|
|
611
|
+
* Shopify's webhook payloads (REST-shaped, as deliveries arrive), filled with believable values. Pass what the test
|
|
612
|
+
* cares about; the rest is made up. Ids are unique within the test run.
|
|
613
|
+
*/
|
|
614
|
+
export const fixtures = {
|
|
615
|
+
shop(o = {}) {
|
|
616
|
+
const domain = typeof o.myshopify_domain === "string" ? o.myshopify_domain : TEST_SHOP;
|
|
617
|
+
return { id: nextId(), name: "Test Shop", email: "owner@example.com", domain, myshopify_domain: domain, currency: "USD", plan_display_name: "Basic", country_code: "US", timezone: "America/New_York", ...o };
|
|
618
|
+
},
|
|
619
|
+
variant(o = {}) {
|
|
620
|
+
const id = typeof o.id === "number" ? o.id : nextId();
|
|
621
|
+
return { id, admin_graphql_api_id: `gid://shopify/ProductVariant/${id}`, title: "Default Title", sku: `SKU-${id}`, price: "19.00", compare_at_price: null, inventory_quantity: 10, position: 1, updated_at: iso(), ...o };
|
|
622
|
+
},
|
|
623
|
+
product(o = {}) {
|
|
624
|
+
const id = typeof o.id === "number" ? o.id : nextId();
|
|
625
|
+
const title = typeof o.title === "string" ? o.title : `Product ${id}`;
|
|
626
|
+
return {
|
|
627
|
+
id, admin_graphql_api_id: `gid://shopify/Product/${id}`, title, handle: title.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, ""), status: "active",
|
|
628
|
+
vendor: "Test Vendor", product_type: "", tags: "", body_html: "", published_at: iso(), created_at: iso(), updated_at: iso(), variants: [fixtures.variant()], ...o,
|
|
629
|
+
};
|
|
630
|
+
},
|
|
631
|
+
collection(o = {}) {
|
|
632
|
+
const id = typeof o.id === "number" ? o.id : nextId();
|
|
633
|
+
return { id, admin_graphql_api_id: `gid://shopify/Collection/${id}`, title: `Collection ${id}`, handle: `collection-${id}`, body_html: "", sort_order: "manual", updated_at: iso(), ...o };
|
|
634
|
+
},
|
|
635
|
+
customer(o = {}) {
|
|
636
|
+
const id = typeof o.id === "number" ? o.id : nextId();
|
|
637
|
+
return { id, admin_graphql_api_id: `gid://shopify/Customer/${id}`, email: `customer${id}@example.com`, first_name: "Test", last_name: "Customer", phone: null, state: "enabled", created_at: iso(), updated_at: iso(), ...o };
|
|
638
|
+
},
|
|
639
|
+
order(o = {}) {
|
|
640
|
+
const id = typeof o.id === "number" ? o.id : nextId();
|
|
641
|
+
const customer = fixtures.customer();
|
|
642
|
+
return {
|
|
643
|
+
id, admin_graphql_api_id: `gid://shopify/Order/${id}`, name: `#${id}`, order_number: id, email: customer.email, currency: "USD", total_price: "19.00", subtotal_price: "19.00", total_tax: "0.00",
|
|
644
|
+
financial_status: "paid", fulfillment_status: null, customer,
|
|
645
|
+
line_items: [{ id: nextId(), product_id: nextId(), variant_id: nextId(), title: "Product", quantity: 1, price: "19.00", sku: "SKU" }],
|
|
646
|
+
created_at: iso(), updated_at: iso(), ...o,
|
|
647
|
+
};
|
|
648
|
+
},
|
|
649
|
+
/** customers/redact, customers/data_request and shop/redact, for the shopify* privacy actions. */
|
|
650
|
+
customersRedact(o = {}) {
|
|
651
|
+
return { shop_id: 1, shop_domain: TEST_SHOP, customer: { id: nextId(), email: "customer@example.com", phone: null }, orders_to_redact: [], ...o };
|
|
652
|
+
},
|
|
653
|
+
customersDataRequest(o = {}) {
|
|
654
|
+
return { shop_id: 1, shop_domain: TEST_SHOP, customer: { id: nextId(), email: "customer@example.com", phone: null }, orders_requested: [], data_request: { id: nextId() }, ...o };
|
|
655
|
+
},
|
|
656
|
+
shopRedact(o = {}) {
|
|
657
|
+
return { shop_id: 1, shop_domain: TEST_SHOP, ...o };
|
|
658
|
+
},
|
|
659
|
+
};
|
|
660
|
+
const gid = (type, id, given) => (typeof given === "string" && given.startsWith("gid://") ? given : typeof id === "string" && id.startsWith("gid://") ? id : `gid://shopify/${type}/${String(id)}`);
|
|
661
|
+
const num = (v) => (v === null || v === undefined || v === "" ? null : Number.isFinite(Number(v)) ? Number(v) : null);
|
|
662
|
+
const str = (v) => (v === null || v === undefined ? null : String(v));
|
|
663
|
+
const tagList = (v) => (Array.isArray(v) ? v.map(String) : typeof v === "string" ? v.split(",").map((t) => t.trim()).filter(Boolean) : []);
|
|
664
|
+
const SYNC_TOPIC = {
|
|
665
|
+
products: { model: "shopifyProduct", type: "Product" }, collections: { model: "shopifyCollection", type: "Collection" }, metaobjects: { model: "shopifyMetaobject", type: "Metaobject" },
|
|
666
|
+
};
|
|
667
|
+
const PRIVACY_TOPIC = { "customers/redact": "shopifyCustomersRedact", "customers/data_request": "shopifyCustomersDataRequest", "shop/redact": "shopifyShopRedact" };
|
|
668
|
+
function syncItem(model, o, shop) {
|
|
669
|
+
const updatedAt = str(o.updated_at ?? o.updatedAt);
|
|
670
|
+
if (model === "shopifyProduct") {
|
|
671
|
+
const id = gid("Product", o.id, o.admin_graphql_api_id);
|
|
672
|
+
return {
|
|
673
|
+
model, shopifyId: id, updatedAt,
|
|
674
|
+
values: { sh_id: id, sh_shop: shop, sh_title: str(o.title), sh_handle: str(o.handle), sh_status: str(o.status)?.toLowerCase() ?? null, sh_vendor: str(o.vendor),
|
|
675
|
+
sh_product_type: str(o.product_type), sh_tags: tagList(o.tags), sh_body_html: str(o.body_html), sh_published_at: str(o.published_at), sh_created_at: str(o.created_at), sh_updated_at: updatedAt },
|
|
676
|
+
...(Array.isArray(o.variants) ? { children: o.variants.map((v) => {
|
|
677
|
+
const vid = gid("ProductVariant", v.id, v.admin_graphql_api_id), at = str(v.updated_at);
|
|
678
|
+
return { model: "shopifyProductVariant", shopifyId: vid, updatedAt: at, values: { sh_id: vid, sh_shop: shop, sh_title: str(v.title), sh_sku: str(v.sku), sh_price: num(v.price), sh_compare_at_price: num(v.compare_at_price), sh_inventory_quantity: num(v.inventory_quantity), sh_position: num(v.position), sh_updated_at: at } };
|
|
679
|
+
}) } : {}),
|
|
680
|
+
};
|
|
681
|
+
}
|
|
682
|
+
if (model === "shopifyCollection") {
|
|
683
|
+
const id = gid("Collection", o.id, o.admin_graphql_api_id);
|
|
684
|
+
return { model, shopifyId: id, updatedAt, values: { sh_id: id, sh_shop: shop, sh_title: str(o.title), sh_handle: str(o.handle), sh_body_html: str(o.body_html), sh_sort_order: str(o.sort_order)?.toLowerCase() ?? null, sh_updated_at: updatedAt } };
|
|
685
|
+
}
|
|
686
|
+
const id = gid("Metaobject", o.id, o.admin_graphql_api_id);
|
|
687
|
+
const fields = Array.isArray(o.fields) ? Object.fromEntries(o.fields.map((x) => [x.key, x.value])) : isObj(o.fields) ? o.fields : {};
|
|
688
|
+
return { model, shopifyId: id, updatedAt, values: { sh_id: id, sh_shop: shop, sh_type: str(o.type), sh_handle: str(o.handle), sh_display_name: str(o.display_name), sh_fields: fields, sh_updated_at: updatedAt } };
|
|
689
|
+
}
|
|
690
|
+
/** An action's `run` threw: nothing it asked for was applied. The API answers the same 422. */
|
|
691
|
+
export class ActionFailedError extends ApiError {
|
|
692
|
+
logs;
|
|
693
|
+
constructor(message, logs, actionStack) {
|
|
694
|
+
super(message, 422, "action_failed");
|
|
695
|
+
this.logs = logs;
|
|
696
|
+
// Where the action threw, rather than where the test app noticed.
|
|
697
|
+
if (actionStack)
|
|
698
|
+
this.stack = `${message}\n${actionStack.split("\n").slice(1).join("\n")}`;
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
const PRIORITY = { high: 0, default: 1, low: 2 };
|
|
702
|
+
export function createTestApp(o = {}) {
|
|
703
|
+
const models = new Map(Object.entries(o.models ?? {}).map(([name, s]) => [name, modelOf(name, s)]));
|
|
704
|
+
const store = new Records(models, o.now ?? Date.now, o.embed);
|
|
705
|
+
const registry = Object.fromEntries(Object.entries(o.actions ?? {}).map(([k, m]) => [k, typeof m === "function" ? m : async () => m]));
|
|
706
|
+
const environment = o.environment ?? "development";
|
|
707
|
+
const shopify = shopifyMock();
|
|
708
|
+
const jobs = [];
|
|
709
|
+
const logs = [];
|
|
710
|
+
let jobSeq = 0;
|
|
711
|
+
async function actionOf(model, action) {
|
|
712
|
+
const load = registry[model ? `${model}/${action}` : `$global/${action}`];
|
|
713
|
+
return load ? load() : null;
|
|
714
|
+
}
|
|
715
|
+
async function run(model, action, input = {}) {
|
|
716
|
+
const name = model ? `${model}.${action}` : action;
|
|
717
|
+
const mod = await actionOf(model, action);
|
|
718
|
+
if (!mod)
|
|
719
|
+
throw notFound(model ? `${model} has no action named ${action}.` : `There's no global action named ${action}.`);
|
|
720
|
+
const type = !model ? "custom" : mod.options?.actionType ?? (action === "create" || action === "update" || action === "delete" ? action : "custom");
|
|
721
|
+
let record = null;
|
|
722
|
+
if (model) {
|
|
723
|
+
store.model(model);
|
|
724
|
+
if (type === "create")
|
|
725
|
+
record = {};
|
|
726
|
+
else {
|
|
727
|
+
if (!input.id)
|
|
728
|
+
throw new ApiError(`${name} works on a record: pass its id.`, 400, "invalid_request");
|
|
729
|
+
record = store.findOne(model, input.id);
|
|
730
|
+
if (!record)
|
|
731
|
+
throw notFound(`There's no ${model} with id ${input.id}.`);
|
|
732
|
+
}
|
|
733
|
+
}
|
|
734
|
+
const params = wire(input.params ?? {});
|
|
735
|
+
const shop = input.shop ?? null;
|
|
736
|
+
const phase = (p, rec, isNew) => runPhase(wire({ phase: p, model, action, params, record: rec, isNew, environment, shop }), registry, { api, shopify: shopify.client, echo: o.echo ?? false });
|
|
737
|
+
const keep = (lines) => { for (const l of lines)
|
|
738
|
+
logs.push({ ...l, action: name }); };
|
|
739
|
+
const reply = await phase("run", record, type === "create");
|
|
740
|
+
keep(reply.logs);
|
|
741
|
+
if (!reply.ok)
|
|
742
|
+
throw new ActionFailedError(`${name} failed: ${reply.error.message}`, reply.logs, reply.error.stack);
|
|
743
|
+
let saved = record;
|
|
744
|
+
if (model && reply.write) {
|
|
745
|
+
if (reply.write.op === "delete") {
|
|
746
|
+
if (input.id)
|
|
747
|
+
store.delete(model, input.id);
|
|
748
|
+
saved = null;
|
|
749
|
+
}
|
|
750
|
+
else
|
|
751
|
+
saved = type === "create" ? await store.create(model, reply.write.values) : await store.update(model, input.id, reply.write.values);
|
|
752
|
+
}
|
|
753
|
+
const after = await phase("onSuccess", saved ?? record, false);
|
|
754
|
+
keep(after.logs);
|
|
755
|
+
return {
|
|
756
|
+
action: name, record: saved, ...("result" in reply && reply.result !== undefined ? { result: wire(reply.result) } : {}),
|
|
757
|
+
logs: [...reply.logs, ...after.logs], onSuccessError: after.ok ? null : after.error.message,
|
|
758
|
+
};
|
|
759
|
+
}
|
|
760
|
+
const parseName = (action) => {
|
|
761
|
+
const dot = action.indexOf(".");
|
|
762
|
+
return dot < 0 ? { model: null, action } : { model: action.slice(0, dot), action: action.slice(dot + 1) };
|
|
763
|
+
};
|
|
764
|
+
const modelClient = (name) => ({
|
|
765
|
+
findOne: async (id) => store.findOne(name, id),
|
|
766
|
+
findMany: async (q = {}) => store.findMany(name, wire(q)),
|
|
767
|
+
findFirst: async (q = {}) => (await store.findMany(name, { ...wire(q), first: 1 })).records[0] ?? null,
|
|
768
|
+
search: async (field, near, q = {}) => (await store.findMany(name, { ...wire(q), search: { field, near } })),
|
|
769
|
+
// As through the API: the model's create, update or delete action when it has one, else a direct write.
|
|
770
|
+
create: async (values) => (registry[`${name}/create`] ? (await run(name, "create", { params: values })).record : store.create(name, wire(values))),
|
|
771
|
+
update: async (id, values) => (registry[`${name}/update`] ? (await run(name, "update", { id, params: values })).record : store.update(name, id, wire(values))),
|
|
772
|
+
delete: async (id) => { if (registry[`${name}/delete`])
|
|
773
|
+
await run(name, "delete", { id });
|
|
774
|
+
else
|
|
775
|
+
store.delete(name, id); },
|
|
776
|
+
checkPassword: async (id, password, field) => store.checkPassword(name, id, password, field),
|
|
777
|
+
run: async (action, q = {}) => {
|
|
778
|
+
const r = await run(name, action, q);
|
|
779
|
+
return { record: r.record, ...(r.result !== undefined ? { result: r.result } : {}) };
|
|
780
|
+
},
|
|
781
|
+
});
|
|
782
|
+
const clients = new Map();
|
|
783
|
+
const $global = async (action, params = {}) => {
|
|
784
|
+
const r = await run(null, action, { params });
|
|
785
|
+
return r.result !== undefined ? { result: r.result } : {};
|
|
786
|
+
};
|
|
787
|
+
const enqueue = async (action, params = {}, q = {}) => {
|
|
788
|
+
const t = (o.now ?? Date.now)();
|
|
789
|
+
const runAt = q.runAt !== undefined ? new Date(q.runAt) : new Date(t + (q.delay ?? 0));
|
|
790
|
+
const job = {
|
|
791
|
+
id: `job_${String(++jobSeq).padStart(6, "0")}`, action, state: "queued", run_at: runAt.toISOString(), attempts: 0, max_attempts: Math.min(20, q.retries ?? 6) + 1, last_error: null,
|
|
792
|
+
params: wire(params), record_id: q.id ?? null, shop: q.shop ?? null, priority: q.priority ?? "default",
|
|
793
|
+
};
|
|
794
|
+
jobs.push(job);
|
|
795
|
+
return wire(job);
|
|
796
|
+
};
|
|
797
|
+
const api = new Proxy({}, {
|
|
798
|
+
get(_t, prop) {
|
|
799
|
+
if (prop === "$global")
|
|
800
|
+
return $global;
|
|
801
|
+
if (prop === "enqueue")
|
|
802
|
+
return enqueue;
|
|
803
|
+
if (typeof prop !== "string" || prop === "then")
|
|
804
|
+
return undefined;
|
|
805
|
+
if (!clients.has(prop))
|
|
806
|
+
clients.set(prop, modelClient(prop));
|
|
807
|
+
return clients.get(prop);
|
|
808
|
+
},
|
|
809
|
+
});
|
|
810
|
+
// ---- Shopify's side ----
|
|
811
|
+
const shopModel = () => models.get("shopifyShop");
|
|
812
|
+
const shopRecord = async (shop) => (shopModel()?.byName.has("myshopifyDomain") ? (await store.findMany("shopifyShop", { filter: { myshopifyDomain: { equals: shop } }, first: 1 })).records[0] ?? null : null);
|
|
813
|
+
async function shopHook(shop, kind, values) {
|
|
814
|
+
const m = shopModel();
|
|
815
|
+
if (!m)
|
|
816
|
+
return null;
|
|
817
|
+
const params = Object.fromEntries(Object.entries(values).filter(([k]) => m.byName.has(k)));
|
|
818
|
+
const existing = await shopRecord(shop);
|
|
819
|
+
if (kind === "uninstall" && !existing)
|
|
820
|
+
return null;
|
|
821
|
+
const name = kind === "install" ? (existing ? "reinstall" : "install") : "uninstall";
|
|
822
|
+
if (registry[`shopifyShop/${name}`])
|
|
823
|
+
return (await run("shopifyShop", name, { ...(existing ? { id: existing.id } : {}), params, shop })).record;
|
|
824
|
+
return existing ? store.update("shopifyShop", existing.id, params) : store.create("shopifyShop", params);
|
|
825
|
+
}
|
|
826
|
+
async function writeSynced(item, shop, runs, parentId) {
|
|
827
|
+
const m = models.get(item.model);
|
|
828
|
+
if (!m)
|
|
829
|
+
return null;
|
|
830
|
+
const names = new Map(m.fields.map((f) => [f.key, f.name]));
|
|
831
|
+
const idName = names.get("sh_id"), updName = names.get("sh_updated_at");
|
|
832
|
+
if (!idName)
|
|
833
|
+
throw new Error(`${item.model} has no shopifyId field (storageKey sh_id)`);
|
|
834
|
+
const params = {};
|
|
835
|
+
for (const [key, value] of Object.entries(item.values)) {
|
|
836
|
+
const n = names.get(key);
|
|
837
|
+
if (n)
|
|
838
|
+
params[n] = value;
|
|
839
|
+
}
|
|
840
|
+
if (item.model === "shopifyProductVariant" && parentId) {
|
|
841
|
+
const n = names.get("sh_product");
|
|
842
|
+
if (n)
|
|
843
|
+
params[n] = parentId;
|
|
844
|
+
}
|
|
845
|
+
const existing = (await store.findMany(item.model, { filter: { [idName]: { equals: item.shopifyId } }, first: 1 })).records[0] ?? null;
|
|
846
|
+
let id = existing?.id ?? null;
|
|
847
|
+
const stored = existing && updName && typeof existing[updName] === "string" ? existing[updName] : null;
|
|
848
|
+
// A delivery older than what's stored is skipped, as Shopify retries can arrive out of order.
|
|
849
|
+
if (!existing || !stored || !item.updatedAt || Date.parse(item.updatedAt) > Date.parse(stored)) {
|
|
850
|
+
const action = existing ? "update" : "create";
|
|
851
|
+
if (registry[`${item.model}/${action}`]) {
|
|
852
|
+
const r = await run(item.model, action, { ...(id ? { id } : {}), params, shop });
|
|
853
|
+
runs.push(r);
|
|
854
|
+
id = r.record?.id ?? id;
|
|
855
|
+
}
|
|
856
|
+
else
|
|
857
|
+
id = (existing ? await store.update(item.model, existing.id, params) : await store.create(item.model, params)).id;
|
|
858
|
+
}
|
|
859
|
+
if (item.children && id && models.has("shopifyProductVariant")) {
|
|
860
|
+
const keep = new Set();
|
|
861
|
+
for (const child of item.children) {
|
|
862
|
+
keep.add(child.shopifyId);
|
|
863
|
+
await writeSynced(child, shop, runs, id);
|
|
864
|
+
}
|
|
865
|
+
await removeVariants(id, keep, shop, runs);
|
|
866
|
+
}
|
|
867
|
+
return id;
|
|
868
|
+
}
|
|
869
|
+
async function removeSynced(model, id, shop, runs) {
|
|
870
|
+
if (registry[`${model}/delete`])
|
|
871
|
+
runs.push(await run(model, "delete", { id, shop }));
|
|
872
|
+
else
|
|
873
|
+
store.delete(model, id);
|
|
874
|
+
}
|
|
875
|
+
async function removeVariants(productId, keep, shop, runs) {
|
|
876
|
+
const m = models.get("shopifyProductVariant");
|
|
877
|
+
const link = m.fields.find((f) => f.key === "sh_product")?.name, idName = m.fields.find((f) => f.key === "sh_id")?.name;
|
|
878
|
+
if (!link || !idName)
|
|
879
|
+
return;
|
|
880
|
+
for (const r of store.all("shopifyProductVariant"))
|
|
881
|
+
if (r[link] === productId && !keep.has(String(r[idName])))
|
|
882
|
+
await removeSynced("shopifyProductVariant", r.id, shop, runs);
|
|
883
|
+
}
|
|
884
|
+
async function webhook(topic, payload, w = {}) {
|
|
885
|
+
const shop = w.shop ?? TEST_SHOP;
|
|
886
|
+
const runs = [];
|
|
887
|
+
let handled = false;
|
|
888
|
+
const sync = SYNC_TOPIC[topic.split("/")[0] ?? ""];
|
|
889
|
+
if (sync && models.has(sync.model)) {
|
|
890
|
+
handled = true;
|
|
891
|
+
if (topic.endsWith("/delete")) {
|
|
892
|
+
const idName = models.get(sync.model).fields.find((f) => f.key === "sh_id")?.name;
|
|
893
|
+
const local = idName ? (await store.findMany(sync.model, { filter: { [idName]: { equals: gid(sync.type, payload.id, payload.admin_graphql_api_id) } }, first: 1 })).records[0] : undefined;
|
|
894
|
+
if (local) {
|
|
895
|
+
if (sync.model === "shopifyProduct" && models.has("shopifyProductVariant"))
|
|
896
|
+
await removeVariants(local.id, new Set(), shop, runs);
|
|
897
|
+
await removeSynced(sync.model, local.id, shop, runs);
|
|
898
|
+
}
|
|
899
|
+
}
|
|
900
|
+
else
|
|
901
|
+
await writeSynced(syncItem(sync.model, wire(payload), shop), shop, runs);
|
|
902
|
+
}
|
|
903
|
+
if (topic === "shop/update" && shopModel()?.byName.has("myshopifyDomain")) {
|
|
904
|
+
handled = true;
|
|
905
|
+
const m = shopModel();
|
|
906
|
+
const names = new Map(m.fields.map((f) => [f.key, f.name]));
|
|
907
|
+
const values = { sh_shop_name: str(payload.name), sh_shop_email: str(payload.email), sh_shop_currency: str(payload.currency), sh_shop_plan: str(payload.plan_display_name) };
|
|
908
|
+
const params = Object.fromEntries(Object.entries(values).flatMap(([k, v]) => (names.get(k) ? [[names.get(k), v]] : [])));
|
|
909
|
+
const rec = await shopRecord(shop);
|
|
910
|
+
if (rec && Object.keys(params).length)
|
|
911
|
+
await store.update("shopifyShop", rec.id, params);
|
|
912
|
+
}
|
|
913
|
+
const privacy = PRIVACY_TOPIC[topic];
|
|
914
|
+
if (privacy) {
|
|
915
|
+
handled = true;
|
|
916
|
+
if (registry[`$global/${privacy}`])
|
|
917
|
+
runs.push(await run(null, privacy, { params: payload, shop }));
|
|
918
|
+
}
|
|
919
|
+
for (const s of o.settings?.webhooks ?? []) {
|
|
920
|
+
if (s.topic !== topic)
|
|
921
|
+
continue;
|
|
922
|
+
handled = true;
|
|
923
|
+
const params = s.includeFields?.length ? Object.fromEntries(s.includeFields.filter((k) => k in payload).map((k) => [k, payload[k]])) : payload;
|
|
924
|
+
runs.push(await run(null, s.action, { params, shop }));
|
|
925
|
+
}
|
|
926
|
+
if (!handled)
|
|
927
|
+
throw new Error(`Nothing handles ${topic}: no synced Shopify model takes it and no webhooks entry in settings.ib.ts names it.`);
|
|
928
|
+
return runs;
|
|
929
|
+
}
|
|
930
|
+
async function runJobs() {
|
|
931
|
+
const ran = [];
|
|
932
|
+
const tried = new Set();
|
|
933
|
+
for (;;) {
|
|
934
|
+
const next = jobs.filter((j) => j.state === "queued" && !tried.has(j.id))
|
|
935
|
+
.sort((a, b) => PRIORITY[a.priority] - PRIORITY[b.priority] || a.run_at.localeCompare(b.run_at) || a.id.localeCompare(b.id))[0];
|
|
936
|
+
if (!next)
|
|
937
|
+
return ran;
|
|
938
|
+
tried.add(next.id);
|
|
939
|
+
next.state = "running";
|
|
940
|
+
next.attempts++;
|
|
941
|
+
const { model, action } = parseName(next.action);
|
|
942
|
+
try {
|
|
943
|
+
await run(model, action, { params: next.params, ...(next.record_id ? { id: next.record_id } : {}), ...(next.shop ? { shop: next.shop } : {}) });
|
|
944
|
+
next.state = "succeeded";
|
|
945
|
+
next.last_error = null;
|
|
946
|
+
}
|
|
947
|
+
catch (e) {
|
|
948
|
+
next.last_error = e.message;
|
|
949
|
+
next.state = next.attempts >= next.max_attempts ? "failed" : "queued";
|
|
950
|
+
}
|
|
951
|
+
ran.push(wire(next));
|
|
952
|
+
}
|
|
953
|
+
}
|
|
954
|
+
return {
|
|
955
|
+
api,
|
|
956
|
+
run: (action, q) => { const n = parseName(action); return run(n.model, n.action, q); },
|
|
957
|
+
seed: (model, values) => store.create(model, wire(values)),
|
|
958
|
+
records: (model) => store.all(model),
|
|
959
|
+
install: (shop = TEST_SHOP, q = {}) => shopHook(shop, "install", { myshopifyDomain: shop, state: "installed", installedAt: iso((o.now ?? Date.now)()), uninstalledAt: null, grantedScopes: q.scopes ?? "" }),
|
|
960
|
+
uninstall: (shop = TEST_SHOP) => shopHook(shop, "uninstall", { state: "uninstalled", uninstalledAt: iso((o.now ?? Date.now)()) }),
|
|
961
|
+
webhook,
|
|
962
|
+
jobs,
|
|
963
|
+
runJobs,
|
|
964
|
+
shopify,
|
|
965
|
+
logs,
|
|
966
|
+
reset() { store.clear(); jobs.length = 0; logs.length = 0; shopify.calls.length = 0; },
|
|
967
|
+
};
|
|
968
|
+
}
|
|
969
|
+
// ---- loading an app's folder ----
|
|
970
|
+
const MODEL_SCHEMA = /^api\/models\/([^/]+)\/schema\.ib\.ts$/;
|
|
971
|
+
const MODEL_ACTION = /^api\/models\/([^/]+)\/actions\/([^/]+)\.(ts|tsx|js|mjs)$/;
|
|
972
|
+
const GLOBAL_ACTION = /^api\/actions\/([^/]+)\.(ts|tsx|js|mjs)$/;
|
|
973
|
+
// Node's modules, imported by name at run time so the rest of the SDK stays free of them.
|
|
974
|
+
const nodeImport = (name) => import(/* @vite-ignore */ name);
|
|
975
|
+
/**
|
|
976
|
+
* A TestApp from an app's folder (the working directory unless named): every model's schema.ib.ts, every action
|
|
977
|
+
* file (loaded when first run, as a deployment does) and settings.ib.ts. Runs under a test runner that loads
|
|
978
|
+
* TypeScript, like Vitest (`ib-apps test`).
|
|
979
|
+
*/
|
|
980
|
+
export async function loadApp(dir, o = {}) {
|
|
981
|
+
const fs = await nodeImport("node:fs");
|
|
982
|
+
const path = await nodeImport("node:path");
|
|
983
|
+
const url = await nodeImport("node:url");
|
|
984
|
+
const root = path.resolve(dir ?? globalThis.process?.cwd() ?? ".");
|
|
985
|
+
const files = [];
|
|
986
|
+
const walk = (rel) => {
|
|
987
|
+
for (const e of fs.readdirSync(path.join(root, rel), { withFileTypes: true })) {
|
|
988
|
+
const p = rel ? `${rel}/${e.name}` : e.name;
|
|
989
|
+
if (e.isDirectory() && e.name !== "node_modules")
|
|
990
|
+
walk(p);
|
|
991
|
+
else if (e.isFile())
|
|
992
|
+
files.push(p);
|
|
993
|
+
}
|
|
994
|
+
};
|
|
995
|
+
if (fs.existsSync(path.join(root, "api")))
|
|
996
|
+
walk("api");
|
|
997
|
+
const load = (rel) => import(/* @vite-ignore */ url.pathToFileURL(path.join(root, rel)).href);
|
|
998
|
+
const models = {};
|
|
999
|
+
const actions = {};
|
|
1000
|
+
for (const f of files.sort()) {
|
|
1001
|
+
const s = MODEL_SCHEMA.exec(f);
|
|
1002
|
+
if (s) {
|
|
1003
|
+
const mod = await load(f);
|
|
1004
|
+
if (!mod.schema)
|
|
1005
|
+
throw new Error(`${f} doesn't export schema.`);
|
|
1006
|
+
models[s[1]] = mod.schema;
|
|
1007
|
+
continue;
|
|
1008
|
+
}
|
|
1009
|
+
const m = MODEL_ACTION.exec(f), g = GLOBAL_ACTION.exec(f);
|
|
1010
|
+
if (m)
|
|
1011
|
+
actions[`${m[1]}/${m[2]}`] = () => load(f);
|
|
1012
|
+
else if (g)
|
|
1013
|
+
actions[`$global/${g[1]}`] = () => load(f);
|
|
1014
|
+
}
|
|
1015
|
+
const settings = fs.existsSync(path.join(root, "settings.ib.ts")) ? (await load("settings.ib.ts")).settings : undefined;
|
|
1016
|
+
return createTestApp({ ...o, models, actions, ...(settings ? { settings } : {}) });
|
|
1017
|
+
}
|