@ikonintegration/ikapi 4.0.1 → 5.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintignore +3 -0
- package/.eslintrc.cjs +81 -0
- package/.github/workflows/npmpublish.yml +8 -19
- package/.github/workflows/prs.yml +12 -0
- package/README.md +89 -99
- package/dist/index.d.ts +16 -0
- package/dist/index.js +27 -0
- package/dist/index.js.map +1 -0
- package/dist/package-lock.json +11881 -0
- package/dist/package.json +81 -0
- package/dist/src/API/Request.d.ts +125 -0
- package/dist/src/API/Request.js +185 -0
- package/dist/src/API/Request.js.map +1 -0
- package/dist/src/API/Response.d.ts +188 -0
- package/dist/src/API/Response.js +270 -0
- package/dist/src/API/Response.js.map +1 -0
- package/dist/src/BaseEvent/DynamoTransaction.d.ts +70 -0
- package/dist/src/BaseEvent/DynamoTransaction.js +104 -0
- package/dist/src/BaseEvent/DynamoTransaction.js.map +1 -0
- package/dist/src/BaseEvent/EventProcessor.d.ts +58 -0
- package/dist/src/BaseEvent/EventProcessor.js +101 -0
- package/dist/src/BaseEvent/EventProcessor.js.map +1 -0
- package/dist/src/BaseEvent/Process.d.ts +50 -0
- package/dist/src/BaseEvent/Process.js +64 -0
- package/dist/src/BaseEvent/Process.js.map +1 -0
- package/dist/src/BaseEvent/StepTransaction.d.ts +23 -0
- package/dist/src/BaseEvent/StepTransaction.js +27 -0
- package/dist/src/BaseEvent/StepTransaction.js.map +1 -0
- package/dist/src/BaseEvent/Transaction.d.ts +149 -0
- package/dist/src/BaseEvent/Transaction.js +224 -0
- package/dist/src/BaseEvent/Transaction.js.map +1 -0
- package/dist/src/Cache/Redis.d.ts +29 -0
- package/dist/src/Cache/Redis.js +80 -0
- package/dist/src/Cache/Redis.js.map +1 -0
- package/dist/src/Cache/types.d.ts +31 -0
- package/dist/src/Cache/types.js +2 -0
- package/dist/src/Cache/types.js.map +1 -0
- package/dist/src/Config/Configuration.d.ts +123 -0
- package/dist/src/Config/Configuration.js +109 -0
- package/dist/src/Config/Configuration.js.map +1 -0
- package/dist/src/Config/EnvironmentVar.d.ts +74 -0
- package/dist/src/Config/EnvironmentVar.js +138 -0
- package/dist/src/Config/EnvironmentVar.js.map +1 -0
- package/dist/src/Crypto/Crypto.d.ts +45 -0
- package/dist/src/Crypto/Crypto.js +72 -0
- package/dist/src/Crypto/Crypto.js.map +1 -0
- package/dist/src/Database/Database.d.ts +21 -0
- package/dist/src/Database/Database.js +15 -0
- package/dist/src/Database/Database.js.map +1 -0
- package/dist/src/Database/DatabaseManager.d.ts +47 -0
- package/dist/src/Database/DatabaseManager.js +60 -0
- package/dist/src/Database/DatabaseManager.js.map +1 -0
- package/dist/src/Database/DatabaseTransaction.d.ts +101 -0
- package/dist/src/Database/DatabaseTransaction.js +126 -0
- package/dist/src/Database/DatabaseTransaction.js.map +1 -0
- package/dist/src/Database/index.d.ts +10 -0
- package/dist/src/Database/index.js +15 -0
- package/dist/src/Database/index.js.map +1 -0
- package/dist/src/Database/integrations/dynamo/DynamoDatabase.d.ts +35 -0
- package/dist/src/Database/integrations/dynamo/DynamoDatabase.js +59 -0
- package/dist/src/Database/integrations/dynamo/DynamoDatabase.js.map +1 -0
- package/dist/src/Database/integrations/kysely/KyselyDatabase.d.ts +66 -0
- package/dist/src/Database/integrations/kysely/KyselyDatabase.js +86 -0
- package/dist/src/Database/integrations/kysely/KyselyDatabase.js.map +1 -0
- package/dist/src/Database/integrations/kysely/KyselyTransaction.d.ts +70 -0
- package/dist/src/Database/integrations/kysely/KyselyTransaction.js +118 -0
- package/dist/src/Database/integrations/kysely/KyselyTransaction.js.map +1 -0
- package/dist/src/Database/integrations/pgsql/PostgresDatabase.d.ts +36 -0
- package/dist/src/Database/integrations/pgsql/PostgresDatabase.js +54 -0
- package/dist/src/Database/integrations/pgsql/PostgresDatabase.js.map +1 -0
- package/dist/src/Database/integrations/pgsql/PostgresTransaction.d.ts +63 -0
- package/dist/src/Database/integrations/pgsql/PostgresTransaction.js +61 -0
- package/dist/src/Database/integrations/pgsql/PostgresTransaction.js.map +1 -0
- package/dist/src/Database/types.d.ts +76 -0
- package/dist/src/Database/types.js +2 -0
- package/dist/src/Database/types.js.map +1 -0
- package/dist/src/Globals.d.ts +93 -0
- package/dist/src/Globals.js +99 -0
- package/dist/src/Globals.js.map +1 -0
- package/dist/src/Logger/Logger.d.ts +161 -0
- package/dist/src/Logger/Logger.js +299 -0
- package/dist/src/Logger/Logger.js.map +1 -0
- package/dist/src/Mailer/Mailer.d.ts +78 -0
- package/dist/src/Mailer/Mailer.js +182 -0
- package/dist/src/Mailer/Mailer.js.map +1 -0
- package/dist/src/Publisher/Publisher.d.ts +39 -0
- package/dist/src/Publisher/Publisher.js +77 -0
- package/dist/src/Publisher/Publisher.js.map +1 -0
- package/dist/src/Server/RouteResolver.d.ts +33 -0
- package/dist/src/Server/RouteResolver.js +100 -0
- package/dist/src/Server/RouteResolver.js.map +1 -0
- package/dist/src/Server/Router.d.ts +157 -0
- package/dist/src/Server/Router.js +32 -0
- package/dist/src/Server/Router.js.map +1 -0
- package/dist/src/Server/lib/ContainerServer.d.ts +42 -0
- package/dist/src/Server/lib/ContainerServer.js +66 -0
- package/dist/src/Server/lib/ContainerServer.js.map +1 -0
- package/dist/src/Server/lib/Server.d.ts +45 -0
- package/dist/src/Server/lib/Server.js +93 -0
- package/dist/src/Server/lib/Server.js.map +1 -0
- package/dist/src/Server/lib/container/GenericHandler.d.ts +9 -0
- package/dist/src/Server/lib/container/GenericHandler.js +82 -0
- package/dist/src/Server/lib/container/GenericHandler.js.map +1 -0
- package/dist/src/Server/lib/container/GenericHandlerEvent.d.ts +52 -0
- package/dist/src/Server/lib/container/GenericHandlerEvent.js +132 -0
- package/dist/src/Server/lib/container/GenericHandlerEvent.js.map +1 -0
- package/dist/src/Server/lib/container/HealthHandler.d.ts +9 -0
- package/dist/src/Server/lib/container/HealthHandler.js +19 -0
- package/dist/src/Server/lib/container/HealthHandler.js.map +1 -0
- package/dist/src/Server/lib/container/Proxy.d.ts +67 -0
- package/dist/src/Server/lib/container/Proxy.js +143 -0
- package/dist/src/Server/lib/container/Proxy.js.map +1 -0
- package/dist/src/Server/lib/container/Utils.d.ts +14 -0
- package/dist/src/Server/lib/container/Utils.js +37 -0
- package/dist/src/Server/lib/container/Utils.js.map +1 -0
- package/dist/src/Util/AsyncSingleton.d.ts +31 -0
- package/dist/src/Util/AsyncSingleton.js +83 -0
- package/dist/src/Util/AsyncSingleton.js.map +1 -0
- package/dist/src/Util/Utils.d.ts +61 -0
- package/dist/src/Util/Utils.js +147 -0
- package/dist/src/Util/Utils.js.map +1 -0
- package/dist/src/Validation/Validator.d.ts +17 -0
- package/dist/src/Validation/Validator.js +39 -0
- package/dist/src/Validation/Validator.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/index.ts +41 -0
- package/jest.config.ts +37 -0
- package/jest.smoke.config.ts +34 -0
- package/package.json +66 -22
- package/src/API/Request.ts +214 -0
- package/src/API/Response.ts +370 -0
- package/src/BaseEvent/DynamoTransaction.ts +175 -0
- package/src/BaseEvent/EventProcessor.ts +140 -0
- package/src/BaseEvent/Process.ts +78 -0
- package/src/BaseEvent/StepTransaction.ts +35 -0
- package/src/BaseEvent/Transaction.ts +323 -0
- package/src/Cache/Redis.ts +89 -0
- package/src/Cache/types.ts +33 -0
- package/src/Config/Configuration.ts +199 -0
- package/src/Config/EnvironmentVar.ts +142 -0
- package/src/Crypto/Crypto.ts +89 -0
- package/src/Database/Database.ts +22 -0
- package/src/Database/DatabaseManager.ts +67 -0
- package/src/Database/DatabaseTransaction.ts +170 -0
- package/src/Database/index.ts +27 -0
- package/src/Database/integrations/dynamo/DynamoDatabase.ts +58 -0
- package/src/Database/integrations/kysely/KyselyDatabase.ts +99 -0
- package/src/Database/integrations/kysely/KyselyTransaction.ts +172 -0
- package/src/Database/integrations/pgsql/PostgresDatabase.ts +56 -0
- package/src/Database/integrations/pgsql/PostgresTransaction.ts +87 -0
- package/src/Database/types.ts +85 -0
- package/src/Globals.ts +103 -0
- package/src/Logger/Logger.ts +363 -0
- package/src/Mailer/Mailer.ts +217 -0
- package/src/Publisher/Publisher.ts +96 -0
- package/src/Server/RouteResolver.ts +124 -0
- package/src/Server/Router.ts +200 -0
- package/src/Server/lib/ContainerServer.ts +65 -0
- package/src/Server/lib/Server.ts +109 -0
- package/src/Server/lib/container/GenericHandler.ts +76 -0
- package/src/Server/lib/container/GenericHandlerEvent.ts +154 -0
- package/src/Server/lib/container/HealthHandler.ts +11 -0
- package/src/Server/lib/container/Proxy.ts +172 -0
- package/src/Server/lib/container/Utils.ts +33 -0
- package/src/Util/AsyncSingleton.ts +86 -0
- package/src/Util/Utils.ts +131 -0
- package/src/Validation/Validator.ts +45 -0
- package/tests/API/Request.test.ts +273 -0
- package/tests/API/Response.test.ts +367 -0
- package/tests/BaseEvent/DynamoTransaction.test.ts +272 -0
- package/tests/BaseEvent/EventProcessor.test.ts +263 -0
- package/tests/BaseEvent/Process.test.ts +47 -0
- package/tests/BaseEvent/StepTransaction.test.ts +44 -0
- package/tests/BaseEvent/Transaction.test.ts +402 -0
- package/tests/Cache/Redis-client.test.ts +90 -0
- package/tests/Cache/Redis-cluster.test.ts +100 -0
- package/tests/Config/Config.test.ts +205 -0
- package/tests/Config/EnvironmentVar.test.ts +251 -0
- package/tests/Crypto/Crypto.test.ts +88 -0
- package/tests/Database/DatabaseManager.test.ts +79 -0
- package/tests/Database/integrations/dynamo/DynamoDatabase.test.ts +44 -0
- package/tests/Database/integrations/kysely/KyselyDatabase.test.ts +113 -0
- package/tests/Database/integrations/kysely/KyselyTransaction.test.ts +119 -0
- package/tests/Database/integrations/pg/PostgresDatabase.test.ts +76 -0
- package/tests/Database/integrations/pg/PostgresTransaction.test.ts +118 -0
- package/tests/Logger/Logger.test.ts +215 -0
- package/tests/Mailer/Mailer.test.ts +59 -0
- package/tests/Publisher/Publisher.test.ts +60 -0
- package/tests/Server/RouteResolver.test.ts +116 -0
- package/tests/Server/Router.test.ts +39 -0
- package/tests/Server/lib/ContainerServer.test.ts +531 -0
- package/tests/Server/lib/Server.test.ts +12 -0
- package/tests/Server/lib/container/GenericHandler.test.ts +131 -0
- package/tests/Server/lib/container/GenericHandlerEvent.test.ts +103 -0
- package/tests/Server/lib/container/HealthHandler.test.ts +30 -0
- package/tests/Server/lib/container/Proxy.test.ts +268 -0
- package/tests/Server/lib/container/Utils.test.ts +47 -0
- package/tests/Test.utils.ts +78 -0
- package/tests/Utils/Utils.test.ts +229 -0
- package/tests/Validation/Validator.test.ts +82 -0
- package/tsconfig.json +26 -0
- package/tsconfig.smoke.json +26 -0
- package/index.js +0 -88
- package/src/API/IKRequest.js +0 -52
- package/src/API/IKResponse.js +0 -119
- package/src/API/IKUtils.js +0 -51
- package/src/BaseEvent/IKProcess.js +0 -77
- package/src/BaseEvent/IKTransaction.js +0 -139
- package/src/Cache/Prototype/IKCache.js +0 -17
- package/src/Cache/Redis/IKRedis.js +0 -148
- package/src/Database/DDB/IKDB.js +0 -56
- package/src/Database/DDB/IKDBBaseExpression.js +0 -130
- package/src/Database/DDB/IKDBBaseQuery.js +0 -151
- package/src/Database/DDB/IKDBQueryBatchGet.js +0 -37
- package/src/Database/DDB/IKDBQueryBatchWrite.js +0 -64
- package/src/Database/DDB/IKDBQueryDelete.js +0 -34
- package/src/Database/DDB/IKDBQueryGet.js +0 -48
- package/src/Database/DDB/IKDBQueryPut.js +0 -87
- package/src/Database/DDB/IKDBQueryScan.js +0 -45
- package/src/Database/DDB/IKDBQueryTransactionalWrite.js +0 -69
- package/src/Database/DDB/IKDBQueryUpdate.js +0 -221
- package/src/Database/DDB/_IKDBQueryTransactionalRead.js +0 -46
- package/src/Database/PSQL/IKDB.js +0 -41
- package/src/Database/PSQL/IKDBBaseQuery.js +0 -26
- package/src/Database/Prototype/IKDB.js +0 -21
- package/src/Database/Prototype/IKDBBaseQuery.js +0 -14
- package/src/IKDynamoStream.js +0 -42
- package/src/IKEventProcessor.js +0 -42
- package/src/IKGlobals.js +0 -24
- package/src/IKRouter.js +0 -47
- package/src/IKStepTransaction.js +0 -14
- package/src/Logger/IKLogger.js +0 -136
- package/src/Mailer/IKMailer.js +0 -69
- package/src/Publisher/IKPublisher.js +0 -44
- package/src/Tracker/IKExecutionTracker.js +0 -79
- package/src/Validation/IKValidation.js +0 -76
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import Email from 'email-templates';
|
|
2
|
+
export default class Mailer {
|
|
3
|
+
/**
|
|
4
|
+
* The starting point of a range.
|
|
5
|
+
* @type {string}
|
|
6
|
+
*/
|
|
7
|
+
private readonly from;
|
|
8
|
+
/**
|
|
9
|
+
* The default file template for the application.
|
|
10
|
+
* @type {string}
|
|
11
|
+
*/
|
|
12
|
+
private readonly templateDefaultFile;
|
|
13
|
+
/**
|
|
14
|
+
* The transporter object used for sending emails using the AWS SES service.
|
|
15
|
+
* @type {ReturnType<typeof nodemailer.createTransport<SESTransport.SentMessageInfo>>}
|
|
16
|
+
*/
|
|
17
|
+
private readonly transporter;
|
|
18
|
+
/**
|
|
19
|
+
* Constructs a new instance of the EmailSender class.
|
|
20
|
+
* @param {string} defaultFrom - The default "from" email address.
|
|
21
|
+
* @param {string} region - The AWS region to use for sending emails.
|
|
22
|
+
* @returns None
|
|
23
|
+
*/
|
|
24
|
+
constructor(defaultFrom: string, region: string);
|
|
25
|
+
/**
|
|
26
|
+
* Sends a raw email with the specified parameters.
|
|
27
|
+
* @param {string | Array<string>} to - The recipient(s) of the email.
|
|
28
|
+
* @param {string} htmlMessage - The HTML content of the email.
|
|
29
|
+
* @param {string} subject - The subject of the email.
|
|
30
|
+
* @param {string | Array<string>} [optionalCC] - The optional CC recipient(s) of the email.
|
|
31
|
+
* @param {string} [optionalFrom] - The optional sender of the email. If not provided, the default sender will be used.
|
|
32
|
+
* @param {string} [optionalReplyTo] - The optional reply-to address for the email.
|
|
33
|
+
* @param {any[]} [optionalAttachments] - The optional attachments to include
|
|
34
|
+
*/
|
|
35
|
+
sendRawEmail(to: string | Array<string>, htmlMessage: string, subject: string, optionalCC?: string | Array<string>, optionalFrom?: string, optionalReplyTo?: string, optionalAttachments?: any[], optionalTransport?: Email.NodeMailerTransportOptions): Promise<null>;
|
|
36
|
+
/**
|
|
37
|
+
* Sends a templated email to the specified recipients.
|
|
38
|
+
* @param {string | Array<string>} to - The email address(es) of the recipient(s).
|
|
39
|
+
* @param {string | Array<string>} templates - The template(s) to use for the email.
|
|
40
|
+
* @param {object} data - The data to be used in the email template.
|
|
41
|
+
* @param {string | Array<string>} [optionalCC] - The email address(es) to CC.
|
|
42
|
+
* @param {string} [optionalFrom] - The email address to send the email from.
|
|
43
|
+
* @param {string} [optionalReplyTo] - The email address to set as the reply-to address.
|
|
44
|
+
* @param {any[]} [optionalAttachments] - An array
|
|
45
|
+
*/
|
|
46
|
+
sendTemplatedEmail(to: string | Array<string>, templates: string | Array<string>, data: object, optionalCC?: string | Array<string>, optionalFrom?: string, optionalReplyTo?: string, optionalAttachments?: any[], optionalTransport?: Email.NodeMailerTransportOptions): Promise<null>;
|
|
47
|
+
/**
|
|
48
|
+
* Creates a new SMTP transporter for sending emails using NodeMailer.
|
|
49
|
+
* @param {string} host - The SMTP server host.
|
|
50
|
+
* @param {number} portNumber - The port number to connect to the SMTP server.
|
|
51
|
+
* @param {string} user - The username for authentication with the SMTP server.
|
|
52
|
+
* @param {string} password - The password for authentication with the SMTP server.
|
|
53
|
+
* @returns {Email.NodeMailerTransportOptions} - The SMTP transporter object.
|
|
54
|
+
*/
|
|
55
|
+
newSMTPTransporter(host: string, portNumber: number, user: string, password: string): Email.NodeMailerTransportOptions;
|
|
56
|
+
/**
|
|
57
|
+
* Chooses a template from the given array of templates or a single template string based on whether it can be rendered with the provided data.
|
|
58
|
+
* @param {string | Array<string>} templates - The template(s) to choose from.
|
|
59
|
+
* @param {object} data - The data to be used for rendering the template.
|
|
60
|
+
* @returns {Promise<string>} - The chosen template.
|
|
61
|
+
* @throws {Error} - If no template can be rendered with the provided data.
|
|
62
|
+
*/
|
|
63
|
+
private chooseTemplate;
|
|
64
|
+
/**
|
|
65
|
+
* Checks if a given email template can be rendered with the provided data.
|
|
66
|
+
* @param {string} template - The name of the email template.
|
|
67
|
+
* @param {object} data - The data to be used for rendering the template.
|
|
68
|
+
* @returns {Promise<boolean>} - A promise that resolves to true if the template can be rendered, false otherwise.
|
|
69
|
+
*/
|
|
70
|
+
private canRenderTemplate;
|
|
71
|
+
/**
|
|
72
|
+
* Renders the given template with the provided data using the Email class.
|
|
73
|
+
* @param {string} template - The name or path of the template to render.
|
|
74
|
+
* @param {object} data - The data object to pass to the template.
|
|
75
|
+
* @returns {Promise<boolean>} - A promise that resolves to true if the template was rendered successfully, false otherwise.
|
|
76
|
+
*/
|
|
77
|
+
static renderTemplate(template: string, data: object): Promise<any>;
|
|
78
|
+
}
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import * as SES from '@aws-sdk/client-ses';
|
|
11
|
+
import { defaultProvider } from '@aws-sdk/credential-provider-node';
|
|
12
|
+
import Email from 'email-templates';
|
|
13
|
+
import nodemailer from 'nodemailer';
|
|
14
|
+
export default class Mailer {
|
|
15
|
+
/**
|
|
16
|
+
* Constructs a new instance of the EmailSender class.
|
|
17
|
+
* @param {string} defaultFrom - The default "from" email address.
|
|
18
|
+
* @param {string} region - The AWS region to use for sending emails.
|
|
19
|
+
* @returns None
|
|
20
|
+
*/
|
|
21
|
+
constructor(defaultFrom, region) {
|
|
22
|
+
/**
|
|
23
|
+
* The default file template for the application.
|
|
24
|
+
* @type {string}
|
|
25
|
+
*/
|
|
26
|
+
this.templateDefaultFile = 'html';
|
|
27
|
+
this.from = defaultFrom;
|
|
28
|
+
this.transporter = nodemailer.createTransport({
|
|
29
|
+
SES: {
|
|
30
|
+
ses: new SES.SESClient({
|
|
31
|
+
credentials: defaultProvider(),
|
|
32
|
+
apiVersion: '2010-12-01',
|
|
33
|
+
region,
|
|
34
|
+
}),
|
|
35
|
+
aws: SES,
|
|
36
|
+
},
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Sends a raw email with the specified parameters.
|
|
41
|
+
* @param {string | Array<string>} to - The recipient(s) of the email.
|
|
42
|
+
* @param {string} htmlMessage - The HTML content of the email.
|
|
43
|
+
* @param {string} subject - The subject of the email.
|
|
44
|
+
* @param {string | Array<string>} [optionalCC] - The optional CC recipient(s) of the email.
|
|
45
|
+
* @param {string} [optionalFrom] - The optional sender of the email. If not provided, the default sender will be used.
|
|
46
|
+
* @param {string} [optionalReplyTo] - The optional reply-to address for the email.
|
|
47
|
+
* @param {any[]} [optionalAttachments] - The optional attachments to include
|
|
48
|
+
*/
|
|
49
|
+
sendRawEmail(to, htmlMessage, subject, optionalCC, optionalFrom, optionalReplyTo,
|
|
50
|
+
// TODO: improve attachment type -> Attachment
|
|
51
|
+
optionalAttachments, optionalTransport) {
|
|
52
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
53
|
+
//Generate emails
|
|
54
|
+
const email = new Email({
|
|
55
|
+
message: Object.assign(Object.assign(Object.assign({ from: optionalFrom || this.from, to: to, html: htmlMessage, subject }, (optionalAttachments ? { attachments: optionalAttachments } : {})), (optionalCC ? { cc: optionalCC } : {})), (optionalReplyTo ? { replyTo: optionalReplyTo } : {})),
|
|
56
|
+
transport: optionalTransport || this.transporter,
|
|
57
|
+
send: true,
|
|
58
|
+
});
|
|
59
|
+
//
|
|
60
|
+
let resp = null;
|
|
61
|
+
try {
|
|
62
|
+
resp = yield email.send();
|
|
63
|
+
console.debug('Mailer resp:', resp);
|
|
64
|
+
}
|
|
65
|
+
catch (e) {
|
|
66
|
+
console.error('Mailer error:', e);
|
|
67
|
+
throw e;
|
|
68
|
+
}
|
|
69
|
+
return resp;
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Sends a templated email to the specified recipients.
|
|
74
|
+
* @param {string | Array<string>} to - The email address(es) of the recipient(s).
|
|
75
|
+
* @param {string | Array<string>} templates - The template(s) to use for the email.
|
|
76
|
+
* @param {object} data - The data to be used in the email template.
|
|
77
|
+
* @param {string | Array<string>} [optionalCC] - The email address(es) to CC.
|
|
78
|
+
* @param {string} [optionalFrom] - The email address to send the email from.
|
|
79
|
+
* @param {string} [optionalReplyTo] - The email address to set as the reply-to address.
|
|
80
|
+
* @param {any[]} [optionalAttachments] - An array
|
|
81
|
+
*/
|
|
82
|
+
sendTemplatedEmail(to, templates, data, optionalCC, optionalFrom, optionalReplyTo,
|
|
83
|
+
// TODO: improve attachment type -> Attachment
|
|
84
|
+
optionalAttachments, optionalTransport) {
|
|
85
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
86
|
+
//Generate emails
|
|
87
|
+
const email = new Email({
|
|
88
|
+
message: Object.assign(Object.assign(Object.assign({ from: optionalFrom || this.from, to: to }, (optionalAttachments ? { attachments: optionalAttachments } : {})), (optionalCC ? { cc: optionalCC } : {})), (optionalReplyTo ? { replyTo: optionalReplyTo } : {})),
|
|
89
|
+
transport: optionalTransport || this.transporter,
|
|
90
|
+
send: true,
|
|
91
|
+
});
|
|
92
|
+
//
|
|
93
|
+
let resp = null;
|
|
94
|
+
try {
|
|
95
|
+
const chosenTemplate = yield this.chooseTemplate(templates, data);
|
|
96
|
+
resp = yield email.send({ template: chosenTemplate, locals: data });
|
|
97
|
+
console.debug('Mailer resp:', resp);
|
|
98
|
+
}
|
|
99
|
+
catch (e) {
|
|
100
|
+
console.error('Mailer error:', e);
|
|
101
|
+
throw e;
|
|
102
|
+
}
|
|
103
|
+
return resp;
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Creates a new SMTP transporter for sending emails using NodeMailer.
|
|
108
|
+
* @param {string} host - The SMTP server host.
|
|
109
|
+
* @param {number} portNumber - The port number to connect to the SMTP server.
|
|
110
|
+
* @param {string} user - The username for authentication with the SMTP server.
|
|
111
|
+
* @param {string} password - The password for authentication with the SMTP server.
|
|
112
|
+
* @returns {Email.NodeMailerTransportOptions} - The SMTP transporter object.
|
|
113
|
+
*/
|
|
114
|
+
newSMTPTransporter(host, portNumber, user, password) {
|
|
115
|
+
const smtpTransporter = nodemailer.createTransport({
|
|
116
|
+
host: host,
|
|
117
|
+
port: portNumber,
|
|
118
|
+
connectionTimeout: 2000,
|
|
119
|
+
auth: {
|
|
120
|
+
user: user,
|
|
121
|
+
pass: password,
|
|
122
|
+
},
|
|
123
|
+
});
|
|
124
|
+
return smtpTransporter;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Chooses a template from the given array of templates or a single template string based on whether it can be rendered with the provided data.
|
|
128
|
+
* @param {string | Array<string>} templates - The template(s) to choose from.
|
|
129
|
+
* @param {object} data - The data to be used for rendering the template.
|
|
130
|
+
* @returns {Promise<string>} - The chosen template.
|
|
131
|
+
* @throws {Error} - If no template can be rendered with the provided data.
|
|
132
|
+
*/
|
|
133
|
+
chooseTemplate(templates, data) {
|
|
134
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
135
|
+
if (Array.isArray(templates)) {
|
|
136
|
+
//For each template check if can render it
|
|
137
|
+
for (const template of templates) {
|
|
138
|
+
if (yield this.canRenderTemplate(template, data))
|
|
139
|
+
return template;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
else if (templates) {
|
|
143
|
+
if (yield this.canRenderTemplate(templates, data))
|
|
144
|
+
return templates;
|
|
145
|
+
}
|
|
146
|
+
throw new Error(`Could not render email with template ${templates} and following data. Please, check logs above! ` +
|
|
147
|
+
JSON.stringify(data, null, 2));
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Checks if a given email template can be rendered with the provided data.
|
|
152
|
+
* @param {string} template - The name of the email template.
|
|
153
|
+
* @param {object} data - The data to be used for rendering the template.
|
|
154
|
+
* @returns {Promise<boolean>} - A promise that resolves to true if the template can be rendered, false otherwise.
|
|
155
|
+
*/
|
|
156
|
+
canRenderTemplate(template, data) {
|
|
157
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
158
|
+
const validRenderResp = yield Mailer.renderTemplate(`${template}/${this.templateDefaultFile}`, data);
|
|
159
|
+
return validRenderResp != null;
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Renders the given template with the provided data using the Email class.
|
|
164
|
+
* @param {string} template - The name or path of the template to render.
|
|
165
|
+
* @param {object} data - The data object to pass to the template.
|
|
166
|
+
* @returns {Promise<boolean>} - A promise that resolves to true if the template was rendered successfully, false otherwise.
|
|
167
|
+
*/
|
|
168
|
+
static renderTemplate(template, data) {
|
|
169
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
170
|
+
let validRenderResp = null;
|
|
171
|
+
try {
|
|
172
|
+
const email = new Email();
|
|
173
|
+
validRenderResp = yield email.render(`${template}`, data);
|
|
174
|
+
}
|
|
175
|
+
catch (e) {
|
|
176
|
+
console.log(`Error while checking renderability of email template ${template}`, e);
|
|
177
|
+
}
|
|
178
|
+
return validRenderResp;
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
//# sourceMappingURL=Mailer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Mailer.js","sourceRoot":"","sources":["../../../src/Mailer/Mailer.ts"],"names":[],"mappings":";;;;;;;;;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CAAwC,SAAS,iDAAiD;gBAChG,IAAI,CAAC,SAAS,CAAC,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC,CAChC,CAAA;QACH,CAAC;KAAA;IAED;;;;;OAKG;IACW,iBAAiB,CAAC,QAAgB,EAAE,IAAY;;YAC5D,MAAM,eAAe,GAAG,MAAM,MAAM,CAAC,cAAc,CACjD,GAAG,QAAQ,IAAI,IAAI,CAAC,mBAAmB,EAAE,EACzC,IAAI,CACL,CAAA;YACD,OAAO,eAAe,IAAI,IAAI,CAAA;QAChC,CAAC;KAAA;IAED;;;;;OAKG;IACI,MAAM,CAAO,cAAc,CAAC,QAAgB,EAAE,IAAY;;YAC/D,IAAI,eAAe,GAAkB,IAAI,CAAA;YACzC,IAAI,CAAC;gBACH,MAAM,KAAK,GAAG,IAAI,KAAK,EAAE,CAAA;gBACzB,eAAe,GAAG,MAAM,KAAK,CAAC,MAAM,CAAC,GAAG,QAAQ,EAAE,EAAE,IAAI,CAAC,CAAA;YAC3D,CAAC;YAAC,OAAO,CAAC,EAAE,CAAC;gBACX,OAAO,CAAC,GAAG,CAAC,wDAAwD,QAAQ,EAAE,EAAE,CAAC,CAAC,CAAA;YACpF,CAAC;YACD,OAAO,eAAe,CAAA;QACxB,CAAC;KAAA;CACF"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { PublishCommandOutput } from '@aws-sdk/client-sns';
|
|
2
|
+
/**
|
|
3
|
+
* Represents the configuration options for a publisher.
|
|
4
|
+
* @typedef {Object} PublisherConfig
|
|
5
|
+
* @property {string} [region] - The region where the publisher is located.
|
|
6
|
+
*/
|
|
7
|
+
export type PublisherConfig = {
|
|
8
|
+
region?: string;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Represents a publisher that can publish messages to an SNS topic.
|
|
12
|
+
*/
|
|
13
|
+
export default class Publisher {
|
|
14
|
+
/**
|
|
15
|
+
* The region of the object or entity.
|
|
16
|
+
* @private
|
|
17
|
+
* @type {string}
|
|
18
|
+
*/
|
|
19
|
+
private region;
|
|
20
|
+
/**
|
|
21
|
+
* Constructs a new instance of the Publisher class.
|
|
22
|
+
* @param {PublisherConfig} [config] - The configuration object for the Publisher.
|
|
23
|
+
* @returns None
|
|
24
|
+
*/
|
|
25
|
+
constructor(config?: PublisherConfig);
|
|
26
|
+
/**
|
|
27
|
+
* Publishes a message on a specified topic.
|
|
28
|
+
* @param {any} messageObject - The message object to be published.
|
|
29
|
+
* @param {string} topic - The ARN of the topic to publish the message to.
|
|
30
|
+
* @param {object} [additionalProps] - Additional properties to include in the publish command.
|
|
31
|
+
* @returns {Promise<PublishCommandOutput>} - A promise that resolves to the response from the publish command.
|
|
32
|
+
*/
|
|
33
|
+
publishOnTopic(messageObject: any, topic: string, additionalProps?: object): Promise<PublishCommandOutput | undefined>;
|
|
34
|
+
/**
|
|
35
|
+
* Establishes a connection to the SNS client if it does not already exist or if the region has changed.
|
|
36
|
+
* @returns None
|
|
37
|
+
*/
|
|
38
|
+
private connect;
|
|
39
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { SNSClient, PublishCommand, } from '@aws-sdk/client-sns';
|
|
11
|
+
import sha1 from 'sha1';
|
|
12
|
+
//reusable client
|
|
13
|
+
/**
|
|
14
|
+
* A variable that holds the connection to the SNS client for publishing messages.
|
|
15
|
+
* @type {SNSClient | null}
|
|
16
|
+
*/
|
|
17
|
+
// eslint-disable-next-line no-var
|
|
18
|
+
let PUBLISHER_CONN = null;
|
|
19
|
+
/**
|
|
20
|
+
* The connection hash for the publisher. It is initially set to null.
|
|
21
|
+
* @type {string | null}
|
|
22
|
+
*/
|
|
23
|
+
// eslint-disable-next-line no-var
|
|
24
|
+
let PUBLISHER_CONN_HASH = null;
|
|
25
|
+
/**
|
|
26
|
+
* Represents a publisher that can publish messages to an SNS topic.
|
|
27
|
+
*/
|
|
28
|
+
export default class Publisher {
|
|
29
|
+
/**
|
|
30
|
+
* Constructs a new instance of the Publisher class.
|
|
31
|
+
* @param {PublisherConfig} [config] - The configuration object for the Publisher.
|
|
32
|
+
* @returns None
|
|
33
|
+
*/
|
|
34
|
+
constructor(config) {
|
|
35
|
+
if (config && config.region) {
|
|
36
|
+
this.region = config.region;
|
|
37
|
+
// console.debug(`Using region: ${this.region}`);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Publishes a message on a specified topic.
|
|
42
|
+
* @param {any} messageObject - The message object to be published.
|
|
43
|
+
* @param {string} topic - The ARN of the topic to publish the message to.
|
|
44
|
+
* @param {object} [additionalProps] - Additional properties to include in the publish command.
|
|
45
|
+
* @returns {Promise<PublishCommandOutput>} - A promise that resolves to the response from the publish command.
|
|
46
|
+
*/
|
|
47
|
+
publishOnTopic(messageObject, topic, additionalProps) {
|
|
48
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
49
|
+
let resp = undefined;
|
|
50
|
+
try {
|
|
51
|
+
this.connect();
|
|
52
|
+
//Send to SNS
|
|
53
|
+
const params = Object.assign({ Message: JSON.stringify(messageObject), TopicArn: topic }, (additionalProps ? additionalProps : {}));
|
|
54
|
+
resp = yield PUBLISHER_CONN.send(new PublishCommand(params));
|
|
55
|
+
}
|
|
56
|
+
catch (e) {
|
|
57
|
+
console.error(`Error while publishing into topic ${topic} with error: ${e}`);
|
|
58
|
+
}
|
|
59
|
+
console.debug('Publisher resp', resp);
|
|
60
|
+
return resp;
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Establishes a connection to the SNS client if it does not already exist or if the region has changed.
|
|
65
|
+
* @returns None
|
|
66
|
+
*/
|
|
67
|
+
connect() {
|
|
68
|
+
if ((!PUBLISHER_CONN && !PUBLISHER_CONN_HASH) || PUBLISHER_CONN_HASH != sha1(this.region)) {
|
|
69
|
+
PUBLISHER_CONN = new SNSClient({
|
|
70
|
+
apiVersion: '2010-03-31',
|
|
71
|
+
region: this.region,
|
|
72
|
+
});
|
|
73
|
+
PUBLISHER_CONN_HASH = sha1(this.region).toString();
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
//# sourceMappingURL=Publisher.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Publisher.js","sourceRoot":"","sources":["../../../src/Publisher/Publisher.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EACL,SAAS,EACT,cAAc,GAGf,MAAM,qBAAqB,CAAA;AAC5B,OAAO,IAAI,MAAM,MAAM,CAAA;AACvB,iBAAiB;AACjB;;;GAGG;AACH,kCAAkC;AAClC,IAAI,cAAc,GAAqB,IAAI,CAAA;AAC3C;;;GAGG;AACH,kCAAkC;AAClC,IAAI,mBAAmB,GAAkB,IAAI,CAAA;AAW7C;;GAEG;AACH,MAAM,CAAC,OAAO,OAAO,SAAS;IAQ5B;;;;OAIG;IACH,YAAY,MAAwB;QAClC,IAAI,MAAM,IAAI,MAAM,CAAC,MAAM,EAAE,CAAC;YAC5B,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC,MAAM,CAAA;YAC3B,iDAAiD;QACnD,CAAC;IACH,CAAC;IAED;;;;;;OAMG;IACU,cAAc,CACzB,aAAkB,EAClB,KAAa,EACb,eAAwB;;YAExB,IAAI,IAAI,GAAqC,SAAS,CAAA;YACtD,IAAI,CAAC;gBACH,IAAI,CAAC,OAAO,EAAE,CAAA;gBACd,aAAa;gBACb,MAAM,MAAM,mBACV,OAAO,EAAE,IAAI,CAAC,SAAS,CAAC,aAAa,CAAC,EACtC,QAAQ,EAAE,KAAK,IACZ,CAAC,eAAe,CAAC,CAAC,CAAC,eAAe,CAAC,CAAC,CAAC,EAAE,CAAC,CAC5C,CAAA;gBACD,IAAI,GAAG,MAAM,cAAe,CAAC,IAAI,CAAC,IAAI,cAAc,CAAC,MAAM,CAAC,CAAC,CAAA;YAC/D,CAAC;YAAC,OAAO,CAAC,EAAE,CAAC;gBACX,OAAO,CAAC,KAAK,CAAC,qCAAqC,KAAK,gBAAgB,CAAC,EAAE,CAAC,CAAA;YAC9E,CAAC;YACD,OAAO,CAAC,KAAK,CAAC,gBAAgB,EAAE,IAAI,CAAC,CAAA;YACrC,OAAO,IAAI,CAAA;QACb,CAAC;KAAA;IAED;;;OAGG;IACK,OAAO;QACb,IAAI,CAAC,CAAC,cAAc,IAAI,CAAC,mBAAmB,CAAC,IAAI,mBAAmB,IAAI,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC;YAC1F,cAAc,GAAG,IAAI,SAAS,CAAC;gBAC7B,UAAU,EAAE,YAAY;gBACxB,MAAM,EAAE,IAAI,CAAC,MAAM;aACpB,CAAC,CAAA;YACF,mBAAmB,GAAG,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,QAAQ,EAAE,CAAA;QACpD,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { AnyRoute, RouterConfig } from './Router.js';
|
|
2
|
+
import { HttpMethod } from '../API/Request.js';
|
|
3
|
+
/**
|
|
4
|
+
* A class that resolves routes based on the provided configuration.
|
|
5
|
+
* @class RouteResolver
|
|
6
|
+
*/
|
|
7
|
+
export default class RouteResolver {
|
|
8
|
+
/**
|
|
9
|
+
* An object that maps HTTP methods to their corresponding routes.
|
|
10
|
+
* @property {object} routes - The routes object.
|
|
11
|
+
* @property {Routes} routes.method - The routes for the specified HTTP method.
|
|
12
|
+
*/
|
|
13
|
+
private routes;
|
|
14
|
+
/**
|
|
15
|
+
* Constructs a new instance of the Router class with the given configuration.
|
|
16
|
+
* @param {RouterConfig} config - The configuration object for the router.
|
|
17
|
+
* @returns None
|
|
18
|
+
*/
|
|
19
|
+
constructor(config: RouterConfig);
|
|
20
|
+
/**
|
|
21
|
+
* Resolves a route based on the given HTTP method and path.
|
|
22
|
+
* @param {string} method - The HTTP method of the request.
|
|
23
|
+
* @param {string} path - The path of the request.
|
|
24
|
+
* @returns {Route | undefined} - The resolved route or undefined if no route is found.
|
|
25
|
+
*/
|
|
26
|
+
resolveRoute(method: HttpMethod, path: string): AnyRoute | undefined;
|
|
27
|
+
/**
|
|
28
|
+
* Builds the routes for the router based on the given configuration.
|
|
29
|
+
* @param {RouterConfig} config - The router configuration object.
|
|
30
|
+
* @returns None
|
|
31
|
+
*/
|
|
32
|
+
private buildRoutes;
|
|
33
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents a collection of routes that can be added and resolved.
|
|
3
|
+
*/
|
|
4
|
+
class Routes {
|
|
5
|
+
/**
|
|
6
|
+
* Initializes a new instance of the class.
|
|
7
|
+
* @constructor
|
|
8
|
+
*/
|
|
9
|
+
constructor() {
|
|
10
|
+
this.next = {};
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Adds a route to the routing system.
|
|
14
|
+
* @param {Route} route - The route to add.
|
|
15
|
+
* @param {string[]} parts - The parts of the route path.
|
|
16
|
+
* @returns None
|
|
17
|
+
* @throws {Error} If a duplicate route is detected.
|
|
18
|
+
*/
|
|
19
|
+
addRoute(route, parts) {
|
|
20
|
+
const first = parts.shift();
|
|
21
|
+
if (first) {
|
|
22
|
+
if (first.startsWith(':')) {
|
|
23
|
+
this.variable = this.variable || new Routes();
|
|
24
|
+
this.variable.addRoute(route, parts);
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
this.next[first] = this.next[first] || new Routes();
|
|
28
|
+
this.next[first].addRoute(route, parts);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
if (this.current)
|
|
33
|
+
throw new Error(`Duplicate route: ${route.method}: ${route.path}`);
|
|
34
|
+
this.current = route;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Resolves a route based on the given parts.
|
|
39
|
+
* @param {string[]} parts - An array of route parts.
|
|
40
|
+
* @returns {Route} The resolved route.
|
|
41
|
+
*/
|
|
42
|
+
resolveRoute(parts) {
|
|
43
|
+
var _a;
|
|
44
|
+
const first = parts.shift();
|
|
45
|
+
if (first) {
|
|
46
|
+
if (first in this.next) {
|
|
47
|
+
return this.next[first].resolveRoute(parts);
|
|
48
|
+
}
|
|
49
|
+
return (_a = this.variable) === null || _a === void 0 ? void 0 : _a.resolveRoute(parts);
|
|
50
|
+
}
|
|
51
|
+
return this.current;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* A class that resolves routes based on the provided configuration.
|
|
56
|
+
* @class RouteResolver
|
|
57
|
+
*/
|
|
58
|
+
export default class RouteResolver {
|
|
59
|
+
/**
|
|
60
|
+
* Constructs a new instance of the Router class with the given configuration.
|
|
61
|
+
* @param {RouterConfig} config - The configuration object for the router.
|
|
62
|
+
* @returns None
|
|
63
|
+
*/
|
|
64
|
+
constructor(config) {
|
|
65
|
+
this.routes = {};
|
|
66
|
+
this.buildRoutes(config);
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Resolves a route based on the given HTTP method and path.
|
|
70
|
+
* @param {string} method - The HTTP method of the request.
|
|
71
|
+
* @param {string} path - The path of the request.
|
|
72
|
+
* @returns {Route | undefined} - The resolved route or undefined if no route is found.
|
|
73
|
+
*/
|
|
74
|
+
resolveRoute(method, path) {
|
|
75
|
+
var _a;
|
|
76
|
+
const parts = path.split('/').filter(p => p.length);
|
|
77
|
+
return (_a = this.routes[method]) === null || _a === void 0 ? void 0 : _a.resolveRoute(parts);
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Builds the routes for the router based on the given configuration.
|
|
81
|
+
* @param {RouterConfig} config - The router configuration object.
|
|
82
|
+
* @returns None
|
|
83
|
+
*/
|
|
84
|
+
buildRoutes(config) {
|
|
85
|
+
const addRoute = (path, route) => {
|
|
86
|
+
const parts = path.split('/').filter(p => p.length);
|
|
87
|
+
this.routes[route.method] = this.routes[route.method] || new Routes();
|
|
88
|
+
this.routes[route.method].addRoute(route, parts);
|
|
89
|
+
};
|
|
90
|
+
for (const route of config.routes) {
|
|
91
|
+
if (Array.isArray(route.path)) {
|
|
92
|
+
for (const path of route.path)
|
|
93
|
+
addRoute(path, route);
|
|
94
|
+
}
|
|
95
|
+
else
|
|
96
|
+
addRoute(route.path, route);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
//# sourceMappingURL=RouteResolver.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"RouteResolver.js","sourceRoot":"","sources":["../../../src/Server/RouteResolver.ts"],"names":[],"mappings":"AAGA;;GAEG;AACH,MAAM,MAAM;IAkBV;;;OAGG;IACH;QACE,IAAI,CAAC,IAAI,GAAG,EAAE,CAAA;IAChB,CAAC;IAED;;;;;;OAMG;IACI,QAAQ,CAAC,KAAe,EAAE,KAAe;QAC9C,MAAM,KAAK,GAAG,KAAK,CAAC,KAAK,EAAE,CAAA;QAC3B,IAAI,KAAK,EAAE,CAAC;YACV,IAAI,KAAK,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC;gBAC1B,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC,QAAQ,IAAI,IAAI,MAAM,EAAE,CAAA;gBAC7C,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,KAAK,EAAE,KAAK,CAAC,CAAA;YACtC,CAAC;iBAAM,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