sdx-mailer-sdk 1.0.19 → 1.0.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.MD CHANGED
@@ -1,6 +1,266 @@
1
- Para publicar el package se debe actualizar la version en el package.json y luego
1
+ # sdx-mailer-sdk
2
2
 
3
- 1 - npm login
4
- 2 - npm publish
3
+ Official Softdirex SDK for integrating with the **SDX Mailer API**. Provides typed request builders and send methods for all supported email flows: courier messages, customer lifecycle emails, and payment notifications.
5
4
 
6
- IMPORTANTE: No publicar endpoints ya que este package aun es publico
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install sdx-mailer-sdk
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ All flows follow the same two-step pattern:
14
+
15
+ 1. **Build** the request with a `create*` function
16
+ 2. **Sign** it (set a `trx` field) and **send** it with the corresponding `send*` function
17
+
18
+ ```js
19
+ const {
20
+ createCustomerMailerRequest,
21
+ sendMailRegister
22
+ } = require('sdx-mailer-sdk');
23
+
24
+ const request = createCustomerMailerRequest(
25
+ 'user@example.com', // user
26
+ 'SecurePass1', // password
27
+ 'MyProduct', // product
28
+ 'verify-token-abc', // token
29
+ 'support@myapp.com' // reply_mail
30
+ );
31
+
32
+ request.trx = yourSigningFunction(request); // request must be signed before sending
33
+
34
+ const result = await sendMailRegister(
35
+ 'Basic dXNlcjpwYXNz', // basicAuth
36
+ 'https://mailer.myapp.com', // endpoint
37
+ request
38
+ );
39
+ ```
40
+
41
+ ## API Reference
42
+
43
+ ### Courier
44
+
45
+ Generic emails with an optional call-to-action button.
46
+
47
+ ---
48
+
49
+ #### `createCourierRequest(title, subject, message, receiver, reply_mail, btn_label, btn_link)`
50
+
51
+ Builds a `CourierRequest` payload.
52
+
53
+ | Parameter | Type | Description |
54
+ |---|---|---|
55
+ | `title` | `string` | Header title displayed in the email template |
56
+ | `subject` | `string` | Subject line of the email |
57
+ | `message` | `string` | Main body content |
58
+ | `receiver` | `string` | Recipient email address |
59
+ | `reply_mail` | `string` | Reply-to email address |
60
+ | `btn_label` | `string \| null` | Label for the optional action button |
61
+ | `btn_link` | `string \| null` | URL for the optional action button |
62
+
63
+ Returns: `CourierRequest`
64
+
65
+ ---
66
+
67
+ #### `createCourierExceptionRequest(error, detail, context)`
68
+
69
+ Builds a `CourierExceptionRequest` payload for error/exception notifications.
70
+
71
+ | Parameter | Type | Description |
72
+ |---|---|---|
73
+ | `error` | `string` | Error name or type (e.g. `'HTTP 500'`) |
74
+ | `detail` | `string` | Detailed error message or stack trace excerpt |
75
+ | `context` | `string` | Context where the error occurred (e.g. service name, endpoint) |
76
+
77
+ Returns: `CourierExceptionRequest`
78
+
79
+ ---
80
+
81
+ #### `sendCourierMail(basicAuth, endpoint, tokenRQ, productId, isBasic)`
82
+
83
+ Sends a courier email. The `tokenRQ` must be signed (have a `trx` field).
84
+
85
+ | Parameter | Type | Description |
86
+ |---|---|---|
87
+ | `basicAuth` | `string \| null` | Authorization header (e.g. `'Basic dXNlcjpwYXNz'`). Pass `null` for no auth |
88
+ | `endpoint` | `string` | Base URL of the SDX Mailer API |
89
+ | `tokenRQ` | `CourierRequest` | Signed request payload |
90
+ | `productId` | `number` | Numeric product identifier sent as `product-id` header |
91
+ | `isBasic` | `boolean` | When `true`, uses the `/basic` endpoint variant |
92
+
93
+ Returns: `Promise<any>`
94
+
95
+ ---
96
+
97
+ #### `sendExceptionMail(basicAuth, endpoint, tokenRQ, productId)`
98
+
99
+ Sends an exception notification email. The `tokenRQ` must be signed.
100
+
101
+ | Parameter | Type | Description |
102
+ |---|---|---|
103
+ | `basicAuth` | `string \| null` | Authorization header. Pass `null` for no auth |
104
+ | `endpoint` | `string` | Base URL of the SDX Mailer API |
105
+ | `tokenRQ` | `CourierExceptionRequest` | Signed request payload |
106
+ | `productId` | `number` | Numeric product identifier |
107
+
108
+ Returns: `Promise<any>`
109
+
110
+ ---
111
+
112
+ ### Customer
113
+
114
+ Emails for the customer lifecycle: registration and password management.
115
+
116
+ ---
117
+
118
+ #### `createCustomerMailerRequest(user, password, product, token, reply_mail)`
119
+
120
+ Builds a `CustomerMailerRequest` payload used by all customer `send*` methods.
121
+
122
+ | Parameter | Type | Description |
123
+ |---|---|---|
124
+ | `user` | `string` | Username or email address of the customer |
125
+ | `password` | `string` | Customer's password (included in registration emails) |
126
+ | `product` | `string` | Product name shown in the email template |
127
+ | `token` | `string` | Verification or action token (e.g. for password reset links) |
128
+ | `reply_mail` | `string` | Reply-to email address |
129
+
130
+ Returns: `CustomerMailerRequest`
131
+
132
+ ---
133
+
134
+ #### `sendMailRegister(basicAuth, endpoint, tokenRQ)`
135
+
136
+ Sends a registration welcome email to the customer.
137
+
138
+ #### `sendMailRegisterByOwner(basicAuth, endpoint, tokenRQ)`
139
+
140
+ Sends a registration email triggered by an owner/admin on behalf of the customer.
141
+
142
+ #### `sendMailUpdatePassword(basicAuth, endpoint, tokenRQ)`
143
+
144
+ Sends a "please update your password" notification email.
145
+
146
+ #### `sendMailSuccessUpdatedPassword(basicAuth, endpoint, tokenRQ)`
147
+
148
+ Sends a "password updated successfully" confirmation email.
149
+
150
+ All four methods share the same signature:
151
+
152
+ | Parameter | Type | Description |
153
+ |---|---|---|
154
+ | `basicAuth` | `string \| null` | Authorization header. Pass `null` for no auth |
155
+ | `endpoint` | `string` | Base URL of the SDX Mailer API |
156
+ | `tokenRQ` | `CustomerMailerRequest` | Signed request payload |
157
+
158
+ Returns: `Promise<any>`
159
+
160
+ ---
161
+
162
+ ### Payments
163
+
164
+ Payment confirmation emails.
165
+
166
+ ---
167
+
168
+ #### `createPaymentsMailerRequest(trxId, product)`
169
+
170
+ Builds a `PaymentsMailerRequest` payload.
171
+
172
+ | Parameter | Type | Description |
173
+ |---|---|---|
174
+ | `trxId` | `string` | Transaction ID associated with the payment |
175
+ | `product` | `string` | Product name or licence identifier |
176
+
177
+ Returns: `PaymentsMailerRequest`
178
+
179
+ ---
180
+
181
+ #### `sendMailPayment(basicAuth, endpoint, tokenRQ)`
182
+
183
+ Sends a payment confirmation email. The `tokenRQ` must be signed.
184
+
185
+ | Parameter | Type | Description |
186
+ |---|---|---|
187
+ | `basicAuth` | `string \| null` | Authorization header. Pass `null` for no auth |
188
+ | `endpoint` | `string` | Base URL of the SDX Mailer API |
189
+ | `tokenRQ` | `PaymentsMailerRequest` | Signed request payload |
190
+
191
+ Returns: `Promise<any>`
192
+
193
+ ---
194
+
195
+ ### Custom Mail
196
+
197
+ Send a fully custom HTML email with free-form content. Uses API key authentication instead of Basic auth.
198
+
199
+ ---
200
+
201
+ #### `createCustomMailRequest(subject, email, content, cc?)`
202
+
203
+ Builds a `CustomMailRequest` payload.
204
+
205
+ | Parameter | Type | Description |
206
+ |---|---|---|
207
+ | `subject` | `string` | Subject line of the email |
208
+ | `email` | `string` | Recipient email address |
209
+ | `content` | `string` | Full HTML content of the email body |
210
+ | `cc` | `string[]` (optional) | List of CC email addresses |
211
+
212
+ Returns: `CustomMailRequest`
213
+
214
+ ---
215
+
216
+ #### `sendCustomMail(apiKey, endpoint, request)`
217
+
218
+ Sends a custom HTML email. Authenticated via `x-api-key` header — no `trx` signing required.
219
+
220
+ | Parameter | Type | Description |
221
+ |---|---|---|
222
+ | `apiKey` | `string` | API key in the format `sdx_<prefix>.<secret>`. Must have `can_execute: true` |
223
+ | `endpoint` | `string` | Base URL of the SDX Mailer API |
224
+ | `request` | `CustomMailRequest` | Request payload |
225
+
226
+ Returns: `Promise<any>`
227
+
228
+ **Example:**
229
+
230
+ ```js
231
+ const { createCustomMailRequest, sendCustomMail } = require('sdx-mailer-sdk');
232
+
233
+ const request = createCustomMailRequest(
234
+ 'Welcome to our platform',
235
+ 'user@example.com',
236
+ '<h1>Hello!</h1><p>Your account is ready.</p>',
237
+ ['manager@example.com']
238
+ );
239
+
240
+ const result = await sendCustomMail(
241
+ 'sdx_abc123xyz.mysecret',
242
+ 'https://mailer.myapp.com',
243
+ request
244
+ );
245
+ ```
246
+
247
+ ---
248
+
249
+ ## Important notes
250
+
251
+ - Courier, Customer and Payment `send*` methods throw a `WebServiceException` if the request is **not signed** (missing `trx` field) or if the API returns an error.
252
+ - `sendCustomMail` does **not** require a `trx` field — it uses an API key instead.
253
+ - `basicAuth` must use the format `'Basic <base64(user:password)>'` when authentication is required.
254
+ - `productId` is sent as the `product-id` HTTP header on Courier/Customer/Payment requests.
255
+ - API keys must follow the format `sdx_<prefix>.<secret>` and have at least one permission with `can_execute: true`.
256
+
257
+ ## Publishing
258
+
259
+ To publish a new version update the version in `package.json`, then:
260
+
261
+ ```bash
262
+ npm login
263
+ npm publish
264
+ ```
265
+
266
+ > **Important:** Do not include API endpoints, credentials, or internal configuration in the package. The `files` field in `package.json` restricts publishing to runtime-only files.
package/index.d.ts ADDED
@@ -0,0 +1,390 @@
1
+ // Type declarations for sdx-mailer-sdk
2
+
3
+ /**
4
+ * Represents a button/link included in a courier email.
5
+ */
6
+ export interface CourierLink {
7
+ /** Text displayed on the action button */
8
+ btn_label: string;
9
+ /** URL the action button points to */
10
+ btn_link: string;
11
+ }
12
+
13
+ /**
14
+ * Request payload for sending a general-purpose courier email.
15
+ * Build this object using `createCourierRequest`, then pass it to `sendCourierMail`.
16
+ */
17
+ export interface CourierRequest {
18
+ /** Email header title */
19
+ title: string;
20
+ /** Email subject line */
21
+ subject: string;
22
+ /** Body content of the email */
23
+ message: string;
24
+ /** Recipient email address */
25
+ receiver: string;
26
+ /** Reply-to email address */
27
+ reply_mail: string;
28
+ /** Optional action button included in the email */
29
+ link: CourierLink | null;
30
+ /** Transaction/signature identifier — set automatically when the request is signed */
31
+ trx?: string;
32
+ }
33
+
34
+ /**
35
+ * Request payload for sending an exception/error notification email.
36
+ * Build this object using `createCourierExceptionRequest`, then pass it to `sendExceptionMail`.
37
+ */
38
+ export interface CourierExceptionRequest {
39
+ /** Error name or type */
40
+ error: string;
41
+ /** Detailed error message */
42
+ detail: string;
43
+ /** Application context where the error occurred (e.g. service name, endpoint) */
44
+ context: string;
45
+ /** Transaction/signature identifier — set automatically when the request is signed */
46
+ trx?: string;
47
+ }
48
+
49
+ /**
50
+ * Request payload for customer-related emails (register, password update, etc.).
51
+ * Build this object using `createCustomerMailerRequest`, then pass it to the
52
+ * corresponding `sendMail*` customer function.
53
+ */
54
+ export interface CustomerMailerRequest {
55
+ /** Username or email of the customer */
56
+ user: string;
57
+ /** Customer password (used in registration emails) */
58
+ password: string;
59
+ /** Product name shown in the email */
60
+ product: string;
61
+ /** Verification or action token included in the email */
62
+ token: string;
63
+ /** Reply-to email address */
64
+ reply_mail: string;
65
+ /** Transaction/signature identifier — set automatically when the request is signed */
66
+ trx?: string;
67
+ }
68
+
69
+ /**
70
+ * Request payload for payment notification emails.
71
+ * Build this object using `createPaymentsMailerRequest`, then pass it to `sendMailPayment`.
72
+ */
73
+ export interface PaymentsMailerRequest {
74
+ /** Transaction ID associated with the payment */
75
+ trxId: string;
76
+ /** Product licence or identifier related to the payment */
77
+ licence: string;
78
+ /** Transaction/signature identifier — set automatically when the request is signed */
79
+ trx?: string;
80
+ }
81
+
82
+ // ---------------------------------------------------------------------------
83
+ // COURIER
84
+ // ---------------------------------------------------------------------------
85
+
86
+ /**
87
+ * Creates a `CourierRequest` object ready to be sent via `sendCourierMail`.
88
+ *
89
+ * @param title - Header title displayed in the email template.
90
+ * @param subject - Subject line of the email.
91
+ * @param message - Main body/content of the email.
92
+ * @param receiver - Recipient email address.
93
+ * @param reply_mail - Reply-to email address.
94
+ * @param btn_label - Label for the optional call-to-action button. Pass `null` to omit the button.
95
+ * @param btn_link - URL for the optional call-to-action button. Pass `null` to omit the button.
96
+ * @returns A `CourierRequest` instance. Sign it (add a `trx` field) before calling `sendCourierMail`.
97
+ *
98
+ * @example
99
+ * const request = createCourierRequest(
100
+ * 'Nuevo mensaje',
101
+ * 'Tienes un mensaje pendiente',
102
+ * 'Hola, revisa tu bandeja.',
103
+ * 'cliente@ejemplo.com',
104
+ * 'soporte@miapp.com',
105
+ * 'Ver mensaje',
106
+ * 'https://miapp.com/mensajes'
107
+ * );
108
+ */
109
+ export function createCourierRequest(
110
+ title: string,
111
+ subject: string,
112
+ message: string,
113
+ receiver: string,
114
+ reply_mail: string,
115
+ btn_label: string | null,
116
+ btn_link: string | null
117
+ ): CourierRequest;
118
+
119
+ /**
120
+ * Creates a `CourierExceptionRequest` object ready to be sent via `sendExceptionMail`.
121
+ *
122
+ * @param error - Error name or type (e.g. `'NullPointerException'`, `'HTTP 500'`).
123
+ * @param detail - Detailed error message or stack trace excerpt.
124
+ * @param context - Context where the error occurred (e.g. service name, endpoint, module).
125
+ * @returns A `CourierExceptionRequest` instance. Sign it (add a `trx` field) before calling `sendExceptionMail`.
126
+ *
127
+ * @example
128
+ * const request = createCourierExceptionRequest(
129
+ * 'UnhandledError',
130
+ * 'Cannot read property of undefined at processPayment',
131
+ * 'PaymentService.processPayment'
132
+ * );
133
+ */
134
+ export function createCourierExceptionRequest(
135
+ error: string,
136
+ detail: string,
137
+ context: string
138
+ ): CourierExceptionRequest;
139
+
140
+ /**
141
+ * Sends a general-purpose courier email via the SDX Mailer API.
142
+ *
143
+ * The `tokenRQ` must be a **signed** `CourierRequest` (i.e. it must have a `trx` field set).
144
+ * Throws a `WebServiceException` if the request is not signed or if the API call fails.
145
+ *
146
+ * @param basicAuth - Authorization header value (e.g. `'Basic dXNlcjpwYXNz'`). Pass `null` to send without auth.
147
+ * @param endpoint - Base URL of the SDX Mailer API (e.g. `'https://mailer.miapp.com'`).
148
+ * @param tokenRQ - Signed `CourierRequest` to send.
149
+ * @param productId - Numeric identifier of the product making the request.
150
+ * @param isBasic - When `true`, sends to the `/basic` variant of the courier endpoint.
151
+ * @returns A promise that resolves with the API response data.
152
+ *
153
+ * @example
154
+ * const response = await sendCourierMail('Basic dXNlcjpwYXNz', 'https://mailer.miapp.com', signedRequest, 1, false);
155
+ */
156
+ export function sendCourierMail(
157
+ basicAuth: string | null,
158
+ endpoint: string,
159
+ tokenRQ: CourierRequest,
160
+ productId: number,
161
+ isBasic: boolean
162
+ ): Promise<any>;
163
+
164
+ /**
165
+ * Sends an exception/error notification email via the SDX Mailer API.
166
+ *
167
+ * The `tokenRQ` must be a **signed** `CourierExceptionRequest` (i.e. it must have a `trx` field set).
168
+ * Throws a `WebServiceException` if the request is not signed or if the API call fails.
169
+ *
170
+ * @param basicAuth - Authorization header value (e.g. `'Basic dXNlcjpwYXNz'`). Pass `null` to send without auth.
171
+ * @param endpoint - Base URL of the SDX Mailer API (e.g. `'https://mailer.miapp.com'`).
172
+ * @param tokenRQ - Signed `CourierExceptionRequest` to send.
173
+ * @param productId - Numeric identifier of the product making the request.
174
+ * @returns A promise that resolves with the API response data.
175
+ *
176
+ * @example
177
+ * const response = await sendExceptionMail('Basic dXNlcjpwYXNz', 'https://mailer.miapp.com', signedRequest, 1);
178
+ */
179
+ export function sendExceptionMail(
180
+ basicAuth: string | null,
181
+ endpoint: string,
182
+ tokenRQ: CourierExceptionRequest,
183
+ productId: number
184
+ ): Promise<any>;
185
+
186
+ // ---------------------------------------------------------------------------
187
+ // CUSTOMER
188
+ // ---------------------------------------------------------------------------
189
+
190
+ /**
191
+ * Creates a `CustomerMailerRequest` object used for all customer email flows
192
+ * (register, password update, etc.).
193
+ *
194
+ * @param user - Username or email address of the customer.
195
+ * @param password - Customer's password (included in registration emails).
196
+ * @param product - Product name displayed in the email template.
197
+ * @param token - Verification or action token to embed in the email (e.g. for password reset links).
198
+ * @param reply_mail - Reply-to email address shown in the email.
199
+ * @returns A `CustomerMailerRequest` instance. Sign it (add a `trx` field) before calling any `sendMail*` function.
200
+ *
201
+ * @example
202
+ * const request = createCustomerMailerRequest(
203
+ * 'usuario@ejemplo.com',
204
+ * 'MiP@ss123',
205
+ * 'MiProducto',
206
+ * 'abc123token',
207
+ * 'soporte@miapp.com'
208
+ * );
209
+ */
210
+ export function createCustomerMailerRequest(
211
+ user: string,
212
+ password: string,
213
+ product: string,
214
+ token: string,
215
+ reply_mail: string
216
+ ): CustomerMailerRequest;
217
+
218
+ /**
219
+ * Sends a "update your password" notification email to the customer.
220
+ *
221
+ * The `tokenRQ` must be a **signed** `CustomerMailerRequest` (i.e. it must have a `trx` field set).
222
+ * Throws a `WebServiceException` if the request is not signed or if the API call fails.
223
+ *
224
+ * @param basicAuth - Authorization header value (e.g. `'Basic dXNlcjpwYXNz'`). Pass `null` to send without auth.
225
+ * @param endpoint - Base URL of the SDX Mailer API (e.g. `'https://mailer.miapp.com'`).
226
+ * @param tokenRQ - Signed `CustomerMailerRequest` to send.
227
+ * @returns A promise that resolves with the API response data.
228
+ */
229
+ export function sendMailUpdatePassword(
230
+ basicAuth: string | null,
231
+ endpoint: string,
232
+ tokenRQ: CustomerMailerRequest
233
+ ): Promise<any>;
234
+
235
+ /**
236
+ * Sends a "password updated successfully" confirmation email to the customer.
237
+ *
238
+ * The `tokenRQ` must be a **signed** `CustomerMailerRequest` (i.e. it must have a `trx` field set).
239
+ * Throws a `WebServiceException` if the request is not signed or if the API call fails.
240
+ *
241
+ * @param basicAuth - Authorization header value (e.g. `'Basic dXNlcjpwYXNz'`). Pass `null` to send without auth.
242
+ * @param endpoint - Base URL of the SDX Mailer API (e.g. `'https://mailer.miapp.com'`).
243
+ * @param tokenRQ - Signed `CustomerMailerRequest` to send.
244
+ * @returns A promise that resolves with the API response data.
245
+ */
246
+ export function sendMailSuccessUpdatedPassword(
247
+ basicAuth: string | null,
248
+ endpoint: string,
249
+ tokenRQ: CustomerMailerRequest
250
+ ): Promise<any>;
251
+
252
+ /**
253
+ * Sends a customer registration welcome email.
254
+ *
255
+ * The `tokenRQ` must be a **signed** `CustomerMailerRequest` (i.e. it must have a `trx` field set).
256
+ * Throws a `WebServiceException` if the request is not signed or if the API call fails.
257
+ *
258
+ * @param basicAuth - Authorization header value (e.g. `'Basic dXNlcjpwYXNz'`). Pass `null` to send without auth.
259
+ * @param endpoint - Base URL of the SDX Mailer API (e.g. `'https://mailer.miapp.com'`).
260
+ * @param tokenRQ - Signed `CustomerMailerRequest` to send.
261
+ * @returns A promise that resolves with the API response data.
262
+ */
263
+ export function sendMailRegister(
264
+ basicAuth: string | null,
265
+ endpoint: string,
266
+ tokenRQ: CustomerMailerRequest
267
+ ): Promise<any>;
268
+
269
+ /**
270
+ * Sends a customer registration email triggered by an owner/admin on behalf of the customer.
271
+ *
272
+ * The `tokenRQ` must be a **signed** `CustomerMailerRequest` (i.e. it must have a `trx` field set).
273
+ * Throws a `WebServiceException` if the request is not signed or if the API call fails.
274
+ *
275
+ * @param basicAuth - Authorization header value (e.g. `'Basic dXNlcjpwYXNz'`). Pass `null` to send without auth.
276
+ * @param endpoint - Base URL of the SDX Mailer API (e.g. `'https://mailer.miapp.com'`).
277
+ * @param tokenRQ - Signed `CustomerMailerRequest` to send.
278
+ * @returns A promise that resolves with the API response data.
279
+ */
280
+ export function sendMailRegisterByOwner(
281
+ basicAuth: string | null,
282
+ endpoint: string,
283
+ tokenRQ: CustomerMailerRequest
284
+ ): Promise<any>;
285
+
286
+ // ---------------------------------------------------------------------------
287
+ // CUSTOM MAIL
288
+ // ---------------------------------------------------------------------------
289
+
290
+ /**
291
+ * Request payload for sending a fully custom HTML email.
292
+ * Build this object using `createCustomMailRequest`, then pass it to `sendCustomMail`.
293
+ */
294
+ export interface CustomMailRequest {
295
+ /** Subject line of the email */
296
+ subject: string;
297
+ /** Recipient email address */
298
+ email: string;
299
+ /** List of CC email addresses */
300
+ cc: string[];
301
+ /** Full HTML content of the email body */
302
+ content: string;
303
+ }
304
+
305
+ /**
306
+ * Creates a `CustomMailRequest` object ready to be sent via `sendCustomMail`.
307
+ *
308
+ * @param subject - Subject line of the email.
309
+ * @param email - Recipient email address.
310
+ * @param content - Full HTML content of the email body.
311
+ * @param cc - Optional list of CC email addresses.
312
+ * @returns A `CustomMailRequest` instance.
313
+ *
314
+ * @example
315
+ * const request = createCustomMailRequest(
316
+ * 'Welcome to our platform',
317
+ * 'user@example.com',
318
+ * '<h1>Hello!</h1><p>Your account is ready.</p>',
319
+ * ['manager@example.com']
320
+ * );
321
+ */
322
+ export function createCustomMailRequest(
323
+ subject: string,
324
+ email: string,
325
+ content: string,
326
+ cc?: string[]
327
+ ): CustomMailRequest;
328
+
329
+ /**
330
+ * Sends a fully custom HTML email via the SDX Mailer API.
331
+ * Authenticated with an API key sent as the `x-api-key` header.
332
+ * The key must follow the format `sdx_<prefix>.<secret>` and have at least one
333
+ * permission with `can_execute: true`.
334
+ *
335
+ * @param apiKey - API key in the format `sdx_<prefix>.<secret>` (e.g. `'sdx_abc123xyz.mysecret'`).
336
+ * @param endpoint - Base URL of the SDX Mailer API (e.g. `'https://mailer.miapp.com'`).
337
+ * @param request - `CustomMailRequest` payload to send.
338
+ * @returns A promise that resolves with the API response data.
339
+ *
340
+ * @example
341
+ * const response = await sendCustomMail(
342
+ * 'sdx_abc123xyz.mysecret',
343
+ * 'https://mailer.miapp.com',
344
+ * request
345
+ * );
346
+ */
347
+ export function sendCustomMail(
348
+ apiKey: string,
349
+ endpoint: string,
350
+ request: CustomMailRequest
351
+ ): Promise<any>;
352
+
353
+ // ---------------------------------------------------------------------------
354
+ // PAYMENTS
355
+ // ---------------------------------------------------------------------------
356
+
357
+ /**
358
+ * Creates a `PaymentsMailerRequest` object ready to be sent via `sendMailPayment`.
359
+ *
360
+ * @param trxId - Transaction ID associated with the payment (e.g. order ID, payment reference).
361
+ * @param product - Product name or licence identifier related to the payment.
362
+ * @returns A `PaymentsMailerRequest` instance. Sign it (add a `trx` field) before calling `sendMailPayment`.
363
+ *
364
+ * @example
365
+ * const request = createPaymentsMailerRequest('TRX-00123', 'PlanPro');
366
+ */
367
+ export function createPaymentsMailerRequest(
368
+ trxId: string,
369
+ product: string
370
+ ): PaymentsMailerRequest;
371
+
372
+ /**
373
+ * Sends a payment confirmation email via the SDX Mailer API.
374
+ *
375
+ * The `tokenRQ` must be a **signed** `PaymentsMailerRequest` (i.e. it must have a `trx` field set).
376
+ * Throws a `WebServiceException` if the request is not signed or if the API call fails.
377
+ *
378
+ * @param basicAuth - Authorization header value (e.g. `'Basic dXNlcjpwYXNz'`). Pass `null` to send without auth.
379
+ * @param endpoint - Base URL of the SDX Mailer API (e.g. `'https://mailer.miapp.com'`).
380
+ * @param tokenRQ - Signed `PaymentsMailerRequest` to send.
381
+ * @returns A promise that resolves with the API response data.
382
+ *
383
+ * @example
384
+ * const response = await sendMailPayment('Basic dXNlcjpwYXNz', 'https://mailer.miapp.com', signedRequest);
385
+ */
386
+ export function sendMailPayment(
387
+ basicAuth: string | null,
388
+ endpoint: string,
389
+ tokenRQ: PaymentsMailerRequest
390
+ ): Promise<any>;
package/index.js CHANGED
@@ -1,140 +1,174 @@
1
1
  const CourierMail = require('./src/CourierMail');
