@dereekb/nestjs 13.3.1 → 13.4.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.
- package/discord/index.cjs.js +31 -8
- package/discord/index.esm.js +31 -8
- package/discord/package.json +3 -3
- package/discord/src/lib/discord.api.d.ts +14 -0
- package/discord/src/lib/discord.api.page.d.ts +23 -5
- package/discord/src/lib/discord.module.d.ts +3 -0
- package/discord/src/lib/discord.util.d.ts +3 -0
- package/discord/src/lib/webhook/webhook.discord.d.ts +1 -0
- package/discord/src/lib/webhook/webhook.discord.module.d.ts +3 -0
- package/discord/src/lib/webhook/webhook.discord.verify.d.ts +1 -0
- package/index.cjs.js +52 -13
- package/index.esm.js +53 -14
- package/mailgun/index.cjs.js +58 -30
- package/mailgun/index.esm.js +59 -31
- package/mailgun/package.json +6 -6
- package/mailgun/src/lib/mailgun.api.d.ts +5 -3
- package/mailgun/src/lib/mailgun.d.ts +20 -4
- package/mailgun/src/lib/mailgun.service.d.ts +1 -0
- package/mailgun/src/lib/mailgun.service.module.d.ts +15 -0
- package/mailgun/src/lib/mailgun.util.d.ts +3 -0
- package/openai/index.cjs.js +31 -9
- package/openai/index.esm.js +31 -9
- package/openai/package.json +6 -6
- package/openai/src/lib/openai.module.d.ts +13 -0
- package/openai/src/lib/webhook/webhook.openai.module.d.ts +13 -0
- package/openai/src/lib/webhook/webhook.openai.verify.d.ts +3 -3
- package/package.json +2 -2
- package/src/lib/decorators/local.decorator.d.ts +8 -0
- package/src/lib/decorators/rawbody.d.ts +0 -13
- package/src/lib/module/client/client.module.d.ts +13 -0
- package/src/lib/module/env/env.config.d.ts +5 -4
- package/src/lib/module/env/env.d.ts +3 -1
- package/src/lib/module/env/env.nest.d.ts +8 -0
- package/src/lib/module/env/env.service.d.ts +2 -1
- package/src/lib/module/module.d.ts +11 -3
- package/src/lib/util/encryption/json.encrypt.d.ts +3 -0
- package/stripe/index.cjs.js +35 -8
- package/stripe/index.esm.js +35 -8
- package/stripe/package.json +6 -6
- package/stripe/src/lib/stripe.api.d.ts +11 -1
- package/stripe/src/lib/stripe.config.d.ts +4 -4
- package/stripe/src/lib/stripe.module.d.ts +13 -0
- package/stripe/src/lib/webhook/webhook.stripe.d.ts +10 -2
- package/typeform/index.cjs.js +56 -15
- package/typeform/index.esm.js +56 -15
- package/typeform/package.json +6 -6
- package/typeform/src/lib/typeform.module.d.ts +13 -0
- package/typeform/src/lib/typeform.type.d.ts +2 -2
- package/typeform/src/lib/typeform.util.d.ts +8 -0
- package/typeform/src/lib/webhook/webhook.typeform.form.d.ts +11 -1
- package/typeform/src/lib/webhook/webhook.typeform.module.d.ts +13 -0
- package/typeform/src/lib/webhook/webhook.typeform.verify.d.ts +2 -2
- package/vapiai/index.cjs.js +35 -13
- package/vapiai/index.esm.js +35 -13
- package/vapiai/package.json +6 -6
- package/vapiai/src/lib/vapiai.api.d.ts +2 -2
- package/vapiai/src/lib/vapiai.module.d.ts +13 -0
- package/vapiai/src/lib/webhook/webhook.vapiai.module.d.ts +13 -0
- package/vapiai/src/lib/webhook/webhook.vapiai.verify.d.ts +5 -3
package/index.esm.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { createParamDecorator, BadRequestException, InternalServerErrorException, Injectable, RequestMethod, Module,
|
|
1
|
+
import { createParamDecorator, Logger, BadRequestException, InternalServerErrorException, Injectable, RequestMethod, Module, Inject } from '@nestjs/common';
|
|
2
2
|
import rawbody from 'raw-body';
|
|
3
3
|
import { parse } from 'querystring';
|
|
4
4
|
import bodyParser from 'body-parser';
|
|
@@ -11,7 +11,14 @@ import { createDecipheriv, randomBytes, createCipheriv } from 'crypto';
|
|
|
11
11
|
*/ var IsRequestFromLocalHost = createParamDecorator(function(data, context) {
|
|
12
12
|
return isLocalhost(context);
|
|
13
13
|
});
|
|
14
|
-
|
|
14
|
+
/**
|
|
15
|
+
* Returns true if the request's origin header indicates the request came from localhost.
|
|
16
|
+
*
|
|
17
|
+
* Checks for both http://localhost and https://localhost origins.
|
|
18
|
+
*
|
|
19
|
+
* @param context - the NestJS execution context containing the HTTP request
|
|
20
|
+
* @returns true if the request origin is localhost
|
|
21
|
+
*/ function isLocalhost(context) {
|
|
15
22
|
var _req_headers_origin;
|
|
16
23
|
var req = context.switchToHttp().getRequest();
|
|
17
24
|
var origin = (_req_headers_origin = req.headers['origin']) !== null && _req_headers_origin !== void 0 ? _req_headers_origin : '';
|
|
@@ -158,7 +165,8 @@ function _ts_generator(thisArg, body) {
|
|
|
158
165
|
* @Post('webhook')
|
|
159
166
|
* handleWebhook(@ParseRawBody() body: RawBodyBuffer) { ... }
|
|
160
167
|
* ```
|
|
161
|
-
*/ var
|
|
168
|
+
*/ var rawBodyLogger = new Logger('RawBody');
|
|
169
|
+
var ParseRawBody = createParamDecorator(function(_, context) {
|
|
162
170
|
return _async_to_generator(function() {
|
|
163
171
|
var req, body;
|
|
164
172
|
return _ts_generator(this, function(_state) {
|
|
@@ -166,7 +174,7 @@ function _ts_generator(thisArg, body) {
|
|
|
166
174
|
case 0:
|
|
167
175
|
req = context.switchToHttp().getRequest();
|
|
168
176
|
if (!req.readable) {
|
|
169
|
-
|
|
177
|
+
rawBodyLogger.error('RawBody request was not readable. This is generally due to bad configuration.');
|
|
170
178
|
throw new BadRequestException('Invalid body');
|
|
171
179
|
}
|
|
172
180
|
return [
|
|
@@ -202,7 +210,7 @@ function _ts_generator(thisArg, body) {
|
|
|
202
210
|
req = context.switchToHttp().getRequest();
|
|
203
211
|
body = req.body;
|
|
204
212
|
if (!Buffer.isBuffer(body)) {
|
|
205
|
-
|
|
213
|
+
rawBodyLogger.error('RawBody expected a buffer set to req.body.');
|
|
206
214
|
throw new InternalServerErrorException('failed parsing body');
|
|
207
215
|
}
|
|
208
216
|
return [
|
|
@@ -554,9 +562,9 @@ function _type_of(obj) {
|
|
|
554
562
|
/**
|
|
555
563
|
* Merges two module metadata entries together.
|
|
556
564
|
*
|
|
557
|
-
* @param base
|
|
558
|
-
* @param additional
|
|
559
|
-
* @returns
|
|
565
|
+
* @param base - the base module metadata
|
|
566
|
+
* @param additional - additional metadata to merge in
|
|
567
|
+
* @returns the merged module metadata
|
|
560
568
|
*/ function mergeModuleMetadata(base) {
|
|
561
569
|
var additional = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
|
|
562
570
|
return {
|
|
@@ -578,7 +586,14 @@ function _type_of(obj) {
|
|
|
578
586
|
])
|
|
579
587
|
};
|
|
580
588
|
}
|
|
581
|
-
|
|
589
|
+
/**
|
|
590
|
+
* Extracts the injection tokens from an array (or single value) of NestJS Providers.
|
|
591
|
+
*
|
|
592
|
+
* For class providers, returns the class itself; for object providers, returns the provide token.
|
|
593
|
+
*
|
|
594
|
+
* @param providers - the providers to extract tokens from
|
|
595
|
+
* @returns an array of injection tokens
|
|
596
|
+
*/ function injectionTokensFromProviders(providers) {
|
|
582
597
|
return asArray(providers).map(function(x) {
|
|
583
598
|
return (typeof x === "undefined" ? "undefined" : _type_of(x)) === 'object' ? x.provide : x;
|
|
584
599
|
});
|
|
@@ -718,7 +733,14 @@ function _class_call_check$2(instance, Constructor) {
|
|
|
718
733
|
throw new TypeError("Cannot call a class as a function");
|
|
719
734
|
}
|
|
720
735
|
}
|
|
721
|
-
|
|
736
|
+
/**
|
|
737
|
+
* Factory that creates a ClientAppServiceConfig from environment variables.
|
|
738
|
+
*
|
|
739
|
+
* Reads the client web app URL from the CLIENT_WEB_APP_URL environment variable.
|
|
740
|
+
*
|
|
741
|
+
* @param configService - NestJS config service for reading environment variables
|
|
742
|
+
* @returns a validated ClientAppServiceConfig
|
|
743
|
+
*/ function clientAppConfigFactory(configService) {
|
|
722
744
|
var config = {
|
|
723
745
|
client: {
|
|
724
746
|
clientWebAppUrl: configService.get(CLIENT_WEB_APP_URL_ENV_VAR)
|
|
@@ -727,7 +749,11 @@ function clientAppConfigFactory(configService) {
|
|
|
727
749
|
ClientAppServiceConfig.assertValidConfig(config);
|
|
728
750
|
return config;
|
|
729
751
|
}
|
|
730
|
-
|
|
752
|
+
/**
|
|
753
|
+
* NestJS module that provides the ClientAppService for accessing client application configuration.
|
|
754
|
+
*
|
|
755
|
+
* Reads the client web app URL from environment variables and makes it available via ClientAppService.
|
|
756
|
+
*/ var ClientAppModule = function ClientAppModule() {
|
|
731
757
|
_class_call_check$2(this, ClientAppModule);
|
|
732
758
|
};
|
|
733
759
|
ClientAppModule = __decorate([
|
|
@@ -742,7 +768,8 @@ ClientAppModule = __decorate([
|
|
|
742
768
|
ConfigService
|
|
743
769
|
],
|
|
744
770
|
useFactory: clientAppConfigFactory
|
|
745
|
-
}
|
|
771
|
+
},
|
|
772
|
+
ClientAppService
|
|
746
773
|
],
|
|
747
774
|
exports: [
|
|
748
775
|
ClientAppService
|
|
@@ -770,7 +797,14 @@ function _class_call_check$1(instance, Constructor) {
|
|
|
770
797
|
/**
|
|
771
798
|
* Token to access a configured ServerEnvironmentServiceConfig for the app.
|
|
772
799
|
*/ var SERVER_ENV_TOKEN = 'SERVER_ENV_TOKEN';
|
|
773
|
-
|
|
800
|
+
/**
|
|
801
|
+
* Creates a NestJS Provider that supplies a ServerEnvironmentConfig under the SERVER_ENV_TOKEN injection token.
|
|
802
|
+
*
|
|
803
|
+
* Use this to register a server environment configuration instance with the NestJS dependency injection container.
|
|
804
|
+
*
|
|
805
|
+
* @param env - the server environment config to provide
|
|
806
|
+
* @returns a NestJS Provider that supplies the config under SERVER_ENV_TOKEN
|
|
807
|
+
*/ function serverEnvTokenProvider(env) {
|
|
774
808
|
return {
|
|
775
809
|
provide: SERVER_ENV_TOKEN,
|
|
776
810
|
useValue: env
|
|
@@ -778,7 +812,9 @@ function serverEnvTokenProvider(env) {
|
|
|
778
812
|
}
|
|
779
813
|
|
|
780
814
|
/**
|
|
781
|
-
* Checks process.env.NODE_ENV for "test"
|
|
815
|
+
* Checks process.env.NODE_ENV for "test".
|
|
816
|
+
*
|
|
817
|
+
* @returns true if the current Node environment is "test", false otherwise
|
|
782
818
|
*/ function isTestNodeEnv() {
|
|
783
819
|
return process.env['NODE_ENV'] === 'test';
|
|
784
820
|
}
|
|
@@ -869,6 +905,9 @@ var ENCRYPTED_FIELD_KEY_LENGTH = 32;
|
|
|
869
905
|
/**
|
|
870
906
|
* Validates that the given secret is a 64-character hexadecimal string (32 bytes for AES-256).
|
|
871
907
|
*
|
|
908
|
+
* @param secret - the hex-encoded secret key string to validate
|
|
909
|
+
* @returns true if the secret is exactly 64 valid hex characters, false otherwise
|
|
910
|
+
*
|
|
872
911
|
* @example
|
|
873
912
|
* ```typescript
|
|
874
913
|
* isValidAES256GCMEncryptionSecret('a'.repeat(64)); // true
|
package/mailgun/index.cjs.js
CHANGED
|
@@ -121,7 +121,8 @@ var MAILGUN_REPLY_TO_EMAIL_HEADER_DATA_VARIABLE_KEY = "h:Reply-To";
|
|
|
121
121
|
*
|
|
122
122
|
* @param config
|
|
123
123
|
* @returns
|
|
124
|
-
*/
|
|
124
|
+
*/ // eslint-disable-next-line sonarjs/cognitive-complexity
|
|
125
|
+
function convertMailgunTemplateEmailRequestToMailgunMessageData(config) {
|
|
125
126
|
var _ref, _ref1, _ref2, _ref3, _request_finalizeRecipientVariables, _request_batchSend;
|
|
126
127
|
var request = config.request, defaultSender = config.defaultSender, testEnvironment = config.isTestingEnvironment, tmp = config.recipientVariablePrefix, inputRecipientVariablePrefix = tmp === void 0 ? DEFAULT_RECIPIENT_VARIABLE_PREFIX : tmp, tmp1 = config.addRecipientVariablePrefixToAllMergedGlobalVariables, inputAddRecipientVariablePrefixToAllMergedGlobalVariables = tmp1 === void 0 ? true : tmp1, tmp2 = config.addCopyOfMergedRecipientVariablesWithoutPrefixToGlobalVariables, inputAddCopyOfMergedRecipientVariablesWithoutPrefixToGlobalVariables = tmp2 === void 0 ? false : tmp2, tmp3 = config.addCopyOfTemplateVariablesWithRecipientVariablePrefix, inputAddCopyOfTemplateVariablesWithRecipientVariablePrefix = tmp3 === void 0 ? false : tmp3;
|
|
127
128
|
var recipientVariablesConfig = request.recipientVariablesConfig;
|
|
@@ -179,9 +180,9 @@ var MAILGUN_REPLY_TO_EMAIL_HEADER_DATA_VARIABLE_KEY = "h:Reply-To";
|
|
|
179
180
|
}
|
|
180
181
|
});
|
|
181
182
|
}
|
|
182
|
-
var hasUserVariables = Boolean(data['recipient-variables']) || toInput.
|
|
183
|
+
var hasUserVariables = Boolean(data['recipient-variables']) || toInput.some(function(x) {
|
|
183
184
|
return x.userVariables != null;
|
|
184
|
-
})
|
|
185
|
+
});
|
|
185
186
|
if (hasUserVariables) {
|
|
186
187
|
var _finalizeRecipientVariables;
|
|
187
188
|
var recipientVariables = {};
|
|
@@ -199,7 +200,7 @@ var MAILGUN_REPLY_TO_EMAIL_HEADER_DATA_VARIABLE_KEY = "h:Reply-To";
|
|
|
199
200
|
forEach: function forEach(x) {
|
|
200
201
|
var _x = _sliced_to_array(x, 2), recipientEmail = _x[0], userVariables = _x[1];
|
|
201
202
|
var email = recipientEmail.toLowerCase();
|
|
202
|
-
if (
|
|
203
|
+
if (email in recipientVariables) {
|
|
203
204
|
util.overrideInObject(recipientVariables[email], {
|
|
204
205
|
from: [
|
|
205
206
|
userVariables
|
|
@@ -272,12 +273,26 @@ var MAILGUN_REPLY_TO_EMAIL_HEADER_DATA_VARIABLE_KEY = "h:Reply-To";
|
|
|
272
273
|
}
|
|
273
274
|
return data;
|
|
274
275
|
}
|
|
275
|
-
|
|
276
|
+
/**
|
|
277
|
+
* Converts an array of MailgunRecipient objects to formatted email address strings.
|
|
278
|
+
*
|
|
279
|
+
* Each recipient is formatted as "Name <email>" when a name is present, or just the email address.
|
|
280
|
+
*
|
|
281
|
+
* @param recipients - the recipients to convert
|
|
282
|
+
* @returns an array of formatted email address strings
|
|
283
|
+
*/ function convertMailgunRecipientsToStrings(recipients) {
|
|
276
284
|
return recipients.map(function(x) {
|
|
277
285
|
return convertMailgunRecipientToString(x);
|
|
278
286
|
});
|
|
279
287
|
}
|
|
280
|
-
|
|
288
|
+
/**
|
|
289
|
+
* Converts a MailgunRecipient to a formatted email address string.
|
|
290
|
+
*
|
|
291
|
+
* Returns "Name <email>" when a name is present, or just the email address otherwise.
|
|
292
|
+
*
|
|
293
|
+
* @param recipient - the recipient to convert
|
|
294
|
+
* @returns a formatted email participant string
|
|
295
|
+
*/ function convertMailgunRecipientToString(recipient) {
|
|
281
296
|
var address = recipient.email;
|
|
282
297
|
if (recipient.name) {
|
|
283
298
|
address = "".concat(recipient.name, " <").concat(address, ">");
|
|
@@ -301,6 +316,10 @@ function convertMailgunRecipientToString(recipient) {
|
|
|
301
316
|
}
|
|
302
317
|
}
|
|
303
318
|
break;
|
|
319
|
+
case 'symbol':
|
|
320
|
+
case 'function':
|
|
321
|
+
case 'undefined':
|
|
322
|
+
break;
|
|
304
323
|
case 'bigint':
|
|
305
324
|
case 'boolean':
|
|
306
325
|
case 'number':
|
|
@@ -309,7 +328,7 @@ function convertMailgunRecipientToString(recipient) {
|
|
|
309
328
|
break;
|
|
310
329
|
default:
|
|
311
330
|
if (value) {
|
|
312
|
-
throw new Error('Invalid value "'.concat(value, '" passed to encodeMailgunTemplateVariableValue().'));
|
|
331
|
+
throw new Error('Invalid value "'.concat(String(value), '" passed to encodeMailgunTemplateVariableValue().'));
|
|
313
332
|
}
|
|
314
333
|
}
|
|
315
334
|
return encodedValue;
|
|
@@ -398,8 +417,6 @@ var MailgunServiceConfig = /*#__PURE__*/ function() {
|
|
|
398
417
|
throw new Error('No client url specified.');
|
|
399
418
|
} else if (!config.sender) {
|
|
400
419
|
throw new Error('No mailgun sender specified.');
|
|
401
|
-
} else if (!config.messages) {
|
|
402
|
-
throw new Error('No mailgun messages config specified.');
|
|
403
420
|
}
|
|
404
421
|
}
|
|
405
422
|
}
|
|
@@ -657,6 +674,7 @@ function _ts_generator(thisArg, body) {
|
|
|
657
674
|
exports.MailgunService = /*#__PURE__*/ function() {
|
|
658
675
|
function MailgunService(mailgunApi, serverEnvironmentService) {
|
|
659
676
|
_class_call_check$1(this, MailgunService);
|
|
677
|
+
_define_property$1(this, "logger", new common.Logger('MailgunService'));
|
|
660
678
|
_define_property$1(this, "_mailgunApi", void 0);
|
|
661
679
|
_define_property$1(this, "_serverEnvironmentService", void 0);
|
|
662
680
|
this._mailgunApi = mailgunApi;
|
|
@@ -679,7 +697,7 @@ exports.MailgunService = /*#__PURE__*/ function() {
|
|
|
679
697
|
key: "sendTemplateEmail",
|
|
680
698
|
value: function sendTemplateEmail(request) {
|
|
681
699
|
return _async_to_generator(function() {
|
|
682
|
-
var domain, sender, isTestingEnvironment, recipientVariablePrefix, data, result, shouldSend, e;
|
|
700
|
+
var _request_sendTestEmails, domain, sender, isTestingEnvironment, recipientVariablePrefix, data, result, shouldSend, e;
|
|
683
701
|
return _ts_generator(this, function(_state) {
|
|
684
702
|
switch(_state.label){
|
|
685
703
|
case 0:
|
|
@@ -693,7 +711,7 @@ exports.MailgunService = /*#__PURE__*/ function() {
|
|
|
693
711
|
recipientVariablePrefix: recipientVariablePrefix,
|
|
694
712
|
isTestingEnvironment: isTestingEnvironment
|
|
695
713
|
});
|
|
696
|
-
shouldSend = !isTestingEnvironment || request.sendTestEmails
|
|
714
|
+
shouldSend = !isTestingEnvironment || ((_request_sendTestEmails = request.sendTestEmails) !== null && _request_sendTestEmails !== void 0 ? _request_sendTestEmails : this.mailgunApi.config.messages.sendTestEmails);
|
|
697
715
|
if (!shouldSend) return [
|
|
698
716
|
3,
|
|
699
717
|
5
|
|
@@ -718,7 +736,7 @@ exports.MailgunService = /*#__PURE__*/ function() {
|
|
|
718
736
|
];
|
|
719
737
|
case 3:
|
|
720
738
|
e = _state.sent();
|
|
721
|
-
|
|
739
|
+
this.logger.error('Failed sending email: ', e);
|
|
722
740
|
throw e;
|
|
723
741
|
case 4:
|
|
724
742
|
return [
|
|
@@ -755,7 +773,16 @@ function _class_call_check(instance, Constructor) {
|
|
|
755
773
|
throw new TypeError("Cannot call a class as a function");
|
|
756
774
|
}
|
|
757
775
|
}
|
|
758
|
-
|
|
776
|
+
var mailgunLogger = new common.Logger('MailgunServiceModule');
|
|
777
|
+
/**
|
|
778
|
+
* Factory that creates a MailgunServiceConfig from environment variables.
|
|
779
|
+
*
|
|
780
|
+
* Automatically switches to sandbox credentials when USE_MAILGUN_SANDBOX is true or the environment is a test environment.
|
|
781
|
+
*
|
|
782
|
+
* @param configService - NestJS config service for reading environment variables
|
|
783
|
+
* @param serverEnvironmentService - service indicating the current server environment
|
|
784
|
+
* @returns a validated MailgunServiceConfig
|
|
785
|
+
*/ function mailgunServiceConfigFactory(configService, serverEnvironmentService) {
|
|
759
786
|
var _configService_get, _configService_get1, _configService_get2;
|
|
760
787
|
var isTestingEnv = serverEnvironmentService.isTestingEnv;
|
|
761
788
|
var useSandbox = configService.get('USE_MAILGUN_SANDBOX') === 'true' || isTestingEnv;
|
|
@@ -768,10 +795,10 @@ function mailgunServiceConfigFactory(configService, serverEnvironmentService) {
|
|
|
768
795
|
if (!key || !domain) {
|
|
769
796
|
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.');
|
|
770
797
|
} else if (!serverEnvironmentService.isTestingEnv) {
|
|
771
|
-
|
|
798
|
+
mailgunLogger.log("Using Mailgun Sandbox Domain: ".concat(domain));
|
|
772
799
|
}
|
|
773
|
-
} else
|
|
774
|
-
|
|
800
|
+
} else {
|
|
801
|
+
mailgunLogger.log("Using Mailgun Production Domain: ".concat(domain));
|
|
775
802
|
}
|
|
776
803
|
var name = configService.get('MAILGUN_SENDER_NAME');
|
|
777
804
|
var email = configService.get('MAILGUN_SENDER_EMAIL');
|
|
@@ -805,7 +832,12 @@ function mailgunServiceConfigFactory(configService, serverEnvironmentService) {
|
|
|
805
832
|
MailgunServiceConfig.assertValidConfig(config);
|
|
806
833
|
return config;
|
|
807
834
|
}
|
|
808
|
-
|
|
835
|
+
/**
|
|
836
|
+
* NestJS module that provides the MailgunApi and MailgunService for sending emails.
|
|
837
|
+
*
|
|
838
|
+
* Reads Mailgun credentials and sender configuration from environment variables,
|
|
839
|
+
* with automatic fallback to sandbox settings in test environments.
|
|
840
|
+
*/ exports.MailgunServiceModule = function MailgunServiceModule() {
|
|
809
841
|
_class_call_check(this, MailgunServiceModule);
|
|
810
842
|
};
|
|
811
843
|
exports.MailgunServiceModule = __decorate([
|
|
@@ -911,6 +943,9 @@ function _unsupported_iterable_to_array(o, minLen) {
|
|
|
911
943
|
*/ var MAILGUN_BATCH_SEND_RECIPIENT_SUBJECT_TEMPLATE = "%recipient.subject%";
|
|
912
944
|
/**
|
|
913
945
|
* Creates a composite key from the from/replyTo email addresses used to group MailgunRecipientBatchSendTarget values.
|
|
946
|
+
*
|
|
947
|
+
* @param recipient - the batch send target whose from/replyTo addresses are used as the grouping key
|
|
948
|
+
* @returns a string key in the form "f:{fromEmail}|r:{replyToEmail}" used to group recipients into batches
|
|
914
949
|
*/ function mailgunRecipientBatchSendTargetFromReplyToBatchGroupKey(recipient) {
|
|
915
950
|
var _ref, _ref1;
|
|
916
951
|
var _recipient_from, _recipient_replyTo;
|
|
@@ -978,21 +1013,13 @@ function _unsupported_iterable_to_array(o, minLen) {
|
|
|
978
1013
|
var replyTo = recipient.replyTo;
|
|
979
1014
|
if (mailgunRecipientBatchSendTargetEntityKeyRecipientLookup) {
|
|
980
1015
|
// try the fromKey, otherwise use the baseRequest.from
|
|
981
|
-
|
|
982
|
-
from = mailgunRecipientBatchSendTargetEntityKeyRecipientLookup.getRecipientOrDefaultForKey(recipient.fromKey, baseRequest.from);
|
|
983
|
-
}
|
|
1016
|
+
from !== null && from !== void 0 ? from : from = mailgunRecipientBatchSendTargetEntityKeyRecipientLookup.getRecipientOrDefaultForKey(recipient.fromKey, baseRequest.from);
|
|
984
1017
|
// try the replyToKey, otherwise use the baseRequest.replyTo
|
|
985
|
-
|
|
986
|
-
replyTo = mailgunRecipientBatchSendTargetEntityKeyRecipientLookup.getRecipientOrDefaultForKey(recipient.replyToKey, baseRequest.replyTo);
|
|
987
|
-
}
|
|
1018
|
+
replyTo !== null && replyTo !== void 0 ? replyTo : replyTo = mailgunRecipientBatchSendTargetEntityKeyRecipientLookup.getRecipientOrDefaultForKey(recipient.replyToKey, baseRequest.replyTo);
|
|
988
1019
|
} else {
|
|
989
1020
|
// use defaults from base request
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
}
|
|
993
|
-
if (!replyTo) {
|
|
994
|
-
replyTo = baseRequest.replyTo;
|
|
995
|
-
}
|
|
1021
|
+
from !== null && from !== void 0 ? from : from = baseRequest.from;
|
|
1022
|
+
replyTo !== null && replyTo !== void 0 ? replyTo : replyTo = baseRequest.replyTo;
|
|
996
1023
|
}
|
|
997
1024
|
var cc = determineCarbonCopyRecipients({
|
|
998
1025
|
baseRequestCarbonCopyRecipients: baseRequestCc,
|
|
@@ -1012,11 +1039,12 @@ function _unsupported_iterable_to_array(o, minLen) {
|
|
|
1012
1039
|
});
|
|
1013
1040
|
return result;
|
|
1014
1041
|
});
|
|
1015
|
-
var allowBatchSend = configAllowBatchSend && (allowSingleRecipientBatchSendRequests
|
|
1042
|
+
var allowBatchSend = configAllowBatchSend && (allowSingleRecipientBatchSendRequests !== null && allowSingleRecipientBatchSendRequests !== void 0 ? allowSingleRecipientBatchSendRequests : recipients.length > 1);
|
|
1016
1043
|
var nonBatchSendRequests = [];
|
|
1017
1044
|
var batchSendRequestRecipients = [];
|
|
1018
1045
|
recipients.forEach(function(recipient) {
|
|
1019
1046
|
var _recipient_cc, _recipient_bcc;
|
|
1047
|
+
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing -- intentional: need || so empty cc array (length 0) falls through to check bcc
|
|
1020
1048
|
var recipientHasCarbonCopy = Boolean(((_recipient_cc = recipient.cc) === null || _recipient_cc === void 0 ? void 0 : _recipient_cc.length) || ((_recipient_bcc = recipient.bcc) === null || _recipient_bcc === void 0 ? void 0 : _recipient_bcc.length));
|
|
1021
1049
|
if (allowBatchSend && !recipientHasCarbonCopy) {
|
|
1022
1050
|
// add to batch send recipients
|
package/mailgun/index.esm.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { isTestNodeEnv, ServerEnvironmentService } from '@dereekb/nestjs';
|
|
2
2
|
import { MAP_IDENTITY, asArray, forEachKeyValue, objectIsEmpty, addToSet, overrideInObject, KeyValueTypleValueFilter, forEachInIterable, makeValuesGroupMap, filterMaybeArrayValues } from '@dereekb/util';
|
|
3
|
-
import { Injectable, Inject, Module } from '@nestjs/common';
|
|
3
|
+
import { Injectable, Inject, Logger, Module } from '@nestjs/common';
|
|
4
4
|
import Mailgun from 'mailgun.js';
|
|
5
5
|
import FormData from 'form-data';
|
|
6
6
|
import { ConfigService, ConfigModule } from '@nestjs/config';
|
|
@@ -119,7 +119,8 @@ var MAILGUN_REPLY_TO_EMAIL_HEADER_DATA_VARIABLE_KEY = "h:Reply-To";
|
|
|
119
119
|
*
|
|
120
120
|
* @param config
|
|
121
121
|
* @returns
|
|
122
|
-
*/
|
|
122
|
+
*/ // eslint-disable-next-line sonarjs/cognitive-complexity
|
|
123
|
+
function convertMailgunTemplateEmailRequestToMailgunMessageData(config) {
|
|
123
124
|
var _ref, _ref1, _ref2, _ref3, _request_finalizeRecipientVariables, _request_batchSend;
|
|
124
125
|
var request = config.request, defaultSender = config.defaultSender, testEnvironment = config.isTestingEnvironment, tmp = config.recipientVariablePrefix, inputRecipientVariablePrefix = tmp === void 0 ? DEFAULT_RECIPIENT_VARIABLE_PREFIX : tmp, tmp1 = config.addRecipientVariablePrefixToAllMergedGlobalVariables, inputAddRecipientVariablePrefixToAllMergedGlobalVariables = tmp1 === void 0 ? true : tmp1, tmp2 = config.addCopyOfMergedRecipientVariablesWithoutPrefixToGlobalVariables, inputAddCopyOfMergedRecipientVariablesWithoutPrefixToGlobalVariables = tmp2 === void 0 ? false : tmp2, tmp3 = config.addCopyOfTemplateVariablesWithRecipientVariablePrefix, inputAddCopyOfTemplateVariablesWithRecipientVariablePrefix = tmp3 === void 0 ? false : tmp3;
|
|
125
126
|
var recipientVariablesConfig = request.recipientVariablesConfig;
|
|
@@ -177,9 +178,9 @@ var MAILGUN_REPLY_TO_EMAIL_HEADER_DATA_VARIABLE_KEY = "h:Reply-To";
|
|
|
177
178
|
}
|
|
178
179
|
});
|
|
179
180
|
}
|
|
180
|
-
var hasUserVariables = Boolean(data['recipient-variables']) || toInput.
|
|
181
|
+
var hasUserVariables = Boolean(data['recipient-variables']) || toInput.some(function(x) {
|
|
181
182
|
return x.userVariables != null;
|
|
182
|
-
})
|
|
183
|
+
});
|
|
183
184
|
if (hasUserVariables) {
|
|
184
185
|
var _finalizeRecipientVariables;
|
|
185
186
|
var recipientVariables = {};
|
|
@@ -197,7 +198,7 @@ var MAILGUN_REPLY_TO_EMAIL_HEADER_DATA_VARIABLE_KEY = "h:Reply-To";
|
|
|
197
198
|
forEach: function forEach(x) {
|
|
198
199
|
var _x = _sliced_to_array(x, 2), recipientEmail = _x[0], userVariables = _x[1];
|
|
199
200
|
var email = recipientEmail.toLowerCase();
|
|
200
|
-
if (
|
|
201
|
+
if (email in recipientVariables) {
|
|
201
202
|
overrideInObject(recipientVariables[email], {
|
|
202
203
|
from: [
|
|
203
204
|
userVariables
|
|
@@ -270,12 +271,26 @@ var MAILGUN_REPLY_TO_EMAIL_HEADER_DATA_VARIABLE_KEY = "h:Reply-To";
|
|
|
270
271
|
}
|
|
271
272
|
return data;
|
|
272
273
|
}
|
|
273
|
-
|
|
274
|
+
/**
|
|
275
|
+
* Converts an array of MailgunRecipient objects to formatted email address strings.
|
|
276
|
+
*
|
|
277
|
+
* Each recipient is formatted as "Name <email>" when a name is present, or just the email address.
|
|
278
|
+
*
|
|
279
|
+
* @param recipients - the recipients to convert
|
|
280
|
+
* @returns an array of formatted email address strings
|
|
281
|
+
*/ function convertMailgunRecipientsToStrings(recipients) {
|
|
274
282
|
return recipients.map(function(x) {
|
|
275
283
|
return convertMailgunRecipientToString(x);
|
|
276
284
|
});
|
|
277
285
|
}
|
|
278
|
-
|
|
286
|
+
/**
|
|
287
|
+
* Converts a MailgunRecipient to a formatted email address string.
|
|
288
|
+
*
|
|
289
|
+
* Returns "Name <email>" when a name is present, or just the email address otherwise.
|
|
290
|
+
*
|
|
291
|
+
* @param recipient - the recipient to convert
|
|
292
|
+
* @returns a formatted email participant string
|
|
293
|
+
*/ function convertMailgunRecipientToString(recipient) {
|
|
279
294
|
var address = recipient.email;
|
|
280
295
|
if (recipient.name) {
|
|
281
296
|
address = "".concat(recipient.name, " <").concat(address, ">");
|
|
@@ -299,6 +314,10 @@ function convertMailgunRecipientToString(recipient) {
|
|
|
299
314
|
}
|
|
300
315
|
}
|
|
301
316
|
break;
|
|
317
|
+
case 'symbol':
|
|
318
|
+
case 'function':
|
|
319
|
+
case 'undefined':
|
|
320
|
+
break;
|
|
302
321
|
case 'bigint':
|
|
303
322
|
case 'boolean':
|
|
304
323
|
case 'number':
|
|
@@ -307,7 +326,7 @@ function convertMailgunRecipientToString(recipient) {
|
|
|
307
326
|
break;
|
|
308
327
|
default:
|
|
309
328
|
if (value) {
|
|
310
|
-
throw new Error('Invalid value "'.concat(value, '" passed to encodeMailgunTemplateVariableValue().'));
|
|
329
|
+
throw new Error('Invalid value "'.concat(String(value), '" passed to encodeMailgunTemplateVariableValue().'));
|
|
311
330
|
}
|
|
312
331
|
}
|
|
313
332
|
return encodedValue;
|
|
@@ -396,8 +415,6 @@ var MailgunServiceConfig = /*#__PURE__*/ function() {
|
|
|
396
415
|
throw new Error('No client url specified.');
|
|
397
416
|
} else if (!config.sender) {
|
|
398
417
|
throw new Error('No mailgun sender specified.');
|
|
399
|
-
} else if (!config.messages) {
|
|
400
|
-
throw new Error('No mailgun messages config specified.');
|
|
401
418
|
}
|
|
402
419
|
}
|
|
403
420
|
}
|
|
@@ -655,6 +672,7 @@ function _ts_generator(thisArg, body) {
|
|
|
655
672
|
var MailgunService = /*#__PURE__*/ function() {
|
|
656
673
|
function MailgunService(mailgunApi, serverEnvironmentService) {
|
|
657
674
|
_class_call_check$1(this, MailgunService);
|
|
675
|
+
_define_property$1(this, "logger", new Logger('MailgunService'));
|
|
658
676
|
_define_property$1(this, "_mailgunApi", void 0);
|
|
659
677
|
_define_property$1(this, "_serverEnvironmentService", void 0);
|
|
660
678
|
this._mailgunApi = mailgunApi;
|
|
@@ -677,7 +695,7 @@ var MailgunService = /*#__PURE__*/ function() {
|
|
|
677
695
|
key: "sendTemplateEmail",
|
|
678
696
|
value: function sendTemplateEmail(request) {
|
|
679
697
|
return _async_to_generator(function() {
|
|
680
|
-
var domain, sender, isTestingEnvironment, recipientVariablePrefix, data, result, shouldSend, e;
|
|
698
|
+
var _request_sendTestEmails, domain, sender, isTestingEnvironment, recipientVariablePrefix, data, result, shouldSend, e;
|
|
681
699
|
return _ts_generator(this, function(_state) {
|
|
682
700
|
switch(_state.label){
|
|
683
701
|
case 0:
|
|
@@ -691,7 +709,7 @@ var MailgunService = /*#__PURE__*/ function() {
|
|
|
691
709
|
recipientVariablePrefix: recipientVariablePrefix,
|
|
692
710
|
isTestingEnvironment: isTestingEnvironment
|
|
693
711
|
});
|
|
694
|
-
shouldSend = !isTestingEnvironment || request.sendTestEmails
|
|
712
|
+
shouldSend = !isTestingEnvironment || ((_request_sendTestEmails = request.sendTestEmails) !== null && _request_sendTestEmails !== void 0 ? _request_sendTestEmails : this.mailgunApi.config.messages.sendTestEmails);
|
|
695
713
|
if (!shouldSend) return [
|
|
696
714
|
3,
|
|
697
715
|
5
|
|
@@ -716,7 +734,7 @@ var MailgunService = /*#__PURE__*/ function() {
|
|
|
716
734
|
];
|
|
717
735
|
case 3:
|
|
718
736
|
e = _state.sent();
|
|
719
|
-
|
|
737
|
+
this.logger.error('Failed sending email: ', e);
|
|
720
738
|
throw e;
|
|
721
739
|
case 4:
|
|
722
740
|
return [
|
|
@@ -753,7 +771,16 @@ function _class_call_check(instance, Constructor) {
|
|
|
753
771
|
throw new TypeError("Cannot call a class as a function");
|
|
754
772
|
}
|
|
755
773
|
}
|
|
756
|
-
|
|
774
|
+
var mailgunLogger = new Logger('MailgunServiceModule');
|
|
775
|
+
/**
|
|
776
|
+
* Factory that creates a MailgunServiceConfig from environment variables.
|
|
777
|
+
*
|
|
778
|
+
* Automatically switches to sandbox credentials when USE_MAILGUN_SANDBOX is true or the environment is a test environment.
|
|
779
|
+
*
|
|
780
|
+
* @param configService - NestJS config service for reading environment variables
|
|
781
|
+
* @param serverEnvironmentService - service indicating the current server environment
|
|
782
|
+
* @returns a validated MailgunServiceConfig
|
|
783
|
+
*/ function mailgunServiceConfigFactory(configService, serverEnvironmentService) {
|
|
757
784
|
var _configService_get, _configService_get1, _configService_get2;
|
|
758
785
|
var isTestingEnv = serverEnvironmentService.isTestingEnv;
|
|
759
786
|
var useSandbox = configService.get('USE_MAILGUN_SANDBOX') === 'true' || isTestingEnv;
|
|
@@ -766,10 +793,10 @@ function mailgunServiceConfigFactory(configService, serverEnvironmentService) {
|
|
|
766
793
|
if (!key || !domain) {
|
|
767
794
|
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.');
|
|
768
795
|
} else if (!serverEnvironmentService.isTestingEnv) {
|
|
769
|
-
|
|
796
|
+
mailgunLogger.log("Using Mailgun Sandbox Domain: ".concat(domain));
|
|
770
797
|
}
|
|
771
|
-
} else
|
|
772
|
-
|
|
798
|
+
} else {
|
|
799
|
+
mailgunLogger.log("Using Mailgun Production Domain: ".concat(domain));
|
|
773
800
|
}
|
|
774
801
|
var name = configService.get('MAILGUN_SENDER_NAME');
|
|
775
802
|
var email = configService.get('MAILGUN_SENDER_EMAIL');
|
|
@@ -803,7 +830,12 @@ function mailgunServiceConfigFactory(configService, serverEnvironmentService) {
|
|
|
803
830
|
MailgunServiceConfig.assertValidConfig(config);
|
|
804
831
|
return config;
|
|
805
832
|
}
|
|
806
|
-
|
|
833
|
+
/**
|
|
834
|
+
* NestJS module that provides the MailgunApi and MailgunService for sending emails.
|
|
835
|
+
*
|
|
836
|
+
* Reads Mailgun credentials and sender configuration from environment variables,
|
|
837
|
+
* with automatic fallback to sandbox settings in test environments.
|
|
838
|
+
*/ var MailgunServiceModule = function MailgunServiceModule() {
|
|
807
839
|
_class_call_check(this, MailgunServiceModule);
|
|
808
840
|
};
|
|
809
841
|
MailgunServiceModule = __decorate([
|
|
@@ -909,6 +941,9 @@ function _unsupported_iterable_to_array(o, minLen) {
|
|
|
909
941
|
*/ var MAILGUN_BATCH_SEND_RECIPIENT_SUBJECT_TEMPLATE = "%recipient.subject%";
|
|
910
942
|
/**
|
|
911
943
|
* Creates a composite key from the from/replyTo email addresses used to group MailgunRecipientBatchSendTarget values.
|
|
944
|
+
*
|
|
945
|
+
* @param recipient - the batch send target whose from/replyTo addresses are used as the grouping key
|
|
946
|
+
* @returns a string key in the form "f:{fromEmail}|r:{replyToEmail}" used to group recipients into batches
|
|
912
947
|
*/ function mailgunRecipientBatchSendTargetFromReplyToBatchGroupKey(recipient) {
|
|
913
948
|
var _ref, _ref1;
|
|
914
949
|
var _recipient_from, _recipient_replyTo;
|
|
@@ -976,21 +1011,13 @@ function _unsupported_iterable_to_array(o, minLen) {
|
|
|
976
1011
|
var replyTo = recipient.replyTo;
|
|
977
1012
|
if (mailgunRecipientBatchSendTargetEntityKeyRecipientLookup) {
|
|
978
1013
|
// try the fromKey, otherwise use the baseRequest.from
|
|
979
|
-
|
|
980
|
-
from = mailgunRecipientBatchSendTargetEntityKeyRecipientLookup.getRecipientOrDefaultForKey(recipient.fromKey, baseRequest.from);
|
|
981
|
-
}
|
|
1014
|
+
from !== null && from !== void 0 ? from : from = mailgunRecipientBatchSendTargetEntityKeyRecipientLookup.getRecipientOrDefaultForKey(recipient.fromKey, baseRequest.from);
|
|
982
1015
|
// try the replyToKey, otherwise use the baseRequest.replyTo
|
|
983
|
-
|
|
984
|
-
replyTo = mailgunRecipientBatchSendTargetEntityKeyRecipientLookup.getRecipientOrDefaultForKey(recipient.replyToKey, baseRequest.replyTo);
|
|
985
|
-
}
|
|
1016
|
+
replyTo !== null && replyTo !== void 0 ? replyTo : replyTo = mailgunRecipientBatchSendTargetEntityKeyRecipientLookup.getRecipientOrDefaultForKey(recipient.replyToKey, baseRequest.replyTo);
|
|
986
1017
|
} else {
|
|
987
1018
|
// use defaults from base request
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
}
|
|
991
|
-
if (!replyTo) {
|
|
992
|
-
replyTo = baseRequest.replyTo;
|
|
993
|
-
}
|
|
1019
|
+
from !== null && from !== void 0 ? from : from = baseRequest.from;
|
|
1020
|
+
replyTo !== null && replyTo !== void 0 ? replyTo : replyTo = baseRequest.replyTo;
|
|
994
1021
|
}
|
|
995
1022
|
var cc = determineCarbonCopyRecipients({
|
|
996
1023
|
baseRequestCarbonCopyRecipients: baseRequestCc,
|
|
@@ -1010,11 +1037,12 @@ function _unsupported_iterable_to_array(o, minLen) {
|
|
|
1010
1037
|
});
|
|
1011
1038
|
return result;
|
|
1012
1039
|
});
|
|
1013
|
-
var allowBatchSend = configAllowBatchSend && (allowSingleRecipientBatchSendRequests
|
|
1040
|
+
var allowBatchSend = configAllowBatchSend && (allowSingleRecipientBatchSendRequests !== null && allowSingleRecipientBatchSendRequests !== void 0 ? allowSingleRecipientBatchSendRequests : recipients.length > 1);
|
|
1014
1041
|
var nonBatchSendRequests = [];
|
|
1015
1042
|
var batchSendRequestRecipients = [];
|
|
1016
1043
|
recipients.forEach(function(recipient) {
|
|
1017
1044
|
var _recipient_cc, _recipient_bcc;
|
|
1045
|
+
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing -- intentional: need || so empty cc array (length 0) falls through to check bcc
|
|
1018
1046
|
var recipientHasCarbonCopy = Boolean(((_recipient_cc = recipient.cc) === null || _recipient_cc === void 0 ? void 0 : _recipient_cc.length) || ((_recipient_bcc = recipient.bcc) === null || _recipient_bcc === void 0 ? void 0 : _recipient_bcc.length));
|
|
1019
1047
|
if (allowBatchSend && !recipientHasCarbonCopy) {
|
|
1020
1048
|
// add to batch send recipients
|
package/mailgun/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dereekb/nestjs/mailgun",
|
|
3
|
-
"version": "13.
|
|
3
|
+
"version": "13.4.1",
|
|
4
4
|
"peerDependencies": {
|
|
5
|
-
"@dereekb/date": "13.
|
|
6
|
-
"@dereekb/model": "13.
|
|
7
|
-
"@dereekb/nestjs": "13.
|
|
8
|
-
"@dereekb/rxjs": "13.
|
|
9
|
-
"@dereekb/util": "13.
|
|
5
|
+
"@dereekb/date": "13.4.1",
|
|
6
|
+
"@dereekb/model": "13.4.1",
|
|
7
|
+
"@dereekb/nestjs": "13.4.1",
|
|
8
|
+
"@dereekb/rxjs": "13.4.1",
|
|
9
|
+
"@dereekb/util": "13.4.1",
|
|
10
10
|
"@nestjs/common": "^11.1.16",
|
|
11
11
|
"@nestjs/config": "^4.0.3",
|
|
12
12
|
"form-data": "^4.0.0",
|
|
@@ -1,11 +1,13 @@
|
|
|
1
|
+
import { type EmailParticipantString, type WebsiteUrl } from '@dereekb/util';
|
|
1
2
|
import { MailgunServiceConfig } from './mailgun.config';
|
|
3
|
+
import { type MailgunSenderDomainString } from './mailgun';
|
|
2
4
|
import { type MailgunClient, type MailgunMessagesClient } from './mailgun.type';
|
|
3
5
|
export declare class MailgunApi {
|
|
4
6
|
readonly config: MailgunServiceConfig;
|
|
5
7
|
readonly client: MailgunClient;
|
|
6
8
|
constructor(config: MailgunServiceConfig);
|
|
7
9
|
get messages(): MailgunMessagesClient;
|
|
8
|
-
get clientUrl():
|
|
9
|
-
get domain():
|
|
10
|
-
get sender():
|
|
10
|
+
get clientUrl(): WebsiteUrl;
|
|
11
|
+
get domain(): MailgunSenderDomainString;
|
|
12
|
+
get sender(): EmailParticipantString;
|
|
11
13
|
}
|