@reopt-ai/brandapp-sdk 3.1.0 → 3.4.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/CHANGELOG.md +147 -0
- package/README.md +39 -14
- package/dist/abort--nOBFN4M.js +50 -0
- package/dist/ai/index.d.ts +2 -314
- package/dist/ai/index.js +3 -7
- package/dist/ai-provider/index.d.ts +79 -163
- package/dist/ai-provider/index.js +752 -671
- package/dist/auth/index.d.ts +27 -27
- package/dist/auth/index.js +41 -33
- package/dist/better-auth/client.d.ts +6 -5
- package/dist/better-auth/client.js +28 -5
- package/dist/better-auth/index.d.ts +19 -17
- package/dist/better-auth/index.js +180 -133
- package/dist/client-B1q5P8rv.js +83 -0
- package/dist/client-B9QHVhdM.js +77 -0
- package/dist/client-BGGd_cpV.d.ts +103 -0
- package/dist/client-BJcy1V1a.js +140 -0
- package/dist/client-Beivhucf.js +55 -0
- package/dist/client-BruP5_nU.d.ts +147 -0
- package/dist/client-D-yNxNfk.js +28 -0
- package/dist/client-D0eBxh8j.d.ts +284 -0
- package/dist/client-D3RHHUUW.d.ts +207 -0
- package/dist/client-D6gs61w8.d.ts +53 -0
- package/dist/client-DBnK7ooQ.js +86 -0
- package/dist/client-DDw5yz_h.d.ts +53 -0
- package/dist/client-DPmoKuR9.js +305 -0
- package/dist/client-DsdakibF.js +503 -0
- package/dist/client-Dxg8_DIH.d.ts +71 -0
- package/dist/client-EbhUIhHq.js +53 -0
- package/dist/client-lk6wBu_0.d.ts +48 -0
- package/dist/client-p49ol9pg.d.ts +124 -0
- package/dist/cms/index.d.ts +52 -108
- package/dist/cms/index.js +105 -84
- package/dist/config-CRytOPPl.d.ts +394 -0
- package/dist/config-Cw0a3-C8.js +487 -0
- package/dist/dev/index.d.ts +2 -509
- package/dist/dev/index.js +2740 -2873
- package/dist/eav/coerce.d.ts +3 -1
- package/dist/eav/coerce.js +72 -46
- package/dist/eav/index.d.ts +72 -100
- package/dist/eav/index.js +158 -125
- package/dist/eav/migrate.d.ts +112 -109
- package/dist/eav/migrate.js +230 -196
- package/dist/eav/schema.d.ts +111 -150
- package/dist/eav/schema.js +624 -603
- package/dist/encoding-Bwfm3iaE.js +21 -0
- package/dist/{errors-DIAS8Rsl.d.ts → errors-CRCcErNC.d.ts} +55 -53
- package/dist/feedback/index.d.ts +2 -132
- package/dist/feedback/index.js +3 -5
- package/dist/files/index.d.ts +14 -14
- package/dist/files/index.js +41 -24
- package/dist/http-client-QKUvvirs.js +386 -0
- package/dist/index-DrVaKfhs.d.ts +456 -0
- package/dist/index-Z6OGcurJ.d.ts +169 -0
- package/dist/index.d.ts +6 -221
- package/dist/index.js +119 -70
- package/dist/logout/index.d.ts +109 -0
- package/dist/logout/index.js +264 -0
- package/dist/plans/index.d.ts +2 -236
- package/dist/plans/index.js +3 -5
- package/dist/push/index.d.ts +2 -79
- package/dist/push/index.js +3 -5
- package/dist/query-BvbCiGWb.js +35 -0
- package/dist/react/hooks.d.ts +97 -95
- package/dist/react/hooks.js +742 -356
- package/dist/sse-parser-FMjxM7sY.js +86 -0
- package/dist/terms/index.d.ts +3 -79
- package/dist/terms/index.js +3 -5
- package/dist/testing/index.d.ts +52 -104
- package/dist/testing/index.js +94 -72
- package/dist/types-B-SMkH6s.d.ts +26 -0
- package/dist/types-BXDcoJHG.d.ts +131 -0
- package/dist/types-BrghN2rJ.d.ts +105 -0
- package/dist/types-DjDRszNj.js +11 -0
- package/dist/webhooks/index.d.ts +77 -47
- package/dist/webhooks/index.js +150 -127
- package/docs/api-reference.md +179 -2
- package/docs/dev-server.md +19 -18
- package/docs/errors.md +72 -15
- package/docs/logout.md +166 -0
- package/package.json +10 -5
- package/dist/chunk-2Q654O63.js +0 -350
- package/dist/chunk-F7BXBTZI.js +0 -54
- package/dist/chunk-IJOXTBIE.js +0 -599
- package/dist/chunk-JWXUVQRC.js +0 -157
- package/dist/chunk-L4DHB36G.js +0 -75
- package/dist/chunk-MRQY45U5.js +0 -465
- package/dist/chunk-N5JVE4WX.js +0 -449
- package/dist/chunk-O3PUWIDJ.js +0 -105
- package/dist/chunk-POLKEOYH.js +0 -63
- package/dist/chunk-R5CCGDUH.js +0 -49
- package/dist/chunk-RBHP7IZF.js +0 -96
- package/dist/chunk-REIKXSMY.js +0 -7
- package/dist/chunk-T7YTITRJ.js +0 -112
- package/dist/chunk-V73GORO3.js +0 -81
- package/dist/chunk-VRNBYSWU.js +0 -32
- package/dist/client-BkcO-0C2.d.ts +0 -216
- package/dist/client-CN5_tNOC.d.ts +0 -147
- package/dist/client-DttyeM_h.d.ts +0 -102
- package/dist/config-CtxyJsDR.d.ts +0 -397
- package/dist/typed-client-BxOVIiMU.d.ts +0 -40
- package/dist/types-6E3ZLL9X.d.ts +0 -24
- package/dist/types-DskYLVb8.d.ts +0 -104
- package/dist/types-ckmDDW3N.d.ts +0 -129
|
@@ -0,0 +1,503 @@
|
|
|
1
|
+
import { t as BaseHttpClient } from "./http-client-QKUvvirs.js";
|
|
2
|
+
import { t as buildQuery } from "./query-BvbCiGWb.js";
|
|
3
|
+
|
|
4
|
+
//#region src/eav/cache.ts
|
|
5
|
+
var SchemaCache = class {
|
|
6
|
+
cache = /* @__PURE__ */ new Map();
|
|
7
|
+
ttl;
|
|
8
|
+
constructor(ttlMs = 300 * 1e3) {
|
|
9
|
+
this.ttl = ttlMs;
|
|
10
|
+
}
|
|
11
|
+
get(key) {
|
|
12
|
+
const entry = this.cache.get(key);
|
|
13
|
+
if (!entry) return void 0;
|
|
14
|
+
if (Date.now() > entry.expiresAt) {
|
|
15
|
+
this.cache.delete(key);
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
return entry.data;
|
|
19
|
+
}
|
|
20
|
+
set(key, data) {
|
|
21
|
+
this.cache.set(key, {
|
|
22
|
+
data,
|
|
23
|
+
expiresAt: Date.now() + this.ttl
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Invalidate cache entries.
|
|
28
|
+
*
|
|
29
|
+
* - No argument: clears the entire cache.
|
|
30
|
+
* - String: prefix match against keys (e.g. `"entity:"` matches all
|
|
31
|
+
* per-entity caches, `"entities"` matches only the top-level list).
|
|
32
|
+
*/
|
|
33
|
+
invalidate(pattern) {
|
|
34
|
+
if (!pattern) {
|
|
35
|
+
this.cache.clear();
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
for (const key of Array.from(this.cache.keys())) if (key === pattern || key.startsWith(pattern)) this.cache.delete(key);
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
//#endregion
|
|
43
|
+
//#region src/eav/client.ts
|
|
44
|
+
function createReoptEavClient(config) {
|
|
45
|
+
const http = new BaseHttpClient(config, `/api/v1/brandapp/${config.brandappId}/eav`);
|
|
46
|
+
let entityNameMap = null;
|
|
47
|
+
let entityMapPromise = null;
|
|
48
|
+
const schemaCache = new SchemaCache();
|
|
49
|
+
function invalidateEntityCaches() {
|
|
50
|
+
entityNameMap = null;
|
|
51
|
+
entityMapPromise = null;
|
|
52
|
+
schemaCache.invalidate("entities");
|
|
53
|
+
schemaCache.invalidate("entity:");
|
|
54
|
+
}
|
|
55
|
+
async function resolveEntityId(entityName) {
|
|
56
|
+
if (!entityNameMap) {
|
|
57
|
+
if (!entityMapPromise) entityMapPromise = client.entities.list().then((entities) => new Map(entities.map((e) => [e.name, e.id]))).catch((err) => {
|
|
58
|
+
entityMapPromise = null;
|
|
59
|
+
throw err;
|
|
60
|
+
});
|
|
61
|
+
entityNameMap = await entityMapPromise;
|
|
62
|
+
}
|
|
63
|
+
const id = entityNameMap.get(entityName);
|
|
64
|
+
if (!id) throw new Error(`ReoptSDK: Entity "${entityName}" not found on server. Run schema sync or check the entity name.`);
|
|
65
|
+
return id;
|
|
66
|
+
}
|
|
67
|
+
const MAX_BULK = 100;
|
|
68
|
+
function assertBulkSize(count, op) {
|
|
69
|
+
if (count > MAX_BULK) throw new Error(`ReoptSDK: ${op} accepts at most ${MAX_BULK} records per call (got ${count}). Split into smaller batches.`);
|
|
70
|
+
if (count === 0) throw new Error(`ReoptSDK: ${op} requires at least one record.`);
|
|
71
|
+
}
|
|
72
|
+
const client = {
|
|
73
|
+
entities: {
|
|
74
|
+
async list() {
|
|
75
|
+
const cached = schemaCache.get("entities");
|
|
76
|
+
if (cached) return cached;
|
|
77
|
+
const res = await http.fetch("GET", "/entities");
|
|
78
|
+
schemaCache.set("entities", res.data);
|
|
79
|
+
return res.data;
|
|
80
|
+
},
|
|
81
|
+
async create(input) {
|
|
82
|
+
const res = await http.fetch("POST", "/entities", input);
|
|
83
|
+
invalidateEntityCaches();
|
|
84
|
+
return res.data;
|
|
85
|
+
},
|
|
86
|
+
async get(entityId) {
|
|
87
|
+
const cacheKey = `entity:${entityId}`;
|
|
88
|
+
const cached = schemaCache.get(cacheKey);
|
|
89
|
+
if (cached) return cached;
|
|
90
|
+
const res = await http.fetch("GET", `/entities/${entityId}`);
|
|
91
|
+
schemaCache.set(cacheKey, res.data);
|
|
92
|
+
return res.data;
|
|
93
|
+
},
|
|
94
|
+
async update(entityId, input) {
|
|
95
|
+
const res = await http.fetch("PUT", `/entities/${entityId}`, input);
|
|
96
|
+
invalidateEntityCaches();
|
|
97
|
+
return res.data;
|
|
98
|
+
},
|
|
99
|
+
async delete(entityId) {
|
|
100
|
+
await http.fetch("DELETE", `/entities/${entityId}`);
|
|
101
|
+
invalidateEntityCaches();
|
|
102
|
+
}
|
|
103
|
+
},
|
|
104
|
+
attributes: {
|
|
105
|
+
async create(entityId, input) {
|
|
106
|
+
const res = await http.fetch("POST", `/entities/${entityId}/attributes`, input);
|
|
107
|
+
schemaCache.invalidate("entity:");
|
|
108
|
+
return res.data;
|
|
109
|
+
},
|
|
110
|
+
async update(entityId, attributeId, input) {
|
|
111
|
+
const res = await http.fetch("PUT", `/entities/${entityId}/attributes/${attributeId}`, input);
|
|
112
|
+
schemaCache.invalidate("entity:");
|
|
113
|
+
return res.data;
|
|
114
|
+
},
|
|
115
|
+
async delete(entityId, attributeId) {
|
|
116
|
+
await http.fetch("DELETE", `/entities/${entityId}/attributes/${attributeId}`);
|
|
117
|
+
schemaCache.invalidate("entity:");
|
|
118
|
+
}
|
|
119
|
+
},
|
|
120
|
+
records: {
|
|
121
|
+
/** List records with pagination, filtering, and sorting.
|
|
122
|
+
* @example
|
|
123
|
+
* ```ts
|
|
124
|
+
* const result = await eav.records.list(entityId, {
|
|
125
|
+
* authUserId: userId,
|
|
126
|
+
* filters: [{ attributeId: "status-attr-id", operator: "eq", value: "active" }],
|
|
127
|
+
* sortBy: "createdAt",
|
|
128
|
+
* sortOrder: "desc",
|
|
129
|
+
* page: 1,
|
|
130
|
+
* limit: 50,
|
|
131
|
+
* });
|
|
132
|
+
* ```
|
|
133
|
+
*/
|
|
134
|
+
async list(entityId, query) {
|
|
135
|
+
const qs = buildQuery(query ?? {});
|
|
136
|
+
return (await http.fetch("GET", `/entities/${entityId}/records${qs}`)).data;
|
|
137
|
+
},
|
|
138
|
+
/** Create a new record with attribute values.
|
|
139
|
+
* @example
|
|
140
|
+
* ```ts
|
|
141
|
+
* const record = await eav.records.create(entityId, {
|
|
142
|
+
* authUserId: userId,
|
|
143
|
+
* values: { [attrs.name]: "John", [attrs.email]: "john@example.com" },
|
|
144
|
+
* });
|
|
145
|
+
* ```
|
|
146
|
+
*/
|
|
147
|
+
async create(entityId, input) {
|
|
148
|
+
return (await http.fetch("POST", `/entities/${entityId}/records`, input)).data;
|
|
149
|
+
},
|
|
150
|
+
/** Get a single record by ID.
|
|
151
|
+
* @example
|
|
152
|
+
* ```ts
|
|
153
|
+
* const record = await eav.records.get(entityId, recordId);
|
|
154
|
+
* ```
|
|
155
|
+
*/
|
|
156
|
+
async get(entityId, recordId) {
|
|
157
|
+
return (await http.fetch("GET", `/entities/${entityId}/records/${recordId}`)).data;
|
|
158
|
+
},
|
|
159
|
+
/** Update a record's attribute values. Only specified values are changed.
|
|
160
|
+
* @example
|
|
161
|
+
* ```ts
|
|
162
|
+
* const updated = await eav.records.update(entityId, recordId, {
|
|
163
|
+
* values: { [attrs.status]: "completed" },
|
|
164
|
+
* });
|
|
165
|
+
* ```
|
|
166
|
+
*/
|
|
167
|
+
async update(entityId, recordId, input) {
|
|
168
|
+
return (await http.fetch("PATCH", `/entities/${entityId}/records/${recordId}`, input)).data;
|
|
169
|
+
},
|
|
170
|
+
/** Delete a single record by ID. */
|
|
171
|
+
async delete(entityId, recordId) {
|
|
172
|
+
await http.fetch("DELETE", `/entities/${entityId}/records/${recordId}`);
|
|
173
|
+
},
|
|
174
|
+
/** Count records matching filters without fetching bodies.
|
|
175
|
+
* @example
|
|
176
|
+
* ```ts
|
|
177
|
+
* // Count active tasks
|
|
178
|
+
* const count = await eav.records.count(entityId, {
|
|
179
|
+
* authUserId: userId,
|
|
180
|
+
* filters: [{ attributeId: attrs.deleted_at, operator: "is_null" }],
|
|
181
|
+
* });
|
|
182
|
+
* ```
|
|
183
|
+
*/
|
|
184
|
+
async count(entityId, query) {
|
|
185
|
+
const qs = buildQuery(query ?? {});
|
|
186
|
+
return (await http.fetch("GET", `/entities/${entityId}/records/count${qs}`)).data.total;
|
|
187
|
+
},
|
|
188
|
+
/** Delete multiple records by ID (max 100). Returns count of deleted records. */
|
|
189
|
+
async bulkDelete(entityId, ids) {
|
|
190
|
+
assertBulkSize(ids.length, "bulkDelete");
|
|
191
|
+
return (await http.fetch("DELETE", `/entities/${entityId}/records`, { ids })).data.count;
|
|
192
|
+
},
|
|
193
|
+
/** Update multiple records in a single transaction (max 100).
|
|
194
|
+
* @example
|
|
195
|
+
* ```ts
|
|
196
|
+
* const updated = await eav.records.bulkUpdate(entityId, [
|
|
197
|
+
* { id: "record-1", values: { [attrs.is_active]: false } },
|
|
198
|
+
* { id: "record-2", values: { [attrs.is_active]: false } },
|
|
199
|
+
* ]);
|
|
200
|
+
* ```
|
|
201
|
+
*/
|
|
202
|
+
async bulkUpdate(entityId, updates) {
|
|
203
|
+
assertBulkSize(updates.length, "bulkUpdate");
|
|
204
|
+
return (await http.fetch("PATCH", `/entities/${entityId}/records`, { updates })).data;
|
|
205
|
+
},
|
|
206
|
+
/** Find a single record matching filters. Returns null if not found.
|
|
207
|
+
* Prefer this over list() + filter when you need exactly one record.
|
|
208
|
+
* @example
|
|
209
|
+
* ```ts
|
|
210
|
+
* // Find setting by key
|
|
211
|
+
* const record = await eav.records.findOne(entityId, {
|
|
212
|
+
* authUserId: userId,
|
|
213
|
+
* filters: [{ attributeId: attrs.key, operator: "eq", value: "theme" }],
|
|
214
|
+
* });
|
|
215
|
+
* ```
|
|
216
|
+
*/
|
|
217
|
+
async findOne(entityId, query) {
|
|
218
|
+
return (await client.records.list(entityId, {
|
|
219
|
+
...query,
|
|
220
|
+
page: 1,
|
|
221
|
+
limit: 1
|
|
222
|
+
})).records[0] ?? null;
|
|
223
|
+
},
|
|
224
|
+
/** Find a record by filters -- update if exists, create if not. Atomic operation.
|
|
225
|
+
* @returns `{ data: EavRecord, created: boolean }`
|
|
226
|
+
* @example
|
|
227
|
+
* ```ts
|
|
228
|
+
* // Upsert a user setting
|
|
229
|
+
* const { data, created } = await eav.records.upsert(entityId, {
|
|
230
|
+
* authUserId: userId,
|
|
231
|
+
* filters: [{ attributeId: attrs.key, operator: "eq", value: "theme" }],
|
|
232
|
+
* values: { [attrs.key]: "theme", [attrs.value]: "dark" },
|
|
233
|
+
* });
|
|
234
|
+
* ```
|
|
235
|
+
*/
|
|
236
|
+
async upsert(entityId, input) {
|
|
237
|
+
const res = await http.fetch("PUT", `/entities/${entityId}/records/upsert`, input);
|
|
238
|
+
return {
|
|
239
|
+
data: res.data.record,
|
|
240
|
+
created: res.data.created
|
|
241
|
+
};
|
|
242
|
+
},
|
|
243
|
+
/** Create multiple records in a single transaction (max 100).
|
|
244
|
+
* @example
|
|
245
|
+
* ```ts
|
|
246
|
+
* const records = await eav.records.bulkCreate(entityId, [
|
|
247
|
+
* { values: { [attrs.name]: "Alice" } },
|
|
248
|
+
* { values: { [attrs.name]: "Bob" } },
|
|
249
|
+
* ]);
|
|
250
|
+
* ```
|
|
251
|
+
*/
|
|
252
|
+
async bulkCreate(entityId, records) {
|
|
253
|
+
assertBulkSize(records.length, "bulkCreate");
|
|
254
|
+
return (await http.fetch("POST", `/entities/${entityId}/records/bulk`, { records })).data;
|
|
255
|
+
},
|
|
256
|
+
/** Delete all records matching filters. Returns count of deleted records.
|
|
257
|
+
* @example
|
|
258
|
+
* ```ts
|
|
259
|
+
* // Delete all inactive records
|
|
260
|
+
* const count = await eav.records.deleteWhere(entityId, {
|
|
261
|
+
* authUserId: userId,
|
|
262
|
+
* filters: [{ attributeId: attrs.status, operator: "eq", value: "inactive" }],
|
|
263
|
+
* });
|
|
264
|
+
* ```
|
|
265
|
+
*/
|
|
266
|
+
async deleteWhere(entityId, input) {
|
|
267
|
+
return (await http.fetch("POST", `/entities/${entityId}/records/delete-where`, input)).data.count;
|
|
268
|
+
},
|
|
269
|
+
/** Iterate all records across pages as an AsyncGenerator.
|
|
270
|
+
* @example
|
|
271
|
+
* ```ts
|
|
272
|
+
* // Stream all records
|
|
273
|
+
* for await (const record of eav.records.listAll(entityId, { authUserId: userId })) {
|
|
274
|
+
* process.stdout.write(".");
|
|
275
|
+
* }
|
|
276
|
+
*
|
|
277
|
+
* // Or collect to array
|
|
278
|
+
* import { collectAll } from "@reopt-ai/brandapp-sdk/eav";
|
|
279
|
+
* const all = await collectAll(eav.records.listAll(entityId));
|
|
280
|
+
* ```
|
|
281
|
+
*/
|
|
282
|
+
async *listAll(entityId, query) {
|
|
283
|
+
let page = 1;
|
|
284
|
+
const limit = query?.limit ?? 100;
|
|
285
|
+
while (true) {
|
|
286
|
+
const res = await client.records.list(entityId, {
|
|
287
|
+
...query,
|
|
288
|
+
page,
|
|
289
|
+
limit
|
|
290
|
+
});
|
|
291
|
+
for (const record of res.records) yield record;
|
|
292
|
+
if (page >= res.totalPages) break;
|
|
293
|
+
page++;
|
|
294
|
+
}
|
|
295
|
+
},
|
|
296
|
+
/**
|
|
297
|
+
* Backfill records by streaming through pages, calling `transform` per
|
|
298
|
+
* record, and applying `update` for each non-null result.
|
|
299
|
+
*
|
|
300
|
+
* Designed for the "expand-contract" middle step of an EAV migration —
|
|
301
|
+
* e.g. copy values from a soon-to-be-deleted attribute to a new one.
|
|
302
|
+
*
|
|
303
|
+
* @example
|
|
304
|
+
* ```ts
|
|
305
|
+
* await sdk.eav.records.backfill(entityId, {
|
|
306
|
+
* filters: [{ attributeId: levelAttrId, operator: "is_null" }],
|
|
307
|
+
* concurrency: 10,
|
|
308
|
+
* transform: (r) => ({ level: r.values.tier ?? "free" }),
|
|
309
|
+
* onProgress: ({ processed, total }) =>
|
|
310
|
+
* console.log(`${processed}/${total}`),
|
|
311
|
+
* });
|
|
312
|
+
* ```
|
|
313
|
+
*
|
|
314
|
+
* Failed updates are collected in `result.failed[]` rather than aborting
|
|
315
|
+
* the run — inspect that array after the call to retry or escalate.
|
|
316
|
+
*/
|
|
317
|
+
async backfill(entityId, opts) {
|
|
318
|
+
const concurrency = Math.max(1, opts.concurrency ?? 5);
|
|
319
|
+
const pageSize = Math.min(Math.max(1, opts.pageSize ?? 100), 100);
|
|
320
|
+
const batchSize = Math.min(Math.max(1, opts.batchSize ?? MAX_BULK), MAX_BULK);
|
|
321
|
+
const total = await client.records.count(entityId, {
|
|
322
|
+
authUserId: opts.authUserId,
|
|
323
|
+
filters: opts.filters
|
|
324
|
+
});
|
|
325
|
+
let updated = 0;
|
|
326
|
+
let skipped = 0;
|
|
327
|
+
const failed = [];
|
|
328
|
+
let processed = 0;
|
|
329
|
+
const emit = () => {
|
|
330
|
+
opts.onProgress?.({
|
|
331
|
+
processed,
|
|
332
|
+
total,
|
|
333
|
+
updated,
|
|
334
|
+
skipped,
|
|
335
|
+
failed: failed.length
|
|
336
|
+
});
|
|
337
|
+
};
|
|
338
|
+
const applyOne = async (item) => {
|
|
339
|
+
try {
|
|
340
|
+
await client.records.update(entityId, item.id, { values: item.values });
|
|
341
|
+
updated++;
|
|
342
|
+
} catch (err) {
|
|
343
|
+
failed.push({
|
|
344
|
+
recordId: item.id,
|
|
345
|
+
error: err instanceof Error ? err.message : String(err)
|
|
346
|
+
});
|
|
347
|
+
}
|
|
348
|
+
};
|
|
349
|
+
const flush = async (items) => {
|
|
350
|
+
try {
|
|
351
|
+
await client.records.bulkUpdate(entityId, items);
|
|
352
|
+
updated += items.length;
|
|
353
|
+
} catch {
|
|
354
|
+
for (const item of items) await applyOne(item);
|
|
355
|
+
} finally {
|
|
356
|
+
processed += items.length;
|
|
357
|
+
emit();
|
|
358
|
+
}
|
|
359
|
+
};
|
|
360
|
+
const inFlight = /* @__PURE__ */ new Set();
|
|
361
|
+
let buffer = [];
|
|
362
|
+
const scheduleFlush = () => {
|
|
363
|
+
const items = buffer;
|
|
364
|
+
buffer = [];
|
|
365
|
+
const task = flush(items).finally(() => {
|
|
366
|
+
inFlight.delete(task);
|
|
367
|
+
});
|
|
368
|
+
inFlight.add(task);
|
|
369
|
+
};
|
|
370
|
+
for await (const record of client.records.listAll(entityId, {
|
|
371
|
+
authUserId: opts.authUserId,
|
|
372
|
+
filters: opts.filters,
|
|
373
|
+
sortBy: opts.sortBy ?? "createdAt",
|
|
374
|
+
sortOrder: opts.sortOrder ?? "asc",
|
|
375
|
+
limit: pageSize
|
|
376
|
+
})) {
|
|
377
|
+
const next = opts.transform(record);
|
|
378
|
+
if (next === null || next === void 0) {
|
|
379
|
+
skipped++;
|
|
380
|
+
processed++;
|
|
381
|
+
emit();
|
|
382
|
+
continue;
|
|
383
|
+
}
|
|
384
|
+
buffer.push({
|
|
385
|
+
id: record.id,
|
|
386
|
+
values: next
|
|
387
|
+
});
|
|
388
|
+
if (buffer.length >= batchSize) {
|
|
389
|
+
scheduleFlush();
|
|
390
|
+
if (inFlight.size >= concurrency) await Promise.race(inFlight);
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
if (buffer.length > 0) scheduleFlush();
|
|
394
|
+
await Promise.all(inFlight);
|
|
395
|
+
return {
|
|
396
|
+
total,
|
|
397
|
+
updated,
|
|
398
|
+
skipped,
|
|
399
|
+
failed
|
|
400
|
+
};
|
|
401
|
+
}
|
|
402
|
+
},
|
|
403
|
+
users: { async getRecords(authUserId, query) {
|
|
404
|
+
const qs = buildQuery(query ?? {});
|
|
405
|
+
return (await http.fetch("GET", `/users/${authUserId}/records${qs}`)).data;
|
|
406
|
+
} },
|
|
407
|
+
/**
|
|
408
|
+
* Fetch the server's current EAV schema fingerprint (Phase C, RFC-0005).
|
|
409
|
+
*
|
|
410
|
+
* Pair with `verifyEavSchema(localHash, client)` to detect drift between
|
|
411
|
+
* a deployed bundle and the live schema. Cacheable for ~60s — the server
|
|
412
|
+
* recomputes on every schema mutation.
|
|
413
|
+
*/
|
|
414
|
+
async schemaHash() {
|
|
415
|
+
return (await http.fetch("GET", "/schema-hash")).data;
|
|
416
|
+
},
|
|
417
|
+
/** Create a type-safe entity-scoped client. Resolves entity name to ID automatically.
|
|
418
|
+
* Requires `schema` in SDK config for type inference.
|
|
419
|
+
* @example
|
|
420
|
+
* ```ts
|
|
421
|
+
* const contacts = sdk.eav.entity("contacts");
|
|
422
|
+
* const record = await contacts.records.create({
|
|
423
|
+
* values: { name: "John", email: "john@example.com" }, // typed!
|
|
424
|
+
* });
|
|
425
|
+
* ```
|
|
426
|
+
*/
|
|
427
|
+
entity(name) {
|
|
428
|
+
const entityName = config.schema ? config.schema.entities[name].name : name;
|
|
429
|
+
return {
|
|
430
|
+
entityName,
|
|
431
|
+
records: {
|
|
432
|
+
async list(query) {
|
|
433
|
+
const id = await resolveEntityId(entityName);
|
|
434
|
+
return client.records.list(id, query);
|
|
435
|
+
},
|
|
436
|
+
async findOne(query) {
|
|
437
|
+
const id = await resolveEntityId(entityName);
|
|
438
|
+
return client.records.findOne(id, query);
|
|
439
|
+
},
|
|
440
|
+
async create(input) {
|
|
441
|
+
const id = await resolveEntityId(entityName);
|
|
442
|
+
return client.records.create(id, input);
|
|
443
|
+
},
|
|
444
|
+
async upsert(input) {
|
|
445
|
+
const id = await resolveEntityId(entityName);
|
|
446
|
+
return client.records.upsert(id, input);
|
|
447
|
+
},
|
|
448
|
+
async get(recordId) {
|
|
449
|
+
const id = await resolveEntityId(entityName);
|
|
450
|
+
return client.records.get(id, recordId);
|
|
451
|
+
},
|
|
452
|
+
async update(recordId, input) {
|
|
453
|
+
const id = await resolveEntityId(entityName);
|
|
454
|
+
return client.records.update(id, recordId, input);
|
|
455
|
+
},
|
|
456
|
+
async delete(recordId) {
|
|
457
|
+
const id = await resolveEntityId(entityName);
|
|
458
|
+
return client.records.delete(id, recordId);
|
|
459
|
+
},
|
|
460
|
+
async count(query) {
|
|
461
|
+
const id = await resolveEntityId(entityName);
|
|
462
|
+
return client.records.count(id, query);
|
|
463
|
+
},
|
|
464
|
+
async bulkCreate(records) {
|
|
465
|
+
const id = await resolveEntityId(entityName);
|
|
466
|
+
return client.records.bulkCreate(id, records);
|
|
467
|
+
},
|
|
468
|
+
async bulkDelete(ids) {
|
|
469
|
+
const id = await resolveEntityId(entityName);
|
|
470
|
+
return client.records.bulkDelete(id, ids);
|
|
471
|
+
},
|
|
472
|
+
async bulkUpdate(updates) {
|
|
473
|
+
const id = await resolveEntityId(entityName);
|
|
474
|
+
return client.records.bulkUpdate(id, updates);
|
|
475
|
+
},
|
|
476
|
+
async deleteWhere(input) {
|
|
477
|
+
const id = await resolveEntityId(entityName);
|
|
478
|
+
return client.records.deleteWhere(id, input);
|
|
479
|
+
},
|
|
480
|
+
async *listAll(query) {
|
|
481
|
+
const id = await resolveEntityId(entityName);
|
|
482
|
+
yield* client.records.listAll(id, query);
|
|
483
|
+
},
|
|
484
|
+
async backfill(opts) {
|
|
485
|
+
const id = await resolveEntityId(entityName);
|
|
486
|
+
return client.records.backfill(id, opts);
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
};
|
|
490
|
+
},
|
|
491
|
+
/** Clear the entity name → ID cache and schema cache. Call after creating/deleting entities externally. */
|
|
492
|
+
invalidateCache() {
|
|
493
|
+
entityNameMap = null;
|
|
494
|
+
entityMapPromise = null;
|
|
495
|
+
schemaCache.invalidate();
|
|
496
|
+
}
|
|
497
|
+
};
|
|
498
|
+
return client;
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
//#endregion
|
|
502
|
+
export { createReoptEavClient as t };
|
|
503
|
+
//# sourceMappingURL=client-DsdakibF.js.map
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { n as ReoptSDKConfig } from "./config-CRytOPPl.js";
|
|
2
|
+
//#region src/auth/types.d.ts
|
|
3
|
+
/**
|
|
4
|
+
* User-scoped auth types.
|
|
5
|
+
*
|
|
6
|
+
* These describe the Bearer-authenticated endpoints exposed by Reopt for
|
|
7
|
+
* apps that have obtained an OAuth access token on behalf of an end user.
|
|
8
|
+
* App-scoped (Basic Auth) operations live under `eav/`, `ai/`, `files/`,
|
|
9
|
+
* `cms/` — they never need an access token.
|
|
10
|
+
*/
|
|
11
|
+
/**
|
|
12
|
+
* OIDC userinfo claims returned by `GET /api/auth/oauth2/userinfo`.
|
|
13
|
+
*
|
|
14
|
+
* Reopt always returns the core claims when the caller's token carries the
|
|
15
|
+
* matching scopes (`openid`, `profile`, `email`). Additional claims may
|
|
16
|
+
* appear when extra scopes are granted.
|
|
17
|
+
*/
|
|
18
|
+
type UserInfoClaims = {
|
|
19
|
+
sub: string;
|
|
20
|
+
email?: string;
|
|
21
|
+
email_verified?: boolean;
|
|
22
|
+
name?: string;
|
|
23
|
+
picture?: string;
|
|
24
|
+
given_name?: string;
|
|
25
|
+
family_name?: string;
|
|
26
|
+
[claim: string]: unknown;
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Result of linking the current OAuth user to the Brandapp's customer table.
|
|
30
|
+
*
|
|
31
|
+
* The call is an idempotent upsert — the first invocation creates
|
|
32
|
+
* `Customer`, `BrandCustomer`, `BrandappCustomer`, and `CustomerExternalId`
|
|
33
|
+
* rows; subsequent calls refresh `authorizedAt` and flip `status` back to
|
|
34
|
+
* `active`.
|
|
35
|
+
*/
|
|
36
|
+
type LinkCurrentUserResult = {
|
|
37
|
+
customerId: string;
|
|
38
|
+
};
|
|
39
|
+
//#endregion
|
|
40
|
+
//#region src/auth/client.d.ts
|
|
41
|
+
declare class ReoptAuthClient {
|
|
42
|
+
/**
|
|
43
|
+
* Root client for `/api/auth/*` endpoints (OIDC userinfo, etc). Pinned to
|
|
44
|
+
* the id server because Better Auth's handler is only mounted there.
|
|
45
|
+
*/
|
|
46
|
+
private readonly rootHttp;
|
|
47
|
+
/** Brandapp-scoped client — prefixed for per-brandapp endpoints. */
|
|
48
|
+
private readonly brandappHttp;
|
|
49
|
+
constructor(config: ReoptSDKConfig);
|
|
50
|
+
/**
|
|
51
|
+
* Upsert the Customer ↔ reoptUser link for the OAuth user represented by
|
|
52
|
+
* the supplied access token.
|
|
53
|
+
*/
|
|
54
|
+
linkCurrentUser(accessToken: string): Promise<LinkCurrentUserResult>;
|
|
55
|
+
/**
|
|
56
|
+
* Soft-withdraw the current OAuth user from this Brandapp: BrandappCustomer
|
|
57
|
+
* flips to `churned`, the Brand↔Customer junction is removed, all
|
|
58
|
+
* terms-consents move to `withdrawn`, and any Stripe subscription is
|
|
59
|
+
* cancelled. The Customer record itself is retained so re-linking later
|
|
60
|
+
* restores the same customerId (see `linkCurrentUser`).
|
|
61
|
+
*/
|
|
62
|
+
withdrawCurrentUser(accessToken: string): Promise<LinkCurrentUserResult>;
|
|
63
|
+
/**
|
|
64
|
+
* Fetch the OIDC userinfo claims for the supplied access token.
|
|
65
|
+
*/
|
|
66
|
+
getUserInfo(accessToken: string): Promise<UserInfoClaims>;
|
|
67
|
+
}
|
|
68
|
+
declare function createReoptAuthClient(config: ReoptSDKConfig): ReoptAuthClient;
|
|
69
|
+
//#endregion
|
|
70
|
+
export { UserInfoClaims as i, createReoptAuthClient as n, LinkCurrentUserResult as r, ReoptAuthClient as t };
|
|
71
|
+
//# sourceMappingURL=client-Dxg8_DIH.d.ts.map
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { t as BaseHttpClient } from "./http-client-QKUvvirs.js";
|
|
2
|
+
|
|
3
|
+
//#region src/auth/client.ts
|
|
4
|
+
function unwrap(body) {
|
|
5
|
+
if (body && typeof body === "object" && "data" in body) {
|
|
6
|
+
const withData = body;
|
|
7
|
+
if (withData.data !== void 0) return withData.data;
|
|
8
|
+
}
|
|
9
|
+
return body;
|
|
10
|
+
}
|
|
11
|
+
var ReoptAuthClient = class {
|
|
12
|
+
/**
|
|
13
|
+
* Root client for `/api/auth/*` endpoints (OIDC userinfo, etc). Pinned to
|
|
14
|
+
* the id server because Better Auth's handler is only mounted there.
|
|
15
|
+
*/
|
|
16
|
+
rootHttp;
|
|
17
|
+
/** Brandapp-scoped client — prefixed for per-brandapp endpoints. */
|
|
18
|
+
brandappHttp;
|
|
19
|
+
constructor(config) {
|
|
20
|
+
this.rootHttp = new BaseHttpClient(config, "", { useIdServer: true });
|
|
21
|
+
this.brandappHttp = new BaseHttpClient(config, `/api/v1/brandapp/${config.brandappId}`);
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Upsert the Customer ↔ reoptUser link for the OAuth user represented by
|
|
25
|
+
* the supplied access token.
|
|
26
|
+
*/
|
|
27
|
+
async linkCurrentUser(accessToken) {
|
|
28
|
+
return unwrap(await this.brandappHttp.fetch("POST", "/external-auth", {}, { bearerToken: accessToken }));
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Soft-withdraw the current OAuth user from this Brandapp: BrandappCustomer
|
|
32
|
+
* flips to `churned`, the Brand↔Customer junction is removed, all
|
|
33
|
+
* terms-consents move to `withdrawn`, and any Stripe subscription is
|
|
34
|
+
* cancelled. The Customer record itself is retained so re-linking later
|
|
35
|
+
* restores the same customerId (see `linkCurrentUser`).
|
|
36
|
+
*/
|
|
37
|
+
async withdrawCurrentUser(accessToken) {
|
|
38
|
+
return unwrap(await this.brandappHttp.fetch("DELETE", "/external-auth", void 0, { bearerToken: accessToken }));
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Fetch the OIDC userinfo claims for the supplied access token.
|
|
42
|
+
*/
|
|
43
|
+
async getUserInfo(accessToken) {
|
|
44
|
+
return this.rootHttp.fetch("GET", "/api/auth/oauth2/userinfo", void 0, { bearerToken: accessToken });
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
function createReoptAuthClient(config) {
|
|
48
|
+
return new ReoptAuthClient(config);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
//#endregion
|
|
52
|
+
export { createReoptAuthClient as n, ReoptAuthClient as t };
|
|
53
|
+
//# sourceMappingURL=client-EbhUIhHq.js.map
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { n as ReoptSDKConfig } from "./config-CRytOPPl.js";
|
|
2
|
+
import { a as CreateConsentsResult, f as WithdrawConsentResult, i as CreateConsentsRequest, l as TermsListResponse, n as ConsentRecord, o as ListTermsOptions, s as TermsItem } from "./types-BrghN2rJ.js";
|
|
3
|
+
//#region src/terms/client.d.ts
|
|
4
|
+
declare class ReoptTermsClient {
|
|
5
|
+
private readonly http;
|
|
6
|
+
constructor(config: ReoptSDKConfig);
|
|
7
|
+
/**
|
|
8
|
+
* List active terms for this Brandapp with their currently-published
|
|
9
|
+
* version. Works with either Basic Auth (default) or Bearer Auth — when
|
|
10
|
+
* called from a server handler that has the user's access token, pass
|
|
11
|
+
* `accessToken` to avoid sending app credentials.
|
|
12
|
+
*
|
|
13
|
+
* Accepts either an `accessToken` string (legacy) or an options object
|
|
14
|
+
* (`country`/`locale`/`jurisdiction`/`accessToken`) for jurisdiction-aware
|
|
15
|
+
* matching. When `country` / `locale` are omitted, the server falls back to
|
|
16
|
+
* request headers (`x-vercel-ip-country`, `Accept-Language`).
|
|
17
|
+
*
|
|
18
|
+
* Returns the matched terms array. For matching metadata
|
|
19
|
+
* (jurisdiction, locale, source) use {@link listWithMeta}.
|
|
20
|
+
*/
|
|
21
|
+
list(optionsOrToken?: string | ListTermsOptions): Promise<TermsItem[]>;
|
|
22
|
+
/**
|
|
23
|
+
* Same as {@link list} but returns the full {@link TermsListResponse} including
|
|
24
|
+
* matched jurisdiction, country and locale — useful for displaying "약관이
|
|
25
|
+
* 어떤 국가 기준으로 적용됐는지" hint in the signup UX.
|
|
26
|
+
*/
|
|
27
|
+
listWithMeta(optionsOrToken?: string | ListTermsOptions): Promise<TermsListResponse>;
|
|
28
|
+
/**
|
|
29
|
+
* Fetch the consent history (granted / withdrawn / pendingReConsent) for
|
|
30
|
+
* the OAuth user represented by `accessToken`.
|
|
31
|
+
*/
|
|
32
|
+
listMyConsents(accessToken: string): Promise<ConsentRecord[]>;
|
|
33
|
+
/**
|
|
34
|
+
* Record consent for one or more terms versions. The server rejects the
|
|
35
|
+
* call with `REQUIRED_NOT_CONSENTED` (422) if any required term is missing
|
|
36
|
+
* or marked as `consented: false`.
|
|
37
|
+
*/
|
|
38
|
+
consent(input: CreateConsentsRequest, accessToken: string): Promise<CreateConsentsResult>;
|
|
39
|
+
/**
|
|
40
|
+
* Withdraw a previously-granted consent. The server returns 409 for
|
|
41
|
+
* required terms or already-withdrawn records.
|
|
42
|
+
*/
|
|
43
|
+
withdraw(consentId: string, accessToken: string): Promise<WithdrawConsentResult>;
|
|
44
|
+
}
|
|
45
|
+
declare function createReoptTermsClient(config: ReoptSDKConfig): ReoptTermsClient;
|
|
46
|
+
//#endregion
|
|
47
|
+
export { createReoptTermsClient as n, ReoptTermsClient as t };
|
|
48
|
+
//# sourceMappingURL=client-lk6wBu_0.d.ts.map
|