2
2
  const CustomerMail = require('./src/CustomerMail')
3
3
  const PaymentsMail = require('./src/PaymentsMail')
4
+ const CustomMail = require('./src/CustomMail')
4
5
 
5
6
  const courierMailInstance = new CourierMail();
6
7
  const customerMailInstance = new CustomerMail();
7
8
  const paymentsMailInstance = new PaymentsMail();
9
+ const customMailInstance = new CustomMail();
8
10
 
9
11
  /**
10
- * Method for CourierRequest creation
11
- * @param {*} title
12
- * @param {*} subject
13
- * @param {*} message
14
- * @param {*} receiver
15
- * @param {*} reply_mail
16
- * @param {*} btn_label
17
- * @param {*} btn_link
18
- * @returns
12
+ * Creates a `CourierRequest` object ready to be sent via `sendCourierMail`.
13
+ * @param {string} title - Header title displayed in the email template.
14
+ * @param {string} subject - Subject line of the email.
15
+ * @param {string} message - Main body/content of the email.
16
+ * @param {string} receiver - Recipient email address.
17
+ * @param {string} reply_mail - Reply-to email address.
18
+ * @param {string|null} btn_label - Label for the optional call-to-action button. Pass `null` to omit it.
19
+ * @param {string|null} btn_link - URL for the optional call-to-action button. Pass `null` to omit it.
20
+ * @returns {import('./src/entities/CourierRequest')} A CourierRequest instance. Sign it before calling `sendCourierMail`.
19
21
  */
