@soulcraft/sdk 1.6.2 → 2.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/dist/client/create-client-sdk.d.ts +16 -2
- package/dist/client/create-client-sdk.d.ts.map +1 -1
- package/dist/client/create-client-sdk.js +2 -7
- package/dist/client/create-client-sdk.js.map +1 -1
- package/dist/client/index.d.ts +48 -37
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +61 -42
- package/dist/client/index.js.map +1 -1
- package/dist/client/namespace-proxy.d.ts +108 -0
- package/dist/client/namespace-proxy.d.ts.map +1 -0
- package/dist/client/namespace-proxy.js +151 -0
- package/dist/client/namespace-proxy.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/modules/app-context/index.d.ts +214 -0
- package/dist/modules/app-context/index.d.ts.map +1 -0
- package/dist/modules/app-context/index.js +569 -0
- package/dist/modules/app-context/index.js.map +1 -0
- package/dist/modules/auth/products.d.ts +208 -0
- package/dist/modules/auth/products.d.ts.map +1 -0
- package/dist/modules/auth/products.js +165 -0
- package/dist/modules/auth/products.js.map +1 -0
- package/dist/namespaces.d.ts +2942 -0
- package/dist/namespaces.d.ts.map +1 -0
- package/dist/namespaces.js +37 -0
- package/dist/namespaces.js.map +1 -0
- package/dist/rpc.d.ts +156 -0
- package/dist/rpc.d.ts.map +1 -0
- package/dist/rpc.js +26 -0
- package/dist/rpc.js.map +1 -0
- package/dist/server/create-sdk.d.ts.map +1 -1
- package/dist/server/create-sdk.js +3 -13
- package/dist/server/create-sdk.js.map +1 -1
- package/dist/server/handlers/annotations.d.ts +52 -0
- package/dist/server/handlers/annotations.d.ts.map +1 -0
- package/dist/server/handlers/annotations.js +204 -0
- package/dist/server/handlers/annotations.js.map +1 -0
- package/dist/server/handlers/auth.d.ts +53 -0
- package/dist/server/handlers/auth.d.ts.map +1 -0
- package/dist/server/handlers/auth.js +66 -0
- package/dist/server/handlers/auth.js.map +1 -0
- package/dist/server/handlers/certification.d.ts +32 -0
- package/dist/server/handlers/certification.d.ts.map +1 -0
- package/dist/server/handlers/certification.js +253 -0
- package/dist/server/handlers/certification.js.map +1 -0
- package/dist/server/handlers/chat/conversations.d.ts +91 -0
- package/dist/server/handlers/chat/conversations.d.ts.map +1 -0
- package/dist/server/handlers/chat/conversations.js +314 -0
- package/dist/server/handlers/chat/conversations.js.map +1 -0
- package/dist/server/handlers/chat/delegator.d.ts +144 -0
- package/dist/server/handlers/chat/delegator.d.ts.map +1 -0
- package/dist/server/handlers/chat/delegator.js +431 -0
- package/dist/server/handlers/chat/delegator.js.map +1 -0
- package/dist/server/handlers/chat/engine.d.ts +81 -0
- package/dist/server/handlers/chat/engine.d.ts.map +1 -0
- package/dist/server/handlers/chat/engine.js +442 -0
- package/dist/server/handlers/chat/engine.js.map +1 -0
- package/dist/server/handlers/chat/executor.d.ts +65 -0
- package/dist/server/handlers/chat/executor.d.ts.map +1 -0
- package/dist/server/handlers/chat/executor.js +375 -0
- package/dist/server/handlers/chat/executor.js.map +1 -0
- package/dist/server/handlers/chat/index.d.ts +62 -0
- package/dist/server/handlers/chat/index.d.ts.map +1 -0
- package/dist/server/handlers/chat/index.js +182 -0
- package/dist/server/handlers/chat/index.js.map +1 -0
- package/dist/server/handlers/chat/memory.d.ts +91 -0
- package/dist/server/handlers/chat/memory.d.ts.map +1 -0
- package/dist/server/handlers/chat/memory.js +293 -0
- package/dist/server/handlers/chat/memory.js.map +1 -0
- package/dist/server/handlers/chat/models.d.ts +180 -0
- package/dist/server/handlers/chat/models.d.ts.map +1 -0
- package/dist/server/handlers/chat/models.js +304 -0
- package/dist/server/handlers/chat/models.js.map +1 -0
- package/dist/server/handlers/chat/planner.d.ts +116 -0
- package/dist/server/handlers/chat/planner.d.ts.map +1 -0
- package/dist/server/handlers/chat/planner.js +344 -0
- package/dist/server/handlers/chat/planner.js.map +1 -0
- package/dist/server/handlers/chat/types.d.ts +500 -0
- package/dist/server/handlers/chat/types.d.ts.map +1 -0
- package/dist/server/handlers/chat/types.js +11 -0
- package/dist/server/handlers/chat/types.js.map +1 -0
- package/dist/server/handlers/collections.d.ts +67 -0
- package/dist/server/handlers/collections.d.ts.map +1 -0
- package/dist/server/handlers/collections.js +484 -0
- package/dist/server/handlers/collections.js.map +1 -0
- package/dist/server/handlers/commerce.d.ts +106 -0
- package/dist/server/handlers/commerce.d.ts.map +1 -0
- package/dist/server/handlers/commerce.js +62 -0
- package/dist/server/handlers/commerce.js.map +1 -0
- package/dist/server/handlers/config.d.ts +112 -0
- package/dist/server/handlers/config.d.ts.map +1 -0
- package/dist/server/handlers/config.js +122 -0
- package/dist/server/handlers/config.js.map +1 -0
- package/dist/server/handlers/export.d.ts +72 -0
- package/dist/server/handlers/export.d.ts.map +1 -0
- package/dist/server/handlers/export.js +175 -0
- package/dist/server/handlers/export.js.map +1 -0
- package/dist/server/handlers/formats.d.ts +77 -0
- package/dist/server/handlers/formats.d.ts.map +1 -0
- package/dist/server/handlers/formats.js +65 -0
- package/dist/server/handlers/formats.js.map +1 -0
- package/dist/server/handlers/graph.d.ts +31 -0
- package/dist/server/handlers/graph.d.ts.map +1 -0
- package/dist/server/handlers/graph.js +490 -0
- package/dist/server/handlers/graph.js.map +1 -0
- package/dist/server/handlers/import.d.ts +96 -0
- package/dist/server/handlers/import.d.ts.map +1 -0
- package/dist/server/handlers/import.js +108 -0
- package/dist/server/handlers/import.js.map +1 -0
- package/dist/server/handlers/index.d.ts +68 -0
- package/dist/server/handlers/index.d.ts.map +1 -0
- package/dist/server/handlers/index.js +71 -0
- package/dist/server/handlers/index.js.map +1 -0
- package/dist/server/handlers/media.d.ts +76 -0
- package/dist/server/handlers/media.d.ts.map +1 -0
- package/dist/server/handlers/media.js +53 -0
- package/dist/server/handlers/media.js.map +1 -0
- package/dist/server/handlers/project.d.ts +45 -0
- package/dist/server/handlers/project.d.ts.map +1 -0
- package/dist/server/handlers/project.js +181 -0
- package/dist/server/handlers/project.js.map +1 -0
- package/dist/server/handlers/publish.d.ts +102 -0
- package/dist/server/handlers/publish.d.ts.map +1 -0
- package/dist/server/handlers/publish.js +130 -0
- package/dist/server/handlers/publish.js.map +1 -0
- package/dist/server/handlers/pulse.d.ts +39 -0
- package/dist/server/handlers/pulse.d.ts.map +1 -0
- package/dist/server/handlers/pulse.js +78 -0
- package/dist/server/handlers/pulse.js.map +1 -0
- package/dist/server/handlers/realtime.d.ts +55 -0
- package/dist/server/handlers/realtime.d.ts.map +1 -0
- package/dist/server/handlers/realtime.js +49 -0
- package/dist/server/handlers/realtime.js.map +1 -0
- package/dist/server/handlers/search.d.ts +21 -0
- package/dist/server/handlers/search.d.ts.map +1 -0
- package/dist/server/handlers/search.js +237 -0
- package/dist/server/handlers/search.js.map +1 -0
- package/dist/server/handlers/session.d.ts +47 -0
- package/dist/server/handlers/session.d.ts.map +1 -0
- package/dist/server/handlers/session.js +286 -0
- package/dist/server/handlers/session.js.map +1 -0
- package/dist/server/handlers/settings.d.ts +97 -0
- package/dist/server/handlers/settings.d.ts.map +1 -0
- package/dist/server/handlers/settings.js +131 -0
- package/dist/server/handlers/settings.js.map +1 -0
- package/dist/server/handlers/workspace.d.ts +78 -0
- package/dist/server/handlers/workspace.d.ts.map +1 -0
- package/dist/server/handlers/workspace.js +270 -0
- package/dist/server/handlers/workspace.js.map +1 -0
- package/dist/server/hono-router.d.ts +66 -0
- package/dist/server/hono-router.d.ts.map +1 -0
- package/dist/server/hono-router.js +203 -0
- package/dist/server/hono-router.js.map +1 -0
- package/dist/server/index.d.ts +29 -19
- package/dist/server/index.d.ts.map +1 -1
- package/dist/server/index.js +33 -19
- package/dist/server/index.js.map +1 -1
- package/dist/server/namespace-router.d.ts +204 -0
- package/dist/server/namespace-router.d.ts.map +1 -0
- package/dist/server/namespace-router.js +262 -0
- package/dist/server/namespace-router.js.map +1 -0
- package/dist/transports/http-namespace.d.ts +210 -0
- package/dist/transports/http-namespace.d.ts.map +1 -0
- package/dist/transports/http-namespace.js +514 -0
- package/dist/transports/http-namespace.js.map +1 -0
- package/dist/transports/workshop.d.ts +173 -0
- package/dist/transports/workshop.d.ts.map +1 -0
- package/dist/transports/workshop.js +307 -0
- package/dist/transports/workshop.js.map +1 -0
- package/dist/types.d.ts +65 -67
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +7 -3
- package/dist/types.js.map +1 -1
- package/docs/ADR-004-product-registry.md +108 -0
- package/package.json +1 -1
|
@@ -0,0 +1,484 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module server/handlers/collections
|
|
3
|
+
* @description Collections namespace handler — view collection CRUD, member
|
|
4
|
+
* management, and sample data seeding.
|
|
5
|
+
*
|
|
6
|
+
* Absorbs Workshop `routes/collections.ts`. Collections are Brainy entities
|
|
7
|
+
* of type `Collection` with `isViewCollection: true` metadata. Members are
|
|
8
|
+
* linked via `Contains` relationships. Virtual collections (Everything,
|
|
9
|
+
* Uncategorized) are computed dynamically.
|
|
10
|
+
*
|
|
11
|
+
* Methods:
|
|
12
|
+
* - `list` — List all view collections (real + virtual)
|
|
13
|
+
* - `get` — Get a single collection by ID
|
|
14
|
+
* - `create` — Create a new collection (optionally with sample data)
|
|
15
|
+
* - `update` — Update collection name/metadata
|
|
16
|
+
* - `delete` — Delete a collection (members are NOT deleted)
|
|
17
|
+
* - `getMembers` — Get entities belonging to a collection
|
|
18
|
+
* - `addMember` — Add an entity to a collection
|
|
19
|
+
* - `removeMember` — Remove an entity from a collection
|
|
20
|
+
* - `loadSampleData` — Seed sample data for a kit
|
|
21
|
+
* - `clearSampleData` — Remove all sample data entities
|
|
22
|
+
*/
|
|
23
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
24
|
+
// Helpers
|
|
25
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
26
|
+
/** Virtual collection IDs. */
|
|
27
|
+
const VIRTUAL_IDS = {
|
|
28
|
+
EXPLORE: '__virtual_explore__',
|
|
29
|
+
UNCATEGORIZED: '__virtual_uncategorized__',
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* Check if a collection ID is a virtual collection.
|
|
33
|
+
*
|
|
34
|
+
* @param id - Collection ID to check.
|
|
35
|
+
* @returns `true` if virtual.
|
|
36
|
+
*/
|
|
37
|
+
function isVirtual(id) {
|
|
38
|
+
return id.startsWith('__virtual_');
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Get the list of virtual collections.
|
|
42
|
+
*
|
|
43
|
+
* @returns Virtual collection definitions.
|
|
44
|
+
*/
|
|
45
|
+
function getVirtualCollections() {
|
|
46
|
+
return [
|
|
47
|
+
{
|
|
48
|
+
id: VIRTUAL_IDS.EXPLORE,
|
|
49
|
+
name: 'Everything',
|
|
50
|
+
type: 'virtual',
|
|
51
|
+
memberCount: 0,
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
id: VIRTUAL_IDS.UNCATEGORIZED,
|
|
55
|
+
name: 'Uncategorized',
|
|
56
|
+
type: 'virtual',
|
|
57
|
+
memberCount: 0,
|
|
58
|
+
},
|
|
59
|
+
];
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Extract a display name from a Brainy entity.
|
|
63
|
+
*
|
|
64
|
+
* @param entity - Brainy entity.
|
|
65
|
+
* @returns Display name.
|
|
66
|
+
*/
|
|
67
|
+
function extractName(entity) {
|
|
68
|
+
const meta = entity.metadata || {};
|
|
69
|
+
return meta.name || meta.label || (typeof entity.data === 'string' ? entity.data : entity.id);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Check if a relationship type is a "Contains" relationship.
|
|
73
|
+
*
|
|
74
|
+
* @param type - Relationship type string.
|
|
75
|
+
* @returns `true` if it's a contains relationship.
|
|
76
|
+
*/
|
|
77
|
+
function isContainsRelation(type) {
|
|
78
|
+
const lower = type.toLowerCase();
|
|
79
|
+
return lower === 'contains' || lower === 'has' || lower === 'includes';
|
|
80
|
+
}
|
|
81
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
82
|
+
// Handler factory
|
|
83
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
84
|
+
/**
|
|
85
|
+
* Creates the `collections` namespace handler.
|
|
86
|
+
*
|
|
87
|
+
* @param options - Optional collections handler configuration.
|
|
88
|
+
* @returns A {@link NamespaceProvider} implementing {@link CollectionsNamespace}.
|
|
89
|
+
*/
|
|
90
|
+
export function createCollectionsHandler(options) {
|
|
91
|
+
const sampleData = options?.sampleData;
|
|
92
|
+
return {
|
|
93
|
+
/**
|
|
94
|
+
* List all view collections in the workspace.
|
|
95
|
+
*
|
|
96
|
+
* Returns virtual collections (Everything, Uncategorized) first, followed
|
|
97
|
+
* by user-created collections. Member counts are deferred (always 0 on list).
|
|
98
|
+
*
|
|
99
|
+
* @param listOptions - Filtering options.
|
|
100
|
+
* @param ctx - Handler context with Brainy instance.
|
|
101
|
+
* @returns Array of collections.
|
|
102
|
+
*/
|
|
103
|
+
async list(listOptions, ctx) {
|
|
104
|
+
const { brain } = ctx;
|
|
105
|
+
// Fetch real collections (Collection entities with isViewCollection metadata)
|
|
106
|
+
const results = await brain.find({
|
|
107
|
+
where: { type: 'Collection' },
|
|
108
|
+
limit: 1000,
|
|
109
|
+
});
|
|
110
|
+
const realCollections = results
|
|
111
|
+
.filter((entity) => {
|
|
112
|
+
const meta = entity.metadata || {};
|
|
113
|
+
return meta.isViewCollection === true;
|
|
114
|
+
})
|
|
115
|
+
.map((entity) => {
|
|
116
|
+
const meta = entity.metadata || {};
|
|
117
|
+
return {
|
|
118
|
+
id: entity.id,
|
|
119
|
+
name: extractName(entity),
|
|
120
|
+
type: meta.viewType || 'graph',
|
|
121
|
+
memberCount: 0, // Deferred — too slow on list
|
|
122
|
+
metadata: {
|
|
123
|
+
icon: meta.icon,
|
|
124
|
+
color: meta.color,
|
|
125
|
+
defaultNounType: meta.defaultNounType,
|
|
126
|
+
createdAt: meta.createdAt,
|
|
127
|
+
},
|
|
128
|
+
};
|
|
129
|
+
});
|
|
130
|
+
// Apply type filter if specified
|
|
131
|
+
const typeFilter = listOptions?.type;
|
|
132
|
+
const filtered = typeFilter
|
|
133
|
+
? realCollections.filter(c => c.type === typeFilter)
|
|
134
|
+
: realCollections;
|
|
135
|
+
return [...getVirtualCollections(), ...filtered];
|
|
136
|
+
},
|
|
137
|
+
/**
|
|
138
|
+
* Get a single collection by ID.
|
|
139
|
+
*
|
|
140
|
+
* @param collectionId - Collection ID (real or virtual).
|
|
141
|
+
* @param ctx - Handler context.
|
|
142
|
+
* @returns The collection or `null`.
|
|
143
|
+
*/
|
|
144
|
+
async get(collectionId, ctx) {
|
|
145
|
+
// Handle virtual collections
|
|
146
|
+
if (isVirtual(collectionId)) {
|
|
147
|
+
const virtualDef = getVirtualCollections().find(v => v.id === collectionId);
|
|
148
|
+
if (!virtualDef)
|
|
149
|
+
return null;
|
|
150
|
+
// Count members for virtual collections
|
|
151
|
+
const allEntities = await ctx.brain.find({ limit: 10000, excludeVFS: true });
|
|
152
|
+
virtualDef.memberCount = allEntities.length;
|
|
153
|
+
return virtualDef;
|
|
154
|
+
}
|
|
155
|
+
// Real collection
|
|
156
|
+
const entity = await ctx.brain.get(collectionId);
|
|
157
|
+
if (!entity)
|
|
158
|
+
return null;
|
|
159
|
+
const meta = entity.metadata || {};
|
|
160
|
+
// Count members via outgoing Contains relationships
|
|
161
|
+
let memberCount = 0;
|
|
162
|
+
try {
|
|
163
|
+
const outgoing = await ctx.brain.getRelations({ from: collectionId }) || [];
|
|
164
|
+
memberCount = outgoing.filter((rel) => isContainsRelation(rel.type)).length;
|
|
165
|
+
}
|
|
166
|
+
catch {
|
|
167
|
+
// No relations
|
|
168
|
+
}
|
|
169
|
+
return {
|
|
170
|
+
id: entity.id,
|
|
171
|
+
name: extractName(entity),
|
|
172
|
+
type: meta.viewType || 'graph',
|
|
173
|
+
memberCount,
|
|
174
|
+
metadata: {
|
|
175
|
+
icon: meta.icon,
|
|
176
|
+
color: meta.color,
|
|
177
|
+
defaultNounType: meta.defaultNounType,
|
|
178
|
+
createdAt: meta.createdAt,
|
|
179
|
+
projectId: meta.projectId,
|
|
180
|
+
},
|
|
181
|
+
};
|
|
182
|
+
},
|
|
183
|
+
/**
|
|
184
|
+
* Create a new view collection.
|
|
185
|
+
*
|
|
186
|
+
* @param createOptions - Collection properties.
|
|
187
|
+
* @param ctx - Handler context.
|
|
188
|
+
* @returns The newly created collection.
|
|
189
|
+
*/
|
|
190
|
+
async create(createOptions, ctx) {
|
|
191
|
+
const { brain } = ctx;
|
|
192
|
+
const { name, type: viewType, metadata: extraMeta } = createOptions;
|
|
193
|
+
const collectionId = await brain.add({
|
|
194
|
+
type: 'collection',
|
|
195
|
+
data: name,
|
|
196
|
+
metadata: {
|
|
197
|
+
name,
|
|
198
|
+
label: name,
|
|
199
|
+
isViewCollection: true,
|
|
200
|
+
viewType,
|
|
201
|
+
viewInstanceId: `${viewType}-${Date.now()}`,
|
|
202
|
+
icon: extraMeta?.icon || 'folder',
|
|
203
|
+
color: extraMeta?.color || '#6366f1',
|
|
204
|
+
defaultNounType: extraMeta?.defaultNounType || 'concept',
|
|
205
|
+
createdAt: new Date().toISOString(),
|
|
206
|
+
...extraMeta,
|
|
207
|
+
},
|
|
208
|
+
});
|
|
209
|
+
return {
|
|
210
|
+
id: collectionId,
|
|
211
|
+
name,
|
|
212
|
+
type: viewType,
|
|
213
|
+
memberCount: 0,
|
|
214
|
+
metadata: extraMeta,
|
|
215
|
+
};
|
|
216
|
+
},
|
|
217
|
+
/**
|
|
218
|
+
* Update a collection's name or metadata.
|
|
219
|
+
*
|
|
220
|
+
* @param collectionId - Collection to update.
|
|
221
|
+
* @param updates - Fields to update.
|
|
222
|
+
* @param ctx - Handler context.
|
|
223
|
+
* @returns The updated collection.
|
|
224
|
+
*/
|
|
225
|
+
async update(collectionId, updates, ctx) {
|
|
226
|
+
if (isVirtual(collectionId)) {
|
|
227
|
+
throw new Error('Cannot modify virtual collections');
|
|
228
|
+
}
|
|
229
|
+
const { brain } = ctx;
|
|
230
|
+
const existing = await brain.get(collectionId);
|
|
231
|
+
if (!existing) {
|
|
232
|
+
throw new Error('Collection not found');
|
|
233
|
+
}
|
|
234
|
+
const existingMeta = existing.metadata || {};
|
|
235
|
+
const metadataUpdates = {
|
|
236
|
+
...existingMeta,
|
|
237
|
+
updatedAt: new Date().toISOString(),
|
|
238
|
+
};
|
|
239
|
+
if (updates.name) {
|
|
240
|
+
metadataUpdates.name = updates.name;
|
|
241
|
+
metadataUpdates.label = updates.name;
|
|
242
|
+
}
|
|
243
|
+
if (updates.metadata) {
|
|
244
|
+
Object.assign(metadataUpdates, updates.metadata);
|
|
245
|
+
}
|
|
246
|
+
await brain.update({
|
|
247
|
+
id: collectionId,
|
|
248
|
+
data: updates.name || existing.data,
|
|
249
|
+
metadata: metadataUpdates,
|
|
250
|
+
});
|
|
251
|
+
const updated = await brain.get(collectionId);
|
|
252
|
+
if (!updated)
|
|
253
|
+
throw new Error('Collection not found after update');
|
|
254
|
+
return {
|
|
255
|
+
id: updated.id,
|
|
256
|
+
name: extractName(updated),
|
|
257
|
+
type: (updated.metadata || {}).viewType || 'graph',
|
|
258
|
+
memberCount: 0,
|
|
259
|
+
metadata: updates.metadata,
|
|
260
|
+
};
|
|
261
|
+
},
|
|
262
|
+
/**
|
|
263
|
+
* Delete a collection. Member entities are NOT deleted.
|
|
264
|
+
*
|
|
265
|
+
* Removes all Contains relationships from the collection first,
|
|
266
|
+
* then deletes the collection entity itself.
|
|
267
|
+
*
|
|
268
|
+
* @param collectionId - Collection to delete.
|
|
269
|
+
* @param ctx - Handler context.
|
|
270
|
+
*/
|
|
271
|
+
async delete(collectionId, ctx) {
|
|
272
|
+
if (isVirtual(collectionId)) {
|
|
273
|
+
throw new Error('Cannot delete virtual collections');
|
|
274
|
+
}
|
|
275
|
+
const { brain } = ctx;
|
|
276
|
+
const existing = await brain.get(collectionId);
|
|
277
|
+
if (!existing) {
|
|
278
|
+
throw new Error('Collection not found');
|
|
279
|
+
}
|
|
280
|
+
// Remove all Contains relationships (release members)
|
|
281
|
+
const outgoing = await brain.getRelations({ from: collectionId }) || [];
|
|
282
|
+
for (const rel of outgoing) {
|
|
283
|
+
if (isContainsRelation(rel.type)) {
|
|
284
|
+
try {
|
|
285
|
+
await brain.unrelate(rel.id);
|
|
286
|
+
}
|
|
287
|
+
catch {
|
|
288
|
+
// Non-fatal — continue with deletion
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
await brain.delete(collectionId);
|
|
293
|
+
},
|
|
294
|
+
/**
|
|
295
|
+
* Get the entities belonging to a collection.
|
|
296
|
+
*
|
|
297
|
+
* @param collectionId - Collection to list members for.
|
|
298
|
+
* @param ctx - Handler context.
|
|
299
|
+
* @returns Array of member entity summaries.
|
|
300
|
+
*/
|
|
301
|
+
async getMembers(collectionId, ctx) {
|
|
302
|
+
const { brain } = ctx;
|
|
303
|
+
// Virtual "Everything" collection — return all non-VFS entities
|
|
304
|
+
if (collectionId === VIRTUAL_IDS.EXPLORE) {
|
|
305
|
+
const allEntities = await brain.find({ limit: 10000, excludeVFS: true });
|
|
306
|
+
return allEntities.map((entity) => ({
|
|
307
|
+
id: entity.id,
|
|
308
|
+
name: extractName(entity),
|
|
309
|
+
type: (entity.type || 'concept').toLowerCase(),
|
|
310
|
+
}));
|
|
311
|
+
}
|
|
312
|
+
// Virtual "Uncategorized" — entities not in any real collection
|
|
313
|
+
if (collectionId === VIRTUAL_IDS.UNCATEGORIZED) {
|
|
314
|
+
const allEntities = await brain.find({ limit: 10000, excludeVFS: true });
|
|
315
|
+
const collected = new Set();
|
|
316
|
+
// Find all entities that are members of any collection
|
|
317
|
+
const collections = await brain.find({ where: { type: 'Collection' }, limit: 1000 });
|
|
318
|
+
for (const col of collections) {
|
|
319
|
+
const meta = col.metadata || {};
|
|
320
|
+
if (!meta.isViewCollection)
|
|
321
|
+
continue;
|
|
322
|
+
const rels = await brain.getRelations({ from: col.id }) || [];
|
|
323
|
+
for (const rel of rels) {
|
|
324
|
+
if (isContainsRelation(rel.type))
|
|
325
|
+
collected.add(rel.to);
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
return allEntities
|
|
329
|
+
.filter((entity) => !collected.has(entity.id) && entity.type !== 'Collection')
|
|
330
|
+
.map((entity) => ({
|
|
331
|
+
id: entity.id,
|
|
332
|
+
name: extractName(entity),
|
|
333
|
+
type: (entity.type || 'concept').toLowerCase(),
|
|
334
|
+
}));
|
|
335
|
+
}
|
|
336
|
+
// Real collection — fetch via Contains relationships
|
|
337
|
+
const outgoing = await brain.getRelations({ from: collectionId }) || [];
|
|
338
|
+
const memberRels = outgoing.filter((rel) => isContainsRelation(rel.type));
|
|
339
|
+
const members = [];
|
|
340
|
+
for (const rel of memberRels) {
|
|
341
|
+
try {
|
|
342
|
+
const entity = await brain.get(rel.to);
|
|
343
|
+
if (entity) {
|
|
344
|
+
members.push({
|
|
345
|
+
id: entity.id,
|
|
346
|
+
name: extractName(entity),
|
|
347
|
+
type: (entity.type || 'concept').toLowerCase(),
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
catch {
|
|
352
|
+
// Entity may have been deleted
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
return members;
|
|
356
|
+
},
|
|
357
|
+
/**
|
|
358
|
+
* Add an entity to a collection.
|
|
359
|
+
*
|
|
360
|
+
* Includes duplicate prevention — won't create a second Contains
|
|
361
|
+
* relationship if one already exists.
|
|
362
|
+
*
|
|
363
|
+
* @param collectionId - Collection to add to.
|
|
364
|
+
* @param entityId - Entity to add.
|
|
365
|
+
* @param ctx - Handler context.
|
|
366
|
+
*/
|
|
367
|
+
async addMember(collectionId, entityId, ctx) {
|
|
368
|
+
if (isVirtual(collectionId)) {
|
|
369
|
+
throw new Error('Cannot add entities to virtual collections');
|
|
370
|
+
}
|
|
371
|
+
const { brain } = ctx;
|
|
372
|
+
// Verify both exist
|
|
373
|
+
const collection = await brain.get(collectionId);
|
|
374
|
+
if (!collection)
|
|
375
|
+
throw new Error('Collection not found');
|
|
376
|
+
const entity = await brain.get(entityId);
|
|
377
|
+
if (!entity)
|
|
378
|
+
throw new Error('Entity not found');
|
|
379
|
+
// Duplicate prevention
|
|
380
|
+
const existingRels = await brain.getRelations({ from: collectionId, to: entityId }) || [];
|
|
381
|
+
const alreadyContains = existingRels.some((rel) => isContainsRelation(rel.type));
|
|
382
|
+
if (alreadyContains)
|
|
383
|
+
return; // Idempotent
|
|
384
|
+
await brain.relate({ from: collectionId, to: entityId, type: 'contains' });
|
|
385
|
+
},
|
|
386
|
+
/**
|
|
387
|
+
* Remove an entity from a collection.
|
|
388
|
+
*
|
|
389
|
+
* @param collectionId - Collection to remove from.
|
|
390
|
+
* @param entityId - Entity to remove.
|
|
391
|
+
* @param ctx - Handler context.
|
|
392
|
+
*/
|
|
393
|
+
async removeMember(collectionId, entityId, ctx) {
|
|
394
|
+
if (isVirtual(collectionId)) {
|
|
395
|
+
throw new Error('Cannot remove entities from virtual collections');
|
|
396
|
+
}
|
|
397
|
+
const { brain } = ctx;
|
|
398
|
+
// Find the Contains relationship
|
|
399
|
+
const rels = await brain.getRelations({ from: collectionId, to: entityId }) || [];
|
|
400
|
+
const containsRel = rels.find((r) => isContainsRelation(r.type));
|
|
401
|
+
if (containsRel) {
|
|
402
|
+
await brain.unrelate(containsRel.id);
|
|
403
|
+
}
|
|
404
|
+
},
|
|
405
|
+
/**
|
|
406
|
+
* Load sample data for a given kit.
|
|
407
|
+
*
|
|
408
|
+
* Creates entities, adds them to a new collection, and creates
|
|
409
|
+
* relationships between them.
|
|
410
|
+
*
|
|
411
|
+
* @param kitId - Kit whose sample data to load.
|
|
412
|
+
* @param ctx - Handler context.
|
|
413
|
+
* @returns Count of entities created.
|
|
414
|
+
*/
|
|
415
|
+
async loadSampleData(kitId, ctx) {
|
|
416
|
+
if (!sampleData)
|
|
417
|
+
return { count: 0 };
|
|
418
|
+
const data = sampleData.getSampleData(kitId);
|
|
419
|
+
if (!data)
|
|
420
|
+
return { count: 0 };
|
|
421
|
+
const { brain } = ctx;
|
|
422
|
+
const nameToId = new Map();
|
|
423
|
+
// Create entities
|
|
424
|
+
for (const e of data.entities) {
|
|
425
|
+
try {
|
|
426
|
+
const entityId = await brain.add({
|
|
427
|
+
type: e.type,
|
|
428
|
+
data: e.name,
|
|
429
|
+
metadata: {
|
|
430
|
+
name: e.name,
|
|
431
|
+
description: e.description || '',
|
|
432
|
+
isSampleData: true,
|
|
433
|
+
...(e.properties || {}),
|
|
434
|
+
},
|
|
435
|
+
});
|
|
436
|
+
nameToId.set(e.name, entityId);
|
|
437
|
+
}
|
|
438
|
+
catch {
|
|
439
|
+
// Skip failing entities
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
// Create relationships
|
|
443
|
+
if (data.relationships) {
|
|
444
|
+
for (const rel of data.relationships) {
|
|
445
|
+
const fromId = nameToId.get(rel.from);
|
|
446
|
+
const toId = nameToId.get(rel.to);
|
|
447
|
+
if (!fromId || !toId)
|
|
448
|
+
continue;
|
|
449
|
+
try {
|
|
450
|
+
await brain.relate({ from: fromId, to: toId, type: rel.type });
|
|
451
|
+
}
|
|
452
|
+
catch {
|
|
453
|
+
// Skip failing relationships
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
return { count: nameToId.size };
|
|
458
|
+
},
|
|
459
|
+
/**
|
|
460
|
+
* Clear all sample data entities from the workspace.
|
|
461
|
+
*
|
|
462
|
+
* Only removes entities with `isSampleData: true` metadata — user-created
|
|
463
|
+
* entities are never affected.
|
|
464
|
+
*
|
|
465
|
+
* @param ctx - Handler context.
|
|
466
|
+
*/
|
|
467
|
+
async clearSampleData(ctx) {
|
|
468
|
+
const { brain } = ctx;
|
|
469
|
+
const allEntities = await brain.find({ limit: 50000 });
|
|
470
|
+
for (const entity of allEntities) {
|
|
471
|
+
const meta = entity.metadata || {};
|
|
472
|
+
if (meta.isSampleData === true) {
|
|
473
|
+
try {
|
|
474
|
+
await brain.delete(entity.id);
|
|
475
|
+
}
|
|
476
|
+
catch {
|
|
477
|
+
// Non-fatal
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
},
|
|
482
|
+
};
|
|
483
|
+
}
|
|
484
|
+
//# sourceMappingURL=collections.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"collections.js","sourceRoot":"","sources":["../../../src/server/handlers/collections.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AA+CH,gFAAgF;AAChF,UAAU;AACV,gFAAgF;AAEhF,8BAA8B;AAC9B,MAAM,WAAW,GAAG;IAClB,OAAO,EAAE,qBAAqB;IAC9B,aAAa,EAAE,2BAA2B;CAClC,CAAA;AAEV;;;;;GAKG;AACH,SAAS,SAAS,CAAC,EAAU;IAC3B,OAAO,EAAE,CAAC,UAAU,CAAC,YAAY,CAAC,CAAA;AACpC,CAAC;AAED;;;;GAIG;AACH,SAAS,qBAAqB;IAC5B,OAAO;QACL;YACE,EAAE,EAAE,WAAW,CAAC,OAAO;YACvB,IAAI,EAAE,YAAY;YAClB,IAAI,EAAE,SAAS;YACf,WAAW,EAAE,CAAC;SACf;QACD;YACE,EAAE,EAAE,WAAW,CAAC,aAAa;YAC7B,IAAI,EAAE,eAAe;YACrB,IAAI,EAAE,SAAS;YACf,WAAW,EAAE,CAAC;SACf;KACF,CAAA;AACH,CAAC;AAED;;;;;GAKG;AACH,SAAS,WAAW,CAAC,MAAW;IAC9B,MAAM,IAAI,GAAG,MAAM,CAAC,QAAQ,IAAI,EAAE,CAAA;IAClC,OAAO,IAAI,CAAC,IAAI,IAAI,IAAI,CAAC,KAAK,IAAI,CAAC,OAAO,MAAM,CAAC,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,MAAM,CAAC,EAAE,CAAC,CAAA;AAC/F,CAAC;AAED;;;;;GAKG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AAC,IAAI,EAAE,CAAA;QACjC,CAAC;QAED;;;;;;;WAOG;QACH,KAAK,CAAC,eAAe,CAAC,GAAmB;YACvC,MAAM,EAAE,KAAK,EAAE,GAAG,GAAG,CAAA;YACrB,MAAM,WAAW,GAAG,MAAM,KAAK,CAAC,IAAI,CAAC,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,CAAA;YAEtD,KAAK,MAAM,MAAM,IAAI,WAAW,EAAE,CAAC;gBACjC,MAAM,IAAI,GAAI,MAAc,CAAC,QAAQ,IAAI,EAAE,CAAA;gBAC3C,IAAI,IAAI,CAAC,YAAY,KAAK,IAAI,EAAE,CAAC;oBAC/B,IAAI,CAAC;wBACH,MAAM,KAAK,CAAC,MAAM,CAAE,MAAc,CAAC,EAAE,CAAC,CAAA;oBACxC,CAAC;oBAAC,MAAM,CAAC;wBACP,YAAY;oBACd,CAAC;gBACH,CAAC;YACH,CAAC;QACH,CAAC;KACF,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module server/handlers/commerce
|
|
3
|
+
* @description Commerce namespace handler — product management, checkout, and
|
|
4
|
+
* payment processing.
|
|
5
|
+
*
|
|
6
|
+
* Stripe-first implementation with a pluggable `PaymentProvider` interface.
|
|
7
|
+
* Products inject their Stripe account credentials via `providers.commerce`
|
|
8
|
+
* in the SDK server config.
|
|
9
|
+
*
|
|
10
|
+
* Methods:
|
|
11
|
+
* - `createProduct` — Create a new product with pricing
|
|
12
|
+
* - `listProducts` — List all products
|
|
13
|
+
* - `getProduct` — Get a single product by ID
|
|
14
|
+
* - `checkout` — Initiate a Stripe Checkout session
|
|
15
|
+
* - `getPayment` — Get a payment record
|
|
16
|
+
* - `listPayments` — List payments for the current user
|
|
17
|
+
*/
|
|
18
|
+
import type { NamespaceProvider } from '../namespace-router.js';
|
|
19
|
+
/**
|
|
20
|
+
* Payment provider interface — Stripe-first, pluggable for other processors.
|
|
21
|
+
*/
|
|
22
|
+
export interface PaymentProvider {
|
|
23
|
+
createProduct(options: {
|
|
24
|
+
name: string;
|
|
25
|
+
description?: string;
|
|
26
|
+
priceAmount: number;
|
|
27
|
+
currency: string;
|
|
28
|
+
images?: string[];
|
|
29
|
+
metadata?: Record<string, string>;
|
|
30
|
+
}): Promise<{
|
|
31
|
+
id: string;
|
|
32
|
+
name: string;
|
|
33
|
+
description: string;
|
|
34
|
+
priceAmount: number;
|
|
35
|
+
currency: string;
|
|
36
|
+
active: boolean;
|
|
37
|
+
createdAt: string;
|
|
38
|
+
}>;
|
|
39
|
+
listProducts(options?: {
|
|
40
|
+
active?: boolean;
|
|
41
|
+
limit?: number;
|
|
42
|
+
}): Promise<Array<{
|
|
43
|
+
id: string;
|
|
44
|
+
name: string;
|
|
45
|
+
description: string;
|
|
46
|
+
priceAmount: number;
|
|
47
|
+
currency: string;
|
|
48
|
+
active: boolean;
|
|
49
|
+
createdAt: string;
|
|
50
|
+
}>>;
|
|
51
|
+
getProduct(productId: string): Promise<{
|
|
52
|
+
id: string;
|
|
53
|
+
name: string;
|
|
54
|
+
description: string;
|
|
55
|
+
priceAmount: number;
|
|
56
|
+
currency: string;
|
|
57
|
+
active: boolean;
|
|
58
|
+
createdAt: string;
|
|
59
|
+
} | null>;
|
|
60
|
+
checkout(options: {
|
|
61
|
+
items: Array<{
|
|
62
|
+
productId: string;
|
|
63
|
+
quantity: number;
|
|
64
|
+
}>;
|
|
65
|
+
successUrl: string;
|
|
66
|
+
cancelUrl: string;
|
|
67
|
+
customerEmail?: string;
|
|
68
|
+
}): Promise<{
|
|
69
|
+
sessionId: string;
|
|
70
|
+
checkoutUrl: string;
|
|
71
|
+
}>;
|
|
72
|
+
getPayment(paymentId: string): Promise<{
|
|
73
|
+
id: string;
|
|
74
|
+
amount: number;
|
|
75
|
+
currency: string;
|
|
76
|
+
status: string;
|
|
77
|
+
customerEmail: string;
|
|
78
|
+
createdAt: string;
|
|
79
|
+
} | null>;
|
|
80
|
+
listPayments(options?: {
|
|
81
|
+
limit?: number;
|
|
82
|
+
customerEmail?: string;
|
|
83
|
+
}): Promise<Array<{
|
|
84
|
+
id: string;
|
|
85
|
+
amount: number;
|
|
86
|
+
currency: string;
|
|
87
|
+
status: string;
|
|
88
|
+
customerEmail: string;
|
|
89
|
+
createdAt: string;
|
|
90
|
+
}>>;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Options for {@link createCommerceHandler}.
|
|
94
|
+
*/
|
|
95
|
+
export interface CommerceHandlerOptions {
|
|
96
|
+
/** Payment provider (Stripe, etc.). */
|
|
97
|
+
paymentProvider: PaymentProvider;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Creates the `commerce` namespace handler.
|
|
101
|
+
*
|
|
102
|
+
* @param options - Commerce handler dependencies. If omitted, all methods throw.
|
|
103
|
+
* @returns A {@link NamespaceProvider} implementing {@link CommerceNamespace}.
|
|
104
|
+
*/
|
|
105
|
+
export declare function createCommerceHandler(options?: CommerceHandlerOptions): NamespaceProvider;
|
|
106
|
+
//# sourceMappingURL=commerce.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"commerce.d.ts","sourceRoot":"","sources":["../../../src/server/handlers/commerce.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAEH,OAAO,KAAK,EAAE,iBAAiB,EAAkB,MAAM,wBAAwB,CAAA;AAM/E;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B,aAAa,CAAC,OAAO,EAAE;QACrB,IAAI,EAAE,MAAM,CAAA;QACZ,WAAW,CAAC,EAAE,MAAM,CAAA;QACpB,WAAW,EAAE,MAAM,CAAA;QACnB,QAAQ,EAAE,MAAM,CAAA;QAChB,MAAM,CAAC,EAAE,MAAM,EAAE,CAAA;QACjB,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;KAClC,GAAG,OAAO,CAAC;QACV,EAAE,EAAE,MAAM,CAAA;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AAE,MAAM,GAAG,OAAO,CAAC;QACrC,EAAE,EAAE,MAAM,CAAA;QACV,MAAM,EAAE,MAAM,CAAA;QACd,QAAQ,EAAE,MAAM,CAAA;QAChB,MAAM,EAAE,MAAM,CAAA;QACd,aAAa,EAAE,MAAM,CAAA;QACrB,SAAS,EAAE,MAAM,CAAA;KAClB,GAAG,IAAI,CAAC,CAAA;IAET,YAAY,CAAC,OAAO,CAAC,EAAE;QAAE,KAAK,CAAC,EAAE,MAAM,CAAC;QAAC,aAAa,CAAC,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,KAAK,CAAC;QAChF,EAAE,EAAE,MAAM,CAAA;QACV,MAAM,EAAE,MAAM,CAAA;QACd,QAAQ,EAAE,MAAM,CAAA;QAChB,MAAM,EAAE,MAAM,CAAA;QACd,aAAa,EAAE,MAAM,CAAA;QACrB,SAAS,EAAE,MAAM,CAAA;KAClB,CAAC,CAAC,CAAA;CACJ;AAED;;GAEG;AACH,MAAM,WAAW,sBAAsB;IACrC,uCAAuC;IACvC,eAAe,EAAE,eAAe,CAAA;CACjC;AAMD;;;;;GAKG;AACH,wBAAgB,qBAAqB,CAAC,OAAO,CAAC,EAAE,sBAAsB,GAAG,iBAAiB,CA2CzF"}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module server/handlers/commerce
|
|
3
|
+
* @description Commerce namespace handler — product management, checkout, and
|
|
4
|
+
* payment processing.
|
|
5
|
+
*
|
|
6
|
+
* Stripe-first implementation with a pluggable `PaymentProvider` interface.
|
|
7
|
+
* Products inject their Stripe account credentials via `providers.commerce`
|
|
8
|
+
* in the SDK server config.
|
|
9
|
+
*
|
|
10
|
+
* Methods:
|
|
11
|
+
* - `createProduct` — Create a new product with pricing
|
|
12
|
+
* - `listProducts` — List all products
|
|
13
|
+
* - `getProduct` — Get a single product by ID
|
|
14
|
+
* - `checkout` — Initiate a Stripe Checkout session
|
|
15
|
+
* - `getPayment` — Get a payment record
|
|
16
|
+
* - `listPayments` — List payments for the current user
|
|
17
|
+
*/
|
|
18
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
19
|
+
// Handler factory
|
|
20
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
21
|
+
/**
|
|
22
|
+
* Creates the `commerce` namespace handler.
|
|
23
|
+
*
|
|
24
|
+
* @param options - Commerce handler dependencies. If omitted, all methods throw.
|
|
25
|
+
* @returns A {@link NamespaceProvider} implementing {@link CommerceNamespace}.
|
|
26
|
+
*/
|
|
27
|
+
export function createCommerceHandler(options) {
|
|
28
|
+
const provider = options?.paymentProvider;
|
|
29
|
+
function requireProvider() {
|
|
30
|
+
if (!provider) {
|
|
31
|
+
throw new Error('commerce namespace is not configured — provide a PaymentProvider via providers.commerce');
|
|
32
|
+
}
|
|
33
|
+
return provider;
|
|
34
|
+
}
|
|
35
|
+
return {
|
|
36
|
+
async createProduct(productOptions, ctx) {
|
|
37
|
+
return requireProvider().createProduct(productOptions);
|
|
38
|
+
},
|
|
39
|
+
async listProducts(listOptions) {
|
|
40
|
+
return requireProvider().listProducts(listOptions);
|
|
41
|
+
},
|
|
42
|
+
async getProduct(productId) {
|
|
43
|
+
return requireProvider().getProduct(productId);
|
|
44
|
+
},
|
|
45
|
+
async checkout(checkoutOptions, ctx) {
|
|
46
|
+
return requireProvider().checkout({
|
|
47
|
+
...checkoutOptions,
|
|
48
|
+
customerEmail: checkoutOptions.customerEmail || ctx.user.email,
|
|
49
|
+
});
|
|
50
|
+
},
|
|
51
|
+
async getPayment(paymentId) {
|
|
52
|
+
return requireProvider().getPayment(paymentId);
|
|
53
|
+
},
|
|
54
|
+
async listPayments(listOptions, ctx) {
|
|
55
|
+
return requireProvider().listPayments({
|
|
56
|
+
...listOptions,
|
|
57
|
+
customerEmail: ctx.user.email,
|
|
58
|
+
});
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
//# sourceMappingURL=commerce.js.map
|