@creator.co/wapi 1.2.5 → 1.2.6
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/README.md +216 -5
- package/dist/index.d.ts +15 -0
- package/dist/index.js.map +1 -1
- package/dist/package.json +6 -1
- package/dist/src/API/Request.d.ts +45 -82
- package/dist/src/API/Request.js +49 -77
- package/dist/src/API/Request.js.map +1 -1
- package/dist/src/API/Response.d.ts +94 -163
- package/dist/src/API/Response.js +101 -161
- package/dist/src/API/Response.js.map +1 -1
- package/dist/src/API/Utils.d.ts +21 -42
- package/dist/src/API/Utils.js +22 -43
- package/dist/src/API/Utils.js.map +1 -1
- package/dist/src/BaseEvent/EventProcessor.d.ts +32 -55
- package/dist/src/BaseEvent/EventProcessor.js +30 -38
- package/dist/src/BaseEvent/EventProcessor.js.map +1 -1
- package/dist/src/BaseEvent/Process.d.ts +20 -43
- package/dist/src/BaseEvent/Process.js +16 -27
- package/dist/src/BaseEvent/Process.js.map +1 -1
- package/dist/src/BaseEvent/Transaction.d.ts +94 -2
- package/dist/src/BaseEvent/Transaction.js +49 -4
- package/dist/src/BaseEvent/Transaction.js.map +1 -1
- package/dist/src/Config/Configuration.d.ts +48 -66
- package/dist/src/Config/Configuration.js +25 -42
- package/dist/src/Config/Configuration.js.map +1 -1
- package/dist/src/Config/EnvironmentVar.d.ts +30 -57
- package/dist/src/Config/EnvironmentVar.js +28 -41
- package/dist/src/Config/EnvironmentVar.js.map +1 -1
- package/dist/src/Crypto/Crypto.d.ts +17 -35
- package/dist/src/Crypto/Crypto.js +12 -21
- package/dist/src/Crypto/Crypto.js.map +1 -1
- package/dist/src/Crypto/JWT.d.ts +21 -32
- package/dist/src/Crypto/JWT.js +14 -22
- package/dist/src/Crypto/JWT.js.map +1 -1
- package/dist/src/Globals.d.ts +26 -94
- package/dist/src/Globals.js +26 -95
- package/dist/src/Globals.js.map +1 -1
- package/dist/src/Logger/Logger.d.ts +82 -105
- package/dist/src/Logger/Logger.js +111 -136
- package/dist/src/Logger/Logger.js.map +1 -1
- package/dist/src/Mailer/Mailer.d.ts +39 -75
- package/dist/src/Mailer/Mailer.js +36 -65
- package/dist/src/Mailer/Mailer.js.map +1 -1
- package/dist/src/Publisher/Publisher.d.ts +17 -25
- package/dist/src/Publisher/Publisher.js +21 -32
- package/dist/src/Publisher/Publisher.js.map +1 -1
- package/dist/src/Server/RouteResolver.d.ts +14 -22
- package/dist/src/Server/RouteResolver.js +21 -34
- package/dist/src/Server/RouteResolver.js.map +1 -1
- package/dist/src/Server/Router.d.ts +72 -51
- package/dist/src/Server/Router.js +8 -17
- package/dist/src/Server/Router.js.map +1 -1
- package/dist/src/Server/lib/ContainerServer.d.ts +15 -31
- package/dist/src/Server/lib/ContainerServer.js +13 -28
- package/dist/src/Server/lib/ContainerServer.js.map +1 -1
- package/dist/src/Server/lib/Server.d.ts +17 -32
- package/dist/src/Server/lib/Server.js +18 -28
- package/dist/src/Server/lib/Server.js.map +1 -1
- package/dist/src/Server/lib/container/GenericHandler.d.ts +5 -0
- package/dist/src/Server/lib/container/GenericHandler.js +16 -3
- package/dist/src/Server/lib/container/GenericHandler.js.map +1 -1
- package/dist/src/Server/lib/container/GenericHandlerEvent.d.ts +22 -37
- package/dist/src/Server/lib/container/GenericHandlerEvent.js +29 -41
- package/dist/src/Server/lib/container/GenericHandlerEvent.js.map +1 -1
- package/dist/src/Server/lib/container/HealthHandler.d.ts +6 -0
- package/dist/src/Server/lib/container/HealthHandler.js +6 -0
- package/dist/src/Server/lib/container/HealthHandler.js.map +1 -1
- package/dist/src/Server/lib/container/Proxy.d.ts +24 -52
- package/dist/src/Server/lib/container/Proxy.js +49 -50
- package/dist/src/Server/lib/container/Proxy.js.map +1 -1
- package/dist/src/Server/lib/container/Utils.d.ts +6 -10
- package/dist/src/Server/lib/container/Utils.js +6 -10
- package/dist/src/Server/lib/container/Utils.js.map +1 -1
- package/dist/src/Validation/Validator.d.ts +9 -13
- package/dist/src/Validation/Validator.js +8 -12
- package/dist/src/Validation/Validator.js.map +1 -1
- package/index.ts +15 -0
- package/package.json +6 -1
- package/src/API/Request.ts +66 -84
- package/src/API/Response.ts +144 -203
- package/src/API/Utils.ts +28 -44
- package/src/BaseEvent/EventProcessor.ts +52 -77
- package/src/BaseEvent/Process.ts +27 -52
- package/src/BaseEvent/Transaction.ts +100 -10
- package/src/Config/Configuration.ts +59 -76
- package/src/Config/EnvironmentVar.ts +39 -62
- package/src/Crypto/Crypto.ts +20 -36
- package/src/Crypto/JWT.ts +31 -35
- package/src/Globals.ts +28 -96
- package/src/Logger/Logger.ts +141 -160
- package/src/Mailer/Mailer.ts +43 -76
- package/src/Publisher/Publisher.ts +31 -40
- package/src/Server/RouteResolver.ts +31 -52
- package/src/Server/Router.ts +75 -54
- package/src/Server/lib/ContainerServer.ts +20 -32
- package/src/Server/lib/Server.ts +19 -34
- package/src/Server/lib/container/GenericHandler.ts +17 -3
- package/src/Server/lib/container/GenericHandlerEvent.ts +44 -54
- package/src/Server/lib/container/HealthHandler.ts +6 -0
- package/src/Server/lib/container/Proxy.ts +37 -57
- package/src/Server/lib/container/Utils.ts +7 -10
- package/src/Validation/Validator.ts +11 -13
- package/tests/API/Response.test.ts +55 -56
- package/tests/BaseEvent/EventProcessor.test.ts +49 -50
- package/tests/BaseEvent/Process.test.ts +2 -2
- package/tests/BaseEvent/Transaction.test.ts +48 -49
- package/tests/Config/Config.test.ts +27 -27
- package/tests/Config/EnvironmentVar.test.ts +54 -18
- package/tests/Publisher/Publisher.test.ts +3 -3
- package/tests/Server/lib/ContainerServer.test.ts +21 -22
- package/tests/Server/lib/container/GenericHandler.test.ts +31 -32
- package/tests/Server/lib/container/GenericHandlerEvent.test.ts +2 -2
- package/tests/Server/lib/container/HealthHandler.test.ts +6 -7
- package/tests/Server/lib/container/Proxy.test.ts +37 -35
- package/tsconfig.json +1 -1
package/src/Mailer/Mailer.ts
CHANGED
|
@@ -4,48 +4,30 @@ import * as Email from 'email-templates'
|
|
|
4
4
|
import * as nodemailer from 'nodemailer'
|
|
5
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
|
-
*/
|
|
14
7
|
export default class Mailer {
|
|
15
8
|
/**
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
* @private
|
|
19
|
-
* @readonly
|
|
9
|
+
* The starting point of a range.
|
|
20
10
|
* @type {string}
|
|
21
11
|
*/
|
|
22
12
|
private readonly from: string
|
|
23
13
|
/**
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
* @private
|
|
27
|
-
* @readonly
|
|
14
|
+
* The default file template for the application.
|
|
28
15
|
* @type {string}
|
|
29
16
|
*/
|
|
30
17
|
private readonly templateDefaultFile: string = 'html'
|
|
31
18
|
/**
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
* @private
|
|
35
|
-
* @readonly
|
|
36
|
-
* @type {ReturnType<
|
|
37
|
-
* typeof nodemailer.createTransport<SESTransport.SentMessageInfo>
|
|
38
|
-
* >}
|
|
19
|
+
* The transporter object used for sending emails using the AWS SES service.
|
|
20
|
+
* @type {ReturnType<typeof nodemailer.createTransport<SESTransport.SentMessageInfo>>}
|
|
39
21
|
*/
|
|
40
22
|
private readonly transporter: ReturnType<
|
|
41
23
|
typeof nodemailer.createTransport<SESTransport.SentMessageInfo>
|
|
42
24
|
>
|
|
25
|
+
|
|
43
26
|
/**
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
* @
|
|
47
|
-
* @
|
|
48
|
-
* @param {string} region
|
|
27
|
+
* Constructs a new instance of the EmailSender class.
|
|
28
|
+
* @param {string} defaultFrom - The default "from" email address.
|
|
29
|
+
* @param {string} region - The AWS region to use for sending emails.
|
|
30
|
+
* @returns None
|
|
49
31
|
*/
|
|
50
32
|
constructor(defaultFrom: string, region: string) {
|
|
51
33
|
this.from = defaultFrom
|
|
@@ -62,19 +44,14 @@ export default class Mailer {
|
|
|
62
44
|
}
|
|
63
45
|
|
|
64
46
|
/**
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
* @
|
|
68
|
-
* @
|
|
69
|
-
* @param {
|
|
70
|
-
* @param {string}
|
|
71
|
-
* @param {string}
|
|
72
|
-
* @param {
|
|
73
|
-
* @param {?string} [optionalFrom]
|
|
74
|
-
* @param {?string} [optionalReplyTo]
|
|
75
|
-
* @param {?any[]} [optionalAttachments]
|
|
76
|
-
* @param {?Email.NodeMailerTransportOptions} [optionalTransport]
|
|
77
|
-
* @returns {unknown}
|
|
47
|
+
* Sends a raw email with the specified parameters.
|
|
48
|
+
* @param {string | Array<string>} to - The recipient(s) of the email.
|
|
49
|
+
* @param {string} htmlMessage - The HTML content of the email.
|
|
50
|
+
* @param {string} subject - The subject of the email.
|
|
51
|
+
* @param {string | Array<string>} [optionalCC] - The optional CC recipient(s) of the email.
|
|
52
|
+
* @param {string} [optionalFrom] - The optional sender of the email. If not provided, the default sender will be used.
|
|
53
|
+
* @param {string} [optionalReplyTo] - The optional reply-to address for the email.
|
|
54
|
+
* @param {any[]} [optionalAttachments] - The optional attachments to include
|
|
78
55
|
*/
|
|
79
56
|
public async sendRawEmail(
|
|
80
57
|
to: string | Array<string>,
|
|
@@ -112,20 +89,16 @@ export default class Mailer {
|
|
|
112
89
|
}
|
|
113
90
|
return resp
|
|
114
91
|
}
|
|
92
|
+
|
|
115
93
|
/**
|
|
116
|
-
*
|
|
117
|
-
*
|
|
118
|
-
* @
|
|
119
|
-
* @
|
|
120
|
-
* @param {
|
|
121
|
-
* @param {
|
|
122
|
-
* @param {
|
|
123
|
-
* @param {
|
|
124
|
-
* @param {?string} [optionalFrom]
|
|
125
|
-
* @param {?string} [optionalReplyTo]
|
|
126
|
-
* @param {?any[]} [optionalAttachments]
|
|
127
|
-
* @param {?Email.NodeMailerTransportOptions} [optionalTransport]
|
|
128
|
-
* @returns {unknown}
|
|
94
|
+
* Sends a templated email to the specified recipients.
|
|
95
|
+
* @param {string | Array<string>} to - The email address(es) of the recipient(s).
|
|
96
|
+
* @param {string | Array<string>} templates - The template(s) to use for the email.
|
|
97
|
+
* @param {object} data - The data to be used in the email template.
|
|
98
|
+
* @param {string | Array<string>} [optionalCC] - The email address(es) to CC.
|
|
99
|
+
* @param {string} [optionalFrom] - The email address to send the email from.
|
|
100
|
+
* @param {string} [optionalReplyTo] - The email address to set as the reply-to address.
|
|
101
|
+
* @param {any[]} [optionalAttachments] - An array
|
|
129
102
|
*/
|
|
130
103
|
public async sendTemplatedEmail(
|
|
131
104
|
to: string | Array<string>,
|
|
@@ -162,15 +135,14 @@ export default class Mailer {
|
|
|
162
135
|
}
|
|
163
136
|
return resp
|
|
164
137
|
}
|
|
138
|
+
|
|
165
139
|
/**
|
|
166
|
-
*
|
|
167
|
-
*
|
|
168
|
-
* @
|
|
169
|
-
* @param {string}
|
|
170
|
-
* @param {
|
|
171
|
-
* @
|
|
172
|
-
* @param {string} password
|
|
173
|
-
* @returns {Email.NodeMailerTransportOptions}
|
|
140
|
+
* Creates a new SMTP transporter for sending emails using NodeMailer.
|
|
141
|
+
* @param {string} host - The SMTP server host.
|
|
142
|
+
* @param {number} portNumber - The port number to connect to the SMTP server.
|
|
143
|
+
* @param {string} user - The username for authentication with the SMTP server.
|
|
144
|
+
* @param {string} password - The password for authentication with the SMTP server.
|
|
145
|
+
* @returns {Email.NodeMailerTransportOptions} - The SMTP transporter object.
|
|
174
146
|
*/
|
|
175
147
|
public newSMTPTransporter(
|
|
176
148
|
host: string,
|
|
@@ -190,15 +162,12 @@ export default class Mailer {
|
|
|
190
162
|
return smtpTransporter
|
|
191
163
|
}
|
|
192
164
|
|
|
193
|
-
/* private */
|
|
194
165
|
/**
|
|
195
|
-
*
|
|
196
|
-
*
|
|
197
|
-
* @
|
|
198
|
-
* @
|
|
199
|
-
* @
|
|
200
|
-
* @param {object} data
|
|
201
|
-
* @returns {Promise<string>}
|
|
166
|
+
* 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.
|
|
167
|
+
* @param {string | Array<string>} templates - The template(s) to choose from.
|
|
168
|
+
* @param {object} data - The data to be used for rendering the template.
|
|
169
|
+
* @returns {Promise<string>} - The chosen template.
|
|
170
|
+
* @throws {Error} - If no template can be rendered with the provided data.
|
|
202
171
|
*/
|
|
203
172
|
private async chooseTemplate(templates: string | Array<string>, data: object): Promise<string> {
|
|
204
173
|
if (Array.isArray(templates)) {
|
|
@@ -214,14 +183,12 @@ export default class Mailer {
|
|
|
214
183
|
JSON.stringify(data, null, 2)
|
|
215
184
|
)
|
|
216
185
|
}
|
|
186
|
+
|
|
217
187
|
/**
|
|
218
|
-
*
|
|
219
|
-
*
|
|
220
|
-
* @
|
|
221
|
-
* @
|
|
222
|
-
* @param {string} template
|
|
223
|
-
* @param {object} data
|
|
224
|
-
* @returns {Promise<boolean>}
|
|
188
|
+
* Checks if a given email template can be rendered with the provided data.
|
|
189
|
+
* @param {string} template - The name of the email template.
|
|
190
|
+
* @param {object} data - The data to be used for rendering the template.
|
|
191
|
+
* @returns {Promise<boolean>} - A promise that resolves to true if the template can be rendered, false otherwise.
|
|
225
192
|
*/
|
|
226
193
|
private async canRenderTemplate(template: string, data: object): Promise<boolean> {
|
|
227
194
|
let validRenderResp = null
|
|
@@ -6,51 +6,43 @@ import {
|
|
|
6
6
|
} from '@aws-sdk/client-sns'
|
|
7
7
|
import * as sha1 from 'sha1'
|
|
8
8
|
//reusable client
|
|
9
|
-
// eslint-disable-next-line no-var
|
|
10
9
|
/**
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
* @type {(SNSClient | null)}
|
|
10
|
+
* A variable that holds the connection to the SNS client for publishing messages.
|
|
11
|
+
* @type {SNSClient | null}
|
|
14
12
|
*/
|
|
15
|
-
let PUBLISHER_CONN: SNSClient | null = null
|
|
16
13
|
// eslint-disable-next-line no-var
|
|
14
|
+
let PUBLISHER_CONN: SNSClient | null = null
|
|
17
15
|
/**
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
* @type {(string | null)}
|
|
16
|
+
* The connection hash for the publisher. It is initially set to null.
|
|
17
|
+
* @type {string | null}
|
|
21
18
|
*/
|
|
19
|
+
// eslint-disable-next-line no-var
|
|
22
20
|
let PUBLISHER_CONN_HASH: string | null = null
|
|
23
|
-
|
|
21
|
+
|
|
24
22
|
/**
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
* @
|
|
28
|
-
* @typedef {PublisherConfig}
|
|
23
|
+
* Represents the configuration options for a publisher.
|
|
24
|
+
* @typedef {Object} PublisherConfig
|
|
25
|
+
* @property {string} [region] - The region where the publisher is located.
|
|
29
26
|
*/
|
|
30
27
|
export type PublisherConfig = {
|
|
31
28
|
region?: string
|
|
32
29
|
}
|
|
33
|
-
|
|
30
|
+
|
|
34
31
|
/**
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
* @export
|
|
38
|
-
* @class Publisher
|
|
39
|
-
* @typedef {Publisher}
|
|
32
|
+
* Represents a publisher that can publish messages to an SNS topic.
|
|
40
33
|
*/
|
|
41
34
|
export default class Publisher {
|
|
42
35
|
/**
|
|
43
|
-
*
|
|
44
|
-
*
|
|
36
|
+
* The region of the object or entity.
|
|
45
37
|
* @private
|
|
46
38
|
* @type {string}
|
|
47
39
|
*/
|
|
48
40
|
private region: string
|
|
41
|
+
|
|
49
42
|
/**
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
* @
|
|
53
|
-
* @param {?PublisherConfig} [config]
|
|
43
|
+
* Constructs a new instance of the Publisher class.
|
|
44
|
+
* @param {PublisherConfig} [config] - The configuration object for the Publisher.
|
|
45
|
+
* @returns None
|
|
54
46
|
*/
|
|
55
47
|
constructor(config?: PublisherConfig) {
|
|
56
48
|
if (config && config.region) {
|
|
@@ -58,42 +50,41 @@ export default class Publisher {
|
|
|
58
50
|
// console.debug(`Using region: ${this.region}`);
|
|
59
51
|
}
|
|
60
52
|
}
|
|
61
|
-
|
|
53
|
+
|
|
62
54
|
/**
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
-
* @
|
|
66
|
-
* @param {
|
|
67
|
-
* @
|
|
68
|
-
* @param {?object} [additionalProps]
|
|
69
|
-
* @returns {Promise<PublishCommandOutput>}
|
|
55
|
+
* Publishes a message on a specified topic.
|
|
56
|
+
* @param {any} messageObject - The message object to be published.
|
|
57
|
+
* @param {string} topic - The ARN of the topic to publish the message to.
|
|
58
|
+
* @param {object} [additionalProps] - Additional properties to include in the publish command.
|
|
59
|
+
* @returns {Promise<PublishCommandOutput>} - A promise that resolves to the response from the publish command.
|
|
70
60
|
*/
|
|
71
61
|
async publishOnTopic(
|
|
72
62
|
messageObject: any,
|
|
73
63
|
topic: string,
|
|
74
64
|
additionalProps?: object
|
|
75
|
-
): Promise<PublishCommandOutput> {
|
|
76
|
-
let resp:
|
|
65
|
+
): Promise<PublishCommandOutput | undefined> {
|
|
66
|
+
let resp: undefined | PublishCommandOutput = undefined
|
|
77
67
|
try {
|
|
78
|
-
this.
|
|
68
|
+
this.connect()
|
|
79
69
|
//Send to SNS
|
|
80
70
|
const params: PublishCommandInput = {
|
|
81
71
|
Message: JSON.stringify(messageObject),
|
|
82
72
|
TopicArn: topic,
|
|
83
73
|
...(additionalProps ? additionalProps : {}),
|
|
84
74
|
}
|
|
85
|
-
resp = await PUBLISHER_CONN
|
|
75
|
+
resp = await PUBLISHER_CONN?.send(new PublishCommand(params))
|
|
86
76
|
} catch (e) {
|
|
87
77
|
console.error(`Error while publishing into topic ${topic} with error: ${e}`)
|
|
88
78
|
}
|
|
89
79
|
console.debug('Publisher resp', resp)
|
|
90
80
|
return resp
|
|
91
81
|
}
|
|
92
|
-
|
|
82
|
+
|
|
93
83
|
/**
|
|
94
|
-
*
|
|
84
|
+
* Establishes a connection to the SNS client if it does not already exist or if the region has changed.
|
|
85
|
+
* @returns None
|
|
95
86
|
*/
|
|
96
|
-
|
|
87
|
+
private connect(): void {
|
|
97
88
|
if ((!PUBLISHER_CONN && !PUBLISHER_CONN_HASH) || PUBLISHER_CONN_HASH != sha1(this.region)) {
|
|
98
89
|
PUBLISHER_CONN = new SNSClient({
|
|
99
90
|
apiVersion: '2010-03-31',
|
|
@@ -1,37 +1,28 @@
|
|
|
1
1
|
import { Route, RouterConfig } from './Router'
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
* @class Routes
|
|
7
|
-
* @typedef {Routes}
|
|
4
|
+
* Represents a collection of routes that can be added and resolved.
|
|
8
5
|
*/
|
|
9
6
|
class Routes {
|
|
10
7
|
/**
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
* @private
|
|
14
|
-
* @type {?Route}
|
|
8
|
+
* Represents the current route.
|
|
9
|
+
* @type {Route | undefined}
|
|
15
10
|
*/
|
|
16
11
|
private current?: Route
|
|
17
12
|
/**
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
* @private
|
|
21
|
-
* @type {?Routes}
|
|
13
|
+
* Optional private variable that represents a collection of routes.
|
|
14
|
+
* @type {Routes | undefined}
|
|
22
15
|
*/
|
|
23
16
|
private variable?: Routes
|
|
24
17
|
/**
|
|
25
|
-
*
|
|
26
|
-
*
|
|
18
|
+
* An object that maps string keys to Routes.
|
|
27
19
|
* @private
|
|
28
20
|
* @readonly
|
|
29
|
-
* @type {
|
|
21
|
+
* @type {Object.<string, Routes>}
|
|
30
22
|
*/
|
|
31
23
|
private readonly next: { [k: string]: Routes }
|
|
32
24
|
/**
|
|
33
|
-
*
|
|
34
|
-
*
|
|
25
|
+
* Initializes a new instance of the class.
|
|
35
26
|
* @constructor
|
|
36
27
|
*/
|
|
37
28
|
constructor() {
|
|
@@ -39,11 +30,11 @@ class Routes {
|
|
|
39
30
|
}
|
|
40
31
|
|
|
41
32
|
/**
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
* @
|
|
45
|
-
* @
|
|
46
|
-
* @
|
|
33
|
+
* Adds a route to the routing system.
|
|
34
|
+
* @param {Route} route - The route to add.
|
|
35
|
+
* @param {string[]} parts - The parts of the route path.
|
|
36
|
+
* @returns None
|
|
37
|
+
* @throws {Error} If a duplicate route is detected.
|
|
47
38
|
*/
|
|
48
39
|
public addRoute(route: Route, parts: string[]): void {
|
|
49
40
|
const first = parts.shift()
|
|
@@ -63,13 +54,11 @@ class Routes {
|
|
|
63
54
|
}
|
|
64
55
|
|
|
65
56
|
/**
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
* @
|
|
69
|
-
* @param {string[]} parts
|
|
70
|
-
* @returns {Route}
|
|
57
|
+
* Resolves a route based on the given parts.
|
|
58
|
+
* @param {string[]} parts - An array of route parts.
|
|
59
|
+
* @returns {Route} The resolved route.
|
|
71
60
|
*/
|
|
72
|
-
public resolveRoute(parts: string[]): Route {
|
|
61
|
+
public resolveRoute(parts: string[]): Route<any, any> | undefined {
|
|
73
62
|
const first = parts.shift()
|
|
74
63
|
if (first) {
|
|
75
64
|
if (first in this.next) {
|
|
@@ -82,26 +71,21 @@ class Routes {
|
|
|
82
71
|
}
|
|
83
72
|
|
|
84
73
|
/**
|
|
85
|
-
*
|
|
86
|
-
*
|
|
87
|
-
* @export
|
|
74
|
+
* A class that resolves routes based on the provided configuration.
|
|
88
75
|
* @class RouteResolver
|
|
89
|
-
* @typedef {RouteResolver}
|
|
90
76
|
*/
|
|
91
77
|
export default class RouteResolver {
|
|
92
78
|
/**
|
|
93
|
-
*
|
|
94
|
-
*
|
|
95
|
-
* @
|
|
96
|
-
* @type {{ [method: string]: Routes }\}
|
|
79
|
+
* An object that maps HTTP methods to their corresponding routes.
|
|
80
|
+
* @property {object} routes - The routes object.
|
|
81
|
+
* @property {Routes} routes.method - The routes for the specified HTTP method.
|
|
97
82
|
*/
|
|
98
83
|
private routes: { [method: string]: Routes }
|
|
99
84
|
|
|
100
85
|
/**
|
|
101
|
-
*
|
|
102
|
-
*
|
|
103
|
-
* @
|
|
104
|
-
* @param {RouterConfig} config
|
|
86
|
+
* Constructs a new instance of the Router class with the given configuration.
|
|
87
|
+
* @param {RouterConfig} config - The configuration object for the router.
|
|
88
|
+
* @returns None
|
|
105
89
|
*/
|
|
106
90
|
constructor(readonly config: RouterConfig) {
|
|
107
91
|
this.routes = {}
|
|
@@ -109,31 +93,26 @@ export default class RouteResolver {
|
|
|
109
93
|
}
|
|
110
94
|
|
|
111
95
|
/**
|
|
112
|
-
*
|
|
113
|
-
*
|
|
114
|
-
* @
|
|
115
|
-
* @
|
|
116
|
-
* @param {string} path
|
|
117
|
-
* @returns {(Route | undefined)}
|
|
96
|
+
* Resolves a route based on the given HTTP method and path.
|
|
97
|
+
* @param {string} method - The HTTP method of the request.
|
|
98
|
+
* @param {string} path - The path of the request.
|
|
99
|
+
* @returns {Route | undefined} - The resolved route or undefined if no route is found.
|
|
118
100
|
*/
|
|
119
101
|
public resolveRoute(method: string, path: string): Route | undefined {
|
|
120
102
|
method = method.toLowerCase()
|
|
121
103
|
const parts = path.split('/').filter(p => p.length)
|
|
122
|
-
|
|
123
104
|
return this.routes[method]?.resolveRoute(parts)
|
|
124
105
|
}
|
|
125
106
|
|
|
126
107
|
/**
|
|
127
|
-
*
|
|
128
|
-
*
|
|
129
|
-
* @
|
|
130
|
-
* @param {RouterConfig} config
|
|
108
|
+
* Builds the routes for the router based on the given configuration.
|
|
109
|
+
* @param {RouterConfig} config - The router configuration object.
|
|
110
|
+
* @returns None
|
|
131
111
|
*/
|
|
132
112
|
private buildRoutes(config: RouterConfig): void {
|
|
133
113
|
for (const route of config.routes) {
|
|
134
114
|
const method = route.method.toLowerCase()
|
|
135
115
|
const parts = route.path.split('/').filter(p => p.length)
|
|
136
|
-
|
|
137
116
|
this.routes[method] = this.routes[method] || new Routes()
|
|
138
117
|
this.routes[method].addRoute(route, parts)
|
|
139
118
|
}
|
package/src/Server/Router.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import { Server as HTTPServer } from 'http'
|
|
2
|
+
|
|
3
|
+
import * as express from 'express'
|
|
1
4
|
import { z } from 'zod'
|
|
2
5
|
|
|
3
6
|
import ContainerServer from './lib/ContainerServer'
|
|
@@ -6,26 +9,24 @@ import { ResponseErrorType } from '../API/Response'
|
|
|
6
9
|
import Utils from '../API/Utils'
|
|
7
10
|
import Transaction, { TransactionConfig, TransactionExecution } from '../BaseEvent/Transaction'
|
|
8
11
|
|
|
9
|
-
// Route
|
|
10
12
|
/**
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
* @
|
|
14
|
-
* @
|
|
15
|
-
* @
|
|
16
|
-
* @
|
|
17
|
-
* @
|
|
13
|
+
* Represents a route in an API.
|
|
14
|
+
* @template InputType - The type of the input data for the route.
|
|
15
|
+
* @template OutputType - The type of the output data for the route.
|
|
16
|
+
* @property {string} path - The path of the route.
|
|
17
|
+
* @property {string} method - The HTTP method of the route.
|
|
18
|
+
* @property {TransactionExecution<Transaction<InputType, OutputType | ResponseErrorType>, OutputType | ResponseErrorType>} handler - The handler function for the route.
|
|
19
|
+
* @property {z.ZodObject<any>} [inputSchema] - The input schema for validating the input data.
|
|
18
20
|
*/
|
|
19
21
|
export interface Route<InputType = any, OutputType = any> {
|
|
20
22
|
/**
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
* @
|
|
23
|
+
* Represents a file path as a string.
|
|
24
|
+
* @param {string} path - The file path.
|
|
25
|
+
* @returns None
|
|
24
26
|
*/
|
|
25
27
|
path: string
|
|
26
28
|
/**
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
+
* Represents the method used in an API request.
|
|
29
30
|
* @type {string}
|
|
30
31
|
*/
|
|
31
32
|
method: string
|
|
@@ -36,93 +37,113 @@ export interface Route<InputType = any, OutputType = any> {
|
|
|
36
37
|
- https://github.com/microsoft/TypeScript/issues/10570
|
|
37
38
|
*/
|
|
38
39
|
/**
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
* @type
|
|
42
|
-
* Transaction<InputType, OutputType | ResponseErrorType>,
|
|
43
|
-
* OutputType | ResponseErrorType
|
|
44
|
-
* >}
|
|
40
|
+
* Represents a handler for executing a transaction with the given input type and output type.
|
|
41
|
+
* @param {Transaction<InputType, OutputType | ResponseErrorType>} transaction - The transaction to execute.
|
|
42
|
+
* @param {OutputType | ResponseErrorType} - The output type or response error type of the transaction.
|
|
45
43
|
*/
|
|
46
44
|
handler: TransactionExecution<
|
|
47
45
|
Transaction<InputType, OutputType | ResponseErrorType>,
|
|
48
46
|
OutputType | ResponseErrorType
|
|
49
47
|
>
|
|
50
48
|
/**
|
|
49
|
+
<<<<<<< HEAD
|
|
50
|
+
* An optional input schema for validating the structure of the input data.
|
|
51
|
+
* @type {z.ZodObject<any>}
|
|
52
|
+
=======
|
|
51
53
|
* Description placeholder
|
|
52
54
|
*
|
|
53
|
-
* @type {?z.ZodObject<any>}
|
|
55
|
+
* @type {?z.ZodObject<any> | z.ZodUnion<any>}
|
|
56
|
+
>>>>>>> 65ffb67 (Improve syntax with tighther eslint, fix lots of test errors, new proxy containerSetupHook and improved validation type)
|
|
54
57
|
*/
|
|
55
|
-
inputSchema?: z.ZodObject<any>
|
|
58
|
+
inputSchema?: z.ZodObject<any> | z.ZodUnion<any>
|
|
56
59
|
}
|
|
57
60
|
|
|
58
|
-
// Config
|
|
59
61
|
/**
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
*
|
|
63
|
-
*
|
|
62
|
+
* Represents the configuration options for a router.
|
|
63
|
+
* @typedef {TransactionConfig & {
|
|
64
|
+
* routes: Route[]
|
|
65
|
+
* port?: number
|
|
66
|
+
* timeout?: number
|
|
67
|
+
* cors?: {
|
|
68
|
+
* origin?: string | string[]
|
|
69
|
+
* headers?: string[]
|
|
70
|
+
* allowCredentials?: boolean
|
|
71
|
+
* }
|
|
72
|
+
* healthCheckRoute?: string
|
|
73
|
+
* }} RouterConfig
|
|
74
|
+
* @property {Route[]} routes - The routes to be configured in the router.
|
|
75
|
+
* @property {number} [port] - The port number to listen on. If not specified, a default port will be used.
|
|
76
|
+
* @property {number} [timeout] - The timeout duration for requests in milliseconds. If not specified, a default timeout will be
|
|
64
77
|
*/
|
|
65
78
|
export type RouterConfig = TransactionConfig & {
|
|
79
|
+
/**
|
|
80
|
+
* An array of route objects representing the available routes in the application.
|
|
81
|
+
* @type {Route[]}
|
|
82
|
+
*/
|
|
66
83
|
routes: Route[]
|
|
67
|
-
|
|
84
|
+
/**
|
|
85
|
+
* The port number for the server to listen on.
|
|
86
|
+
* @type {number | undefined}
|
|
87
|
+
*/
|
|
68
88
|
port?: number
|
|
89
|
+
/**
|
|
90
|
+
* Optional timeout value in milliseconds.
|
|
91
|
+
* @type {number | undefined}
|
|
92
|
+
*/
|
|
69
93
|
timeout?: number
|
|
94
|
+
/**
|
|
95
|
+
* Configuration options for Cross-Origin Resource Sharing (CORS).
|
|
96
|
+
* @property {string | string[]} [origin] - The allowed origin(s) for CORS requests.
|
|
97
|
+
* @property {string[]} [headers] - The allowed headers for CORS requests.
|
|
98
|
+
* @property {boolean} [allowCredentials] - Whether to allow credentials (cookies, HTTP authentication, and client-side SSL certificates) to be sent in CORS requests.
|
|
99
|
+
*/
|
|
70
100
|
cors?: {
|
|
71
101
|
origin?: string | string[]
|
|
72
102
|
headers?: string[]
|
|
73
103
|
allowCredentials?: boolean
|
|
74
104
|
}
|
|
105
|
+
/**
|
|
106
|
+
* The route for the health check endpoint.
|
|
107
|
+
* @type {string | undefined}
|
|
108
|
+
*/
|
|
75
109
|
healthCheckRoute?: string
|
|
110
|
+
containerSetupHook?: (server: HTTPServer, app: express.Express) => Promise<void>
|
|
76
111
|
}
|
|
77
112
|
|
|
78
113
|
/**
|
|
79
|
-
*
|
|
80
|
-
*
|
|
81
|
-
* @export
|
|
82
|
-
* @class Router
|
|
83
|
-
* @typedef {Router}
|
|
114
|
+
* Represents a router that handles routing logic for a web application.
|
|
84
115
|
*/
|
|
85
116
|
export default class Router {
|
|
86
117
|
/**
|
|
87
|
-
*
|
|
88
|
-
*
|
|
89
|
-
* @private
|
|
90
|
-
* @readonly
|
|
91
|
-
* @type {RouterConfig}
|
|
118
|
+
* The configuration object for the router.
|
|
92
119
|
*/
|
|
93
120
|
private readonly config: RouterConfig
|
|
94
121
|
/**
|
|
95
|
-
*
|
|
96
|
-
*
|
|
97
|
-
* @private
|
|
98
|
-
* @readonly
|
|
99
|
-
* @type {Server}
|
|
122
|
+
* The private readonly server instance.
|
|
100
123
|
*/
|
|
101
124
|
private readonly server: Server
|
|
125
|
+
|
|
102
126
|
/**
|
|
103
|
-
*
|
|
104
|
-
*
|
|
105
|
-
* @
|
|
106
|
-
* @param {RouterConfig} config
|
|
127
|
+
* Constructs a new instance of the Router class.
|
|
128
|
+
* @param {RouterConfig} config - The configuration object for the router.
|
|
129
|
+
* @returns None
|
|
107
130
|
*/
|
|
108
131
|
constructor(config: RouterConfig) {
|
|
109
132
|
this.config = config
|
|
110
133
|
this.server = this.isContainer() ? new ContainerServer(config) : new Server(config)
|
|
111
134
|
}
|
|
135
|
+
|
|
112
136
|
/**
|
|
113
|
-
*
|
|
114
|
-
*
|
|
115
|
-
* @public
|
|
116
|
-
* @returns {CallableFunction}
|
|
137
|
+
* Retrieves the export function from the server.
|
|
138
|
+
* @returns {CallableFunction} The export function from the server.
|
|
117
139
|
*/
|
|
118
140
|
public getExport(): CallableFunction {
|
|
119
141
|
return this.server.getExport()
|
|
120
142
|
}
|
|
143
|
+
|
|
121
144
|
/**
|
|
122
|
-
*
|
|
123
|
-
*
|
|
124
|
-
* @private
|
|
125
|
-
* @returns {boolean}
|
|
145
|
+
* Checks if the current element is a container.
|
|
146
|
+
* @returns {boolean} - True if the element is a container, false otherwise.
|
|
126
147
|
*/
|
|
127
148
|
private isContainer(): boolean {
|
|
128
149
|
return Utils.isHybridlessContainer()
|