20
22
  function createCourierRequest(title, subject, message, receiver, reply_mail, btn_label, btn_link) {
21
23
  return courierMailInstance.createCourierRequest(title, subject, message, receiver, reply_mail, btn_label, btn_link)
22
24
  }
23
25
 
24
26
  /**
25
- * Method for CourierExceptionRequest creation
26
- * @param {*} error
27
- * @param {*} detail
28
- * @param {*} context
29
- * @returns
27
+ * Creates a `CourierExceptionRequest` object ready to be sent via `sendExceptionMail`.
28
+ * @param {string} error - Error name or type (e.g. `'HTTP 500'`, `'UnhandledError'`).
29
+ * @param {string} detail - Detailed error message or stack trace excerpt.
30
+ * @param {string} context - Context where the error occurred (e.g. service name, endpoint).
31
+ * @returns {import('./src/entities/CourierExceptionRequest')} A CourierExceptionRequest instance. Sign it before calling `sendExceptionMail`.
30
32
  */
31
33
  function createCourierExceptionRequest(error, detail, context) {
32
34
  return courierMailInstance.createCourierExceptionRequest(error, detail, context)
33
35
  }
34
36
 
35
37
  /**
36
- * Method for send CourierRequest
37
- * @param {*} basicAuth
38
- * @param {*} endpoint
39
- * @param {*} tokenRQ
40
- * @param {*} productId
41
- * @param {*} isBasic
42
- * @returns
38
+ * Sends a general-purpose courier email via the SDX Mailer API.
39
+ * The `tokenRQ` must be signed (have a `trx` field). Throws if not signed or if the API fails.
40
+ * @param {string|null} basicAuth - Authorization header value (e.g. `'Basic dXNlcjpwYXNz'`). Pass `null` to send without auth.
41
+ * @param {string} endpoint - Base URL of the SDX Mailer API (e.g. `'https://mailer.miapp.com'`).
42
+ * @param {import('./src/entities/CourierRequest')} tokenRQ - Signed CourierRequest to send.
43
+ * @param {number} productId - Numeric identifier of the product making the request.
44
+ * @param {boolean} isBasic - When `true`, sends to the `/basic` variant of the courier endpoint.
45
+ * @returns {Promise<any>} Resolves with the API response data.
43
46
  */
