Skip to content

Commit 284aef2

Browse files
authored
Replace catalog bulk update with partial/replace APIs (#26)
1 parent 36b56cc commit 284aef2

5 files changed

Lines changed: 488 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: 102 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,84 @@ 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-
>;
5+
const CATALOG_ITEM_ID_REGEX = /^[a-zA-Z0-9-]{1,255}$/;
6+
const MAX_CATALOG_BULK_DOCUMENTS = 1000;
7+
8+
export const CatalogItemIdSchema = z
9+
.string()
10+
.regex(
11+
CATALOG_ITEM_ID_REGEX,
12+
"Invalid catalog item ID (alphanumeric and dashes only, max 255 chars)"
13+
);
2814

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-
});
15+
function validateCatalogItemFieldNames(
16+
fields: Record<string, unknown>,
17+
ctx: z.RefinementCtx
18+
): void {
19+
for (const fieldName of Object.keys(fields)) {
20+
if (fieldName.includes(".")) {
21+
ctx.addIssue({
22+
code: "custom",
23+
message: `Field name "${fieldName}" must not contain periods`,
24+
});
25+
}
26+
}
27+
}
28+
29+
export const CatalogDocumentFieldsSchema = z
30+
.record(z.string(), z.unknown())
31+
.superRefine(validateCatalogItemFieldNames)
32+
.describe("Catalog item field values");
33+
34+
export type CatalogDocumentFields = z.infer<typeof CatalogDocumentFieldsSchema>;
35+
36+
function validateCatalogDocuments(
37+
documents: Record<string, CatalogDocumentFields>,
38+
ctx: z.RefinementCtx
39+
): void {
40+
const itemIds = Object.keys(documents);
41+
42+
if (itemIds.length === 0) {
43+
ctx.addIssue({
44+
code: "custom",
45+
message: "documents must contain at least one catalog item",
46+
});
47+
return;
48+
}
49+
50+
if (itemIds.length > MAX_CATALOG_BULK_DOCUMENTS) {
51+
ctx.addIssue({
52+
code: "custom",
53+
message: `documents may contain at most ${MAX_CATALOG_BULK_DOCUMENTS} items`,
54+
});
55+
}
56+
57+
for (const itemId of itemIds) {
58+
if (!CATALOG_ITEM_ID_REGEX.test(itemId)) {
59+
ctx.addIssue({
60+
code: "custom",
61+
message: `Invalid catalog item ID "${itemId}" (alphanumeric and dashes only, max 255 chars)`,
62+
});
63+
}
64+
}
65+
}
3366

3467
export const GetCatalogItemParamsSchema = z.object({
3568
catalogName: z.string().describe("Name of the catalog"),
36-
itemId: z.string().describe("ID of the catalog item to retrieve"),
69+
itemId: CatalogItemIdSchema.describe("ID of the catalog item to retrieve"),
3770
});
3871

72+
export type GetCatalogItemParams = z.infer<typeof GetCatalogItemParamsSchema>;
73+
3974
export const DeleteCatalogItemParamsSchema = z.object({
4075
catalogName: z.string().describe("Name of the catalog"),
41-
itemId: z.string().describe("ID of the catalog item to delete"),
76+
itemId: CatalogItemIdSchema.describe("ID of the catalog item to delete"),
4277
});
4378

79+
export type DeleteCatalogItemParams = z.infer<
80+
typeof DeleteCatalogItemParamsSchema
81+
>;
82+
4483
export const CatalogNameSchema = z.object({
4584
name: z.string(),
4685
});
@@ -193,10 +232,39 @@ export type UpdateCatalogFieldMappingsParams = z.infer<
193232
typeof UpdateCatalogFieldMappingsParamsSchema
194233
>;
195234

235+
export const CatalogDocumentsSchema = z
236+
.record(z.string(), CatalogDocumentFieldsSchema)
237+
.superRefine(validateCatalogDocuments)
238+
.describe("Map of catalog item ID to field values");
239+
240+
export type CatalogDocuments = z.infer<typeof CatalogDocumentsSchema>;
241+
242+
const BulkCatalogItemsParamsSchema = z.object({
243+
catalogName: z.string().describe("Name of the catalog"),
244+
documents: CatalogDocumentsSchema,
245+
});
246+
247+
// Bulk partial update catalog items
248+
export const PartialUpdateCatalogItemsParamsSchema =
249+
BulkCatalogItemsParamsSchema;
250+
251+
export type PartialUpdateCatalogItemsParams = z.infer<
252+
typeof PartialUpdateCatalogItemsParamsSchema
253+
>;
254+
255+
// Bulk replace catalog items
256+
export const ReplaceCatalogItemsParamsSchema = BulkCatalogItemsParamsSchema;
257+
258+
export type ReplaceCatalogItemsParams = z.infer<
259+
typeof ReplaceCatalogItemsParamsSchema
260+
>;
261+
196262
// Bulk delete catalog items
197263
export const BulkDeleteCatalogItemsParamsSchema = z.object({
198264
catalogName: z.string().describe("Name of the catalog"),
199-
itemIds: z.array(z.string()).describe("Array of item IDs to delete"),
265+
itemIds: z
266+
.array(CatalogItemIdSchema)
267+
.describe("Array of item IDs to delete"),
200268
});
201269

202270
export type BulkDeleteCatalogItemsParams = z.infer<
@@ -206,8 +274,8 @@ export type BulkDeleteCatalogItemsParams = z.infer<
206274
// Partial update catalog item (PATCH)
207275
export const PartialUpdateCatalogItemParamsSchema = z.object({
208276
catalogName: z.string().describe("Name of the catalog"),
209-
itemId: z.string().describe("ID of the catalog item"),
210-
update: z.record(z.string(), z.any()).describe("Fields to update"),
277+
itemId: CatalogItemIdSchema.describe("ID of the catalog item"),
278+
update: CatalogDocumentFieldsSchema.describe("Fields to update"),
211279
});
212280

213281
export type PartialUpdateCatalogItemParams = z.infer<
@@ -217,8 +285,8 @@ export type PartialUpdateCatalogItemParams = z.infer<
217285
// Replace catalog item (PUT)
218286
export const ReplaceCatalogItemParamsSchema = z.object({
219287
catalogName: z.string().describe("Name of the catalog"),
220-
itemId: z.string().describe("ID of the catalog item"),
221-
value: z.record(z.string(), z.any()).describe("New value for the item"),
288+
itemId: CatalogItemIdSchema.describe("ID of the catalog item"),
289+
value: CatalogDocumentFieldsSchema.describe("New value for the item"),
222290
});
223291

224292
export type ReplaceCatalogItemParams = z.infer<

0 commit comments

Comments
 (0)