@creator.co/wapi 1.2.1-beta6 → 1.2.3

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 (129) hide show
  1. package/.eslintrc.cjs +29 -22
  2. package/.github/workflows/npmpublish.yml +11 -19
  3. package/.github/workflows/prs.yml +13 -0
  4. package/index.ts +12 -10
  5. package/jest.config.ts +39 -0
  6. package/package.json +15 -4
  7. package/src/API/Request.ts +136 -44
  8. package/src/API/Response.ts +256 -46
  9. package/src/API/Utils.ts +60 -7
  10. package/src/BaseEvent/EventProcessor.ts +93 -28
  11. package/src/BaseEvent/Process.ts +69 -13
  12. package/src/BaseEvent/Transaction.ts +29 -50
  13. package/src/Config/Configuration.ts +119 -19
  14. package/src/Config/EnvironmentVar.ts +100 -21
  15. package/src/Crypto/Crypto.ts +78 -19
  16. package/src/Crypto/JWT.ts +53 -13
  17. package/src/Globals.ts +159 -27
  18. package/src/Logger/Logger.ts +204 -65
  19. package/src/Mailer/Mailer.ts +114 -31
  20. package/src/Publisher/Publisher.ts +57 -16
  21. package/src/Server/RouteResolver.ts +141 -0
  22. package/src/Server/Router.ts +84 -12
  23. package/src/Server/lib/ContainerServer.ts +53 -6
  24. package/src/Server/lib/Server.ts +82 -54
  25. package/src/Server/lib/container/GenericHandler.ts +15 -18
  26. package/src/Server/lib/container/GenericHandlerEvent.ts +78 -50
  27. package/src/Server/lib/container/HealthHandler.ts +2 -2
  28. package/src/Server/lib/container/Proxy.ts +114 -45
  29. package/src/Server/lib/container/Utils.ts +18 -27
  30. package/src/Validation/Validator.ts +23 -7
  31. package/tests/API/Request.test.ts +259 -0
  32. package/tests/API/Response.test.ts +367 -0
  33. package/tests/API/Utils.test.ts +157 -0
  34. package/tests/BaseEvent/EventProcessor.test.ts +262 -0
  35. package/tests/BaseEvent/Process.test.ts +49 -0
  36. package/tests/BaseEvent/Transaction.test.ts +222 -0
  37. package/tests/Config/Config.test.ts +193 -0
  38. package/tests/Config/EnvironmentVar.test.ts +214 -0
  39. package/tests/Crypto/Crypto.test.ts +88 -0
  40. package/tests/Crypto/JWT.test.ts +92 -0
  41. package/tests/Logger/Logger.test.ts +96 -0
  42. package/tests/Mailer/Mailer.test.ts +59 -0
  43. package/tests/Publisher/Publisher.test.ts +60 -0
  44. package/tests/Server/RouteResolver.test.ts +103 -0
  45. package/tests/Server/Router.test.ts +38 -0
  46. package/tests/Server/lib/ContainerServer.test.ts +327 -0
  47. package/tests/Server/lib/Server.test.ts +12 -0
  48. package/tests/Server/lib/container/GenericHandler.test.ts +131 -0
  49. package/tests/Server/lib/container/GenericHandlerEvent.test.ts +102 -0
  50. package/tests/Server/lib/container/HealthHandler.test.ts +30 -0
  51. package/tests/Server/lib/container/Proxy.test.ts +265 -0
  52. package/tests/Server/lib/container/Utils.test.ts +47 -0
  53. package/tests/Test.utils.ts +95 -0
  54. package/tests/Validation/Validator.test.ts +76 -0
  55. package/tests/main.test.ts +15 -0
  56. package/tsconfig.json +1 -0
  57. package/dist/index.d.ts +0 -11
  58. package/dist/index.js +0 -24
  59. package/dist/index.js.map +0 -1
  60. package/dist/package.json +0 -53
  61. package/dist/src/API/Request.d.ts +0 -21
  62. package/dist/src/API/Request.js +0 -86
  63. package/dist/src/API/Request.js.map +0 -1
  64. package/dist/src/API/Response.d.ts +0 -39
  65. package/dist/src/API/Response.js +0 -232
  66. package/dist/src/API/Response.js.map +0 -1
  67. package/dist/src/API/Utils.d.ts +0 -8
  68. package/dist/src/API/Utils.js +0 -49
  69. package/dist/src/API/Utils.js.map +0 -1
  70. package/dist/src/BaseEvent/EventProcessor.d.ts +0 -13
  71. package/dist/src/BaseEvent/EventProcessor.js +0 -151
  72. package/dist/src/BaseEvent/EventProcessor.js.map +0 -1
  73. package/dist/src/BaseEvent/Process.d.ts +0 -12
  74. package/dist/src/BaseEvent/Process.js +0 -114
  75. package/dist/src/BaseEvent/Process.js.map +0 -1
  76. package/dist/src/BaseEvent/Transaction.d.ts +0 -29
  77. package/dist/src/BaseEvent/Transaction.js +0 -248
  78. package/dist/src/BaseEvent/Transaction.js.map +0 -1
  79. package/dist/src/Config/Configuration.d.ts +0 -34
  80. package/dist/src/Config/Configuration.js +0 -93
  81. package/dist/src/Config/Configuration.js.map +0 -1
  82. package/dist/src/Config/EnvironmentVar.d.ts +0 -17
  83. package/dist/src/Config/EnvironmentVar.js +0 -152
  84. package/dist/src/Config/EnvironmentVar.js.map +0 -1
  85. package/dist/src/Crypto/Crypto.d.ts +0 -8
  86. package/dist/src/Crypto/Crypto.js +0 -84
  87. package/dist/src/Crypto/Crypto.js.map +0 -1
  88. package/dist/src/Crypto/JWT.d.ts +0 -16
  89. package/dist/src/Crypto/JWT.js +0 -49
  90. package/dist/src/Crypto/JWT.js.map +0 -1
  91. package/dist/src/Globals.d.ts +0 -21
  92. package/dist/src/Globals.js +0 -35
  93. package/dist/src/Globals.js.map +0 -1
  94. package/dist/src/Logger/Logger.d.ts +0 -34
  95. package/dist/src/Logger/Logger.js +0 -345
  96. package/dist/src/Logger/Logger.js.map +0 -1
  97. package/dist/src/Mailer/Mailer.d.ts +0 -12
  98. package/dist/src/Mailer/Mailer.js +0 -234
  99. package/dist/src/Mailer/Mailer.js.map +0 -1
  100. package/dist/src/Publisher/Publisher.d.ts +0 -10
  101. package/dist/src/Publisher/Publisher.js +0 -109
  102. package/dist/src/Publisher/Publisher.js.map +0 -1
  103. package/dist/src/Server/Router.d.ts +0 -27
  104. package/dist/src/Server/Router.js +0 -22
  105. package/dist/src/Server/Router.js.map +0 -1
  106. package/dist/src/Server/lib/ContainerServer.d.ts +0 -11
  107. package/dist/src/Server/lib/ContainerServer.js +0 -103
  108. package/dist/src/Server/lib/ContainerServer.js.map +0 -1
  109. package/dist/src/Server/lib/Server.d.ts +0 -9
  110. package/dist/src/Server/lib/Server.js +0 -141
  111. package/dist/src/Server/lib/Server.js.map +0 -1
  112. package/dist/src/Server/lib/container/GenericHandler.d.ts +0 -4
  113. package/dist/src/Server/lib/container/GenericHandler.js +0 -136
  114. package/dist/src/Server/lib/container/GenericHandler.js.map +0 -1
  115. package/dist/src/Server/lib/container/GenericHandlerEvent.d.ts +0 -14
  116. package/dist/src/Server/lib/container/GenericHandlerEvent.js +0 -164
  117. package/dist/src/Server/lib/container/GenericHandlerEvent.js.map +0 -1
  118. package/dist/src/Server/lib/container/HealthHandler.d.ts +0 -3
  119. package/dist/src/Server/lib/container/HealthHandler.js +0 -44
  120. package/dist/src/Server/lib/container/HealthHandler.js.map +0 -1
  121. package/dist/src/Server/lib/container/Proxy.d.ts +0 -15
  122. package/dist/src/Server/lib/container/Proxy.js +0 -157
  123. package/dist/src/Server/lib/container/Proxy.js.map +0 -1
  124. package/dist/src/Server/lib/container/Utils.d.ts +0 -6
  125. package/dist/src/Server/lib/container/Utils.js +0 -109
  126. package/dist/src/Server/lib/container/Utils.js.map +0 -1
  127. package/dist/src/Validation/Validator.d.ts +0 -5
  128. package/dist/src/Validation/Validator.js +0 -31
  129. package/dist/src/Validation/Validator.js.map +0 -1