44
47
  async function sendCourierMail(basicAuth, endpoint, tokenRQ, productId, isBasic) {
45
48
  return courierMailInstance.sendCourierMail(basicAuth, endpoint, tokenRQ, productId, isBasic)
46
49
  }
47
50
 
48
51
  /**
49
- * Method for send CourierExceptionRequest
50
- * @param {*} basicAuth
51
- * @param {*} endpoint
52
- * @param {*} tokenRQ
53
- * @param {*} productId
54
- * @returns
52
+ * Sends an exception/error notification email via the SDX Mailer API.
53
+ * The `tokenRQ` must be signed (have a `trx` field). Throws if not signed or if the API fails.
54
+ * @param {string|null} basicAuth - Authorization header value (e.g. `'Basic dXNlcjpwYXNz'`). Pass `null` to send without auth.
55
+ * @param {string} endpoint - Base URL of the SDX Mailer API (e.g. `'https://mailer.miapp.com'`).
56
+ * @param {import('./src/entities/CourierExceptionRequest')} tokenRQ - Signed CourierExceptionRequest to send.
57
+ * @param {number} productId - Numeric identifier of the product making the request.
58
+ * @returns {Promise<any>} Resolves with the API response data.
55
59
  */
56
60
  async function sendExceptionMail(basicAuth, endpoint, tokenRQ, productId) {
57
61
  return courierMailInstance.sendExceptionMail(basicAuth, endpoint, tokenRQ, productId)
58
62
  }
