Skip to content

Commit 9daeed9

Browse files
committed
Replace catalog bulk update with partial/replace APIs
1 parent 36b56cc commit 9daeed9

5 files changed

Lines changed: 425 additions & 148 deletions

File tree

‎src/client/catalogs.ts‎

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import type { AxiosInstance } from "axios";
2+
3+
import type { CatalogDocuments } from "../types/catalogs.js";
14
import {
25
BulkDeleteCatalogItemsParams,
36
CatalogFieldMappingsResponse,
@@ -16,9 +19,10 @@ import {
1619
GetCatalogsResponse,
1720
GetCatalogsResponseSchema,
1821
PartialUpdateCatalogItemParams,
22+
PartialUpdateCatalogItemsParams,
1923
ReplaceCatalogItemParams,
24+
ReplaceCatalogItemsParams,
2025
UpdateCatalogFieldMappingsParams,
21-
UpdateCatalogItemParams,
2226
} from "../types/catalogs.js";
2327
import {
2428
IterableSuccessResponse,
@@ -28,6 +32,19 @@ import type { Constructor } from "./base.js";
2832
import type { BaseIterableClient } from "./base.js";
2933
import { validateResponse } from "./base.js";
3034

35+
async function bulkUploadCatalogItems(
36+
client: AxiosInstance,
37+
catalogName: string,
38+
documents: CatalogDocuments,
39+
replaceUploadedFieldsOnly: boolean
40+
): Promise<IterableSuccessResponse> {
41+
const response = await client.post(
42+
`/api/catalogs/${encodeURIComponent(catalogName)}/items`,
43+
{ documents, replaceUploadedFieldsOnly }
44+
);
45+
return validateResponse(response, IterableSuccessResponseSchema);
46+
}
47+
3148
/**
3249
* Catalogs operations mixin
3350
*/
@@ -42,31 +59,32 @@ export function Catalogs<T extends Constructor<BaseIterableClient>>(Base: T) {
4259
return validateResponse(response, IterableSuccessResponseSchema);
4360
}
4461

45-
async updateCatalogItems(
46-
options: UpdateCatalogItemParams
62+
/**
63+
* Bulk partial update catalog items
64+
*/
65+
async partialUpdateCatalogItems(
66+
params: PartialUpdateCatalogItemsParams
4767
): Promise<IterableSuccessResponse> {
48-
// Convert items array to documents object (map of id to values)
49-
const documents: Record<string, any> = {};
50-
options.items.forEach((item) => {
51-
documents[item.id] = {
52-
name: item.name,
53-
description: item.description,
54-
price: item.price,
55-
categories: item.categories,
56-
imageUrl: item.imageUrl,
57-
url: item.url,
58-
...item.dataFields,
59-
};
60-
});
68+
return bulkUploadCatalogItems(
69+
this.client,
70+
params.catalogName,
71+
params.documents,
72+
true
73+
);
74+
}
6175

62-
const response = await this.client.post(
63-
`/api/catalogs/${encodeURIComponent(options.catalogName)}/items`,
64-
{
65-
documents,
66-
replaceUploadedFieldsOnly: false,
67-
}
76+
/**
77+
* Bulk replace catalog items
78+
*/
79+
async replaceCatalogItems(
80+
params: ReplaceCatalogItemsParams
81+
): Promise<IterableSuccessResponse> {
82+
return bulkUploadCatalogItems(
83+
this.client,
84+
params.catalogName,
85+
params.documents,
86+
false
6887
);
69-
return validateResponse(response, IterableSuccessResponseSchema);
7088
}
7189

7290
async getCatalogItem(

‎src/types/catalogs.ts‎

Lines changed: 84 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,22 @@ import { z } from "zod";
22

33
import { UnixTimestampSchema } from "./common.js";
44

5-
/**
6-
* Catalog management schemas and types
7-
*/
8-
9-
export const CatalogItemSchema = z.object({
10-
id: z.string(),
11-
name: z.string(),
12-
description: z.string().optional(),
13-
price: z.number().optional(),
14-
categories: z.array(z.string()).optional(),
15-
imageUrl: z.string().optional(),
16-
url: z.string().optional(),
17-
dataFields: z.record(z.string(), z.any()).optional(),
18-
});
19-
20-
export type CatalogItem = z.infer<typeof CatalogItemSchema>;
21-
export type UpdateCatalogItemParams = z.infer<
22-
typeof UpdateCatalogItemParamsSchema
23-
>;
24-
export type GetCatalogItemParams = z.infer<typeof GetCatalogItemParamsSchema>;
25-
export type DeleteCatalogItemParams = z.infer<
26-
typeof DeleteCatalogItemParamsSchema
27-
>;
28-
29-
export const UpdateCatalogItemParamsSchema = z.object({
30-
catalogName: z.string().describe("Name of the catalog"),
31-
items: z.array(CatalogItemSchema).describe("Catalog items to update"),
32-
});
33-
345
export const GetCatalogItemParamsSchema = z.object({
356
catalogName: z.string().describe("Name of the catalog"),
367
itemId: z.string().describe("ID of the catalog item to retrieve"),
378
});
389

10+
export type GetCatalogItemParams = z.infer<typeof GetCatalogItemParamsSchema>;
11+
3912
export const DeleteCatalogItemParamsSchema = z.object({
4013
catalogName: z.string().describe("Name of the catalog"),
4114
itemId: z.string().describe("ID of the catalog item to delete"),
4215
});
4316

17+
export type DeleteCatalogItemParams = z.infer<
18+
typeof DeleteCatalogItemParamsSchema
19+
>;
20+
4421
export const CatalogNameSchema = z.object({
4522
name: z.string(),
4623
});
@@ -193,6 +170,82 @@ export type UpdateCatalogFieldMappingsParams = z.infer<
193170
typeof UpdateCatalogFieldMappingsParamsSchema
194171
>;
195172

173+
const CATALOG_ITEM_ID_REGEX = /^[a-zA-Z0-9-]{1,255}$/;
174+
const MAX_CATALOG_BULK_DOCUMENTS = 1000;
175+
176+
export const CatalogDocumentFieldsSchema = z
177+
.record(z.string(), z.unknown())
178+
.describe("Catalog item field values");
179+
180+
export type CatalogDocumentFields = z.infer<typeof CatalogDocumentFieldsSchema>;
181+
182+
function validateCatalogDocuments(
183+
documents: Record<string, CatalogDocumentFields>,
184+
ctx: z.RefinementCtx
185+
): void {
186+
const itemIds = Object.keys(documents);
187+
188+
if (itemIds.length === 0) {
189+
ctx.addIssue({
190+
code: "custom",
191+
message: "documents must contain at least one catalog item",
192+
});
193+
return;
194+
}
195+
196+
if (itemIds.length > MAX_CATALOG_BULK_DOCUMENTS) {
197+
ctx.addIssue({
198+
code: "custom",
199+
message: `documents may contain at most ${MAX_CATALOG_BULK_DOCUMENTS} items`,
200+
});
201+
}
202+
203+
for (const itemId of itemIds) {
204+
if (!CATALOG_ITEM_ID_REGEX.test(itemId)) {
205+
ctx.addIssue({
206+
code: "custom",
207+
message: `Invalid catalog item ID "${itemId}" (alphanumeric and dashes only, max 255 chars)`,
208+
});
209+
}
210+
211+
for (const fieldName of Object.keys(documents[itemId] ?? {})) {
212+
if (fieldName.includes(".")) {
213+
ctx.addIssue({
214+
code: "custom",
215+
message: `Field name "${fieldName}" must not contain periods`,
216+
});
217+
}
218+
}
219+
}
220+
}
221+
222+
export const CatalogDocumentsSchema = z
223+
.record(z.string(), CatalogDocumentFieldsSchema)
224+
.superRefine(validateCatalogDocuments)
225+
.describe("Map of catalog item ID to field values");
226+
227+
export type CatalogDocuments = z.infer<typeof CatalogDocumentsSchema>;
228+
229+
const BulkCatalogItemsParamsSchema = z.object({
230+
catalogName: z.string().describe("Name of the catalog"),
231+
documents: CatalogDocumentsSchema,
232+
});
233+
234+
// Bulk partial update catalog items
235+
export const PartialUpdateCatalogItemsParamsSchema =
236+
BulkCatalogItemsParamsSchema;
237+
238+
export type PartialUpdateCatalogItemsParams = z.infer<
239+
typeof PartialUpdateCatalogItemsParamsSchema
240+
>;
241+
242+
// Bulk replace catalog items
243+
export const ReplaceCatalogItemsParamsSchema = BulkCatalogItemsParamsSchema;
244+
245+
export type ReplaceCatalogItemsParams = z.infer<
246+
typeof ReplaceCatalogItemsParamsSchema
247+
>;
248+
196249
// Bulk delete catalog items
197250
export const BulkDeleteCatalogItemsParamsSchema = z.object({
198251
catalogName: z.string().describe("Name of the catalog"),
@@ -207,7 +260,7 @@ export type BulkDeleteCatalogItemsParams = z.infer<
207260
export const PartialUpdateCatalogItemParamsSchema = z.object({
208261
catalogName: z.string().describe("Name of the catalog"),
209262
itemId: z.string().describe("ID of the catalog item"),
210-
update: z.record(z.string(), z.any()).describe("Fields to update"),
263+
update: CatalogDocumentFieldsSchema.describe("Fields to update"),
211264
});
212265

213266
export type PartialUpdateCatalogItemParams = z.infer<
@@ -218,7 +271,7 @@ export type PartialUpdateCatalogItemParams = z.infer<
218271
export const ReplaceCatalogItemParamsSchema = z.object({
219272
catalogName: z.string().describe("Name of the catalog"),
220273
itemId: z.string().describe("ID of the catalog item"),
221-
value: z.record(z.string(), z.any()).describe("New value for the item"),
274+
value: CatalogDocumentFieldsSchema.describe("New value for the item"),
222275
});
223276

224277
export type ReplaceCatalogItemParams = z.infer<

0 commit comments

Comments
 (0)