@@ -1,46 +1,117 @@
1
- import * as abind from "abind"
2
- import * as stringify from "json-stringify-safe"
3
- import * as stackTrace from "stack-trace"
1
+ import * as abind from 'abind'
2
+ import * as stringify from 'json-stringify-safe'
3
+ import * as stackTrace from 'stack-trace'
4
4
 
5
5
  //
6
- import Utils from "../API/Utils"
6
+ import Utils from '../API/Utils'
7
7
  //
8
+ /**
9
+ * ${1:Description placeholder}
10
+ *
11
+ * @export
12
+ * @enum {number}
13
+ */
8
14
  export enum LOG_LEVELS {
9
- DEBUG = "DEBUG",
10
- INFO = "INFO",
11
- WARN = "WARN",
12
- ERROR = "ERROR",
15
+ DEBUG = 'DEBUG',
16
+ INFO = 'INFO',
17
+ WARN = 'WARN',
18
+ ERROR = 'ERROR',
13
19
  }
14
- const PURE_CONSOLE = console["flushLogs"] ? console["origin"] : console
20
+ /**
21
+ * ${1:Description placeholder}
22
+ *
23
+ * @type {*}
24
+ */
25
+ const PURE_CONSOLE = console['notGlobalLogger'] ? console['origin'] : console
26
+ /**
27
+ * ${1:Description placeholder}
28
+ *
29
+ * @type {*}
30
+ */
15
31
  const DEFAULT_LOG_FUNCTION = PURE_CONSOLE.log.bind(PURE_CONSOLE)