59
63
 
60
64
  /**
61
- * Method for CustomerMailerRequest creation
62
- * @param {*} user
63
- * @param {*} password
64
- * @param {*} product
65
- * @param {*} token
66
- * @param {*} reply_mail
67
- * @returns
65
+ * Creates a `CustomerMailerRequest` object used for all customer email flows (register, password update, etc.).
66
+ * @param {string} user - Username or email address of the customer.
67
+ * @param {string} password - Customer's password (included in registration emails).
68
+ * @param {string} product - Product name displayed in the email template.
69
+ * @param {string} token - Verification or action token to embed in the email (e.g. for password reset links).
70
+ * @param {string} reply_mail - Reply-to email address shown in the email.
71
+ * @returns {import('./src/entities/CustomerMailerRequest')} A CustomerMailerRequest instance. Sign it before calling any `sendMail*` function.
68
72
  */
69
73
  function createCustomerMailerRequest(user, password, product, token, reply_mail) {
70
74
  return customerMailInstance.createCustomerMailerRequest(user, password, product, token, reply_mail)
71
75
  }
72
76
 
73
77
  /**
74
- * Send Customer update password Mail
75
- * @param {*} basicAuth
76
- * @param {*} endpoint
77
- * @param {*} tokenRQ
78
- * @returns
78
+ * Sends a "update your password" notification email to the customer.
79
+ * The `tokenRQ` must be signed (have a `trx` field). Throws if not signed or if the API fails.
80
+ * @param {string|null} basicAuth - Authorization header value (e.g. `'Basic dXNlcjpwYXNz'`). Pass `null` to send without auth.
81
+ * @param {string} endpoint - Base URL of the SDX Mailer API (e.g. `'https://mailer.miapp.com'`).
82
+ * @param {import('./src/entities/CustomerMailerRequest')} tokenRQ - Signed CustomerMailerRequest to send.
83
+ * @returns {Promise<any>} Resolves with the API response data.
79
84
  */
