@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.
Files changed (115) hide show
  1. package/README.md +216 -5
  2. package/dist/index.d.ts +15 -0
  3. package/dist/index.js.map +1 -1
  4. package/dist/package.json +6 -1
  5. package/dist/src/API/Request.d.ts +45 -82
  6. package/dist/src/API/Request.js +49 -77
  7. package/dist/src/API/Request.js.map +1 -1
  8. package/dist/src/API/Response.d.ts +94 -163
  9. package/dist/src/API/Response.js +101 -161
  10. package/dist/src/API/Response.js.map +1 -1
  11. package/dist/src/API/Utils.d.ts +21 -42
  12. package/dist/src/API/Utils.js +22 -43
  13. package/dist/src/API/Utils.js.map +1 -1
  14. package/dist/src/BaseEvent/EventProcessor.d.ts +32 -55
  15. package/dist/src/BaseEvent/EventProcessor.js +30 -38
  16. package/dist/src/BaseEvent/EventProcessor.js.map +1 -1
  17. package/dist/src/BaseEvent/Process.d.ts +20 -43
  18. package/dist/src/BaseEvent/Process.js +16 -27
  19. package/dist/src/BaseEvent/Process.js.map +1 -1
  20. package/dist/src/BaseEvent/Transaction.d.ts +94 -2
  21. package/dist/src/BaseEvent/Transaction.js +49 -4
  22. package/dist/src/BaseEvent/Transaction.js.map +1 -1
  23. package/dist/src/Config/Configuration.d.ts +48 -66
  24. package/dist/src/Config/Configuration.js +25 -42
  25. package/dist/src/Config/Configuration.js.map +1 -1
  26. package/dist/src/Config/EnvironmentVar.d.ts +30 -57
  27. package/dist/src/Config/EnvironmentVar.js +28 -41
  28. package/dist/src/Config/EnvironmentVar.js.map +1 -1
  29. package/dist/src/Crypto/Crypto.d.ts +17 -35
  30. package/dist/src/Crypto/Crypto.js +12 -21
  31. package/dist/src/Crypto/Crypto.js.map +1 -1
  32. package/dist/src/Crypto/JWT.d.ts +21 -32
  33. package/dist/src/Crypto/JWT.js +14 -22
  34. package/dist/src/Crypto/JWT.js.map +1 -1
  35. package/dist/src/Globals.d.ts +26 -94
  36. package/dist/src/Globals.js +26 -95
  37. package/dist/src/Globals.js.map +1 -1
  38. package/dist/src/Logger/Logger.d.ts +82 -105
  39. package/dist/src/Logger/Logger.js +111 -136
  40. package/dist/src/Logger/Logger.js.map +1 -1
  41. package/dist/src/Mailer/Mailer.d.ts +39 -75
  42. package/dist/src/Mailer/Mailer.js +36 -65
  43. package/dist/src/Mailer/Mailer.js.map +1 -1
  44. package/dist/src/Publisher/Publisher.d.ts +17 -25
  45. package/dist/src/Publisher/Publisher.js +21 -32
  46. package/dist/src/Publisher/Publisher.js.map +1 -1
  47. package/dist/src/Server/RouteResolver.d.ts +14 -22
  48. package/dist/src/Server/RouteResolver.js +21 -34
  49. package/dist/src/Server/RouteResolver.js.map +1 -1
  50. package/dist/src/Server/Router.d.ts +72 -51
  51. package/dist/src/Server/Router.js +8 -17
  52. package/dist/src/Server/Router.js.map +1 -1
  53. package/dist/src/Server/lib/ContainerServer.d.ts +15 -31
  54. package/dist/src/Server/lib/ContainerServer.js +13 -28
  55. package/dist/src/Server/lib/ContainerServer.js.map +1 -1
  56. package/dist/src/Server/lib/Server.d.ts +17 -32
  57. package/dist/src/Server/lib/Server.js +18 -28
  58. package/dist/src/Server/lib/Server.js.map +1 -1
  59. package/dist/src/Server/lib/container/GenericHandler.d.ts +5 -0
  60. package/dist/src/Server/lib/container/GenericHandler.js +16 -3
  61. package/dist/src/Server/lib/container/GenericHandler.js.map +1 -1
  62. package/dist/src/Server/lib/container/GenericHandlerEvent.d.ts +22 -37
  63. package/dist/src/Server/lib/container/GenericHandlerEvent.js +29 -41
  64. package/dist/src/Server/lib/container/GenericHandlerEvent.js.map +1 -1
  65. package/dist/src/Server/lib/container/HealthHandler.d.ts +6 -0
  66. package/dist/src/Server/lib/container/HealthHandler.js +6 -0
  67. package/dist/src/Server/lib/container/HealthHandler.js.map +1 -1
  68. package/dist/src/Server/lib/container/Proxy.d.ts +24 -52
  69. package/dist/src/Server/lib/container/Proxy.js +49 -50
  70. package/dist/src/Server/lib/container/Proxy.js.map +1 -1
  71. package/dist/src/Server/lib/container/Utils.d.ts +6 -10
  72. package/dist/src/Server/lib/container/Utils.js +6 -10
  73. package/dist/src/Server/lib/container/Utils.js.map +1 -1
  74. package/dist/src/Validation/Validator.d.ts +9 -13
  75. package/dist/src/Validation/Validator.js +8 -12
  76. package/dist/src/Validation/Validator.js.map +1 -1
  77. package/index.ts +15 -0
  78. package/package.json +6 -1
  79. package/src/API/Request.ts +66 -84
  80. package/src/API/Response.ts +144 -203
  81. package/src/API/Utils.ts +28 -44
  82. package/src/BaseEvent/EventProcessor.ts +52 -77
  83. package/src/BaseEvent/Process.ts +27 -52
  84. package/src/BaseEvent/Transaction.ts +100 -10
  85. package/src/Config/Configuration.ts +59 -76
  86. package/src/Config/EnvironmentVar.ts +39 -62
  87. package/src/Crypto/Crypto.ts +20 -36
  88. package/src/Crypto/JWT.ts +31 -35
  89. package/src/Globals.ts +28 -96
  90. package/src/Logger/Logger.ts +141 -160
  91. package/src/Mailer/Mailer.ts +43 -76
  92. package/src/Publisher/Publisher.ts +31 -40
  93. package/src/Server/RouteResolver.ts +31 -52
  94. package/src/Server/Router.ts +75 -54
  95. package/src/Server/lib/ContainerServer.ts +20 -32
  96. package/src/Server/lib/Server.ts +19 -34
  97. package/src/Server/lib/container/GenericHandler.ts +17 -3
  98. package/src/Server/lib/container/GenericHandlerEvent.ts +44 -54
  99. package/src/Server/lib/container/HealthHandler.ts +6 -0
  100. package/src/Server/lib/container/Proxy.ts +37 -57
  101. package/src/Server/lib/container/Utils.ts +7 -10
  102. package/src/Validation/Validator.ts +11 -13
  103. package/tests/API/Response.test.ts +55 -56
  104. package/tests/BaseEvent/EventProcessor.test.ts +49 -50
  105. package/tests/BaseEvent/Process.test.ts +2 -2
  106. package/tests/BaseEvent/Transaction.test.ts +48 -49
  107. package/tests/Config/Config.test.ts +27 -27
  108. package/tests/Config/EnvironmentVar.test.ts +54 -18
  109. package/tests/Publisher/Publisher.test.ts +3 -3
  110. package/tests/Server/lib/ContainerServer.test.ts +21 -22
  111. package/tests/Server/lib/container/GenericHandler.test.ts +31 -32
  112. package/tests/Server/lib/container/GenericHandlerEvent.test.ts +2 -2
  113. package/tests/Server/lib/container/HealthHandler.test.ts +6 -7
  114. package/tests/Server/lib/container/Proxy.test.ts +37 -35
  115. package/tsconfig.json +1 -1
