@dereekb/nestjs 12.6.21 → 13.0.0
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/LICENSE +1 -1
- package/index.cjs.js +292 -0
- package/index.d.ts +1 -0
- package/index.esm.js +270 -0
- package/mailgun/index.cjs.js +646 -0
- package/mailgun/index.d.ts +1 -0
- package/mailgun/index.esm.js +633 -0
- package/mailgun/package.json +24 -3
- package/mailgun/src/lib/mailgun.d.ts +2 -2
- package/mailgun/src/lib/mailgun.type.d.ts +1 -1
- package/mailgun/src/lib/mailgun.util.d.ts +22 -17
- package/openai/index.cjs.js +310 -0
- package/openai/index.d.ts +1 -0
- package/openai/index.esm.js +293 -0
- package/openai/package.json +24 -5
- package/openai/src/lib/index.d.ts +0 -1
- package/openai/src/lib/webhook/webhook.openai.controller.d.ts +1 -1
- package/openai/src/lib/webhook/webhook.openai.d.ts +2 -2
- package/openai/src/lib/webhook/webhook.openai.service.d.ts +1 -1
- package/package.json +58 -28
- package/stripe/index.cjs.js +223 -0
- package/stripe/index.d.ts +1 -0
- package/stripe/index.esm.js +215 -0
- package/stripe/package.json +24 -5
- package/stripe/src/lib/index.d.ts +1 -1
- package/stripe/src/lib/stripe.module.d.ts +2 -2
- package/stripe/src/lib/webhook/webhook.stripe.controller.d.ts +1 -1
- package/typeform/index.cjs.js +378 -0
- package/typeform/index.d.ts +1 -0
- package/typeform/index.esm.js +363 -0
- package/typeform/package.json +24 -5
- package/typeform/src/lib/index.d.ts +0 -1
- package/typeform/src/lib/webhook/webhook.typeform.controller.d.ts +1 -1
- package/vapiai/index.cjs.js +306 -0
- package/vapiai/index.d.ts +1 -0
- package/vapiai/index.esm.js +297 -0
- package/vapiai/package.json +24 -5
- package/vapiai/src/lib/index.d.ts +0 -1
- package/vapiai/src/lib/vapiai.type.d.ts +3 -1
- package/vapiai/src/lib/webhook/webhook.vapiai.controller.d.ts +1 -1
- package/vapiai/src/lib/webhook/webhook.vapiai.d.ts +1 -9
- package/vapiai/src/lib/webhook/webhook.vapiai.types.d.ts +6 -34
- package/CHANGELOG.md +0 -1997
- package/mailgun/LICENSE +0 -21
- package/mailgun/src/index.js +0 -5
- package/mailgun/src/index.js.map +0 -1
- package/mailgun/src/lib/index.js +0 -10
- package/mailgun/src/lib/index.js.map +0 -1
- package/mailgun/src/lib/mailgun.api.js +0 -37
- package/mailgun/src/lib/mailgun.api.js.map +0 -1
- package/mailgun/src/lib/mailgun.config.js +0 -45
- package/mailgun/src/lib/mailgun.config.js.map +0 -1
- package/mailgun/src/lib/mailgun.js +0 -205
- package/mailgun/src/lib/mailgun.js.map +0 -1
- package/mailgun/src/lib/mailgun.service.js +0 -53
- package/mailgun/src/lib/mailgun.service.js.map +0 -1
- package/mailgun/src/lib/mailgun.service.module.js +0 -84
- package/mailgun/src/lib/mailgun.service.module.js.map +0 -1
- package/mailgun/src/lib/mailgun.type.js +0 -3
- package/mailgun/src/lib/mailgun.type.js.map +0 -1
- package/mailgun/src/lib/mailgun.util.js +0 -206
- package/mailgun/src/lib/mailgun.util.js.map +0 -1
- package/openai/LICENSE +0 -21
- package/openai/src/index.js +0 -5
- package/openai/src/index.js.map +0 -1
- package/openai/src/lib/index.js +0 -11
- package/openai/src/lib/index.js.map +0 -1
- package/openai/src/lib/openai.api.js +0 -22
- package/openai/src/lib/openai.api.js.map +0 -1
- package/openai/src/lib/openai.config.js +0 -29
- package/openai/src/lib/openai.config.js.map +0 -1
- package/openai/src/lib/openai.module.js +0 -41
- package/openai/src/lib/openai.module.js.map +0 -1
- package/openai/src/lib/openai.type.js +0 -3
- package/openai/src/lib/openai.type.js.map +0 -1
- package/openai/src/lib/openai.util.js +0 -48
- package/openai/src/lib/openai.util.js.map +0 -1
- package/openai/src/lib/webhook/index.js +0 -10
- package/openai/src/lib/webhook/index.js.map +0 -1
- package/openai/src/lib/webhook/webhook.openai.config.js +0 -17
- package/openai/src/lib/webhook/webhook.openai.config.js.map +0 -1
- package/openai/src/lib/webhook/webhook.openai.controller.js +0 -30
- package/openai/src/lib/webhook/webhook.openai.controller.js.map +0 -1
- package/openai/src/lib/webhook/webhook.openai.js +0 -41
- package/openai/src/lib/webhook/webhook.openai.js.map +0 -1
- package/openai/src/lib/webhook/webhook.openai.module.js +0 -39
- package/openai/src/lib/webhook/webhook.openai.module.js.map +0 -1
- package/openai/src/lib/webhook/webhook.openai.service.js +0 -48
- package/openai/src/lib/webhook/webhook.openai.service.js.map +0 -1
- package/openai/src/lib/webhook/webhook.openai.verify.js +0 -37
- package/openai/src/lib/webhook/webhook.openai.verify.js.map +0 -1
- package/src/index.js +0 -5
- package/src/index.js.map +0 -1
- package/src/lib/decorators/index.js +0 -6
- package/src/lib/decorators/index.js.map +0 -1
- package/src/lib/decorators/local.decorator.js +0 -17
- package/src/lib/decorators/local.decorator.js.map +0 -1
- package/src/lib/decorators/rawbody.js +0 -44
- package/src/lib/decorators/rawbody.js.map +0 -1
- package/src/lib/index.js +0 -7
- package/src/lib/index.js.map +0 -1
- package/src/lib/middlewares/index.js +0 -7
- package/src/lib/middlewares/index.js.map +0 -1
- package/src/lib/middlewares/json.middleware.js +0 -16
- package/src/lib/middlewares/json.middleware.js.map +0 -1
- package/src/lib/middlewares/rawbody.middleware.js +0 -16
- package/src/lib/middlewares/rawbody.middleware.js.map +0 -1
- package/src/lib/middlewares/webhook.js +0 -51
- package/src/lib/middlewares/webhook.js.map +0 -1
- package/src/lib/module/client/client.config.js +0 -13
- package/src/lib/module/client/client.config.js.map +0 -1
- package/src/lib/module/client/client.js +0 -8
- package/src/lib/module/client/client.js.map +0 -1
- package/src/lib/module/client/client.module.js +0 -36
- package/src/lib/module/client/client.module.js.map +0 -1
- package/src/lib/module/client/client.service.js +0 -30
- package/src/lib/module/client/client.service.js.map +0 -1
- package/src/lib/module/client/index.js +0 -8
- package/src/lib/module/client/index.js.map +0 -1
- package/src/lib/module/env/env.config.js +0 -14
- package/src/lib/module/env/env.config.js.map +0 -1
- package/src/lib/module/env/env.js +0 -10
- package/src/lib/module/env/env.js.map +0 -1
- package/src/lib/module/env/env.nest.js +0 -16
- package/src/lib/module/env/env.nest.js.map +0 -1
- package/src/lib/module/env/env.service.js +0 -33
- package/src/lib/module/env/env.service.js.map +0 -1
- package/src/lib/module/env/index.js +0 -8
- package/src/lib/module/env/index.js.map +0 -1
- package/src/lib/module/index.js +0 -7
- package/src/lib/module/index.js.map +0 -1
- package/src/lib/module/module.js +0 -26
- package/src/lib/module/module.js.map +0 -1
- package/stripe/LICENSE +0 -21
- package/stripe/src/index.js +0 -5
- package/stripe/src/index.js.map +0 -1
- package/stripe/src/lib/index.js +0 -8
- package/stripe/src/lib/index.js.map +0 -1
- package/stripe/src/lib/stripe.api.js +0 -40
- package/stripe/src/lib/stripe.api.js.map +0 -1
- package/stripe/src/lib/stripe.config.js +0 -19
- package/stripe/src/lib/stripe.config.js.map +0 -1
- package/stripe/src/lib/stripe.module.js +0 -41
- package/stripe/src/lib/stripe.module.js.map +0 -1
- package/stripe/src/lib/stripe.type.js +0 -4
- package/stripe/src/lib/stripe.type.js.map +0 -1
- package/stripe/src/lib/webhook/index.js +0 -8
- package/stripe/src/lib/webhook/index.js.map +0 -1
- package/stripe/src/lib/webhook/webhook.stripe.controller.js +0 -30
- package/stripe/src/lib/webhook/webhook.stripe.controller.js.map +0 -1
- package/stripe/src/lib/webhook/webhook.stripe.js +0 -67
- package/stripe/src/lib/webhook/webhook.stripe.js.map +0 -1
- package/stripe/src/lib/webhook/webhook.stripe.module.js +0 -20
- package/stripe/src/lib/webhook/webhook.stripe.module.js.map +0 -1
- package/stripe/src/lib/webhook/webhook.stripe.service.js +0 -36
- package/stripe/src/lib/webhook/webhook.stripe.service.js.map +0 -1
- package/typeform/LICENSE +0 -21
- package/typeform/src/index.js +0 -5
- package/typeform/src/index.js.map +0 -1
- package/typeform/src/lib/index.js +0 -11
- package/typeform/src/lib/index.js.map +0 -1
- package/typeform/src/lib/typeform.api.js +0 -22
- package/typeform/src/lib/typeform.api.js.map +0 -1
- package/typeform/src/lib/typeform.config.js +0 -21
- package/typeform/src/lib/typeform.config.js.map +0 -1
- package/typeform/src/lib/typeform.module.js +0 -39
- package/typeform/src/lib/typeform.module.js.map +0 -1
- package/typeform/src/lib/typeform.type.js +0 -3
- package/typeform/src/lib/typeform.type.js.map +0 -1
- package/typeform/src/lib/typeform.util.js +0 -26
- package/typeform/src/lib/typeform.util.js.map +0 -1
- package/typeform/src/lib/webhook/index.js +0 -12
- package/typeform/src/lib/webhook/index.js.map +0 -1
- package/typeform/src/lib/webhook/webhook.typeform.config.js +0 -17
- package/typeform/src/lib/webhook/webhook.typeform.config.js.map +0 -1
- package/typeform/src/lib/webhook/webhook.typeform.controller.js +0 -30
- package/typeform/src/lib/webhook/webhook.typeform.controller.js.map +0 -1
- package/typeform/src/lib/webhook/webhook.typeform.form.js +0 -113
- package/typeform/src/lib/webhook/webhook.typeform.form.js.map +0 -1
- package/typeform/src/lib/webhook/webhook.typeform.js +0 -27
- package/typeform/src/lib/webhook/webhook.typeform.js.map +0 -1
- package/typeform/src/lib/webhook/webhook.typeform.module.js +0 -38
- package/typeform/src/lib/webhook/webhook.typeform.module.js.map +0 -1
- package/typeform/src/lib/webhook/webhook.typeform.service.js +0 -45
- package/typeform/src/lib/webhook/webhook.typeform.service.js.map +0 -1
- package/typeform/src/lib/webhook/webhook.typeform.type.js +0 -3
- package/typeform/src/lib/webhook/webhook.typeform.type.js.map +0 -1
- package/typeform/src/lib/webhook/webhook.typeform.verify.js +0 -28
- package/typeform/src/lib/webhook/webhook.typeform.verify.js.map +0 -1
- package/vapiai/LICENSE +0 -21
- package/vapiai/src/index.js +0 -5
- package/vapiai/src/index.js.map +0 -1
- package/vapiai/src/lib/index.js +0 -10
- package/vapiai/src/lib/index.js.map +0 -1
- package/vapiai/src/lib/vapiai.api.js +0 -32
- package/vapiai/src/lib/vapiai.api.js.map +0 -1
- package/vapiai/src/lib/vapiai.config.js +0 -17
- package/vapiai/src/lib/vapiai.config.js.map +0 -1
- package/vapiai/src/lib/vapiai.module.js +0 -38
- package/vapiai/src/lib/vapiai.module.js.map +0 -1
- package/vapiai/src/lib/vapiai.type.js +0 -3
- package/vapiai/src/lib/vapiai.type.js.map +0 -1
- package/vapiai/src/lib/webhook/index.js +0 -10
- package/vapiai/src/lib/webhook/index.js.map +0 -1
- package/vapiai/src/lib/webhook/webhook.vapi.config.js +0 -20
- package/vapiai/src/lib/webhook/webhook.vapi.config.js.map +0 -1
- package/vapiai/src/lib/webhook/webhook.vapiai.controller.js +0 -38
- package/vapiai/src/lib/webhook/webhook.vapiai.controller.js.map +0 -1
- package/vapiai/src/lib/webhook/webhook.vapiai.js +0 -42
- package/vapiai/src/lib/webhook/webhook.vapiai.js.map +0 -1
- package/vapiai/src/lib/webhook/webhook.vapiai.module.js +0 -41
- package/vapiai/src/lib/webhook/webhook.vapiai.module.js.map +0 -1
- package/vapiai/src/lib/webhook/webhook.vapiai.service.js +0 -52
- package/vapiai/src/lib/webhook/webhook.vapiai.service.js.map +0 -1
- package/vapiai/src/lib/webhook/webhook.vapiai.types.js +0 -25
- package/vapiai/src/lib/webhook/webhook.vapiai.types.js.map +0 -1
- package/vapiai/src/lib/webhook/webhook.vapiai.verify.js +0 -49
- package/vapiai/src/lib/webhook/webhook.vapiai.verify.js.map +0 -1
|
@@ -0,0 +1,633 @@
|
|
|
1
|
+
import { isTestNodeEnv, ServerEnvironmentService } from '@dereekb/nestjs';
|
|
2
|
+
import { MAP_IDENTITY, asArray, forEachKeyValue, objectIsEmpty, addToSet, overrideInObject, KeyValueTypleValueFilter, forEachInIterable, makeValuesGroupMap, filterMaybeArrayValues } from '@dereekb/util';
|
|
3
|
+
import { Injectable, Inject, Module } from '@nestjs/common';
|
|
4
|
+
import Mailgun from 'mailgun.js';
|
|
5
|
+
import FormData from 'form-data';
|
|
6
|
+
import { ConfigService, ConfigModule } from '@nestjs/config';
|
|
7
|
+
|
|
8
|
+
const DEFAULT_RECIPIENT_VARIABLE_PREFIX = 'recipient-';
|
|
9
|
+
const MAILGUN_REPLY_TO_EMAIL_HEADER_DATA_VARIABLE_KEY = `h:Reply-To`;
|
|
10
|
+
/**
|
|
11
|
+
* The maximum number of recipients allowed in a single batch send request.
|
|
12
|
+
*/
|
|
13
|
+
const MAX_BATCH_SEND_RECIPIENTS = 1000;
|
|
14
|
+
/**
|
|
15
|
+
* Converts a MailgunTemplateEmailRequest to a MailgunMessageData.
|
|
16
|
+
*
|
|
17
|
+
* @param config
|
|
18
|
+
* @returns
|
|
19
|
+
*/
|
|
20
|
+
function convertMailgunTemplateEmailRequestToMailgunMessageData(config) {
|
|
21
|
+
const {
|
|
22
|
+
request,
|
|
23
|
+
defaultSender,
|
|
24
|
+
isTestingEnvironment: testEnvironment,
|
|
25
|
+
recipientVariablePrefix: inputRecipientVariablePrefix = DEFAULT_RECIPIENT_VARIABLE_PREFIX,
|
|
26
|
+
addRecipientVariablePrefixToAllMergedGlobalVariables: inputAddRecipientVariablePrefixToAllMergedGlobalVariables = true,
|
|
27
|
+
addCopyOfMergedRecipientVariablesWithoutPrefixToGlobalVariables: inputAddCopyOfMergedRecipientVariablesWithoutPrefixToGlobalVariables = false,
|
|
28
|
+
addCopyOfTemplateVariablesWithRecipientVariablePrefix: inputAddCopyOfTemplateVariablesWithRecipientVariablePrefix = false
|
|
29
|
+
} = config;
|
|
30
|
+
const {
|
|
31
|
+
recipientVariablesConfig
|
|
32
|
+
} = request;
|
|
33
|
+
const recipientVariablePrefix = recipientVariablesConfig?.recipientVariablePrefix ?? inputRecipientVariablePrefix;
|
|
34
|
+
const addRecipientVariablePrefixToAllMergedGlobalVariables = recipientVariablesConfig?.addRecipientVariablePrefixToAllMergedGlobalVariables ?? inputAddRecipientVariablePrefixToAllMergedGlobalVariables;
|
|
35
|
+
const addCopyOfMergedRecipientVariablesWithoutPrefixToGlobalVariables = recipientVariablesConfig?.addCopyOfMergedRecipientVariablesWithoutPrefixToGlobalVariables ?? inputAddCopyOfMergedRecipientVariablesWithoutPrefixToGlobalVariables;
|
|
36
|
+
const addCopyOfTemplateVariablesWithRecipientVariablePrefix = recipientVariablesConfig?.addCopyOfTemplateVariablesWithRecipientVariablePrefix ?? inputAddCopyOfTemplateVariablesWithRecipientVariablePrefix;
|
|
37
|
+
const finalizeRecipientVariables = request.finalizeRecipientVariables ?? MAP_IDENTITY;
|
|
38
|
+
const allowBatchSending = request.batchSend ?? true;
|
|
39
|
+
const mergeRecipientVariablesIntoGlobalVariable = !allowBatchSending;
|
|
40
|
+
function mapEmailToLowercase(x) {
|
|
41
|
+
return {
|
|
42
|
+
...x,
|
|
43
|
+
email: x.email.toLowerCase()
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
const toInput = asArray(request.to).map(mapEmailToLowercase);
|
|
47
|
+
const ccInput = request.cc ? asArray(request.cc).map(mapEmailToLowercase) : undefined;
|
|
48
|
+
const bccInput = request.bcc ? asArray(request.bcc).map(mapEmailToLowercase) : undefined;
|
|
49
|
+
const from = request.from ? convertMailgunRecipientToString(request.from) : defaultSender;
|
|
50
|
+
const to = convertMailgunRecipientsToStrings(toInput);
|
|
51
|
+
const cc = ccInput?.length ? convertMailgunRecipientsToStrings(ccInput) : undefined;
|
|
52
|
+
const bcc = bccInput?.length ? convertMailgunRecipientsToStrings(bccInput) : undefined;
|
|
53
|
+
// throw an error if batchSend is not defined and cc or bcc is defined
|
|
54
|
+
if (request.batchSend == null && (ccInput || bccInput)) {
|
|
55
|
+
throw new Error('convertMailgunTemplateEmailRequestToMailgunMessageData(): batchSend must be false when either "cc" or "bcc" is defined.');
|
|
56
|
+
}
|
|
57
|
+
if (allowBatchSending && to.length > MAX_BATCH_SEND_RECIPIENTS) {
|
|
58
|
+
throw new Error(`convertMailgunTemplateEmailRequestToMailgunMessageData(): Can only batch send to a maximum of ${MAX_BATCH_SEND_RECIPIENTS} recipients.`);
|
|
59
|
+
}
|
|
60
|
+
const data = {
|
|
61
|
+
from,
|
|
62
|
+
to,
|
|
63
|
+
cc,
|
|
64
|
+
bcc,
|
|
65
|
+
subject: request.subject,
|
|
66
|
+
template: request.template,
|
|
67
|
+
...request.messageData
|
|
68
|
+
};
|
|
69
|
+
if (request.replyTo != null) {
|
|
70
|
+
data[MAILGUN_REPLY_TO_EMAIL_HEADER_DATA_VARIABLE_KEY] = convertMailgunRecipientToString(request.replyTo);
|
|
71
|
+
}
|
|
72
|
+
if (request.testEmail === true || (testEnvironment ?? isTestNodeEnv()) && request.testEmail !== false) {
|
|
73
|
+
data['o:testmode'] = true;
|
|
74
|
+
}
|
|
75
|
+
if (request.templateVariables) {
|
|
76
|
+
forEachKeyValue(request.templateVariables, {
|
|
77
|
+
forEach: x => {
|
|
78
|
+
const [key, value] = x;
|
|
79
|
+
const encodedValue = encodeMailgunTemplateVariableValue(value);
|
|
80
|
+
if (encodedValue != null) {
|
|
81
|
+
data[`v:${key}`] = encodedValue;
|
|
82
|
+
if (recipientVariablePrefix && addCopyOfTemplateVariablesWithRecipientVariablePrefix) {
|
|
83
|
+
data[`v:${recipientVariablePrefix}${key}`] = encodedValue;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
const hasUserVariables = Boolean(data['recipient-variables']) || toInput.findIndex(x => x.userVariables != null) !== -1;
|
|
90
|
+
if (hasUserVariables) {
|
|
91
|
+
let recipientVariables = {};
|
|
92
|
+
const allRecipientVariableKeys = new Set();
|
|
93
|
+
toInput.forEach(({
|
|
94
|
+
email,
|
|
95
|
+
userVariables
|
|
96
|
+
}) => {
|
|
97
|
+
if (userVariables != null && !objectIsEmpty(userVariables)) {
|
|
98
|
+
recipientVariables[email] = userVariables;
|
|
99
|
+
addToSet(allRecipientVariableKeys, Object.keys(userVariables));
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
if (data['recipient-variables']) {
|
|
103
|
+
const decoded = JSON.parse(data['recipient-variables']);
|
|
104
|
+
forEachKeyValue(decoded, {
|
|
105
|
+
forEach: x => {
|
|
106
|
+
const [recipientEmail, userVariables] = x;
|
|
107
|
+
const email = recipientEmail.toLowerCase();
|
|
108
|
+
if (recipientVariables[email] != null) {
|
|
109
|
+
overrideInObject(recipientVariables[email], {
|
|
110
|
+
from: [userVariables],
|
|
111
|
+
filter: {
|
|
112
|
+
valueFilter: KeyValueTypleValueFilter.UNDEFINED
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
} else {
|
|
116
|
+
recipientVariables[email] = userVariables;
|
|
117
|
+
}
|
|
118
|
+
addToSet(allRecipientVariableKeys, Object.keys(userVariables));
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
// Finalize the recipient variables before they are re-encoded back to JSON
|
|
123
|
+
recipientVariables = finalizeRecipientVariables(recipientVariables, {
|
|
124
|
+
messageData: data,
|
|
125
|
+
config
|
|
126
|
+
}) ?? recipientVariables;
|
|
127
|
+
if (mergeRecipientVariablesIntoGlobalVariable) {
|
|
128
|
+
const addRecipientPrefixVariable = recipientVariablePrefix && addRecipientVariablePrefixToAllMergedGlobalVariables;
|
|
129
|
+
// iterate all recipient variables and merge them into the global variables
|
|
130
|
+
forEachKeyValue(recipientVariables, {
|
|
131
|
+
forEach: x => {
|
|
132
|
+
const [email, userVariables] = x;
|
|
133
|
+
forEachKeyValue(userVariables, {
|
|
134
|
+
forEach: y => {
|
|
135
|
+
const [key, value] = y;
|
|
136
|
+
const encodedValue = encodeMailgunTemplateVariableValue(value);
|
|
137
|
+
if (encodedValue != null) {
|
|
138
|
+
if (addRecipientPrefixVariable) {
|
|
139
|
+
// also add the variable using the recipient variable prefix
|
|
140
|
+
const recipientVariableKey = `${recipientVariablePrefix}${key}`;
|
|
141
|
+
data[`v:${recipientVariableKey}`] = encodedValue;
|
|
142
|
+
}
|
|
143
|
+
// add if not adding the prefix variable, or if it was requested
|
|
144
|
+
if (!addRecipientPrefixVariable || addCopyOfMergedRecipientVariablesWithoutPrefixToGlobalVariables) {
|
|
145
|
+
data[`v:${key}`] = encodedValue;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
} else {
|
|
153
|
+
// set back on the data object
|
|
154
|
+
data['recipient-variables'] = JSON.stringify(recipientVariables);
|
|
155
|
+
// add all recipient variable to the other variables so they can be used easily/directly in templates as variables too.
|
|
156
|
+
// https://documentation.mailgun.com/en/latest/user_manual.html#attaching-data-to-messages
|
|
157
|
+
if (recipientVariablePrefix) {
|
|
158
|
+
forEachInIterable(allRecipientVariableKeys, key => {
|
|
159
|
+
const recipientVariableKey = `${recipientVariablePrefix}${key}`;
|
|
160
|
+
// v:recipient-id=%recipient.id%
|
|
161
|
+
data[`v:${recipientVariableKey}`] = `%recipient.${key}%`;
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
// double check and remove the recipient variables if we merged them into the global variables
|
|
167
|
+
if (mergeRecipientVariablesIntoGlobalVariable) {
|
|
168
|
+
delete data['recipient-variables'];
|
|
169
|
+
}
|
|
170
|
+
const inputAttachments = request.attachments;
|
|
171
|
+
if (inputAttachments) {
|
|
172
|
+
if (data.attachment) {
|
|
173
|
+
throw new Error(`Cannot specify attachments in both messageData and in the request's attachments field.`);
|
|
174
|
+
}
|
|
175
|
+
data.attachment = inputAttachments;
|
|
176
|
+
}
|
|
177
|
+
return data;
|
|
178
|
+
}
|
|
179
|
+
function convertMailgunRecipientsToStrings(recipients) {
|
|
180
|
+
return recipients.map(x => convertMailgunRecipientToString(x));
|
|
181
|
+
}
|
|
182
|
+
function convertMailgunRecipientToString(recipient) {
|
|
183
|
+
let address = recipient.email;
|
|
184
|
+
if (recipient.name) {
|
|
185
|
+
address = `${recipient.name} <${address}>`;
|
|
186
|
+
}
|
|
187
|
+
return address;
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Encodes a value to a string for use as a Mailgun template variable. Throws an error if the value is not supported.
|
|
191
|
+
*
|
|
192
|
+
* @param value The value to encode.
|
|
193
|
+
* @returns The encoded value, or undefined if the value is null or undefined.
|
|
194
|
+
*/
|
|
195
|
+
function encodeMailgunTemplateVariableValue(value) {
|
|
196
|
+
let encodedValue;
|
|
197
|
+
switch (typeof value) {
|
|
198
|
+
case 'object':
|
|
199
|
+
if (value) {
|
|
200
|
+
if (value instanceof Date) {
|
|
201
|
+
encodedValue = value.toISOString();
|
|
202
|
+
} else {
|
|
203
|
+
encodedValue = JSON.stringify(value);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
break;
|
|
207
|
+
case 'bigint':
|
|
208
|
+
case 'boolean':
|
|
209
|
+
case 'number':
|
|
210
|
+
case 'string':
|
|
211
|
+
encodedValue = String(value); // encoded as a string value
|
|
212
|
+
break;
|
|
213
|
+
default:
|
|
214
|
+
if (value) {
|
|
215
|
+
throw new Error(`Invalid value "${value}" passed to encodeMailgunTemplateVariableValue().`);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
return encodedValue;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/******************************************************************************
|
|
222
|
+
Copyright (c) Microsoft Corporation.
|
|
223
|
+
|
|
224
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
225
|
+
purpose with or without fee is hereby granted.
|
|
226
|
+
|
|
227
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
228
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
229
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
230
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
231
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
232
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
233
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
234
|
+
***************************************************************************** */
|
|
235
|
+
/* global Reflect, Promise, SuppressedError, Symbol, Iterator */
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
function __decorate(decorators, target, key, desc) {
|
|
239
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
240
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
241
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
242
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
function __param(paramIndex, decorator) {
|
|
246
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function __metadata(metadataKey, metadataValue) {
|
|
250
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(metadataKey, metadataValue);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
254
|
+
var e = new Error(message);
|
|
255
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
256
|
+
};
|
|
257
|
+
|
|
258
|
+
class MailgunServiceConfig {
|
|
259
|
+
// Mailgun Config
|
|
260
|
+
mailgun;
|
|
261
|
+
/**
|
|
262
|
+
* Base URL to the client.
|
|
263
|
+
*/
|
|
264
|
+
clientUrl;
|
|
265
|
+
/**
|
|
266
|
+
* Main domain to send emails from.
|
|
267
|
+
*/
|
|
268
|
+
domain;
|
|
269
|
+
/**
|
|
270
|
+
* Mailgun sender string.
|
|
271
|
+
*/
|
|
272
|
+
sender;
|
|
273
|
+
/**
|
|
274
|
+
* Additional messages config
|
|
275
|
+
*/
|
|
276
|
+
messages;
|
|
277
|
+
static assertValidConfig(config) {
|
|
278
|
+
if (!config.mailgun.username) {
|
|
279
|
+
throw new Error('No mailgun username specified.');
|
|
280
|
+
} else if (!config.mailgun.key) {
|
|
281
|
+
throw new Error('No mailgun key specified.');
|
|
282
|
+
} else if (!config.domain) {
|
|
283
|
+
throw new Error('No mailgun domain specified.');
|
|
284
|
+
} else if (!config.clientUrl) {
|
|
285
|
+
throw new Error('No client url specified.');
|
|
286
|
+
} else if (!config.sender) {
|
|
287
|
+
throw new Error('No mailgun sender specified.');
|
|
288
|
+
} else if (!config.messages) {
|
|
289
|
+
throw new Error('No mailgun messages config specified.');
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
let MailgunApi = class MailgunApi {
|
|
295
|
+
config;
|
|
296
|
+
client;
|
|
297
|
+
constructor(config) {
|
|
298
|
+
this.config = config;
|
|
299
|
+
this.client = new Mailgun(FormData).client({
|
|
300
|
+
...config.mailgun
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
get messages() {
|
|
304
|
+
return this.client.messages;
|
|
305
|
+
}
|
|
306
|
+
get clientUrl() {
|
|
307
|
+
return this.config.clientUrl;
|
|
308
|
+
}
|
|
309
|
+
get domain() {
|
|
310
|
+
return this.config.domain;
|
|
311
|
+
}
|
|
312
|
+
get sender() {
|
|
313
|
+
return this.config.sender;
|
|
314
|
+
}
|
|
315
|
+
};
|
|
316
|
+
MailgunApi = __decorate([Injectable(), __param(0, Inject(MailgunServiceConfig)), __metadata("design:paramtypes", [MailgunServiceConfig])], MailgunApi);
|
|
317
|
+
|
|
318
|
+
let MailgunService = class MailgunService {
|
|
319
|
+
_mailgunApi;
|
|
320
|
+
_serverEnvironmentService;
|
|
321
|
+
constructor(mailgunApi, serverEnvironmentService) {
|
|
322
|
+
this._mailgunApi = mailgunApi;
|
|
323
|
+
this._serverEnvironmentService = serverEnvironmentService;
|
|
324
|
+
}
|
|
325
|
+
get mailgunApi() {
|
|
326
|
+
return this._mailgunApi;
|
|
327
|
+
}
|
|
328
|
+
get serverEnvironmentService() {
|
|
329
|
+
return this._serverEnvironmentService;
|
|
330
|
+
}
|
|
331
|
+
async sendTemplateEmail(request) {
|
|
332
|
+
const domain = this.mailgunApi.domain;
|
|
333
|
+
const sender = this.mailgunApi.sender;
|
|
334
|
+
const isTestingEnvironment = this.serverEnvironmentService.isTestingEnv;
|
|
335
|
+
const {
|
|
336
|
+
recipientVariablePrefix
|
|
337
|
+
} = this.mailgunApi.config.messages;
|
|
338
|
+
const data = convertMailgunTemplateEmailRequestToMailgunMessageData({
|
|
339
|
+
request,
|
|
340
|
+
defaultSender: sender,
|
|
341
|
+
recipientVariablePrefix,
|
|
342
|
+
isTestingEnvironment
|
|
343
|
+
});
|
|
344
|
+
let result;
|
|
345
|
+
const shouldSend = !isTestingEnvironment || request.sendTestEmails || this.mailgunApi.config.messages.sendTestEmails;
|
|
346
|
+
if (shouldSend) {
|
|
347
|
+
try {
|
|
348
|
+
result = await this.mailgunApi.messages.create(domain, data);
|
|
349
|
+
} catch (e) {
|
|
350
|
+
console.error('Failed sending email: ', e);
|
|
351
|
+
throw e;
|
|
352
|
+
}
|
|
353
|
+
} else {
|
|
354
|
+
result = {
|
|
355
|
+
status: 200,
|
|
356
|
+
message: 'Success. Env prevented sending email.'
|
|
357
|
+
};
|
|
358
|
+
}
|
|
359
|
+
return result;
|
|
360
|
+
}
|
|
361
|
+
};
|
|
362
|
+
MailgunService = __decorate([Injectable(), __param(0, Inject(MailgunApi)), __param(1, Inject(ServerEnvironmentService)), __metadata("design:paramtypes", [MailgunApi, ServerEnvironmentService])], MailgunService);
|
|
363
|
+
|
|
364
|
+
function mailgunServiceConfigFactory(configService, serverEnvironmentService) {
|
|
365
|
+
const isTestingEnv = serverEnvironmentService.isTestingEnv;
|
|
366
|
+
const useSandbox = configService.get('USE_MAILGUN_SANDBOX') === 'true' || isTestingEnv;
|
|
367
|
+
const sendTestEmails = configService.get('MAILGUN_SEND_TEST_EMAILS') === 'true';
|
|
368
|
+
let key = configService.get('MAILGUN_API_KEY');
|
|
369
|
+
let domain = configService.get('MAILGUN_DOMAIN');
|
|
370
|
+
if (useSandbox) {
|
|
371
|
+
key = configService.get('MAILGUN_SANDBOX_API_KEY');
|
|
372
|
+
domain = configService.get('MAILGUN_SANDBOX_DOMAIN');
|
|
373
|
+
if (!key || !domain) {
|
|
374
|
+
throw new Error('USE_MAILGUN_SANDBOX is set to "true" (or current environment is a testing environment), but no environment variables for the sandbox (MAILGUN_SANDBOX_API_KEY, MAILGUN_SANDBOX_DOMAIN) are provided.');
|
|
375
|
+
} else if (!serverEnvironmentService.isTestingEnv) {
|
|
376
|
+
console.log('Using Mailgun Sandbox Domain: ', domain);
|
|
377
|
+
}
|
|
378
|
+
} else if (!serverEnvironmentService.isTestingEnv) {
|
|
379
|
+
console.log('Using Mailgun Production Domain: ', domain);
|
|
380
|
+
}
|
|
381
|
+
const name = configService.get('MAILGUN_SENDER_NAME');
|
|
382
|
+
const email = configService.get('MAILGUN_SENDER_EMAIL');
|
|
383
|
+
const url = configService.get('MAILGUN_API_URL');
|
|
384
|
+
const recipientVariablePrefix = configService.get('MAILGUN_MESSAGES_RECIPIENT_VARIABLE_PREFIX') ?? undefined;
|
|
385
|
+
if (!email) {
|
|
386
|
+
throw new Error('MAILGUN_SENDER_EMAIL is required but was not configured.');
|
|
387
|
+
} else if (!key) {
|
|
388
|
+
throw new Error('MAILGUN_API_KEY (or MAILGUN_SANDBOX_API_KEY for tests) is required but was not configured.');
|
|
389
|
+
} else if (!domain) {
|
|
390
|
+
throw new Error('MAILGUN_DOMAIN (or MAILGUN_SANDBOX_DOMAIN for tests) is required but was not configured.');
|
|
391
|
+
}
|
|
392
|
+
const clientUrl = configService.get('CLIENT_APP_URL') ?? domain;
|
|
393
|
+
const config = {
|
|
394
|
+
mailgun: {
|
|
395
|
+
username: configService.get('MAILGUN_USERNAME') ?? 'api',
|
|
396
|
+
key,
|
|
397
|
+
url
|
|
398
|
+
},
|
|
399
|
+
domain,
|
|
400
|
+
clientUrl,
|
|
401
|
+
sender: convertMailgunRecipientToString({
|
|
402
|
+
name,
|
|
403
|
+
email
|
|
404
|
+
}),
|
|
405
|
+
messages: {
|
|
406
|
+
sendTestEmails,
|
|
407
|
+
recipientVariablePrefix
|
|
408
|
+
}
|
|
409
|
+
};
|
|
410
|
+
MailgunServiceConfig.assertValidConfig(config);
|
|
411
|
+
return config;
|
|
412
|
+
}
|
|
413
|
+
let MailgunServiceModule = class MailgunServiceModule {};
|
|
414
|
+
MailgunServiceModule = __decorate([Module({
|
|
415
|
+
imports: [ConfigModule],
|
|
416
|
+
providers: [{
|
|
417
|
+
provide: MailgunServiceConfig,
|
|
418
|
+
inject: [ConfigService, ServerEnvironmentService],
|
|
419
|
+
useFactory: mailgunServiceConfigFactory
|
|
420
|
+
}, MailgunApi, MailgunService],
|
|
421
|
+
exports: [MailgunApi, MailgunService]
|
|
422
|
+
})], MailgunServiceModule);
|
|
423
|
+
|
|
424
|
+
/**
|
|
425
|
+
* The default template subject to use when batch sending emails.
|
|
426
|
+
*
|
|
427
|
+
* This pulls the subject from each recipient's user variables.
|
|
428
|
+
*/
|
|
429
|
+
const MAILGUN_BATCH_SEND_RECIPIENT_SUBJECT_TEMPLATE = `%recipient.subject%`;
|
|
430
|
+
/**
|
|
431
|
+
* Creates a composite key from the from/replyTo email addresses used to group MailgunRecipientBatchSendTarget values.
|
|
432
|
+
*/
|
|
433
|
+
function mailgunRecipientBatchSendTargetFromReplyToBatchGroupKey(recipient) {
|
|
434
|
+
const fromEmail = (recipient.from?.email ?? '').toLowerCase();
|
|
435
|
+
const replyToEmail = (recipient.replyTo?.email ?? '').toLowerCase();
|
|
436
|
+
return `f:${fromEmail}|r:${replyToEmail}`;
|
|
437
|
+
}
|
|
438
|
+
/**
|
|
439
|
+
* Creates a ExpandMailgunRecipientBatchSendTargetRequestFactory from the input config.
|
|
440
|
+
*
|
|
441
|
+
* @param config
|
|
442
|
+
* @returns
|
|
443
|
+
*/
|
|
444
|
+
function expandMailgunRecipientBatchSendTargetRequestFactory(config) {
|
|
445
|
+
const {
|
|
446
|
+
request: inputBaseRequest,
|
|
447
|
+
useSubjectFromRecipientUserVariables,
|
|
448
|
+
allowSingleRecipientBatchSendRequests,
|
|
449
|
+
recipientVariablesConfig,
|
|
450
|
+
mailgunRecipientBatchSendTargetEntityKeyRecipientLookup,
|
|
451
|
+
overrideCarbonCopyVariablesWithCarbonCopyKeyRecipients
|
|
452
|
+
} = config;
|
|
453
|
+
const defaultSubject = inputBaseRequest.subject;
|
|
454
|
+
if (!defaultSubject && !useSubjectFromRecipientUserVariables) {
|
|
455
|
+
throw new Error('defaultSubject must be set when "useSubjectFromRecipientUserVariables" is false');
|
|
456
|
+
}
|
|
457
|
+
/**
|
|
458
|
+
* Returns the carbon copy recipients, based on the input.
|
|
459
|
+
*
|
|
460
|
+
* Will return undefined if the array would be empty.
|
|
461
|
+
*
|
|
462
|
+
* @param input
|
|
463
|
+
* @returns
|
|
464
|
+
*/
|
|
465
|
+
function determineCarbonCopyRecipients(input) {
|
|
466
|
+
const {
|
|
467
|
+
baseRequestCarbonCopyRecipients,
|
|
468
|
+
carbonCopyRecipients,
|
|
469
|
+
carbonCopyRecipientsKeys
|
|
470
|
+
} = input;
|
|
471
|
+
let cc = carbonCopyRecipients ? asArray(carbonCopyRecipients) : baseRequestCarbonCopyRecipients;
|
|
472
|
+
const resolvedCc = mailgunRecipientBatchSendTargetEntityKeyRecipientLookup?.getRecipientsForKeys(carbonCopyRecipientsKeys);
|
|
473
|
+
if (resolvedCc?.length) {
|
|
474
|
+
if (overrideCarbonCopyVariablesWithCarbonCopyKeyRecipients) {
|
|
475
|
+
cc = resolvedCc;
|
|
476
|
+
} else {
|
|
477
|
+
cc = [...(cc ?? []), ...resolvedCc];
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
return cc?.length ? cc : undefined;
|
|
481
|
+
}
|
|
482
|
+
const baseRequestCc = determineCarbonCopyRecipients({
|
|
483
|
+
carbonCopyRecipients: inputBaseRequest.cc,
|
|
484
|
+
carbonCopyRecipientsKeys: inputBaseRequest.ccKeys
|
|
485
|
+
});
|
|
486
|
+
const baseRequestBcc = determineCarbonCopyRecipients({
|
|
487
|
+
carbonCopyRecipients: inputBaseRequest.bcc,
|
|
488
|
+
carbonCopyRecipientsKeys: inputBaseRequest.bccKeys
|
|
489
|
+
});
|
|
490
|
+
const baseRequestFrom = inputBaseRequest.from ?? mailgunRecipientBatchSendTargetEntityKeyRecipientLookup?.getRecipientOrDefaultForKey(inputBaseRequest.fromKey);
|
|
491
|
+
const baseRequestReplyTo = inputBaseRequest.replyTo ?? mailgunRecipientBatchSendTargetEntityKeyRecipientLookup?.getRecipientOrDefaultForKey(inputBaseRequest.replyToKey);
|
|
492
|
+
const baseRequest = {
|
|
493
|
+
...inputBaseRequest,
|
|
494
|
+
from: baseRequestFrom,
|
|
495
|
+
replyTo: baseRequestReplyTo,
|
|
496
|
+
cc: baseRequestCc,
|
|
497
|
+
bcc: baseRequestBcc
|
|
498
|
+
};
|
|
499
|
+
delete baseRequest.fromKey;
|
|
500
|
+
delete baseRequest.replyToKey;
|
|
501
|
+
delete baseRequest.ccKeys;
|
|
502
|
+
delete baseRequest.bccKeys;
|
|
503
|
+
const configAllowBatchSend = baseRequest.batchSend !== false;
|
|
504
|
+
return inputRecipients => {
|
|
505
|
+
// Process recipients to resolve keys
|
|
506
|
+
const recipients = inputRecipients.map(recipient => {
|
|
507
|
+
let from = recipient.from;
|
|
508
|
+
let replyTo = recipient.replyTo;
|
|
509
|
+
if (mailgunRecipientBatchSendTargetEntityKeyRecipientLookup) {
|
|
510
|
+
// try the fromKey, otherwise use the baseRequest.from
|
|
511
|
+
if (!from) {
|
|
512
|
+
from = mailgunRecipientBatchSendTargetEntityKeyRecipientLookup.getRecipientOrDefaultForKey(recipient.fromKey, baseRequest.from);
|
|
513
|
+
}
|
|
514
|
+
// try the replyToKey, otherwise use the baseRequest.replyTo
|
|
515
|
+
if (!replyTo) {
|
|
516
|
+
replyTo = mailgunRecipientBatchSendTargetEntityKeyRecipientLookup.getRecipientOrDefaultForKey(recipient.replyToKey, baseRequest.replyTo);
|
|
517
|
+
}
|
|
518
|
+
} else {
|
|
519
|
+
// use defaults from base request
|
|
520
|
+
if (!from) {
|
|
521
|
+
from = baseRequest.from;
|
|
522
|
+
}
|
|
523
|
+
if (!replyTo) {
|
|
524
|
+
replyTo = baseRequest.replyTo;
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
const cc = determineCarbonCopyRecipients({
|
|
528
|
+
baseRequestCarbonCopyRecipients: baseRequestCc,
|
|
529
|
+
carbonCopyRecipients: recipient.cc,
|
|
530
|
+
carbonCopyRecipientsKeys: recipient.ccKeys
|
|
531
|
+
});
|
|
532
|
+
const bcc = determineCarbonCopyRecipients({
|
|
533
|
+
baseRequestCarbonCopyRecipients: baseRequestBcc,
|
|
534
|
+
carbonCopyRecipients: recipient.bcc,
|
|
535
|
+
carbonCopyRecipientsKeys: recipient.bccKeys
|
|
536
|
+
});
|
|
537
|
+
const result = {
|
|
538
|
+
...recipient,
|
|
539
|
+
from,
|
|
540
|
+
replyTo,
|
|
541
|
+
cc,
|
|
542
|
+
bcc
|
|
543
|
+
};
|
|
544
|
+
return result;
|
|
545
|
+
});
|
|
546
|
+
const allowBatchSend = configAllowBatchSend && (allowSingleRecipientBatchSendRequests || recipients.length > 1);
|
|
547
|
+
const nonBatchSendRequests = [];
|
|
548
|
+
const batchSendRequestRecipients = [];
|
|
549
|
+
recipients.forEach(recipient => {
|
|
550
|
+
const recipientHasCarbonCopy = Boolean(recipient.cc?.length || recipient.bcc?.length);
|
|
551
|
+
if (allowBatchSend && !recipientHasCarbonCopy) {
|
|
552
|
+
// add to batch send recipients
|
|
553
|
+
batchSendRequestRecipients.push(recipient);
|
|
554
|
+
} else {
|
|
555
|
+
// add to non-batch send requests
|
|
556
|
+
// use the subject from the recipient's user variables if available as a default
|
|
557
|
+
const cc = recipient.cc;
|
|
558
|
+
const bcc = recipient.bcc;
|
|
559
|
+
const subject = (useSubjectFromRecipientUserVariables ? recipient.userVariables?.['subject'] : undefined) ?? defaultSubject ?? recipient.userVariables?.['subject'];
|
|
560
|
+
const request = {
|
|
561
|
+
...baseRequest,
|
|
562
|
+
from: recipient.from ?? baseRequest.from,
|
|
563
|
+
replyTo: recipient.replyTo ?? baseRequest.replyTo,
|
|
564
|
+
recipientVariablesConfig: baseRequest.recipientVariablesConfig ?? recipientVariablesConfig,
|
|
565
|
+
to: recipient,
|
|
566
|
+
cc,
|
|
567
|
+
bcc,
|
|
568
|
+
subject,
|
|
569
|
+
batchSend: false // explicitly disable batch send for non-batch requests
|
|
570
|
+
};
|
|
571
|
+
nonBatchSendRequests.push(request);
|
|
572
|
+
}
|
|
573
|
+
});
|
|
574
|
+
// create batch send request(s)
|
|
575
|
+
const batchSendRequests = [];
|
|
576
|
+
if (batchSendRequestRecipients.length > 0) {
|
|
577
|
+
const subject = useSubjectFromRecipientUserVariables ? MAILGUN_BATCH_SEND_RECIPIENT_SUBJECT_TEMPLATE : defaultSubject;
|
|
578
|
+
// Group recipients by their from/replyTo values
|
|
579
|
+
const batchSendRecipientGroups = makeValuesGroupMap(batchSendRequestRecipients, mailgunRecipientBatchSendTargetFromReplyToBatchGroupKey);
|
|
580
|
+
batchSendRecipientGroups.forEach(groupRecipients => {
|
|
581
|
+
// All recipients in this group should share the same from/replyTo values
|
|
582
|
+
const firstRecipient = groupRecipients[0];
|
|
583
|
+
const batchRequest = {
|
|
584
|
+
...baseRequest,
|
|
585
|
+
from: firstRecipient.from,
|
|
586
|
+
replyTo: firstRecipient.replyTo,
|
|
587
|
+
recipientVariablesConfig: baseRequest.recipientVariablesConfig ?? recipientVariablesConfig,
|
|
588
|
+
to: groupRecipients,
|
|
589
|
+
subject,
|
|
590
|
+
batchSend: true
|
|
591
|
+
};
|
|
592
|
+
batchSendRequests.push(batchRequest);
|
|
593
|
+
});
|
|
594
|
+
}
|
|
595
|
+
return filterMaybeArrayValues([...batchSendRequests, ...nonBatchSendRequests]);
|
|
596
|
+
};
|
|
597
|
+
}
|
|
598
|
+
/**
|
|
599
|
+
* Creates a MailgunRecipientBatchSendTargetEntityKeyRecipientLookup given the input configuration.
|
|
600
|
+
*
|
|
601
|
+
* @param config The configuration for the lookup.
|
|
602
|
+
* @returns The lookup.
|
|
603
|
+
*/
|
|
604
|
+
function mailgunRecipientBatchSendTargetEntityKeyRecipientLookup(config) {
|
|
605
|
+
const {
|
|
606
|
+
recipientsMap
|
|
607
|
+
} = config;
|
|
608
|
+
function getRecipientOrDefaultForKey(input, defaultRecipient) {
|
|
609
|
+
let result = defaultRecipient;
|
|
610
|
+
if (input) {
|
|
611
|
+
result = recipientsMap.get(input) ?? defaultRecipient;
|
|
612
|
+
}
|
|
613
|
+
return result;
|
|
614
|
+
}
|
|
615
|
+
function getRecipientsForKeys(input) {
|
|
616
|
+
let result = undefined;
|
|
617
|
+
if (input) {
|
|
618
|
+
const keysArray = asArray(input);
|
|
619
|
+
const recipients = filterMaybeArrayValues(keysArray.map(key => recipientsMap.get(key)));
|
|
620
|
+
if (recipients.length > 0) {
|
|
621
|
+
result = recipients;
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
return result;
|
|
625
|
+
}
|
|
626
|
+
return {
|
|
627
|
+
recipientsMap,
|
|
628
|
+
getRecipientOrDefaultForKey,
|
|
629
|
+
getRecipientsForKeys
|
|
630
|
+
};
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
export { DEFAULT_RECIPIENT_VARIABLE_PREFIX, MAILGUN_BATCH_SEND_RECIPIENT_SUBJECT_TEMPLATE, MAILGUN_REPLY_TO_EMAIL_HEADER_DATA_VARIABLE_KEY, MAX_BATCH_SEND_RECIPIENTS, MailgunApi, MailgunService, MailgunServiceModule, convertMailgunRecipientToString, convertMailgunRecipientsToStrings, convertMailgunTemplateEmailRequestToMailgunMessageData, encodeMailgunTemplateVariableValue, expandMailgunRecipientBatchSendTargetRequestFactory, mailgunRecipientBatchSendTargetEntityKeyRecipientLookup, mailgunRecipientBatchSendTargetFromReplyToBatchGroupKey, mailgunServiceConfigFactory };
|
package/mailgun/package.json
CHANGED
|
@@ -1,7 +1,28 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dereekb/nestjs/mailgun",
|
|
3
|
-
"version": "
|
|
4
|
-
"type": "commonjs",
|
|
3
|
+
"version": "13.0.0",
|
|
5
4
|
"types": "./src/index.d.ts",
|
|
6
|
-
"
|
|
5
|
+
"module": "./index.esm.js",
|
|
6
|
+
"main": "./index.cjs.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./src/index.d.ts",
|
|
10
|
+
"node": {
|
|
11
|
+
"require": "./index.cjs.js"
|
|
12
|
+
},
|
|
13
|
+
"browser": {
|
|
14
|
+
"require": "./index.cjs.js",
|
|
15
|
+
"import": "./index.esm.js"
|
|
16
|
+
},
|
|
17
|
+
"default": "./index.cjs.js"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"peerDependencies": {
|
|
21
|
+
"@dereekb/date": "13.0.0",
|
|
22
|
+
"@dereekb/nestjs": "13.0.0",
|
|
23
|
+
"@dereekb/model": "13.0.0",
|
|
24
|
+
"@dereekb/rxjs": "13.0.0",
|
|
25
|
+
"@dereekb/util": "13.0.0",
|
|
26
|
+
"core-js": "^3.0.0"
|
|
27
|
+
}
|
|
7
28
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type ArrayOrValue, type EmailAddress, type EmailAddressDomain, type NameEmailPair, type EmailParticipantString, type Maybe } from '@dereekb/util';
|
|
2
|
-
import { type
|
|
3
|
-
import { type
|
|
2
|
+
import { type CustomFileData, type MailgunMessageData, type MessagesSendResult } from 'node_modules/mailgun.js/Types/Types/Messages/Messages';
|
|
3
|
+
import { type APIResponse } from 'node_modules/mailgun.js/Types/Types/Common/ApiResponse';
|
|
4
4
|
export type MailgunSenderDomainString = EmailAddressDomain;
|
|
5
5
|
export type MailgunTemplateKey = string;
|
|
6
6
|
/**
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type Mailgun from 'mailgun.js';
|
|
2
|
-
import { type MailgunClientOptions } from 'mailgun.js/Types/MailgunClient/MailgunClientOptions';
|
|
2
|
+
import { type MailgunClientOptions } from 'node_modules/mailgun.js/Types/Types/MailgunClient/MailgunClientOptions';
|
|
3
3
|
export type MailgunOptions = MailgunClientOptions;
|
|
4
4
|
export type MailgunClient = ReturnType<Mailgun['client']>;
|
|
5
5
|
export type MailgunMessagesClient = ReturnType<Mailgun['client']>['messages'];
|