80
85
  async function sendMailUpdatePassword(basicAuth, endpoint, tokenRQ) {
81
86
  return customerMailInstance.sendMailUpdatePassword(basicAuth, endpoint, tokenRQ)
82
87
  }
83
88
 
84
89
  /**
85
- * Send Customer success update password Mail
86
- * @param {*} basicAuth
87
- * @param {*} endpoint
88
- * @param {*} tokenRQ
89
- * @returns
90
+ * Sends a "password updated successfully" confirmation email to the customer.
91
+ * The `tokenRQ` must be signed (have a `trx` field). Throws if not signed or if the API fails.
92
+ * @param {string|null} basicAuth - Authorization header value (e.g. `'Basic dXNlcjpwYXNz'`). Pass `null` to send without auth.
93
+ * @param {string} endpoint - Base URL of the SDX Mailer API (e.g. `'https://mailer.miapp.com'`).
94
+ * @param {import('./src/entities/CustomerMailerRequest')} tokenRQ - Signed CustomerMailerRequest to send.
95
+ * @returns {Promise<any>} Resolves with the API response data.
90
96
  */
91
97
  async function sendMailSuccessUpdatedPassword(basicAuth, endpoint, tokenRQ) {
92
98
  return customerMailInstance.sendMailSuccessUpdatedPassword(basicAuth, endpoint, tokenRQ)
93
99
  }