@@ -3,38 +3,30 @@ import Server from './Server'
3
3
  import { RouterConfig } from '../Router'
4
4
 
5
5
  /**
6
- * ${1:Description placeholder}
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
- * ${1:Description placeholder}
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
- * Creates an instance of ContainerServer.
24
- *
25
- * @constructor
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
- * ${1:Description placeholder}
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
- /* private */
37
+
46
38
  /**
47
- * ${1:Description placeholder}
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
- * ${1:Description placeholder}
58
- *
59
- * @public
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
- * ${1:Description placeholder}
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
@@ -1,5 +1,5 @@
1
1
  import { APIGatewayProxyEvent, Context } from 'aws-lambda'
2
- import * as pathToRegexp from 'path-to-regexp'
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
- * ${1:Description placeholder}
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
- * ${1:Description placeholder}
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
- * ${1:Description placeholder}
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
- * Creates an instance of Server.
38
- *
39
- * @constructor
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
- * ${1:Description placeholder}
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
- * ${1:Description placeholder}
59
- *
60
- * @public
61
- * @async
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
- * ${1:Description placeholder}
90
- *
91
- * @private
92
- * @param {Request<any>} req
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
  }
@@ -5,7 +5,18 @@ import Utils from '../../../API/Utils'
5
5
  import Globals from '../../../Globals'
6
6
  import Server from '../Server'
7
7
 
8
+ /**
9
+ * Creates an async function that handles serverless events and sends a response.
10
+ * @param {Server['handleServerlessEvent']} serverlessHandler - The serverless handler function.
11
+ * @returns {Function} - An async function that handles the request and sends a response.
12
+ */
8
13
  export default (serverlessHandler: Server['handleServerlessEvent']) => {
14
+ /**
15
+ * Handles an HTTP request by invoking a serverless function and processing the response.
16
+ * @param {Request} request - The HTTP request object.
17
+ * @param {Response} res - The HTTP response object.
18
+ * @returns None
19
+ */
9
20
  return async (request: Request, res: Response) => {
10
21
  const startTime = Date.now()
11
22
  try {
@@ -27,10 +38,13 @@ export default (serverlessHandler: Server['handleServerlessEvent']) => {
27
38
  console.debug(`[Proxy] - Request took ${Date.now() - startTime}ms`)
28
39
  }
29
40
  }
41
+
30
42
  /**
31
- * ${1:Description placeholder}
32
- *
33
- **/
43
+ * Processes the response received from a serverless function invocation and sends the appropriate response back to the client.
44
+ * @param {GenericHandlerEventResponse} invokation - The response object received from the serverless function invocation.
45
+ * @param {Response} res - The response object to send back to the client.
46
+ * @returns The response object to send back to the client.
47
+ */
34
48
  const processServerlessResponse = (invokation: GenericHandlerEventResponse, res: Response) => {
35
49
  // translate answer to http layer
36
50
  if (invokation && invokation.err) {
@@ -5,54 +5,45 @@ import { Request } from 'express'
5
5
  import { parseMultiValueQueryStringParameters, parseQueryStringParameters } from './Utils'
6
6
  import Globals from '../../../Globals'
7
7
  import Server from '../Server'
8
- //
8
+
9
9
  /**
10
- * ${1:Description placeholder}
11
- *
12
- * @export
13
- * @typedef {GenericHandlerEventResponse}
10
+ * Represents the response object returned by a generic event handler.
11
+ * @typedef {Object} GenericHandlerEventResponse
12
+ * @property {Error | string} [err] - An optional error object or error message.
13
+ * @property {*} [data] - An optional data object.
14
14
  */
15
15
  export type GenericHandlerEventResponse = { err?: Error | string; data?: any }
16
- //
16
+
17
17
  /**
18
- * ${1:Description placeholder}
19
- *
20
- * @export
21
- * @class GenericHandlerEvent
22
- * @typedef {GenericHandlerEvent}
18
+ * Represents a generic handler event for serverless functions.
23
19
  */
24
20
  export default class GenericHandlerEvent {
25
21
  /**
26
- * ${1:Description placeholder}
27
- *
28
- * @public
29
- * @type {Request}
22
+ * Represents an HTTP request.
23
+ * @property {Request} request - The HTTP request object.
30
24
  */
31
25
  public request: Request
32
26
  /**
33
- * ${1:Description placeholder}
34
- *
35
- * @public
36
- * @type {Server["handleServerlessEvent"]}
27
+ * The handler function for serverless events in a server.
28
+ * @param {Server['handleServerlessEvent']} serverlessHandler - The function that handles serverless events.
29
+ * @returns None
37
30
  */
38
31
  public serverlessHandler: Server['handleServerlessEvent']
32
+
39
33
  /**
40
- * Creates an instance of GenericHandlerEvent.
41
- *
42
- * @constructor
43
- * @param {Request} request
44
- * @param {Server["handleServerlessEvent"]} serverlessHandler
34
+ * Constructs a new instance of the class.
35
+ * @param {Request} request - The request object.
36
+ * @param {Server['handleServerlessEvent']} serverlessHandler - The serverless handler function.
37
+ * @returns None
45
38
  */
46
39
  constructor(request: Request, serverlessHandler: Server['handleServerlessEvent']) {
47
40
  this.request = request
48
41
  this.serverlessHandler = serverlessHandler
49
42
  }
43
+
50
44
  /**
51
- * ${1:Description placeholder}
52
- *
53
- * @public
54
- * @async
55
- * @returns {Promise<GenericHandlerEventResponse>}
45
+ * Invokes the handler function asynchronously and returns a promise that resolves to a GenericHandlerEventResponse.
46
+ * @returns {Promise<GenericHandlerEventResponse>} A promise that resolves to a GenericHandlerEventResponse.
56
47
  */
57
48
  public async invoke(): Promise<GenericHandlerEventResponse> {
58
49
  // eslint-disable-next-line no-async-promise-executor
@@ -70,12 +61,10 @@ export default class GenericHandlerEvent {
70
61
  }
71
62
  })
72
63
  }
73
- /* private */
64
+
74
65
  /**
75
- * ${1:Description placeholder}
76
- *
77
- * @private
78
- * @returns {APIGatewayProxyEvent}
66
+ * Builds and returns an APIGatewayProxyEvent object based on the current request.
67
+ * @returns {APIGatewayProxyEvent} - The constructed APIGatewayProxyEvent object.
79
68
  */
80
69
  private buildEvent(): APIGatewayProxyEvent {
81
70
  return {
@@ -91,13 +80,13 @@ export default class GenericHandlerEvent {
91
80
  ? parseQueryStringParameters(this.request.query)
92
81
  : null,
93
82
  requestContext: {
94
- accountId: process.env.AWS_ACCOUNT_ID || null,
95
- apiId: null,
83
+ accountId: process.env.AWS_ACCOUNT_ID || 'undefined',
84
+ apiId: '',
96
85
  authorizer: null,
97
- domainName: null,
98
- domainPrefix: null,
86
+ domainName: undefined,
87
+ domainPrefix: undefined,
99
88
  extendedRequestId: cuid(),
100
- httpMethod: this.request.method ? this.request.method.toUpperCase() : null,
89
+ httpMethod: this.request.method ? this.request.method.toUpperCase() : '',
101
90
  identity: {
102
91
  accessKey: null,
103
92
  accountId: process.env.AWS_ACCOUNT_ID || null,
@@ -111,9 +100,11 @@ export default class GenericHandlerEvent {
111
100
  cognitoIdentityPoolId: null,
112
101
  principalOrgId: null,
113
102
  sourceIp:
114
- <string>this.request.headers?.['x-forwarded-for'] || this.request.socket?.remoteAddress,
103
+ <string>this.request.headers?.['x-forwarded-for'] ||
104
+ this.request.socket?.remoteAddress ||
105
+ '',
115
106
  user: null,
116
- userAgent: this.request.headers?.['user-agent'],
107
+ userAgent: this.request.headers?.['user-agent'] || null,
117
108
  userArn: null,
118
109
  },
119
110
  path: this.request.path,
@@ -121,21 +112,20 @@ export default class GenericHandlerEvent {
121
112
  requestId: `${cuid()}-${cuid()}`,
122
113
  requestTime: new Date().toISOString(),
123
114
  requestTimeEpoch: Date.now(),
124
- resourceId: null,
115
+ resourceId: '',
125
116
  resourcePath: Globals.Listener_HTTP_ProxyRoute,
126
- stage: process.env.STAGE,
117
+ stage: process.env.STAGE || '',
127
118
  },
128
119
  resource: Globals.Listener_HTTP_ProxyRoute,
129
120
  stageVariables: null,
130
121
  }
131
122
  }
123
+
132
124
  /**
133
- * ${1:Description placeholder}
134
- *
135
- * @private
136
- * @param {APIGatewayProxyEvent} event
137
- * @param {(err?: Error | string, data?: any) => void} callback
138
- * @returns {Context}
125
+ * Builds and returns a context object for an AWS Lambda function.
126
+ * @param {APIGatewayProxyEvent} event - The event object passed to the Lambda function.
127
+ * @param {(err?: Error | string, data?: any) => void} callback - The callback function to be called when the Lambda function completes.
128
+ * @returns {Context} - The context object for the Lambda function.
139
129
  */
140
130
  private buildContext(
141
131
  event: APIGatewayProxyEvent,
@@ -149,13 +139,13 @@ export default class GenericHandlerEvent {
149
139
  },
150
140
  done: (err, data) => callback(err, data),
151
141
  fail: err => callback(err),
152
- succeed: res => callback(null, res),
153
- functionName: null,
142
+ succeed: res => callback(undefined, res),
143
+ functionName: 'container-function',
154
144
  functionVersion: 'LATEST',
155
- invokedFunctionArn: null,
145
+ invokedFunctionArn: 'none',
156
146
  memoryLimitInMB: '-1',
157
- logGroupName: null,
158
- logStreamName: null,
147
+ logGroupName: 'undefined',
148
+ logStreamName: 'undefined',
159
149
  }
160
150
  }
161
151
  }
@@ -1,5 +1,11 @@
1
1
  import { Request, Response } from 'express'
2
2
 
3
+ /**
4
+ * Handles a request and sends a response with the message 'Healthy!'.
5
+ * @param {Request} _request - The request object.
6
+ * @param {Response} res - The response object.
7
+ * @returns None
8
+ */
3
9
  export default async (_request: Request, res: Response) => {
4
10
  res.send('Healthy!')
5
11
  }
@@ -12,57 +12,41 @@ import Globals from '../../../Globals'
12
12
  import { RouterConfig } from '../../Router'
13
13
 
14
14
  /**
15
- * ${1:Description placeholder}
16
- *
17
- * @export
18
- * @class Proxy
19
- * @typedef {Proxy}
15
+ * Represents a Proxy class that handles routing and server functionality.
20
16
  */
21
17
  export default class Proxy {
22
18
  /**
23
- * ${1:Description placeholder}
24
- *
25
- * @private
26
- * @type {boolean}
19
+ * A boolean flag indicating whether the process is currently stopping or not.
27
20
  */
28
21
  private stopping: boolean
29
22
  /**
30
- * ${1:Description placeholder}
31
- *
32
- * @private
33
- * @readonly
34
- * @type {RouterConfig}
23
+ * The configuration object for the router.
35
24
  */
36
25
  private readonly config: RouterConfig
37
26
  /**
38
- * ${1:Description placeholder}
39
- *
40
- * @private
27
+ * The Express application instance for the server.
41
28
  * @readonly
42
29
  * @type {express.Express}
43
30
  */
44
31
  private readonly app: express.Express
45
32
  /**
46
- * ${1:Description placeholder}
47
- *
48
- * @private
49
- * @readonly
50
- * @type {Server["handleServerlessEvent"]}
33
+ * The handler function for serverless events in the Server class.
34
+ * @param {ServerlessEvent} event - The serverless event object.
35
+ * @returns None
51
36
  */
52
37
  private readonly serverlessHandler: Server['handleServerlessEvent']
53
38
  /**
54
- * ${1:Description placeholder}
55
- *
39
+ * Represents a listener for an HTTP server.
56
40
  * @private
57
41
  * @type {HTTPServer}
58
42
  */
59
43
  private listener: HTTPServer
44
+
60
45
  /**
61
- * Creates an instance of Proxy.
62
- *
63
- * @constructor
64
- * @param {RouterConfig} config
65
- * @param {Server["handleServerlessEvent"]} serverlessHandler
46
+ * Constructs a new instance of the Router class.
47
+ * @param {RouterConfig} config - The configuration object for the router.
48
+ * @param {Server['handleServerlessEvent']} serverlessHandler - The handler function for serverless events.
49
+ * @returns None
66
50
  */
67
51
  constructor(config: RouterConfig, serverlessHandler: Server['handleServerlessEvent']) {
68
52
  this.stopping = false
@@ -93,42 +77,41 @@ export default class Proxy {
93
77
  // this.listener.listener.keepAliveTimeout = 120e3
94
78
  // this.listener.listener.headersTimeout = 120e3
95
79
  }
80
+
96
81
  /**
97
- * ${1:Description placeholder}
98
- *
99
- * @async
100
- * @returns {*}
82
+ * Loads the necessary components and initializes the application.
83
+ * @returns None
101
84
  */
102
85
  async load() {
103
- await this.startListeners()
104
86
  this.installRoutes()
87
+ await this.startListeners()
105
88
  }
89
+
106
90
  /**
107
- * ${1:Description placeholder}
108
- *
109
- * @async
110
- * @param {?*} [err]
111
- * @returns {*}
91
+ * Unloads the current module, stopping any active listeners.
92
+ * @param {any} [err] - Optional error object to pass to the stopListeners method.
93
+ * @returns {Promise<void>} - A promise that resolves once the listeners have been stopped.
112
94
  */
113
95
  async unload(err?: any) {
114
96
  await this.stopListeners(err)
115
97
  }
116
- /* lifecycle */
98
+
117
99
  /**
118
- * ${1:Description placeholder}
119
- *
120
- * @private
121
- * @async
122
- * @returns {Promise<void>}
100
+ * Starts the listeners for the proxy server.
101
+ * @returns {Promise<void>} A promise that resolves when the listeners have started.
123
102
  */
124
103
  private async startListeners(): Promise<void> {
125
- return new Promise(resolve => {
104
+ // eslint-disable-next-line no-async-promise-executor
105
+ return new Promise(async resolve => {
126
106
  const port = this.config.port || Globals.Listener_HTTP_DefaultPort
127
107
  console.log(`[Proxy] - [STARTING] - v.${appVersion} - :${port}`)
128
108
  // Create Server
129
109
  this.listener = createServer(this.app)
130
110
  // Set defaults
131
111
  this.listener.setTimeout(this.config.timeout || Globals.Listener_HTTP_DefaultTimeout)
112
+ // Call hook if available
113
+ if (this.config.containerSetupHook)
114
+ await this.config.containerSetupHook(this.listener, this.app)
132
115
  // Start Server
133
116
  this.listener.listen(port, () => {
134
117
  console.log(`[Proxy] - [STARTED]`)
@@ -136,13 +119,11 @@ export default class Proxy {
136
119
  })
137
120
  })
138
121
  }
122
+
139
123
  /**
140
- * ${1:Description placeholder}
141
- *
142
- * @private
143
- * @async
144
- * @param {?*} [err]
145
- * @returns {unknown}
124
+ * Stops the listeners and exits the process.
125
+ * @param {any} [err] - Optional error object.
126
+ * @returns {Promise<void>} - A promise that resolves when the listeners are stopped and the process is exited.
146
127
  */
147
128
  private async stopListeners(err?: any) {
148
129
  if (this.stopping) return
@@ -153,16 +134,15 @@ export default class Proxy {
153
134
  const err2 = err || _err
154
135
  if (err2) console.log('[Proxy] - exit output:', err2)
155
136
  console.log('[Proxy] - [STOPPED]')
156
- process.exit(err || _err ? 1 : 0)
137
+ process.exit(err2 ? 1 : 0)
157
138
  resolve(null)
158
139
  })
159
140
  })
160
141
  }
161
- /* routing */
142
+
162
143
  /**
163
- * ${1:Description placeholder}
164
- *
165
- * @private
144
+ * Installs the routes for the proxy server.
145
+ * @returns None
166
146
  */
167
147
  private installRoutes() {
168
148
  //Health check route -- This is a bypass route to only check if
@@ -1,9 +1,7 @@
1
1
  /**
2
- * ${1:Description placeholder}
3
- *
4
- * @export
5
- * @param {string} url
6
- * @returns {*}
2
+ * Parses the multi-value query string parameters from the given URL.
3
+ * @param {string} url - The URL containing the query string parameters.
4
+ * @returns {Object} - An object representing the parsed query string parameters.
7
5
  */
8
6
  export function parseMultiValueQueryStringParameters(url: string) {
9
7
  // dummy placeholder url for the WHATWG URL constructor
@@ -20,12 +18,11 @@ export function parseMultiValueQueryStringParameters(url: string) {
20
18
  }
21
19
  return Object.fromEntries(map)
22
20
  }
21
+
23
22
  /**
24
- * ${1:Description placeholder}
25
- *
26
- * @export
27
- * @param {*} url
28
- * @returns {*}
23
+ * Parses the query string parameters from a given URL.
24
+ * @param {string} url - The URL to parse the query string parameters from.
25
+ * @returns {object | null} - An object containing the parsed query string parameters, or null if there are no parameters.
29
26
  */
30
27
  export function parseQueryStringParameters(url) {
31
28
  // dummy placeholder url for the WHATWG URL constructor
@@ -4,27 +4,24 @@ import Response, { ResponseErrorType } from '../API/Response'
4
4
  import Globals from '../Globals'
5
5
 
6
6
  /**
7
- * Description placeholder
8
- *
9
- * @export
10
- * @class Validator
11
- * @typedef {Validator}
7
+ * Validates the given data against the provided schema.
8
+ * @param {any} data - The data to be validated.
9
+ * @param {z.ZodObject<any>} schema - The schema to validate against.
10
+ * @returns {boolean | Response<ResponseErrorType>} - Returns true if the data is valid, otherwise returns a response object with an error message.
12
11
  */
13
12
  export default class Validator {
14
13
  /**
15
- * Description placeholder
16
- *
17
- * @public
18
- * @static
19
- * @param {*} data
20
- * @param {z.ZodObject<any>} schema
21
- * @returns {(boolean | Response<ResponseErrorType>)}
14
+ * Validates the given data against the provided schema.
15
+ * @param {any} data - The data to be validated.
16
+ * @param {z.ZodObject<any> | z.ZodUnion<any>} schema - The schema to validate against.
17
+ * @returns {boolean | Response<ResponseErrorType>} - Returns either true if the data is valid or a Response object with an error message if validation fails.
22
18
  */
23
19
  public static validateSchema(
24
20
  data: any,
25
- schema: z.ZodObject<any>
21
+ schema: z.ZodObject<any> | z.ZodUnion<any>
26
22
  ): boolean | Response<ResponseErrorType> {
27
23
  let error, validatedInput
24
+
28
25
  // Validate body against known zod schema
29
26
  try {
30
27
  validatedInput = schema.parse(data) as typeof schema
@@ -32,6 +29,7 @@ export default class Validator {
32
29
  if (err instanceof z.ZodError) error = JSON.parse(err.message)
33
30
  else error = 'Unknown validation error!' //unhandled case, hard to test
34
31
  }
32
+
35
33
  // Error validation
36
34
  if (!validatedInput || error) {
37
35
  return Response.BadRequestResponse(