16
32
  //
33
+ /**
34
+ * ${1:Description placeholder}
35
+ *
36
+ * @type {{}\}
37
+ */
17
38
  const blacklist = [
18
- "password",
19
- "phonenumber" /*,'code'*/,
20
- "resetCode",
21
- "recaptchaToken",
22
- "token",
23
- "mfa",
24
- "REFRESH_TOKEN",
25
- "SECRET_HASH",
26
- "SecretHash",
27
- "AccessToken",
28
- "UserCode",
29
- "paymentMethodNonce",
39
+ 'password',
40
+ 'phonenumber' /*,'code'*/,
41
+ 'resetCode',
42
+ 'recaptchaToken',
43
+ 'token',
44
+ 'mfa',
45
+ 'REFRESH_TOKEN',
46
+ 'SECRET_HASH',
47
+ 'SecretHash',
48
+ 'AccessToken',
49
+ 'UserCode',
50
+ 'paymentMethodNonce',
30
51
  ]
31
52
  //
53
+ /**
54
+ * ${1:Description placeholder}
55
+ *
56
+ * @export
57
+ * @typedef {LoggerConfig}
58
+ */
32
59
  export type LoggerConfig = {
33
60
  sensitiveFilteringKeywords?: boolean | Array<string>
34
61
  logLevel?: LOG_LEVELS | string
35
62
  }
36
63
  //