94
100
 
95
101
  /**
96
- * Send Customer register Mail
97
- * @param {*} basicAuth
98
- * @param {*} endpoint
99
- * @param {*} tokenRQ
100
- * @returns
102
+ * Sends a customer registration welcome email.
103
+ * The `tokenRQ` must be signed (have a `trx` field). Throws if not signed or if the API fails.
104
+ * @param {string|null} basicAuth - Authorization header value (e.g. `'Basic dXNlcjpwYXNz'`). Pass `null` to send without auth.
105
+ * @param {string} endpoint - Base URL of the SDX Mailer API (e.g. `'https://mailer.miapp.com'`).
106
+ * @param {import('./src/entities/CustomerMailerRequest')} tokenRQ - Signed CustomerMailerRequest to send.
107
+ * @returns {Promise<any>} Resolves with the API response data.
101
108
  */
102
109
  async function sendMailRegister(basicAuth, endpoint, tokenRQ) {
103
110
  return customerMailInstance.sendMailRegister(basicAuth, endpoint, tokenRQ)
104
111
  }
105
112
 
106
113
  /**
107
- * Send Customer register by owner Mail
108
- * @param {*} basicAuth
109
- * @param {*} endpoint
110
- * @param {*} tokenRQ
111
- * @returns
114
+ * Sends a customer registration email triggered by an owner/admin on behalf of the customer.
115
+ * The `tokenRQ` must be signed (have a `trx` field). Throws if not signed or if the API fails.
116
+ * @param {string|null} basicAuth - Authorization header value (e.g. `'Basic dXNlcjpwYXNz'`). Pass `null` to send without auth.
117
+ * @param {string} endpoint - Base URL of the SDX Mailer API (e.g. `'https://mailer.miapp.com'`).
118
+ * @param {import('./src/entities/CustomerMailerRequest')} tokenRQ - Signed CustomerMailerRequest to send.
119
+ * @returns {Promise<any>} Resolves with the API response data.
112
120
  */
113
121
  async function sendMailRegisterByOwner(basicAuth, endpoint, tokenRQ) {
114
122
  return customerMailInstance.sendMailRegisterByOwner(basicAuth, endpoint, tokenRQ)
115
123
  }
116
124
 
117
125
  /**
118
- * Method for PaymentsMailerRequest creation
119
- * @param {*} trxId
120
- * @param {*} product
121
- * @returns
126
+ * Creates a `PaymentsMailerRequest` object ready to be sent via `sendMailPayment`.
127
+ * @param {string} trxId - Transaction ID associated with the payment (e.g. order ID, payment reference).
128
+ * @param {string} product - Product name or licence identifier related to the payment.
129
+ * @returns {import('./src/entities/PaymentsMailerRequest')} A PaymentsMailerRequest instance. Sign it before calling `sendMailPayment`.
122
130
  */
123
131
  function createPaymentsMailerRequest(trxId, product) {
124
132
  return paymentsMailInstance.createPaymentsMailerRequest(trxId, product)
125
133
  }
126
134
 
127
135
  /**
128
- * Send payment Mail
129
- * @param {*} basicAuth
130
- * @param {*} endpoint
131
- * @param {*} tokenRQ
132
- * @returns
136
+ * Sends a payment confirmation email via the SDX Mailer API.
137
+ * The `tokenRQ` must be signed (have a `trx` field). Throws if not signed or if the API fails.
138
+ * @param {string|null} basicAuth - Authorization header value (e.g. `'Basic dXNlcjpwYXNz'`). Pass `null` to send without auth.
139
+ * @param {string} endpoint - Base URL of the SDX Mailer API (e.g. `'https://mailer.miapp.com'`).
140
+ * @param {import('./src/entities/PaymentsMailerRequest')} tokenRQ - Signed PaymentsMailerRequest to send.
141
+ * @returns {Promise<any>} Resolves with the API response data.
133
142
  */
