-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplates.ts
More file actions
266 lines (235 loc) · 8.08 KB
/
templates.ts
File metadata and controls
266 lines (235 loc) · 8.08 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import { z } from "zod";
import { IterableSuccessResponse } from "../types/common.js";
import { IterableSuccessResponseSchema } from "../types/common.js";
import {
BulkDeleteTemplatesParams,
BulkDeleteTemplatesResponse,
BulkDeleteTemplatesResponseSchema,
EmailTemplate,
EmailTemplateSchema,
GetTemplateByClientIdResponse,
GetTemplateByClientIdResponseSchema,
GetTemplateParams,
GetTemplatesParams,
GetTemplatesResponse,
GetTemplatesResponseSchema,
InAppTemplate,
InAppTemplateSchema,
PreviewTemplateParams,
PushTemplate,
PushTemplateSchema,
SendTemplateProofParams,
SMSTemplate,
SMSTemplateSchema,
UpdateEmailTemplateParams,
UpdateInAppTemplateParams,
UpdatePushTemplateParams,
UpdateSMSTemplateParams,
UpsertEmailTemplateParams,
UpsertInAppTemplateParams,
UpsertPushTemplateParams,
UpsertSMSTemplateParams,
} from "../types/templates.js";
import type { Constructor } from "./base.js";
import type { BaseIterableClient } from "./base.js";
/**
* Templates operations mixin
*/
export function Templates<T extends Constructor<BaseIterableClient>>(Base: T) {
return class extends Base {
// Helper methods
async #getTemplateByType<T>(
pathSegment: string,
params: GetTemplateParams,
schema: z.ZodSchema<T>
): Promise<T> {
const queryParams = new URLSearchParams();
queryParams.append("templateId", params.templateId.toString());
if (params.locale) {
queryParams.append("locale", params.locale);
}
const response = await this.client.get(
`/api/templates/${pathSegment}/get?${queryParams.toString()}`
);
return this.validateResponse(response, schema);
}
async #createTemplateByType<T>(
pathSegment: string,
template: T
): Promise<IterableSuccessResponse> {
const response = await this.client.post(
`/api/templates/${pathSegment}/upsert`,
template
);
return this.validateResponse(response, IterableSuccessResponseSchema);
}
async #updateTemplateByType<T>(
pathSegment: string,
options: T
): Promise<IterableSuccessResponse> {
const response = await this.client.post(
`/api/templates/${pathSegment}/update`,
options
);
return this.validateResponse(response, IterableSuccessResponseSchema);
}
// Template deletion - works for all template types
async deleteTemplates(
templateIds: number[]
): Promise<BulkDeleteTemplatesResponse> {
const response = await this.client.post(`/api/templates/bulkDelete`, {
ids: templateIds,
});
return this.validateResponse(response, BulkDeleteTemplatesResponseSchema);
}
async #sendTemplateProof(
pathSegment: string,
request: SendTemplateProofParams
): Promise<IterableSuccessResponse> {
const response = await this.client.post(
`/api/templates/${pathSegment}/proof`,
request
);
return this.validateResponse(response, IterableSuccessResponseSchema);
}
async #previewTemplate(
pathSegment: string,
params: PreviewTemplateParams
): Promise<string> {
const queryParams = new URLSearchParams();
queryParams.append("templateId", params.templateId.toString());
if (params.locale) {
queryParams.append("locale", params.locale);
}
const response = await this.client.post(
`/api/templates/${pathSegment}/preview?${queryParams.toString()}`,
params.data || {}
);
// The API returns raw HTML as a string
return this.validateResponse(response, z.string());
}
// General template operations
async getTemplates(
options?: GetTemplatesParams
): Promise<GetTemplatesResponse> {
const params = new URLSearchParams();
if (options?.templateType)
params.append("templateType", options.templateType);
if (options?.messageMedium)
params.append("messageMedium", options.messageMedium);
if (options?.startDateTime)
params.append("startDateTime", options.startDateTime);
if (options?.endDateTime)
params.append("endDateTime", options.endDateTime);
const response = await this.client.get(
`/api/templates?${params.toString()}`
);
return this.validateResponse(response, GetTemplatesResponseSchema);
}
async getTemplateByClientId(
clientTemplateId: string
): Promise<GetTemplateByClientIdResponse> {
const response = await this.client.get(
"/api/templates/getByClientTemplateId",
{
params: { clientTemplateId },
}
);
return this.validateResponse(
response,
GetTemplateByClientIdResponseSchema
);
}
async bulkDeleteTemplates(
params: BulkDeleteTemplatesParams
): Promise<IterableSuccessResponse> {
const response = await this.client.post(
`/api/templates/bulkDelete`,
params
);
return this.validateResponse(response, IterableSuccessResponseSchema);
}
// Email Template Management
async getEmailTemplate(params: GetTemplateParams): Promise<EmailTemplate> {
return this.#getTemplateByType("email", params, EmailTemplateSchema);
}
async upsertEmailTemplate(
template: UpsertEmailTemplateParams
): Promise<IterableSuccessResponse> {
return this.#createTemplateByType("email", template);
}
async updateEmailTemplate(
options: UpdateEmailTemplateParams
): Promise<IterableSuccessResponse> {
return this.#updateTemplateByType("email", options);
}
async sendEmailTemplateProof(
request: SendTemplateProofParams
): Promise<IterableSuccessResponse> {
return this.#sendTemplateProof("email", request);
}
async previewEmailTemplate(params: PreviewTemplateParams): Promise<string> {
return this.#previewTemplate("email", params);
}
// SMS Template Management
async getSMSTemplate(params: GetTemplateParams): Promise<SMSTemplate> {
return this.#getTemplateByType("sms", params, SMSTemplateSchema);
}
async upsertSMSTemplate(
template: UpsertSMSTemplateParams
): Promise<IterableSuccessResponse> {
return this.#createTemplateByType("sms", template);
}
async updateSMSTemplate(
options: UpdateSMSTemplateParams
): Promise<IterableSuccessResponse> {
return this.#updateTemplateByType("sms", options);
}
async sendSMSTemplateProof(
request: SendTemplateProofParams
): Promise<IterableSuccessResponse> {
return this.#sendTemplateProof("sms", request);
}
// Push Template Management
async getPushTemplate(params: GetTemplateParams): Promise<PushTemplate> {
return this.#getTemplateByType("push", params, PushTemplateSchema);
}
async upsertPushTemplate(
template: UpsertPushTemplateParams
): Promise<IterableSuccessResponse> {
return this.#createTemplateByType("push", template);
}
async updatePushTemplate(
options: UpdatePushTemplateParams
): Promise<IterableSuccessResponse> {
return this.#updateTemplateByType("push", options);
}
async sendPushTemplateProof(
request: SendTemplateProofParams
): Promise<IterableSuccessResponse> {
return this.#sendTemplateProof("push", request);
}
// In-App Template Management
async getInAppTemplate(params: GetTemplateParams): Promise<InAppTemplate> {
return this.#getTemplateByType("inapp", params, InAppTemplateSchema);
}
async upsertInAppTemplate(
template: UpsertInAppTemplateParams
): Promise<IterableSuccessResponse> {
return this.#createTemplateByType("inapp", template);
}
async updateInAppTemplate(
options: UpdateInAppTemplateParams
): Promise<IterableSuccessResponse> {
return this.#updateTemplateByType("inapp", options);
}
async sendInAppTemplateProof(
request: SendTemplateProofParams
): Promise<IterableSuccessResponse> {
return this.#sendTemplateProof("inapp", request);
}
async previewInAppTemplate(params: PreviewTemplateParams): Promise<string> {
return this.#previewTemplate("inapp", params);
}
};
}