@nymbalhq/platform 0.2.0 → 1.1.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/{chunk-74KODSYJ.js → chunk-2GFZHF5W.js} +99 -16
- package/dist/chunk-2GFZHF5W.js.map +1 -0
- package/dist/index.d.ts +132 -63
- package/dist/index.js +1276 -823
- package/dist/index.js.map +1 -1
- package/dist/{register-IVFYBJSX.js → register-FJA7YJV3.js} +2 -2
- package/migrations/postgres/0007_native_search.sql +12 -0
- package/migrations/postgres/meta/0007_snapshot.json +1598 -0
- package/migrations/postgres/meta/_journal.json +7 -0
- package/migrations/sqlite/0007_native_search.sql +39 -0
- package/migrations/sqlite/meta/0007_snapshot.json +1470 -0
- package/migrations/sqlite/meta/_journal.json +7 -0
- package/package.json +48 -10
- package/recipes/postgres.json +17 -0
- package/dist/chunk-74KODSYJ.js.map +0 -1
- /package/dist/{register-IVFYBJSX.js.map → register-FJA7YJV3.js.map} +0 -0
|
@@ -195,25 +195,68 @@ function buildCategoryFacet(products) {
|
|
|
195
195
|
}))
|
|
196
196
|
};
|
|
197
197
|
}
|
|
198
|
-
function
|
|
198
|
+
async function searchCategoryFacet(facet, documentStore, storeName, query, filters) {
|
|
199
|
+
const counts = await facet("categoryIds", { query, ...filters !== void 0 && { filters } });
|
|
200
|
+
const wanted = new Set(counts.values.map((v) => v.value));
|
|
201
|
+
const byId = /* @__PURE__ */ new Map();
|
|
202
|
+
let cursor;
|
|
203
|
+
while (byId.size < wanted.size) {
|
|
204
|
+
const page = await documentStore.query(
|
|
205
|
+
"categories",
|
|
206
|
+
{
|
|
207
|
+
partitionKey: { field: "storeId", value: storeName },
|
|
208
|
+
limit: 500,
|
|
209
|
+
...cursor !== void 0 && { cursor }
|
|
210
|
+
}
|
|
211
|
+
);
|
|
212
|
+
for (const c of page.items) if (wanted.has(c.id)) byId.set(c.id, c);
|
|
213
|
+
if (page.nextCursor === null) break;
|
|
214
|
+
cursor = page.nextCursor;
|
|
215
|
+
}
|
|
216
|
+
return {
|
|
217
|
+
field: "category",
|
|
218
|
+
label: "Category",
|
|
219
|
+
values: counts.values.flatMap((v) => {
|
|
220
|
+
const category = byId.get(v.value);
|
|
221
|
+
return category === void 0 ? [] : [
|
|
222
|
+
{
|
|
223
|
+
value: category.slug,
|
|
224
|
+
label: category.name,
|
|
225
|
+
...v.count !== void 0 && { count: v.count }
|
|
226
|
+
}
|
|
227
|
+
];
|
|
228
|
+
})
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
function registerProductRoutes(adapter, documentStore, storeName, reviewsAdapter, searchService, canFacet = () => true, searchFacet) {
|
|
199
232
|
adapter.registerRoute("GET", "/api/products/search", async (ctx) => {
|
|
200
233
|
try {
|
|
234
|
+
const nothing = { items: [], nextCursor: null, total: 0, facets: [] };
|
|
201
235
|
const rawQ = ctx.query.q;
|
|
202
|
-
const q = Array.isArray(rawQ) ? rawQ[0] : rawQ;
|
|
203
|
-
if (!q || !
|
|
204
|
-
if (!searchService) return ok({ items: [], nextCursor: null, facets: [] });
|
|
236
|
+
const q = (Array.isArray(rawQ) ? rawQ[0] : rawQ)?.trim();
|
|
237
|
+
if (!q || !searchService) return ok(nothing);
|
|
205
238
|
const rawLimit = ctx.query.limit;
|
|
206
239
|
const limit = rawLimit ? Math.min(100, Math.max(1, Number(Array.isArray(rawLimit) ? rawLimit[0] : rawLimit) || 20)) : 20;
|
|
207
240
|
const rawCursor = ctx.query.cursor;
|
|
208
241
|
const cursor = Array.isArray(rawCursor) ? rawCursor[0] : rawCursor;
|
|
209
|
-
const
|
|
242
|
+
const rawCategory = ctx.query.category;
|
|
243
|
+
const categorySlug = Array.isArray(rawCategory) ? rawCategory[0] : rawCategory;
|
|
244
|
+
let filters;
|
|
245
|
+
if (categorySlug) {
|
|
246
|
+
const category = await documentStore.get("categories", categorySlug);
|
|
247
|
+
if (!category) return ok(nothing);
|
|
248
|
+
filters = { categoryIds: category.id };
|
|
249
|
+
}
|
|
250
|
+
const searchResult = await searchService.products(q, {
|
|
210
251
|
limit,
|
|
211
|
-
...cursor !== void 0 && { cursor }
|
|
252
|
+
...cursor !== void 0 && { cursor },
|
|
253
|
+
...filters !== void 0 && { filters }
|
|
212
254
|
});
|
|
213
255
|
const slugs = searchResult.items.map((doc) => String(doc.fields?.slug ?? "")).filter(Boolean);
|
|
214
256
|
const products = await Promise.all(slugs.map((slug) => documentStore.get("products", slug)));
|
|
215
257
|
const items = products.filter(Boolean);
|
|
216
|
-
|
|
258
|
+
const facets = canFacet() && searchFacet !== void 0 && searchResult.total > 0 ? [await searchCategoryFacet(searchFacet, documentStore, storeName, q, filters)] : [];
|
|
259
|
+
return ok({ items, nextCursor: searchResult.nextCursor, total: searchResult.total, facets });
|
|
217
260
|
} catch (err) {
|
|
218
261
|
return renderError(err);
|
|
219
262
|
}
|
|
@@ -242,6 +285,7 @@ function registerProductRoutes(adapter, documentStore, storeName, reviewsAdapter
|
|
|
242
285
|
limit,
|
|
243
286
|
...cursor !== void 0 && { cursor }
|
|
244
287
|
}));
|
|
288
|
+
if (!canFacet()) return ok({ items: result.items, nextCursor: result.nextCursor });
|
|
245
289
|
const allResult = await documentStore.query("products", {
|
|
246
290
|
partitionKey: { field: "storeId", value: storeName },
|
|
247
291
|
limit: 1e3
|
|
@@ -283,6 +327,33 @@ function registerProductRoutes(adapter, documentStore, storeName, reviewsAdapter
|
|
|
283
327
|
});
|
|
284
328
|
}
|
|
285
329
|
|
|
330
|
+
// src/routes/capabilities.ts
|
|
331
|
+
import {
|
|
332
|
+
DEFAULT_PRODUCT_SORT as DEFAULT_PRODUCT_SORT2,
|
|
333
|
+
PRODUCT_SORT_KEYS
|
|
334
|
+
} from "@nymbalhq/types";
|
|
335
|
+
function storeCapabilities(adapters) {
|
|
336
|
+
const client = adapters.payments.client?.() ?? null;
|
|
337
|
+
return {
|
|
338
|
+
payments: {
|
|
339
|
+
capabilities: [...adapters.payments.capabilities()],
|
|
340
|
+
client: client === null ? null : { driver: client.driver, options: { ...client.options } }
|
|
341
|
+
},
|
|
342
|
+
search: { capabilities: [...adapters.search.capabilities()] },
|
|
343
|
+
// The document store runs the listing's sort, so these are the catalogue's keys, not the search adapter's.
|
|
344
|
+
catalog: { sortKeys: [...PRODUCT_SORT_KEYS], defaultSort: DEFAULT_PRODUCT_SORT2 }
|
|
345
|
+
};
|
|
346
|
+
}
|
|
347
|
+
function registerCapabilitiesRoute(adapter, adapters) {
|
|
348
|
+
adapter.registerRoute("GET", "/api/capabilities", async () => {
|
|
349
|
+
try {
|
|
350
|
+
return ok(storeCapabilities(adapters));
|
|
351
|
+
} catch (err) {
|
|
352
|
+
return renderError(err);
|
|
353
|
+
}
|
|
354
|
+
});
|
|
355
|
+
}
|
|
356
|
+
|
|
286
357
|
// src/routes/categories.ts
|
|
287
358
|
function registerCategoryRoutes(adapter, documentStore, storeName) {
|
|
288
359
|
adapter.registerRoute("GET", "/api/categories", async () => {
|
|
@@ -1894,10 +1965,14 @@ function registerEventsSseRoute(adapter, deps) {
|
|
|
1894
1965
|
stream.send({
|
|
1895
1966
|
id: event.id,
|
|
1896
1967
|
event: event.type,
|
|
1897
|
-
data: {
|
|
1968
|
+
data: {
|
|
1969
|
+
id: event.id,
|
|
1970
|
+
type: event.type,
|
|
1971
|
+
timestamp: event.timestamp,
|
|
1972
|
+
payload: event.payload
|
|
1973
|
+
}
|
|
1898
1974
|
});
|
|
1899
|
-
}
|
|
1900
|
-
{ name: "sse-admin-events" }
|
|
1975
|
+
}
|
|
1901
1976
|
);
|
|
1902
1977
|
stream.onClose(() => {
|
|
1903
1978
|
subscription.unsubscribe().catch(() => void 0);
|
|
@@ -1984,7 +2059,7 @@ import {
|
|
|
1984
2059
|
|
|
1985
2060
|
// src/routes/schemas.ts
|
|
1986
2061
|
import { z } from "zod";
|
|
1987
|
-
import { PRODUCT_SORT_KEYS } from "@nymbalhq/types";
|
|
2062
|
+
import { PRODUCT_SORT_KEYS as PRODUCT_SORT_KEYS2 } from "@nymbalhq/types";
|
|
1988
2063
|
var id = z.string().min(1);
|
|
1989
2064
|
var queryValue = z.union([z.string(), z.array(z.string())]).optional();
|
|
1990
2065
|
var paging = { limit: queryValue, offset: queryValue };
|
|
@@ -2066,6 +2141,11 @@ var routeSchemas = {
|
|
|
2066
2141
|
description: "Revoke the refresh token and clear its cookie.",
|
|
2067
2142
|
body: optionalRefresh
|
|
2068
2143
|
},
|
|
2144
|
+
// Store
|
|
2145
|
+
"GET /api/capabilities": {
|
|
2146
|
+
name: "capabilities.get",
|
|
2147
|
+
description: "What the store can do, for a storefront: payments client half, search capabilities and sort keys."
|
|
2148
|
+
},
|
|
2069
2149
|
// Catalogue
|
|
2070
2150
|
"GET /api/categories": {
|
|
2071
2151
|
name: "categories.list",
|
|
@@ -2073,8 +2153,8 @@ var routeSchemas = {
|
|
|
2073
2153
|
},
|
|
2074
2154
|
"GET /api/products/search": {
|
|
2075
2155
|
name: "products.search",
|
|
2076
|
-
description: "Search products by text.",
|
|
2077
|
-
query: z.object({ q: queryValue, limit: queryValue, cursor: queryValue })
|
|
2156
|
+
description: "Search products by text, optionally in one category, in relevance order, with the total and the category facet.",
|
|
2157
|
+
query: z.object({ q: queryValue, limit: queryValue, cursor: queryValue, category: queryValue })
|
|
2078
2158
|
},
|
|
2079
2159
|
"GET /api/products": {
|
|
2080
2160
|
name: "products.list",
|
|
@@ -2083,7 +2163,7 @@ var routeSchemas = {
|
|
|
2083
2163
|
limit: queryValue,
|
|
2084
2164
|
cursor: queryValue,
|
|
2085
2165
|
category: queryValue,
|
|
2086
|
-
sort: z.enum(
|
|
2166
|
+
sort: z.enum(PRODUCT_SORT_KEYS2).optional()
|
|
2087
2167
|
})
|
|
2088
2168
|
},
|
|
2089
2169
|
"GET /api/products/:slug": {
|
|
@@ -2512,13 +2592,16 @@ function registerAllRoutes(deps) {
|
|
|
2512
2592
|
const adapter = validatedAdapter(deps.adapter);
|
|
2513
2593
|
adapter.registerMiddleware(createAuthMiddleware(services.auth));
|
|
2514
2594
|
registerAuthRoutes(adapter, services.auth);
|
|
2595
|
+
registerCapabilitiesRoute(adapter, adapters);
|
|
2515
2596
|
registerCategoryRoutes(adapter, documentStore, config.store.name);
|
|
2516
2597
|
registerProductRoutes(
|
|
2517
2598
|
adapter,
|
|
2518
2599
|
documentStore,
|
|
2519
2600
|
config.store.name,
|
|
2520
2601
|
adapters.reviews,
|
|
2521
|
-
services.search
|
|
2602
|
+
services.search,
|
|
2603
|
+
() => adapters.search.capabilities().includes("facet"),
|
|
2604
|
+
(field, params) => adapters.search.facet(field, params)
|
|
2522
2605
|
);
|
|
2523
2606
|
registerCartRoutes(adapter, {
|
|
2524
2607
|
cart: services.cart,
|
|
@@ -2561,4 +2644,4 @@ export {
|
|
|
2561
2644
|
routeSchemas,
|
|
2562
2645
|
registerAllRoutes
|
|
2563
2646
|
};
|
|
2564
|
-
//# sourceMappingURL=chunk-
|
|
2647
|
+
//# sourceMappingURL=chunk-2GFZHF5W.js.map
|