134
143
  async function sendMailPayment(basicAuth, endpoint, tokenRQ) {
135
144
  return paymentsMailInstance.sendMailPayment(basicAuth, endpoint, tokenRQ)
136
145
  }
137
146
 
147
+ /**
148
+ * Creates a `CustomMailRequest` object ready to be sent via `sendCustomMail`.
149
+ * @param {string} subject - Subject line of the email.
150
+ * @param {string} email - Recipient email address.
151
+ * @param {string} content - Full HTML content of the email body.
152
+ * @param {string[]} [cc] - Optional list of CC email addresses.
153
+ * @returns {import('./src/entities/CustomMailRequest')} A CustomMailRequest instance.
154
+ */
155
+ function createCustomMailRequest(subject, email, content, cc) {
156
+ return customMailInstance.createCustomMailRequest(subject, email, content, cc)
157
+ }
158
+
159
+ /**
160
+ * Sends a fully custom HTML email via the SDX Mailer API.
161
+ * Authenticated with an API key (`x-api-key` header) in the format `sdx_<prefix>.<secret>`.
162
+ * The API key must have at least one permission with `can_execute: true`.
163
+ * @param {string} apiKey - API key in the format `sdx_<prefix>.<secret>`.
164
+ * @param {string} endpoint - Base URL of the SDX Mailer API (e.g. `'https://mailer.miapp.com'`).
165
+ * @param {import('./src/entities/CustomMailRequest')} request - CustomMailRequest to send.
166
+ * @returns {Promise<any>} Resolves with the API response data.
167
+ */
168
+ async function sendCustomMail(apiKey, endpoint, request) {
169
+ return customMailInstance.sendCustomMail(apiKey, endpoint, request)
170
+ }
171
+
138
172
  module.exports = {
139
173
  sendCourierMail, // COURIER
140
174
  sendExceptionMail, // COURIER
@@ -146,5 +180,7 @@ module.exports = {
146
180
  sendMailRegisterByOwner, // CUSTOMER
147
181
  createCustomerMailerRequest, // CUSTOMER
148
182
  sendMailPayment, // PAYMENT
149
- createPaymentsMailerRequest // PAYMENT
183
+ createPaymentsMailerRequest, // PAYMENT
184
+ createCustomMailRequest, // CUSTOM
185
+ sendCustomMail // CUSTOM
150
186
  };
package/package.json CHANGED
@@ -1,8 +1,14 @@
1
1
  {
2
2
  "name": "sdx-mailer-sdk",
3
- "version": "1.0.19",
3
+ "version": "1.0.21",
4
4
  "description": "A api rest sdx-mailer-sender sdk",
5
5
  "main": "index.js",
6
+ "types": "index.d.ts",
7
+ "files": [
8
+ "index.js",
9
+ "index.d.ts",
10
+ "src/"
11
+ ],
6
12
  "scripts": {
7
13
  "test": "echo \"Error: no test specified\" && exit 1"
8
14
  },
@@ -0,0 +1,40 @@
1
+ const CustomMailRequest = require('./entities/CustomMailRequest');
2
+ const WebServiceException = require('./entities/WebServiceException');
3
+ const CourierService = require('./services/CourierService');
4
+ const Commons = require('./utils/Commons');
5
+
6
+ class CustomMail {
7
+
8
+ static BASE_PATH = '/v1/courier/custom-mail'
9
+
10
+ constructor() {
11
+ this.service = new CourierService();
12
+ }
13
+
14
+ createCustomMailRequest(subject, email, content, cc) {
15
+ let request = new CustomMailRequest();
16
+ request.subject = subject;
17
+ request.email = email;
18
+ request.content = content;
19
+ request.cc = Array.isArray(cc) ? cc : [];
20
+ return request;
21
+ }
22
+
23
+ /**
24
+ * Send a fully custom HTML email via the SDX Mailer API.
25
+ * Authenticated with an API key (x-api-key header).
26
+ * @param {string} apiKey
27
+ * @param {string} endpoint
28
+ * @param {CustomMailRequest} request
29
+ * @returns
30
+ */
31
+ async sendCustomMail(apiKey, endpoint, request) {
32
+ if (!Commons.validField(apiKey)) {
33
+ throw WebServiceException.BAD_REQUEST('An API key is required to send a custom mail');
34
+ }
35
+ const url = endpoint + CustomMail.BASE_PATH;
36
+ return await this.service.postMailWithApiKey(apiKey, request, url, 'sendCustomMail');
37
+ }
38
+ }
39
+
40
+ module.exports = CustomMail;
@@ -13,7 +13,7 @@ class PaymentsMail {
13
13
 
14
14
  createPaymentsMailerRequest(trxId, licence) {
15
15
  let request = new PaymentsMailerRequest();
16
- request.user = trxId;
16
+ request.trxId = trxId;
17
17
  request.licence = licence;
18
18
  return request;
19
19
  }
@@ -0,0 +1,10 @@
1
+ class CustomMailRequest {
2
+ constructor() {
3
+ this.subject = '';
4
+ this.email = '';
5
+ this.cc = [];
6
+ this.content = '';
7
+ }
8
+ }
9
+
10
+ module.exports = CustomMailRequest;
@@ -11,6 +11,15 @@ class CourierService {
11
11
  }
12
12
  }
13
13
 
14
+ async postMailWithApiKey(apiKey, body, url, methodName) {
15
+ try {
16
+ const res = await axios.post(url, body, { headers: { 'x-api-key': apiKey } });
17
+ return res.data;
18
+ } catch (err) {
19
+ throw Commons.buildWebServiceException(err, 'POST', methodName);
20
+ }
21
+ }
22
+
14
23
  buildOptions(basicAuth, productId) {
15
24
  if (basicAuth !== null) {
16
25
  return {