Skip to content

Commit d4dd44c

Browse files
committed
Use params schemas for all methods
1 parent dd2ae2b commit d4dd44c

File tree

9 files changed

+94
-47
lines changed

9 files changed

+94
-47
lines changed

src/client/catalogs.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ import {
44
CatalogFieldMappingsResponseSchema,
55
CatalogItemWithProperties,
66
CatalogItemWithPropertiesSchema,
7+
CreateCatalogParams,
8+
DeleteCatalogItemParams,
9+
DeleteCatalogParams,
710
GetCatalogFieldMappingsParams,
11+
GetCatalogItemParams,
812
GetCatalogItemsParams,
913
GetCatalogItemsResponse,
1014
GetCatalogItemsResponseSchema,
@@ -28,9 +32,11 @@ import type { BaseIterableClient } from "./base.js";
2832
*/
2933
export function Catalogs<T extends Constructor<BaseIterableClient>>(Base: T) {
3034
return class extends Base {
31-
async createCatalog(catalogName: string): Promise<IterableSuccessResponse> {
35+
async createCatalog(
36+
params: CreateCatalogParams
37+
): Promise<IterableSuccessResponse> {
3238
const response = await this.client.post(
33-
`/api/catalogs/${encodeURIComponent(catalogName)}`
39+
`/api/catalogs/${encodeURIComponent(params.catalogName)}`
3440
);
3541
return this.validateResponse(response, IterableSuccessResponseSchema);
3642
}
@@ -63,21 +69,19 @@ export function Catalogs<T extends Constructor<BaseIterableClient>>(Base: T) {
6369
}
6470

6571
async getCatalogItem(
66-
catalogName: string,
67-
itemId: string
72+
params: GetCatalogItemParams
6873
): Promise<CatalogItemWithProperties> {
6974
const response = await this.client.get(
70-
`/api/catalogs/${encodeURIComponent(catalogName)}/items/${encodeURIComponent(itemId)}`
75+
`/api/catalogs/${encodeURIComponent(params.catalogName)}/items/${encodeURIComponent(params.itemId)}`
7176
);
7277
return this.validateResponse(response, CatalogItemWithPropertiesSchema);
7378
}
7479

7580
async deleteCatalogItem(
76-
catalogName: string,
77-
itemId: string
81+
params: DeleteCatalogItemParams
7882
): Promise<IterableSuccessResponse> {
7983
const response = await this.client.delete(
80-
`/api/catalogs/${encodeURIComponent(catalogName)}/items/${encodeURIComponent(itemId)}`
84+
`/api/catalogs/${encodeURIComponent(params.catalogName)}/items/${encodeURIComponent(params.itemId)}`
8185
);
8286
return this.validateResponse(response, IterableSuccessResponseSchema);
8387
}
@@ -168,9 +172,11 @@ export function Catalogs<T extends Constructor<BaseIterableClient>>(Base: T) {
168172
/**
169173
* Delete a catalog
170174
*/
171-
async deleteCatalog(catalogName: string): Promise<IterableSuccessResponse> {
175+
async deleteCatalog(
176+
params: DeleteCatalogParams
177+
): Promise<IterableSuccessResponse> {
172178
const response = await this.client.delete(
173-
`/api/catalogs/${encodeURIComponent(catalogName)}`
179+
`/api/catalogs/${encodeURIComponent(params.catalogName)}`
174180
);
175181
return this.validateResponse(response, IterableSuccessResponseSchema);
176182
}

src/client/lists.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
CreateListParams,
77
CreateListResponse,
88
CreateListResponseSchema,
9+
DeleteListParams,
910
GetListPreviewUsersParams,
1011
GetListPreviewUsersResponse,
1112
GetListPreviewUsersResponseSchema,
@@ -91,8 +92,8 @@ export function Lists<T extends Constructor<BaseIterableClient>>(Base: T) {
9192
return this.validateResponse(response, CreateListResponseSchema);
9293
}
9394

94-
async deleteList(listId: number): Promise<IterableSuccessResponse> {
95-
const response = await this.client.delete(`/api/lists/${listId}`);
95+
async deleteList(params: DeleteListParams): Promise<IterableSuccessResponse> {
96+
const response = await this.client.delete(`/api/lists/${params.listId}`);
9697
return this.validateResponse(response, IterableSuccessResponseSchema);
9798
}
9899

src/client/templates.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
BulkDeleteTemplatesResponseSchema,
1212
EmailTemplate,
1313
EmailTemplateSchema,
14+
GetTemplateByClientIdParams,
1415
GetTemplateByClientIdResponse,
1516
GetTemplateByClientIdResponseSchema,
1617
GetTemplateParams,
@@ -145,12 +146,12 @@ export function Templates<T extends Constructor<BaseIterableClient>>(Base: T) {
145146
}
146147

147148
async getTemplateByClientId(
148-
clientTemplateId: string
149+
params: GetTemplateByClientIdParams
149150
): Promise<GetTemplateByClientIdResponse> {
150151
const response = await this.client.get(
151152
"/api/templates/getByClientTemplateId",
152153
{
153-
params: { clientTemplateId },
154+
params: { clientTemplateId: params.clientTemplateId },
154155
}
155156
);
156157
return this.validateResponse(
@@ -174,9 +175,9 @@ export function Templates<T extends Constructor<BaseIterableClient>>(Base: T) {
174175
* @deprecated Use {@link bulkDeleteTemplates} instead
175176
*/
176177
async deleteTemplates(
177-
templateIds: number[]
178+
params: BulkDeleteTemplatesParams
178179
): Promise<BulkDeleteTemplatesResponse> {
179-
return this.bulkDeleteTemplates({ ids: templateIds });
180+
return this.bulkDeleteTemplates(params);
180181
}
181182

182183
// Email Template Management

src/client/users.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@ import {
88
} from "../types/lists.js";
99
import {
1010
BulkUpdateUsersParams,
11+
DeleteUserByEmailParams,
12+
DeleteUserByUserIdParams,
1113
GetSentMessagesParams,
1214
GetSentMessagesResponse,
1315
GetSentMessagesResponseSchema,
16+
GetUserByEmailParams,
17+
GetUserByIdParams,
1418
GetUserFieldsResponse,
1519
GetUserFieldsResponseSchema,
1620
UpdateEmailParams,
@@ -30,12 +34,10 @@ export function Users<T extends Constructor<BaseIterableClient>>(Base: T) {
3034
* Get a user by email address
3135
*/
3236
async getUserByEmail(
33-
email: string,
34-
opts?: { signal?: AbortSignal }
37+
params: GetUserByEmailParams
3538
): Promise<UserResponse> {
3639
const response = await this.client.get(
37-
`/api/users/${encodeURIComponent(email)}`,
38-
opts?.signal ? { signal: opts.signal } : {}
40+
`/api/users/${encodeURIComponent(params.email)}`
3941
);
4042
return this.validateResponse(response, UserResponseSchema);
4143
}
@@ -44,12 +46,10 @@ export function Users<T extends Constructor<BaseIterableClient>>(Base: T) {
4446
* Get a user by userId
4547
*/
4648
async getUserByUserId(
47-
userId: string,
48-
opts?: { signal?: AbortSignal }
49+
params: GetUserByIdParams
4950
): Promise<UserResponse> {
5051
const response = await this.client.get(
51-
`/api/users/byUserId/${encodeURIComponent(userId)}`,
52-
opts?.signal ? { signal: opts.signal } : {}
52+
`/api/users/byUserId/${encodeURIComponent(params.userId)}`
5353
);
5454
return this.validateResponse(response, UserResponseSchema);
5555
}
@@ -69,9 +69,11 @@ export function Users<T extends Constructor<BaseIterableClient>>(Base: T) {
6969
* Delete a user by email address
7070
* Asynchronous operation - does not prevent future data collection
7171
*/
72-
async deleteUserByEmail(email: string): Promise<IterableSuccessResponse> {
72+
async deleteUserByEmail(
73+
params: DeleteUserByEmailParams
74+
): Promise<IterableSuccessResponse> {
7375
const response = await this.client.delete(
74-
`/api/users/${encodeURIComponent(email)}`
76+
`/api/users/${encodeURIComponent(params.email)}`
7577
);
7678
return this.validateResponse(response, IterableSuccessResponseSchema);
7779
}
@@ -81,9 +83,11 @@ export function Users<T extends Constructor<BaseIterableClient>>(Base: T) {
8183
* Asynchronous operation - does not prevent future data collection
8284
* If multiple users share the same userId, they'll all be deleted
8385
*/
84-
async deleteUserByUserId(userId: string): Promise<IterableSuccessResponse> {
86+
async deleteUserByUserId(
87+
params: DeleteUserByUserIdParams
88+
): Promise<IterableSuccessResponse> {
8589
const response = await this.client.delete(
86-
`/api/users/byUserId/${encodeURIComponent(userId)}`
90+
`/api/users/byUserId/${encodeURIComponent(params.userId)}`
8791
);
8892
return this.validateResponse(response, IterableSuccessResponseSchema);
8993
}

src/types/catalogs.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,16 @@ export type GetCatalogFieldMappingsParams = z.infer<
109109
>;
110110
export type CreateCatalogParams = z.infer<typeof CreateCatalogParamsSchema>;
111111

112+
export const DeleteCatalogParamsSchema = z.object({
113+
catalogName: z
114+
.string()
115+
.max(255)
116+
.regex(/^[a-zA-Z0-9-]+$/)
117+
.describe("Name of the catalog to delete"),
118+
});
119+
120+
export type DeleteCatalogParams = z.infer<typeof DeleteCatalogParamsSchema>;
121+
112122
// Catalog items schemas
113123
export const CatalogItemWithPropertiesSchema = z.object({
114124
catalogName: z.string(),

src/types/users.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ export const GetUserByEmailParamsSchema = z.object({
3535
email: z.email().describe("Email address of the user to retrieve"),
3636
});
3737

38+
export type GetUserByEmailParams = z.infer<typeof GetUserByEmailParamsSchema>;
39+
3840
export const UpdateUserParamsSchema = z
3941
.object({
4042
email: z.email().optional().describe("User email address"),
@@ -69,6 +71,8 @@ export const GetUserByIdParamsSchema = z.object({
6971
userId: z.string().describe("User ID to retrieve"),
7072
});
7173

74+
export type GetUserByIdParams = z.infer<typeof GetUserByIdParamsSchema>;
75+
7276
export const BulkUpdateUsersParamsSchema = z.object({
7377
users: z
7478
.array(
@@ -95,10 +99,18 @@ export const DeleteUserByEmailParamsSchema = z.object({
9599
email: z.email().describe("Email address of the user to delete"),
96100
});
97101

102+
export type DeleteUserByEmailParams = z.infer<
103+
typeof DeleteUserByEmailParamsSchema
104+
>;
105+
98106
export const DeleteUserByUserIdParamsSchema = z.object({
99107
userId: z.string().describe("User ID of the user to delete"),
100108
});
101109

110+
export type DeleteUserByUserIdParams = z.infer<
111+
typeof DeleteUserByUserIdParamsSchema
112+
>;
113+
102114
export const UserEventsResponseSchema = z.object({
103115
events: z.array(z.record(z.string(), z.any())),
104116
});

tests/unit/catalogs.test.ts

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ describe("Catalog Operations", () => {
3939
};
4040
mockAxiosInstance.post.mockResolvedValue(mockResponse);
4141

42-
const result = await client.createCatalog("test-catalog");
42+
const result = await client.createCatalog({ catalogName: "test-catalog" });
4343

4444
expect(mockAxiosInstance.post).toHaveBeenCalledWith(
4545
"/api/catalogs/test-catalog"
@@ -56,7 +56,7 @@ describe("Catalog Operations", () => {
5656
};
5757
mockAxiosInstance.post.mockResolvedValue(mockResponse);
5858

59-
await client.createCatalog("test catalog/with+special");
59+
await client.createCatalog({ catalogName: "test catalog/with+special" });
6060

6161
expect(mockAxiosInstance.post).toHaveBeenCalledWith(
6262
"/api/catalogs/test%20catalog%2Fwith%2Bspecial"
@@ -99,7 +99,10 @@ describe("Catalog Operations", () => {
9999
};
100100
mockAxiosInstance.get.mockResolvedValue(mockResponse);
101101

102-
const result = await client.getCatalogItem("products", "item1");
102+
const result = await client.getCatalogItem({
103+
catalogName: "products",
104+
itemId: "item1",
105+
});
103106

104107
expect(mockAxiosInstance.get).toHaveBeenCalledWith(
105108
"/api/catalogs/products/items/item1"
@@ -119,7 +122,10 @@ describe("Catalog Operations", () => {
119122
};
120123
mockAxiosInstance.get.mockResolvedValue(mockResponse);
121124

122-
await client.getCatalogItem("my catalog", "item/1");
125+
await client.getCatalogItem({
126+
catalogName: "my catalog",
127+
itemId: "item/1",
128+
});
123129

124130
expect(mockAxiosInstance.get).toHaveBeenCalledWith(
125131
"/api/catalogs/my%20catalog/items/item%2F1"
@@ -137,7 +143,10 @@ describe("Catalog Operations", () => {
137143
};
138144
mockAxiosInstance.delete.mockResolvedValue(mockResponse);
139145

140-
const result = await client.deleteCatalogItem("products", "item1");
146+
const result = await client.deleteCatalogItem({
147+
catalogName: "products",
148+
itemId: "item1",
149+
});
141150

142151
expect(mockAxiosInstance.delete).toHaveBeenCalledWith(
143152
"/api/catalogs/products/items/item1"
@@ -154,7 +163,10 @@ describe("Catalog Operations", () => {
154163
};
155164
mockAxiosInstance.delete.mockResolvedValue(mockResponse);
156165

157-
await client.deleteCatalogItem("my catalog", "item+1");
166+
await client.deleteCatalogItem({
167+
catalogName: "my catalog",
168+
itemId: "item+1",
169+
});
158170

159171
expect(mockAxiosInstance.delete).toHaveBeenCalledWith(
160172
"/api/catalogs/my%20catalog/items/item%2B1"
@@ -427,7 +439,10 @@ describe("Catalog Operations", () => {
427439
const expectedError = createIterableError(axiosError);
428440
mockAxiosInstance.get.mockRejectedValue(expectedError);
429441

430-
const promise = client.getCatalogItem("nonexistent", "item123");
442+
const promise = client.getCatalogItem({
443+
catalogName: "nonexistent",
444+
itemId: "item123",
445+
});
431446
await expect(promise).rejects.toThrow(IterableApiError);
432447

433448
try {
@@ -452,7 +467,7 @@ describe("Catalog Operations", () => {
452467
};
453468
mockAxiosInstance.delete.mockResolvedValue(mockResponse);
454469

455-
const result = await client.deleteCatalog("test-catalog");
470+
const result = await client.deleteCatalog({ catalogName: "test-catalog" });
456471

457472
expect(mockAxiosInstance.delete).toHaveBeenCalledWith(
458473
"/api/catalogs/test-catalog"
@@ -469,7 +484,7 @@ describe("Catalog Operations", () => {
469484
};
470485
mockAxiosInstance.delete.mockResolvedValue(mockResponse);
471486

472-
await client.deleteCatalog("test catalog with spaces");
487+
await client.deleteCatalog({ catalogName: "test catalog with spaces" });
473488

474489
expect(mockAxiosInstance.delete).toHaveBeenCalledWith(
475490
"/api/catalogs/test%20catalog%20with%20spaces"

tests/unit/templates.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ describe("Template Management", () => {
132132
};
133133
mockAxiosInstance.post.mockResolvedValue(mockResponse);
134134

135-
const result = await client.deleteTemplates(templateIds);
135+
const result = await client.deleteTemplates({ ids: templateIds });
136136

137137
expect(mockAxiosInstance.post).toHaveBeenCalledWith(
138138
"/api/templates/bulkDelete",

0 commit comments

Comments
 (0)