@creator.co/wapi 1.2.4 → 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/.github/workflows/npmpublish.yml +1 -1
- package/README.md +216 -5
- package/dist/index.d.ts +15 -0
- package/dist/index.js.map +1 -1
- package/dist/jest.config.js +1 -1
- package/dist/jest.config.js.map +1 -1
- package/dist/package.json +13 -2
- 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 +104 -2
- package/dist/src/BaseEvent/Transaction.js +196 -41
- 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/Database/Database.d.ts +18 -0
- package/dist/src/Database/Database.js +18 -0
- package/dist/src/Database/Database.js.map +1 -0
- package/dist/src/Database/DatabaseManager.d.ts +32 -0
- package/dist/src/Database/DatabaseManager.js +50 -0
- package/dist/src/Database/DatabaseManager.js.map +1 -0
- package/dist/src/Database/DatabaseTransaction.d.ts +65 -0
- package/dist/src/Database/DatabaseTransaction.js +183 -0
- package/dist/src/Database/DatabaseTransaction.js.map +1 -0
- package/dist/src/Database/integrations/knex/KnexDatabase.d.ts +22 -0
- package/dist/src/Database/integrations/knex/KnexDatabase.js +108 -0
- package/dist/src/Database/integrations/knex/KnexDatabase.js.map +1 -0
- package/dist/src/Database/integrations/knex/KnexTransaction.d.ts +37 -0
- package/dist/src/Database/integrations/knex/KnexTransaction.js +60 -0
- package/dist/src/Database/integrations/knex/KnexTransaction.js.map +1 -0
- package/dist/src/Database/integrations/pgsql/PostgresDatabase.d.ts +30 -0
- package/dist/src/Database/integrations/pgsql/PostgresDatabase.js +108 -0
- package/dist/src/Database/integrations/pgsql/PostgresDatabase.js.map +1 -0
- package/dist/src/Database/integrations/pgsql/PostgresTransaction.d.ts +37 -0
- package/dist/src/Database/integrations/pgsql/PostgresTransaction.js +60 -0
- package/dist/src/Database/integrations/pgsql/PostgresTransaction.js.map +1 -0
- 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 +52 -52
- 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/jest.config.ts +1 -1
- package/package.json +13 -2
- 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 +147 -27
- 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/Database/Database.ts +19 -0
- package/src/Database/DatabaseManager.ts +51 -0
- package/src/Database/DatabaseTransaction.ts +118 -0
- package/src/Database/integrations/knex/KnexDatabase.ts +47 -0
- package/src/Database/integrations/knex/KnexTransaction.ts +51 -0
- package/src/Database/integrations/pgsql/PostgresDatabase.ts +49 -0
- package/src/Database/integrations/pgsql/PostgresTransaction.ts +54 -0
- package/src/Database/types.d.ts +49 -0
- 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 +39 -58
- 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 +102 -44
- package/tests/Config/Config.test.ts +27 -27
- package/tests/Config/EnvironmentVar.test.ts +54 -18
- package/tests/Database/DatabaseManager.test.ts +55 -0
- package/tests/Database/integrations/knex/KnexDatabase.test.ts +53 -0
- package/tests/Database/integrations/knex/KnexTransaction.test.ts +133 -0
- package/tests/Database/integrations/pg/PostgresDatabase.test.ts +50 -0
- package/tests/Database/integrations/pg/PostgresTransaction.test.ts +51 -0
- 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 +6 -1
|
@@ -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()
|
|
@@ -3,38 +3,30 @@ import Server from './Server'
|
|
|
3
3
|
import { RouterConfig } from '../Router'
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
* @export
|
|
9
|
-
* @class ContainerServer
|
|
10
|
-
* @typedef {ContainerServer}
|
|
11
|
-
* @extends {Server}
|
|
6
|
+
* Represents a server container that extends the Server class and handles serverless events.
|
|
12
7
|
*/
|
|
13
8
|
export default class ContainerServer extends Server {
|
|
14
9
|
/**
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
* @protected
|
|
18
|
-
* @readonly
|
|
10
|
+
* The proxy object used for handling requests and responses.
|
|
19
11
|
* @type {Proxy}
|
|
12
|
+
* @protected
|
|
20
13
|
*/
|
|
21
14
|
protected readonly proxy: Proxy
|
|
15
|
+
|
|
22
16
|
/**
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
* @
|
|
26
|
-
* @param {RouterConfig} config
|
|
17
|
+
* Constructs a new instance of the Router class.
|
|
18
|
+
* @param {RouterConfig} config - The configuration object for the router.
|
|
19
|
+
* @returns None
|
|
27
20
|
*/
|
|
28
21
|
constructor(config: RouterConfig) {
|
|
29
22
|
super(config)
|
|
30
23
|
this.proxy = new Proxy(config, this.handleServerlessEvent.bind(this))
|
|
31
24
|
this.listenProcessEvents()
|
|
32
25
|
}
|
|
26
|
+
|
|
33
27
|
/**
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
* @public
|
|
37
|
-
* @returns {CallableFunction}
|
|
28
|
+
* Returns a callable function that can be used to export data.
|
|
29
|
+
* @returns {CallableFunction} - A callable function that can be used to export data.
|
|
38
30
|
*/
|
|
39
31
|
public getExport(): CallableFunction {
|
|
40
32
|
// start server socket
|
|
@@ -42,32 +34,28 @@ export default class ContainerServer extends Server {
|
|
|
42
34
|
// return empty function, we are all done
|
|
43
35
|
return () => {}
|
|
44
36
|
}
|
|
45
|
-
|
|
37
|
+
|
|
46
38
|
/**
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
* @public
|
|
50
|
-
* @async
|
|
51
|
-
* @returns {*}
|
|
39
|
+
* Starts the application by loading the proxy.
|
|
40
|
+
* @returns {Promise<void>} - A promise that resolves when the proxy is loaded.
|
|
52
41
|
*/
|
|
53
42
|
public async start() {
|
|
54
43
|
await this.proxy.load()
|
|
55
44
|
}
|
|
45
|
+
|
|
56
46
|
/**
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
* @
|
|
60
|
-
* @async
|
|
61
|
-
* @param {?*} [err]
|
|
62
|
-
* @returns {*}
|
|
47
|
+
* Stops the execution of the program and unloads the proxy.
|
|
48
|
+
* @param {any} [err] - Optional error object to pass to the unload method.
|
|
49
|
+
* @returns {Promise<void>} - A promise that resolves once the proxy is unloaded.
|
|
63
50
|
*/
|
|
64
51
|
public async stop(err?: any) {
|
|
65
52
|
await this.proxy.unload(err)
|
|
66
53
|
}
|
|
54
|
+
|
|
67
55
|
/**
|
|
68
|
-
*
|
|
69
|
-
*
|
|
56
|
+
* Listens for process events and handles them accordingly.
|
|
70
57
|
* @private
|
|
58
|
+
* @returns None
|
|
71
59
|
*/
|
|
72
60
|
private listenProcessEvents() {
|
|
73
61
|
// start process listeners
|
package/src/Server/lib/Server.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { APIGatewayProxyEvent, Context } from 'aws-lambda'
|
|
2
|
-
import
|
|
2
|
+
import { pathToRegexp } from 'path-to-regexp'
|
|
3
3
|
|
|
4
4
|
import Request from '../../API/Request'
|
|
5
5
|
import Response from '../../API/Response'
|
|
@@ -9,35 +9,26 @@ import { RouterConfig } from '../Router'
|
|
|
9
9
|
import RouteResolver from '../RouteResolver'
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
* @export
|
|
15
|
-
* @class Server
|
|
16
|
-
* @typedef {Server}
|
|
12
|
+
* Represents a server that handles serverless events and routes them to appropriate handlers.
|
|
17
13
|
*/
|
|
18
14
|
export default class Server {
|
|
19
15
|
/**
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
* @protected
|
|
16
|
+
* The configuration object for the router.
|
|
23
17
|
* @readonly
|
|
24
18
|
* @type {RouterConfig}
|
|
25
19
|
*/
|
|
26
20
|
protected readonly config: RouterConfig
|
|
27
21
|
/**
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
* @protected
|
|
31
|
-
* @readonly
|
|
22
|
+
* A protected property that holds a RouteResolver object.
|
|
23
|
+
* The RouteResolver is responsible for resolving routes and returning the appropriate response.
|
|
32
24
|
* @type {RouteResolver}
|
|
33
25
|
*/
|
|
34
26
|
protected readonly routeResolver: RouteResolver
|
|
35
27
|
|
|
36
28
|
/**
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
* @
|
|
40
|
-
* @param {RouterConfig} config
|
|
29
|
+
* Constructs a new instance of the Router class.
|
|
30
|
+
* @param {RouterConfig} config - The configuration object for the router.
|
|
31
|
+
* @returns None
|
|
41
32
|
*/
|
|
42
33
|
constructor(config: RouterConfig) {
|
|
43
34
|
this.config = config
|
|
@@ -45,23 +36,18 @@ export default class Server {
|
|
|
45
36
|
}
|
|
46
37
|
|
|
47
38
|
/**
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
* @public
|
|
51
|
-
* @returns {CallableFunction}
|
|
39
|
+
* Returns a callable function that is bound to the `handleServerlessEvent` method of the current object.
|
|
40
|
+
* @returns {CallableFunction} - A callable function that is bound to the `handleServerlessEvent` method.
|
|
52
41
|
*/
|
|
53
42
|
public getExport(): CallableFunction {
|
|
54
43
|
return this.handleServerlessEvent.bind(this)
|
|
55
44
|
}
|
|
56
45
|
|
|
57
46
|
/**
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
* @
|
|
61
|
-
* @
|
|
62
|
-
* @param {APIGatewayProxyEvent} event
|
|
63
|
-
* @param {Context} context
|
|
64
|
-
* @returns {*}
|
|
47
|
+
* Handles a serverless event by executing a transaction and resolving the route based on the event.
|
|
48
|
+
* @param {APIGatewayProxyEvent} event - The serverless event object.
|
|
49
|
+
* @param {Context} context - The serverless context object.
|
|
50
|
+
* @returns None
|
|
65
51
|
*/
|
|
66
52
|
public async handleServerlessEvent(event: APIGatewayProxyEvent, context: Context) {
|
|
67
53
|
// init transaction
|
|
@@ -86,16 +72,15 @@ export default class Server {
|
|
|
86
72
|
}
|
|
87
73
|
|
|
88
74
|
/**
|
|
89
|
-
*
|
|
90
|
-
*
|
|
91
|
-
* @
|
|
92
|
-
* @
|
|
93
|
-
* @param {string} routePath
|
|
75
|
+
* Parses the path parameters from the request URL based on the given route path.
|
|
76
|
+
* @param {Request<any>} req - The request object.
|
|
77
|
+
* @param {string} routePath - The route path pattern to match against.
|
|
78
|
+
* @returns None
|
|
94
79
|
*/
|
|
95
80
|
private parsePathParams(req: Request<any>, routePath: string) {
|
|
96
81
|
const path = req.getPath()
|
|
97
82
|
const keys = []
|
|
98
83
|
const result = pathToRegexp(routePath, keys).exec(path)
|
|
99
|
-
req.setFixedPathParams(keys, result)
|
|
84
|
+
if (result) req.setFixedPathParams(keys, result)
|
|
100
85
|
}
|
|
101
86
|
}
|