-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlists.ts
More file actions
151 lines (136 loc) · 4.83 KB
/
lists.ts
File metadata and controls
151 lines (136 loc) · 4.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import {
IterableSuccessResponse,
IterableSuccessResponseSchema,
} from "../types/common.js";
import {
CreateListParams,
CreateListResponse,
CreateListResponseSchema,
GetListPreviewUsersParams,
GetListPreviewUsersResponse,
GetListPreviewUsersResponseSchema,
GetListSizeParams,
GetListSizeResponse,
GetListSizeResponseSchema,
GetListsResponse,
GetListsResponseSchema,
GetListUsersParams,
GetListUsersResponse,
GetListUsersResponseSchema,
SubscribeToListParams,
UnsubscribeFromListParams,
UserBulkUpdateListResponse,
UserBulkUpdateListResponseSchema,
} from "../types/lists.js";
import type { BaseIterableClient, Constructor } from "./base.js";
/**
* Lists operations mixin
*/
export function Lists<T extends Constructor<BaseIterableClient>>(Base: T) {
return class extends Base {
async getLists(): Promise<GetListsResponse> {
const response = await this.client.get("/api/lists");
return this.validateResponse(response, GetListsResponseSchema);
}
async subscribeUserToList(
options: SubscribeToListParams
): Promise<UserBulkUpdateListResponse> {
const response = await this.client.post("/api/lists/subscribe", options);
return this.validateResponse(response, UserBulkUpdateListResponseSchema);
}
async unsubscribeUserFromList(
options: UnsubscribeFromListParams
): Promise<UserBulkUpdateListResponse> {
const response = await this.client.post(
"/api/lists/unsubscribe",
options
);
return this.validateResponse(response, UserBulkUpdateListResponseSchema);
}
async getListUsers(
options: GetListUsersParams,
opts?: { signal?: AbortSignal }
): Promise<GetListUsersResponse> {
const params = new URLSearchParams();
params.append("listId", options.listId.toString());
if (options.maxResults)
params.append("maxResults", options.maxResults.toString());
const response = await this.client.get(
`/api/lists/getUsers?${params.toString()}`,
opts?.signal ? { signal: opts.signal } : {}
);
// The API returns newline-delimited email addresses as plain text
const responseData = response.data;
if (typeof responseData === "string") {
const emails = responseData
.trim()
.split("\n")
.filter((email) => email.trim());
const result = {
users: emails.map((email) => ({ email: email.trim() })),
};
// Validate the constructed response
return this.validateResponse(
{ data: result },
GetListUsersResponseSchema
);
}
// Fallback to original format if it's already JSON
return this.validateResponse(response, GetListUsersResponseSchema);
}
async createList(options: CreateListParams): Promise<CreateListResponse> {
const response = await this.client.post("/api/lists", options);
return this.validateResponse(response, CreateListResponseSchema);
}
async deleteList(listId: number): Promise<IterableSuccessResponse> {
const response = await this.client.delete(`/api/lists/${listId}`);
return this.validateResponse(response, IterableSuccessResponseSchema);
}
/**
* Get the count of users in a list
*/
async getListSize(params: GetListSizeParams): Promise<GetListSizeResponse> {
const response = await this.client.get(
`/api/lists/${params.listId}/size`
);
// API returns a string number, convert to proper object and validate
const size = parseInt(response.data, 10);
return this.validateResponse(
{ data: { size } },
GetListSizeResponseSchema
);
}
/**
* Preview users in a list (up to 5000 users)
*/
async getListPreviewUsers(
params: GetListPreviewUsersParams
): Promise<GetListPreviewUsersResponse> {
const queryParams = new URLSearchParams();
queryParams.append("listId", params.listId.toString());
if (params.preferUserId !== undefined) {
queryParams.append("preferUserId", params.preferUserId.toString());
}
if (params.size !== undefined) {
queryParams.append("size", params.size.toString());
}
const response = await this.client.get(
`/api/lists/previewUsers?${queryParams.toString()}`
);
// API returns plain text with users separated by newlines
const responseData = response.data;
if (typeof responseData === "string") {
const users = responseData
.trim()
.split("\n")
.filter((user) => user.length > 0);
return this.validateResponse(
{ data: { users } },
GetListPreviewUsersResponseSchema
);
}
// Fallback to JSON validation if response is not plain text
return this.validateResponse(response, GetListPreviewUsersResponseSchema);
}
};
}