64
+ /**
65
+ * ${1:Description placeholder}
66
+ *
67
+ * @export
68
+ * @class Logger
69
+ * @typedef {Logger}
70
+ */
37
71
  export default class Logger {
72
+ /**
73
+ * ${1:Description placeholder}
74
+ *
75
+ * @private
76
+ * @type {?LoggerConfig}
77
+ */
38
78
  private _config?: LoggerConfig
79
+ /**
80
+ * ${1:Description placeholder}
81
+ *
82
+ * @private
83
+ * @type {string}
84
+ */
39
85
  private _transactionID: string
86
+ /**
87
+ * ${1:Description placeholder}
88
+ *
89
+ * @private
90
+ * @type {(Array<string> | false)}
91
+ */
40
92
  private _filterBlacklist: Array<string> | false
93
+ /**
94
+ * ${1:Description placeholder}
95
+ *
96
+ * @private
97
+ * @type {LOG_LEVELS}
98
+ */
41
99
  private _LOG_LEVEL: LOG_LEVELS
100
+ /**
101
+ * ${1:Description placeholder}
102
+ *
103
+ * @private
104
+ * @type {*}
105
+ */
42
106
  private origin: any
43
107
 
108
+ /**
109
+ * Creates an instance of Logger.
110
+ *
111
+ * @constructor
112
+ * @param {(LoggerConfig | undefined)} _config
113
+ * @param {string} transactionID
114
+ */
44
115
  constructor(_config: LoggerConfig | undefined, transactionID: string) {
45
116
  abind(this)
46
117
  //
@@ -58,37 +129,84 @@ export default class Logger {
58
129
  //
59
130
  this._setupBindings()
60
131
  //
61
- this.log("Using logger with level: " + this._LOG_LEVEL.toString())
62
- this.debug("logger config: ", this._config)
132
+ this.log('Using logger with level: ' + this._LOG_LEVEL.toString())
133
+ this.debug('logger config: ', this._config)
134
+ }
135
+ /**
136
+ * ${1:Description placeholder}
137
+ *
138
+ * @public
139
+ * @returns {boolean}
140
+ */
141
+ public notGlobalLogger() {
142
+ return true
63
143
  }
64
144
 
65
145
  //Public
146
+ /**
147
+ * ${1:Description placeholder}
148
+ *
149
+ * @param {...{}\} args
150
+ */
66
151
  debug(...args) {
67
152
  this._processLog(LOG_LEVELS.DEBUG, args)
68
153
  }
154
+ /**
155
+ * ${1:Description placeholder}
156
+ *
157
+ * @param {...{}\} args
158
+ */
69
159
  log(...args) {
70
160
  this._processLog(LOG_LEVELS.INFO, args)
71
161
  }
162
+ /**
163
+ * ${1:Description placeholder}
164
+ *
165
+ * @param {...{}\} args
166
+ */
72
167
  info(...args) {
73
168
  this._processLog(LOG_LEVELS.INFO, args)
74
169
  }
170
+ /**
171
+ * ${1:Description placeholder}
172
+ *
173
+ * @param {...{}\} args
174
+ */
75
175
  warning(...args) {
76
176
  this._processLog(LOG_LEVELS.WARN, args)
77
177
  }
178
+ /**
179
+ * ${1:Description placeholder}
180
+ *
181
+ * @param {...{}\} args
182
+ */
78
183
  warn(...args) {
79
184
  this._processLog(LOG_LEVELS.WARN, args)
80
185
  }
186
+ /**
187
+ * ${1:Description placeholder}
188
+ *
189
+ * @param {...{}\} args
190
+ */
81
191
  error(...args) {
82
192
  this._processLog(LOG_LEVELS.ERROR, args)
83
193
  }
194
+ /**
195
+ * ${1:Description placeholder}
196
+ *
197
+ * @param {*} exception
198
+ * @param {...{}\} args
199
+ */
84
200
  exception(exception, ...args) {
85
201
  this._exception(exception, args)
86
202
  }
87
- //
88
- cleanUp() {}
89
- async flushLogs() {}
90
203
 
91
204
  //initialization
205
+ /**
206
+ * ${1:Description placeholder}
207
+ *
208
+ * @private
209
+ */
92
210
  private _setupBindings(): void {
93
211
  global.console = {
94
212
  debug: (...args) => this._processLog(LOG_LEVELS.DEBUG, args),
@@ -98,28 +216,38 @@ export default class Logger {
98
216
  error: (...args) => this._processLog(LOG_LEVELS.ERROR, args),
99
217
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
100
218
  // @ts-ignore
101
- warning: (...args) => this._processLog(false, LOG_LEVELS.WARN, args),
219
+ warning: (...args) => this._processLog(LOG_LEVELS.WARN, args),
102
220
  exception: (exception, ...args) => this._exception(exception, args),
103
221
  }
104
222
  }
105
223
 
106
224
  /* Formatters */
107
- private _formattedLog(
108
- level: LOG_LEVELS,
109
- msg: Array<string>,
110
- caller: string,
111
- ): string {
225
+ /**
226
+ * ${1:Description placeholder}
227
+ *
228
+ * @private
229
+ * @param {LOG_LEVELS} level
230
+ * @param {Array<string>} msg
231
+ * @param {string} caller
232
+ * @returns {string}
233
+ */
234
+ private _formattedLog(level: LOG_LEVELS, msg: Array<string>, caller: string): string {
112
235
  if (Utils.isHybridlessContainer() && this._transactionID) {
113
236
  return (
114
- ` ${this._transactionID}` +
115
- ` [${level.toString()}] [${caller}] ${msg
116
- .map(this._suppressSensitiveInfo)
117
- .join(" ")}`
237
+ `${this._transactionID}` +
238
+ ` [${level.toString()}] [${caller}] ${msg.map(this._suppressSensitiveInfo).join(' ')}`
118
239
  )
119
240
  } else {
120
- return ` [${level.toString()}] [${caller}] ${msg.join(" ")}`
241
+ return `[${level.toString()}] [${caller}] ${msg.map(this._suppressSensitiveInfo).join(' ')}`
121
242
  }
122
243
  }
244
+ /**
245
+ * ${1:Description placeholder}
246
+ *
247
+ * @private
248
+ * @param {number} index
249
+ * @returns {string}
250
+ */
123
251
  private _callerName(index: number): string {
124
252
  const safeIndex = Math.min(index, stackTrace.get().length)
125
253
  if (stackTrace.get()[safeIndex]) {
@@ -127,76 +255,87 @@ export default class Logger {
127
255
  ? stackTrace.get()[safeIndex].getTypeName()
128
256
  : null
129
257
  if (!callerName) {
130
- callerName = stackTrace?.get()?.[safeIndex]?.getFileName()?.split("/")
131
- callerName = callerName?.slice(callerName?.indexOf("src"))?.join("/")
258
+ callerName = stackTrace?.get()?.[safeIndex]?.getFileName()?.split('/')
259
+ callerName = callerName?.slice(callerName?.indexOf('src'))?.join('/')
132
260
  }
133
- return callerName + ":" + stackTrace?.get()?.[safeIndex]?.getLineNumber()
261
+ return callerName + ':' + stackTrace?.get()?.[safeIndex]?.getLineNumber()
134
262
  }
135
- return ""
263
+ return ''
136
264
  }
137
265
  /* Helpers (core functionality) */
266
+ /**
267
+ * ${1:Description placeholder}
268
+ *
269
+ * @private
270
+ * @param {LOG_LEVELS} level
271
+ * @param {*} args
272
+ */
138
273
  private _processLog(level: LOG_LEVELS, args: any): void {
139
274
  if (level < this._LOG_LEVEL) return
140
275
  //get args
141
276
  const msg: Array<string> = []
142
277
  for (const arg of args) {
143
278
  const fMsg =
144
- typeof arg === "object" && !(arg instanceof Error)
145
- ? stringify(arg, null, 2)
146
- : arg
279
+ typeof arg === 'object' && !(arg instanceof Error) ? stringify(arg, null, 2) : arg
147
280
  msg.push(fMsg)
148
281
  }
149
282
  //push into logs stack
150
283
  // todo: improve error stack
151
284
  this._pushLog(level, this._formattedLog(level, msg, this._callerName(3)))
152
285
  }
286
+ /**
287
+ * ${1:Description placeholder}
288
+ *
289
+ * @private
290
+ * @param {Error} exception
291
+ * @param {...{}\} args
292
+ */
153
293
  private _exception(exception: Error, ...args): void {
154
294
  //format message
155
295
  const msg: Array<string> = []
156
296
  //push exeception
157
- msg.push(exception.toString() + " -")
297
+ msg.push(exception.toString() + ' -')
158
298
  //get args
159
299
  for (const arg of args) if (arg != exception) msg.push(arg)
160
300
  if (exception.stack) msg.push(exception.stack) //push Exeception stack at the end
161
301
  //push into logs stack
162
- this._pushLog(
163
- LOG_LEVELS.ERROR,
164
- this._formattedLog(LOG_LEVELS.ERROR, msg, this._callerName(3)),
165
- )
302
+ this._pushLog(LOG_LEVELS.ERROR, this._formattedLog(LOG_LEVELS.ERROR, msg, this._callerName(3)))
166
303
  }
304
+ /**
305
+ * ${1:Description placeholder}
306
+ *
307
+ * @private
308
+ * @param {LOG_LEVELS} level
309
+ * @param {string} fMsg
310
+ */
167
311
  private _pushLog(level: LOG_LEVELS, fMsg: string): void {
168
312
  // push into logs stack
169
313
  // this._logs.push(fMsg);
170
314
  DEFAULT_LOG_FUNCTION.apply(PURE_CONSOLE, [fMsg])
171
315
  }
172
316
  /* Sensitive information handling */
317
+ /**
318
+ * ${1:Description placeholder}
319
+ *
320
+ * @private
321
+ * @param {*} value
322
+ * @returns {string}
323
+ */
173
324
  private _suppressSensitiveInfo(value: any): string {
174
325
  //realy false
175
326
  if (!this._filterBlacklist) return value
176
- if (typeof value == "string") {
327
+ if (typeof value == 'string') {
177
328
  //content based replacement
178
329
  if (this._filterBlacklist) {
179
330
  this._filterBlacklist.forEach(() => {
180
331
  const match = this._filterBlacklist
181
- ? this._filterBlacklist.find((f) =>
182
- value.toLowerCase().includes(f.toLowerCase()),
183
- )
332
+ ? this._filterBlacklist.find(f => value.toLowerCase().includes(f.toLowerCase()))
184
333
  : false
185
- if (match) value = "**SUPRESSED_SENSITIVE_DATA**"
334
+ if (match) value = '**SUPPRESSED_SENSITIVE_DATA**'
186
335
  })
187
336
  }
188
337
  return value
189
- } else if (typeof value == "object") {
190
- //key based replacement
191
- Object.keys(value).forEach(function (elt) {
192
- const match = this._filterBlacklist.find((f) =>
193
- elt.toLowerCase().includes(f.toLowerCase()),
194
- )
195
- if (match) value[elt] = "**SUPRESSED_SENSITIVE_DATA**"
196
- })
197
- return value
198
- } else if (Array.isArray(value))
199
- return value.map((v) => this._suppressSensitiveInfo(v)).join(" ")
338
+ }
200
339
  return value
201
340
  }
202
341
  }
@@ -1,22 +1,59 @@
1
- import * as SES from "@aws-sdk/client-ses"
2
- import { defaultProvider } from "@aws-sdk/credential-provider-node"
3
- import * as Email from "email-templates"
4
- import * as nodemailer from "nodemailer"
5
- import type * as SESTransport from "nodemailer/lib/ses-transport"
1
+ import * as SES from '@aws-sdk/client-ses'
2
+ import { defaultProvider } from '@aws-sdk/credential-provider-node'
3
+ import * as Email from 'email-templates'
4
+ import * as nodemailer from 'nodemailer'
5
+ import type * as SESTransport from 'nodemailer/lib/ses-transport'
6
6
 
7
+ /**
8
+ * ${1:Description placeholder}
9
+ *
10
+ * @export
11
+ * @class Mailer
12
+ * @typedef {Mailer}
13
+ */
7
14
  export default class Mailer {
15
+ /**
16
+ * ${1:Description placeholder}
17
+ *
18
+ * @private
19
+ * @readonly
20
+ * @type {string}
21
+ */
8
22
  private readonly from: string
9
- private readonly templateDefaultFile: string = "html"
23
+ /**
24
+ * ${1:Description placeholder}
25
+ *
26
+ * @private
27
+ * @readonly
28
+ * @type {string}
29
+ */
30
+ private readonly templateDefaultFile: string = 'html'
31
+ /**
32
+ * ${1:Description placeholder}
33
+ *
34
+ * @private
35
+ * @readonly
36
+ * @type {ReturnType<
37
+ * typeof nodemailer.createTransport<SESTransport.SentMessageInfo>
38
+ * >}
39
+ */
10
40
  private readonly transporter: ReturnType<
11
41
  typeof nodemailer.createTransport<SESTransport.SentMessageInfo>
12
42
  >
43
+ /**
44
+ * Creates an instance of Mailer.
45
+ *
46
+ * @constructor
47
+ * @param {string} defaultFrom
48
+ * @param {string} region
49
+ */
13
50
  constructor(defaultFrom: string, region: string) {
14
51
  this.from = defaultFrom
15
52
  this.transporter = nodemailer.createTransport({
16
53
  SES: {
17
54
  ses: new SES.SESClient({
18
55
  credentials: defaultProvider(),
19
- apiVersion: "2010-12-01",
56
+ apiVersion: '2010-12-01',
20
57
  region,
21
58
  }),
22
59
  aws: SES,
@@ -24,6 +61,21 @@ export default class Mailer {
24
61
  })
25
62
  }
26
63
 
64
+ /**
65
+ * ${1:Description placeholder}
66
+ *
67
+ * @public
68
+ * @async
69
+ * @param {(string | Array<string>)} to
70
+ * @param {string} htmlMessage
71
+ * @param {string} subject
72
+ * @param {?(string | Array<string>)} [optionalCC]
73
+ * @param {?string} [optionalFrom]
74
+ * @param {?string} [optionalReplyTo]
75
+ * @param {?any[]} [optionalAttachments]
76
+ * @param {?Email.NodeMailerTransportOptions} [optionalTransport]
77
+ * @returns {unknown}
78
+ */
27
79
  public async sendRawEmail(
28
80
  to: string | Array<string>,
29
81
  htmlMessage: string,
@@ -33,7 +85,7 @@ export default class Mailer {
33
85
  optionalReplyTo?: string,
34
86
  // TODO: improve attachment type -> Attachment
35
87
  optionalAttachments?: any[],
36
- optionalTransport?: Email.NodeMailerTransportOptions,
88
+ optionalTransport?: Email.NodeMailerTransportOptions
37
89
  ) {
38
90
  //Generate emails
39
91
  const email = new Email({
@@ -53,13 +105,28 @@ export default class Mailer {
53
105
  let resp = null
54
106
  try {
55
107
  resp = await email.send()
56
- console.debug("Mailer resp:", resp)
108
+ console.debug('Mailer resp:', resp)
57
109
  } catch (e) {
58
- console.error("Mailer error:", e)
110
+ console.error('Mailer error:', e)
59
111
  throw e
60
112
  }
61
113
  return resp
62
114
  }
115
+ /**
116
+ * ${1:Description placeholder}
117
+ *
118
+ * @public
119
+ * @async
120
+ * @param {(string | Array<string>)} to
121
+ * @param {(string | Array<string>)} templates
122
+ * @param {object} data
123
+ * @param {?(string | Array<string>)} [optionalCC]
124
+ * @param {?string} [optionalFrom]
125
+ * @param {?string} [optionalReplyTo]
126
+ * @param {?any[]} [optionalAttachments]
127
+ * @param {?Email.NodeMailerTransportOptions} [optionalTransport]
128
+ * @returns {unknown}
129
+ */
63
130
  public async sendTemplatedEmail(
64
131
  to: string | Array<string>,
65
132
  templates: string | Array<string>,
@@ -69,7 +136,7 @@ export default class Mailer {
69
136
  optionalReplyTo?: string,
70
137
  // TODO: improve attachment type -> Attachment
71
138
  optionalAttachments?: any[],
72
- optionalTransport?: Email.NodeMailerTransportOptions,
139
+ optionalTransport?: Email.NodeMailerTransportOptions
73
140
  ) {
74
141
  //Generate emails
75
142
  const email = new Email({
@@ -88,18 +155,28 @@ export default class Mailer {
88
155
  try {
89
156
  const chosenTemplate = await this.chooseTemplate(templates, data)
90
157
  resp = await email.send({ template: chosenTemplate, locals: data })
91
- console.debug("Mailer resp:", resp)
158
+ console.debug('Mailer resp:', resp)
92
159
  } catch (e) {
93
- console.error("Mailer error:", e)
160
+ console.error('Mailer error:', e)
94
161
  throw e
95
162
  }
96
163
  return resp
97
164
  }
165
+ /**
166
+ * ${1:Description placeholder}
167
+ *
168
+ * @public
169
+ * @param {string} host
170
+ * @param {number} portNumber
171
+ * @param {string} user
172
+ * @param {string} password
173
+ * @returns {Email.NodeMailerTransportOptions}
174
+ */
98
175
  public newSMTPTransporter(
99
176
  host: string,
100
177
  portNumber: number,
101
178
  user: string,
102
- password: string,
179
+ password: string
103
180
  ): Email.NodeMailerTransportOptions {
104
181
  const smtpTransporter = nodemailer.createTransport({
105
182
  host: host,
@@ -114,10 +191,16 @@ export default class Mailer {
114
191
  }
115
192
 
116
193
  /* private */
117
- private async chooseTemplate(
118
- templates: string | Array<string>,
119
- data: object,
120
- ): Promise<string> {
194
+ /**
195
+ * ${1:Description placeholder}
196
+ *
197
+ * @private
198
+ * @async
199
+ * @param {(string | Array<string>)} templates
200
+ * @param {object} data
201
+ * @returns {Promise<string>}
202
+ */
203
+ private async chooseTemplate(templates: string | Array<string>, data: object): Promise<string> {
121
204
  if (Array.isArray(templates)) {
122
205
  //For each template check if can render it
123
206
  for (const template of templates) {
@@ -128,25 +211,25 @@ export default class Mailer {
128
211
  }
129
212
  throw new Error(
130
213
  `Could not render email with template ${templates} and following data. Please, check logs above! ` +
131
- JSON.stringify(data, null, 2),
214
+ JSON.stringify(data, null, 2)
132
215
  )
133
216
  }
134
- private async canRenderTemplate(
135
- template: string,
136
- data: object,
137
- ): Promise<boolean> {
217
+ /**
218
+ * ${1:Description placeholder}
219
+ *
220
+ * @private
221
+ * @async
222
+ * @param {string} template
223
+ * @param {object} data
224
+ * @returns {Promise<boolean>}
225
+ */
226
+ private async canRenderTemplate(template: string, data: object): Promise<boolean> {
138
227
  let validRenderResp = null
139
228
  try {
140
229
  const email = new Email()
141
- validRenderResp = await email.render(
142
- `${template}/${this.templateDefaultFile}`,
143
- data,
144
- )
230
+ validRenderResp = await email.render(`${template}/${this.templateDefaultFile}`, data)
145
231
  } catch (e) {
146
- console.log(
147
- `Error while checking renderability of email template ${template}`,
148
- e,
149
- )
232
+ console.log(`Error while checking renderability of email template ${template}`, e)
150
233
  }
151
234
  return validRenderResp != null
152
235
  }