@dereekb/nestjs 12.7.0 → 13.0.1

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