@ikonintegration/ikapi 4.0.1 → 5.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintignore +3 -0
- package/.eslintrc.cjs +81 -0
- package/.github/workflows/npmpublish.yml +8 -19
- package/.github/workflows/prs.yml +12 -0
- package/README.md +89 -99
- package/dist/index.d.ts +16 -0
- package/dist/index.js +27 -0
- package/dist/index.js.map +1 -0
- package/dist/package-lock.json +11881 -0
- package/dist/package.json +81 -0
- package/dist/src/API/Request.d.ts +125 -0
- package/dist/src/API/Request.js +185 -0
- package/dist/src/API/Request.js.map +1 -0
- package/dist/src/API/Response.d.ts +188 -0
- package/dist/src/API/Response.js +270 -0
- package/dist/src/API/Response.js.map +1 -0
- package/dist/src/BaseEvent/DynamoTransaction.d.ts +70 -0
- package/dist/src/BaseEvent/DynamoTransaction.js +104 -0
- package/dist/src/BaseEvent/DynamoTransaction.js.map +1 -0
- package/dist/src/BaseEvent/EventProcessor.d.ts +58 -0
- package/dist/src/BaseEvent/EventProcessor.js +101 -0
- package/dist/src/BaseEvent/EventProcessor.js.map +1 -0
- package/dist/src/BaseEvent/Process.d.ts +50 -0
- package/dist/src/BaseEvent/Process.js +64 -0
- package/dist/src/BaseEvent/Process.js.map +1 -0
- package/dist/src/BaseEvent/StepTransaction.d.ts +23 -0
- package/dist/src/BaseEvent/StepTransaction.js +27 -0
- package/dist/src/BaseEvent/StepTransaction.js.map +1 -0
- package/dist/src/BaseEvent/Transaction.d.ts +149 -0
- package/dist/src/BaseEvent/Transaction.js +224 -0
- package/dist/src/BaseEvent/Transaction.js.map +1 -0
- package/dist/src/Cache/Redis.d.ts +29 -0
- package/dist/src/Cache/Redis.js +80 -0
- package/dist/src/Cache/Redis.js.map +1 -0
- package/dist/src/Cache/types.d.ts +31 -0
- package/dist/src/Cache/types.js +2 -0
- package/dist/src/Cache/types.js.map +1 -0
- package/dist/src/Config/Configuration.d.ts +123 -0
- package/dist/src/Config/Configuration.js +109 -0
- package/dist/src/Config/Configuration.js.map +1 -0
- package/dist/src/Config/EnvironmentVar.d.ts +74 -0
- package/dist/src/Config/EnvironmentVar.js +138 -0
- package/dist/src/Config/EnvironmentVar.js.map +1 -0
- package/dist/src/Crypto/Crypto.d.ts +45 -0
- package/dist/src/Crypto/Crypto.js +72 -0
- package/dist/src/Crypto/Crypto.js.map +1 -0
- package/dist/src/Database/Database.d.ts +21 -0
- package/dist/src/Database/Database.js +15 -0
- package/dist/src/Database/Database.js.map +1 -0
- package/dist/src/Database/DatabaseManager.d.ts +47 -0
- package/dist/src/Database/DatabaseManager.js +60 -0
- package/dist/src/Database/DatabaseManager.js.map +1 -0
- package/dist/src/Database/DatabaseTransaction.d.ts +101 -0
- package/dist/src/Database/DatabaseTransaction.js +126 -0
- package/dist/src/Database/DatabaseTransaction.js.map +1 -0
- package/dist/src/Database/index.d.ts +10 -0
- package/dist/src/Database/index.js +15 -0
- package/dist/src/Database/index.js.map +1 -0
- package/dist/src/Database/integrations/dynamo/DynamoDatabase.d.ts +35 -0
- package/dist/src/Database/integrations/dynamo/DynamoDatabase.js +59 -0
- package/dist/src/Database/integrations/dynamo/DynamoDatabase.js.map +1 -0
- package/dist/src/Database/integrations/kysely/KyselyDatabase.d.ts +66 -0
- package/dist/src/Database/integrations/kysely/KyselyDatabase.js +86 -0
- package/dist/src/Database/integrations/kysely/KyselyDatabase.js.map +1 -0
- package/dist/src/Database/integrations/kysely/KyselyTransaction.d.ts +70 -0
- package/dist/src/Database/integrations/kysely/KyselyTransaction.js +118 -0
- package/dist/src/Database/integrations/kysely/KyselyTransaction.js.map +1 -0
- package/dist/src/Database/integrations/pgsql/PostgresDatabase.d.ts +36 -0
- package/dist/src/Database/integrations/pgsql/PostgresDatabase.js +54 -0
- package/dist/src/Database/integrations/pgsql/PostgresDatabase.js.map +1 -0
- package/dist/src/Database/integrations/pgsql/PostgresTransaction.d.ts +63 -0
- package/dist/src/Database/integrations/pgsql/PostgresTransaction.js +61 -0
- package/dist/src/Database/integrations/pgsql/PostgresTransaction.js.map +1 -0
- package/dist/src/Database/types.d.ts +76 -0
- package/dist/src/Database/types.js +2 -0
- package/dist/src/Database/types.js.map +1 -0
- package/dist/src/Globals.d.ts +93 -0
- package/dist/src/Globals.js +99 -0
- package/dist/src/Globals.js.map +1 -0
- package/dist/src/Logger/Logger.d.ts +161 -0
- package/dist/src/Logger/Logger.js +299 -0
- package/dist/src/Logger/Logger.js.map +1 -0
- package/dist/src/Mailer/Mailer.d.ts +78 -0
- package/dist/src/Mailer/Mailer.js +182 -0
- package/dist/src/Mailer/Mailer.js.map +1 -0
- package/dist/src/Publisher/Publisher.d.ts +39 -0
- package/dist/src/Publisher/Publisher.js +77 -0
- package/dist/src/Publisher/Publisher.js.map +1 -0
- package/dist/src/Server/RouteResolver.d.ts +33 -0
- package/dist/src/Server/RouteResolver.js +100 -0
- package/dist/src/Server/RouteResolver.js.map +1 -0
- package/dist/src/Server/Router.d.ts +157 -0
- package/dist/src/Server/Router.js +32 -0
- package/dist/src/Server/Router.js.map +1 -0
- package/dist/src/Server/lib/ContainerServer.d.ts +42 -0
- package/dist/src/Server/lib/ContainerServer.js +66 -0
- package/dist/src/Server/lib/ContainerServer.js.map +1 -0
- package/dist/src/Server/lib/Server.d.ts +45 -0
- package/dist/src/Server/lib/Server.js +93 -0
- package/dist/src/Server/lib/Server.js.map +1 -0
- package/dist/src/Server/lib/container/GenericHandler.d.ts +9 -0
- package/dist/src/Server/lib/container/GenericHandler.js +82 -0
- package/dist/src/Server/lib/container/GenericHandler.js.map +1 -0
- package/dist/src/Server/lib/container/GenericHandlerEvent.d.ts +52 -0
- package/dist/src/Server/lib/container/GenericHandlerEvent.js +132 -0
- package/dist/src/Server/lib/container/GenericHandlerEvent.js.map +1 -0
- package/dist/src/Server/lib/container/HealthHandler.d.ts +9 -0
- package/dist/src/Server/lib/container/HealthHandler.js +19 -0
- package/dist/src/Server/lib/container/HealthHandler.js.map +1 -0
- package/dist/src/Server/lib/container/Proxy.d.ts +67 -0
- package/dist/src/Server/lib/container/Proxy.js +143 -0
- package/dist/src/Server/lib/container/Proxy.js.map +1 -0
- package/dist/src/Server/lib/container/Utils.d.ts +14 -0
- package/dist/src/Server/lib/container/Utils.js +37 -0
- package/dist/src/Server/lib/container/Utils.js.map +1 -0
- package/dist/src/Util/AsyncSingleton.d.ts +31 -0
- package/dist/src/Util/AsyncSingleton.js +83 -0
- package/dist/src/Util/AsyncSingleton.js.map +1 -0
- package/dist/src/Util/Utils.d.ts +61 -0
- package/dist/src/Util/Utils.js +147 -0
- package/dist/src/Util/Utils.js.map +1 -0
- package/dist/src/Validation/Validator.d.ts +17 -0
- package/dist/src/Validation/Validator.js +39 -0
- package/dist/src/Validation/Validator.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/index.ts +41 -0
- package/jest.config.ts +37 -0
- package/jest.smoke.config.ts +34 -0
- package/package.json +66 -22
- package/src/API/Request.ts +214 -0
- package/src/API/Response.ts +370 -0
- package/src/BaseEvent/DynamoTransaction.ts +175 -0
- package/src/BaseEvent/EventProcessor.ts +140 -0
- package/src/BaseEvent/Process.ts +78 -0
- package/src/BaseEvent/StepTransaction.ts +35 -0
- package/src/BaseEvent/Transaction.ts +323 -0
- package/src/Cache/Redis.ts +89 -0
- package/src/Cache/types.ts +33 -0
- package/src/Config/Configuration.ts +199 -0
- package/src/Config/EnvironmentVar.ts +142 -0
- package/src/Crypto/Crypto.ts +89 -0
- package/src/Database/Database.ts +22 -0
- package/src/Database/DatabaseManager.ts +67 -0
- package/src/Database/DatabaseTransaction.ts +170 -0
- package/src/Database/index.ts +27 -0
- package/src/Database/integrations/dynamo/DynamoDatabase.ts +58 -0
- package/src/Database/integrations/kysely/KyselyDatabase.ts +99 -0
- package/src/Database/integrations/kysely/KyselyTransaction.ts +172 -0
- package/src/Database/integrations/pgsql/PostgresDatabase.ts +56 -0
- package/src/Database/integrations/pgsql/PostgresTransaction.ts +87 -0
- package/src/Database/types.ts +85 -0
- package/src/Globals.ts +103 -0
- package/src/Logger/Logger.ts +363 -0
- package/src/Mailer/Mailer.ts +217 -0
- package/src/Publisher/Publisher.ts +96 -0
- package/src/Server/RouteResolver.ts +124 -0
- package/src/Server/Router.ts +200 -0
- package/src/Server/lib/ContainerServer.ts +65 -0
- package/src/Server/lib/Server.ts +109 -0
- package/src/Server/lib/container/GenericHandler.ts +76 -0
- package/src/Server/lib/container/GenericHandlerEvent.ts +154 -0
- package/src/Server/lib/container/HealthHandler.ts +11 -0
- package/src/Server/lib/container/Proxy.ts +172 -0
- package/src/Server/lib/container/Utils.ts +33 -0
- package/src/Util/AsyncSingleton.ts +86 -0
- package/src/Util/Utils.ts +131 -0
- package/src/Validation/Validator.ts +45 -0
- package/tests/API/Request.test.ts +273 -0
- package/tests/API/Response.test.ts +367 -0
- package/tests/BaseEvent/DynamoTransaction.test.ts +272 -0
- package/tests/BaseEvent/EventProcessor.test.ts +263 -0
- package/tests/BaseEvent/Process.test.ts +47 -0
- package/tests/BaseEvent/StepTransaction.test.ts +44 -0
- package/tests/BaseEvent/Transaction.test.ts +402 -0
- package/tests/Cache/Redis-client.test.ts +90 -0
- package/tests/Cache/Redis-cluster.test.ts +100 -0
- package/tests/Config/Config.test.ts +205 -0
- package/tests/Config/EnvironmentVar.test.ts +251 -0
- package/tests/Crypto/Crypto.test.ts +88 -0
- package/tests/Database/DatabaseManager.test.ts +79 -0
- package/tests/Database/integrations/dynamo/DynamoDatabase.test.ts +44 -0
- package/tests/Database/integrations/kysely/KyselyDatabase.test.ts +113 -0
- package/tests/Database/integrations/kysely/KyselyTransaction.test.ts +119 -0
- package/tests/Database/integrations/pg/PostgresDatabase.test.ts +76 -0
- package/tests/Database/integrations/pg/PostgresTransaction.test.ts +118 -0
- package/tests/Logger/Logger.test.ts +215 -0
- package/tests/Mailer/Mailer.test.ts +59 -0
- package/tests/Publisher/Publisher.test.ts +60 -0
- package/tests/Server/RouteResolver.test.ts +116 -0
- package/tests/Server/Router.test.ts +39 -0
- package/tests/Server/lib/ContainerServer.test.ts +531 -0
- package/tests/Server/lib/Server.test.ts +12 -0
- package/tests/Server/lib/container/GenericHandler.test.ts +131 -0
- package/tests/Server/lib/container/GenericHandlerEvent.test.ts +103 -0
- package/tests/Server/lib/container/HealthHandler.test.ts +30 -0
- package/tests/Server/lib/container/Proxy.test.ts +268 -0
- package/tests/Server/lib/container/Utils.test.ts +47 -0
- package/tests/Test.utils.ts +78 -0
- package/tests/Utils/Utils.test.ts +229 -0
- package/tests/Validation/Validator.test.ts +82 -0
- package/tsconfig.json +26 -0
- package/tsconfig.smoke.json +26 -0
- package/index.js +0 -88
- package/src/API/IKRequest.js +0 -52
- package/src/API/IKResponse.js +0 -119
- package/src/API/IKUtils.js +0 -51
- package/src/BaseEvent/IKProcess.js +0 -77
- package/src/BaseEvent/IKTransaction.js +0 -139
- package/src/Cache/Prototype/IKCache.js +0 -17
- package/src/Cache/Redis/IKRedis.js +0 -148
- package/src/Database/DDB/IKDB.js +0 -56
- package/src/Database/DDB/IKDBBaseExpression.js +0 -130
- package/src/Database/DDB/IKDBBaseQuery.js +0 -151
- package/src/Database/DDB/IKDBQueryBatchGet.js +0 -37
- package/src/Database/DDB/IKDBQueryBatchWrite.js +0 -64
- package/src/Database/DDB/IKDBQueryDelete.js +0 -34
- package/src/Database/DDB/IKDBQueryGet.js +0 -48
- package/src/Database/DDB/IKDBQueryPut.js +0 -87
- package/src/Database/DDB/IKDBQueryScan.js +0 -45
- package/src/Database/DDB/IKDBQueryTransactionalWrite.js +0 -69
- package/src/Database/DDB/IKDBQueryUpdate.js +0 -221
- package/src/Database/DDB/_IKDBQueryTransactionalRead.js +0 -46
- package/src/Database/PSQL/IKDB.js +0 -41
- package/src/Database/PSQL/IKDBBaseQuery.js +0 -26
- package/src/Database/Prototype/IKDB.js +0 -21
- package/src/Database/Prototype/IKDBBaseQuery.js +0 -14
- package/src/IKDynamoStream.js +0 -42
- package/src/IKEventProcessor.js +0 -42
- package/src/IKGlobals.js +0 -24
- package/src/IKRouter.js +0 -47
- package/src/IKStepTransaction.js +0 -14
- package/src/Logger/IKLogger.js +0 -136
- package/src/Mailer/IKMailer.js +0 -69
- package/src/Publisher/IKPublisher.js +0 -44
- package/src/Tracker/IKExecutionTracker.js +0 -79
- package/src/Validation/IKValidation.js +0 -76
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { Server as HTTPServer } from 'http';
|
|
2
|
+
import express from 'express';
|
|
3
|
+
import { z } from 'zod';
|
|
4
|
+
import { HttpMethod } from '../API/Request.js';
|
|
5
|
+
import { ResponseErrorType } from '../API/Response.js';
|
|
6
|
+
import Transaction, { TransactionConfig, TransactionExecution, StringMap } from '../BaseEvent/Transaction.js';
|
|
7
|
+
/**
|
|
8
|
+
* Represents a route in an API.
|
|
9
|
+
* @template InputType - The type of the input data for the route.
|
|
10
|
+
* @template OutputType - The type of the output data for the route.
|
|
11
|
+
* @property {string} path - The path of the route.
|
|
12
|
+
* @property {string} method - The HTTP method of the route.
|
|
13
|
+
* @property {TransactionExecution<Transaction<InputType, OutputType | ResponseErrorType>, OutputType | ResponseErrorType>} handler - The handler function for the route.
|
|
14
|
+
* @property {?z.ZodObject<any> | z.ZodUnion<any> | z.ZodIntersection<any, any>}[inputSchema] - The input schema for validating the input data.
|
|
15
|
+
* @property {?z.ZodObject<any> | z.ZodUnion<any> | z.ZodIntersection<any, any>}[pathSchema] - The path schema for validating the path data.
|
|
16
|
+
* @property {?z.ZodObject<any> | z.ZodUnion<any> | z.ZodIntersection<any, any>}[querySchema] - The query schema for validating the query data.
|
|
17
|
+
*/
|
|
18
|
+
export interface Route<InputType = never, OutputType = never, PathParamsType = StringMap, QueryParamsType = StringMap> {
|
|
19
|
+
/**
|
|
20
|
+
* Represents a file path as a string.
|
|
21
|
+
* @param {string} path - The file path.
|
|
22
|
+
* @returns None
|
|
23
|
+
*/
|
|
24
|
+
path: string | string[];
|
|
25
|
+
/**
|
|
26
|
+
* Represents the method used in an API request.
|
|
27
|
+
* @type {string}
|
|
28
|
+
*/
|
|
29
|
+
method: HttpMethod;
|
|
30
|
+
/**
|
|
31
|
+
* Represents a handler for executing a transaction with the given input type and output type.
|
|
32
|
+
* @param {Transaction<InputType, OutputType | ResponseErrorType, PathParamsType, QueryParamsType>} transaction - The transaction to execute.
|
|
33
|
+
* @param {OutputType | ResponseErrorType} - The output type or response error type of the transaction.
|
|
34
|
+
*/
|
|
35
|
+
handler: TransactionExecution<Transaction<InputType, OutputType | ResponseErrorType, never, PathParamsType, QueryParamsType>, OutputType | ResponseErrorType>;
|
|
36
|
+
/**
|
|
37
|
+
* An optional input schema for validating the structure of the input data.
|
|
38
|
+
*
|
|
39
|
+
* @type {?z.ZodObject<any> | z.ZodUnion<any> | z.ZodIntersection<any, any>}
|
|
40
|
+
*/
|
|
41
|
+
inputSchema?: z.ZodObject<any> | z.ZodUnion<any> | z.ZodIntersection<any, any>;
|
|
42
|
+
/**
|
|
43
|
+
* An optional input schema for validating the structure of the path params.
|
|
44
|
+
*
|
|
45
|
+
* @type {?z.ZodObject<any> | z.ZodUnion<any> | z.ZodIntersection<any, any>}
|
|
46
|
+
*/
|
|
47
|
+
pathSchema?: z.ZodObject<any> | z.ZodUnion<any> | z.ZodIntersection<any, any>;
|
|
48
|
+
/**
|
|
49
|
+
* An optional input schema for validating the structure of the query params.
|
|
50
|
+
*
|
|
51
|
+
* @type {?z.ZodObject<any> | z.ZodUnion<any> | z.ZodIntersection<any, any>}
|
|
52
|
+
*/
|
|
53
|
+
querySchema?: z.ZodObject<any> | z.ZodUnion<any> | z.ZodIntersection<any, any>;
|
|
54
|
+
/**
|
|
55
|
+
* An optional openApi object with extra metadata for docs generation.
|
|
56
|
+
*/
|
|
57
|
+
openApi?: {
|
|
58
|
+
summary: string;
|
|
59
|
+
description: string;
|
|
60
|
+
tags?: string[];
|
|
61
|
+
outputSchema?: z.ZodObject<any> | z.ZodUnion<any> | z.ZodIntersection<any, any> | z.ZodType<any>;
|
|
62
|
+
successCode?: number;
|
|
63
|
+
security?: {
|
|
64
|
+
[key: string]: string[] | never[];
|
|
65
|
+
}[];
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Represents a route that can accept any type for its parameters and return values.
|
|
70
|
+
*/
|
|
71
|
+
export type AnyRoute = Route<any | never, any | never, any | never, any | never>;
|
|
72
|
+
/**
|
|
73
|
+
* Represents the configuration options for a router.
|
|
74
|
+
* @typedef {TransactionConfig & {
|
|
75
|
+
* routes: Route[]
|
|
76
|
+
* port?: number
|
|
77
|
+
* timeout?: number
|
|
78
|
+
* cors?: {
|
|
79
|
+
* origin?: string | string[]
|
|
80
|
+
* headers?: string[]
|
|
81
|
+
* allowCredentials?: boolean
|
|
82
|
+
* }
|
|
83
|
+
* healthCheckRoute?: string
|
|
84
|
+
* }} RouterConfig
|
|
85
|
+
* @property {Route[]} routes - The routes to be configured in the router.
|
|
86
|
+
* @property {number} [port] - The port number to listen on. If not specified, a default port will be used.
|
|
87
|
+
* @property {number} [timeout] - The timeout duration for requests in milliseconds. If not specified, a default timeout will be
|
|
88
|
+
*/
|
|
89
|
+
export type RouterConfig = TransactionConfig & {
|
|
90
|
+
/**
|
|
91
|
+
* An array of route objects representing the available routes in the application.
|
|
92
|
+
* @type {Route[]}
|
|
93
|
+
*/
|
|
94
|
+
routes: AnyRoute[];
|
|
95
|
+
/**
|
|
96
|
+
* The port number for the server to listen on.
|
|
97
|
+
* @type {number | undefined}
|
|
98
|
+
*/
|
|
99
|
+
port?: number;
|
|
100
|
+
/**
|
|
101
|
+
* Optional timeout value in milliseconds.
|
|
102
|
+
* @type {number | undefined}
|
|
103
|
+
*/
|
|
104
|
+
timeout?: number;
|
|
105
|
+
/**
|
|
106
|
+
* Configuration options for Cross-Origin Resource Sharing (CORS).
|
|
107
|
+
* @property {string | string[]} [origin] - The allowed origin(s) for CORS requests.
|
|
108
|
+
* @property {string[]} [headers] - The allowed headers for CORS requests.
|
|
109
|
+
* @property {boolean} [allowCredentials] - Whether to allow credentials (cookies, HTTP authentication, and client-side SSL certificates) to be sent in CORS requests.
|
|
110
|
+
*/
|
|
111
|
+
cors?: {
|
|
112
|
+
origin?: string | string[];
|
|
113
|
+
headers?: string[];
|
|
114
|
+
allowCredentials?: boolean;
|
|
115
|
+
};
|
|
116
|
+
/**
|
|
117
|
+
* The route for the health check endpoint.
|
|
118
|
+
* @type {string | undefined}
|
|
119
|
+
*/
|
|
120
|
+
healthCheckRoute?: string;
|
|
121
|
+
/**
|
|
122
|
+
* Optional hook function that sets up the container with the provided HTTP server and Express app.
|
|
123
|
+
* @param {HTTPServer} server - The HTTP server instance.
|
|
124
|
+
* @param {express.Express} app - The Express application instance.
|
|
125
|
+
* @returns {Promise<void>} A promise that resolves when the setup is complete.
|
|
126
|
+
*/
|
|
127
|
+
containerSetupHook?: (server: HTTPServer, app: express.Express) => Promise<void>;
|
|
128
|
+
};
|
|
129
|
+
/**
|
|
130
|
+
* Represents a router that handles routing logic for a web application.
|
|
131
|
+
*/
|
|
132
|
+
export default class Router {
|
|
133
|
+
/**
|
|
134
|
+
* The configuration object for the router.
|
|
135
|
+
*/
|
|
136
|
+
private readonly config;
|
|
137
|
+
/**
|
|
138
|
+
* The private readonly server instance.
|
|
139
|
+
*/
|
|
140
|
+
private readonly server;
|
|
141
|
+
/**
|
|
142
|
+
* Constructs a new instance of the Router class.
|
|
143
|
+
* @param {RouterConfig} config - The configuration object for the router.
|
|
144
|
+
* @returns None
|
|
145
|
+
*/
|
|
146
|
+
constructor(config: RouterConfig);
|
|
147
|
+
/**
|
|
148
|
+
* Retrieves the export function from the server.
|
|
149
|
+
* @returns {CallableFunction} The export function from the server.
|
|
150
|
+
*/
|
|
151
|
+
getExport(): CallableFunction;
|
|
152
|
+
/**
|
|
153
|
+
* Checks if the current element is a container.
|
|
154
|
+
* @returns {boolean} - True if the element is a container, false otherwise.
|
|
155
|
+
*/
|
|
156
|
+
private isContainer;
|
|
157
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import ContainerServer from './lib/ContainerServer.js';
|
|
2
|
+
import Server from './lib/Server.js';
|
|
3
|
+
import Utils from '../Util/Utils.js';
|
|
4
|
+
/**
|
|
5
|
+
* Represents a router that handles routing logic for a web application.
|
|
6
|
+
*/
|
|
7
|
+
export default class Router {
|
|
8
|
+
/**
|
|
9
|
+
* Constructs a new instance of the Router class.
|
|
10
|
+
* @param {RouterConfig} config - The configuration object for the router.
|
|
11
|
+
* @returns None
|
|
12
|
+
*/
|
|
13
|
+
constructor(config) {
|
|
14
|
+
this.config = config;
|
|
15
|
+
this.server = this.isContainer() ? new ContainerServer(config) : new Server(config);
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Retrieves the export function from the server.
|
|
19
|
+
* @returns {CallableFunction} The export function from the server.
|
|
20
|
+
*/
|
|
21
|
+
getExport() {
|
|
22
|
+
return this.server.getExport();
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Checks if the current element is a container.
|
|
26
|
+
* @returns {boolean} - True if the element is a container, false otherwise.
|
|
27
|
+
*/
|
|
28
|
+
isContainer() {
|
|
29
|
+
return Utils.isHybridlessContainer();
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
//# sourceMappingURL=Router.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Router.js","sourceRoot":"","sources":["../../../src/Server/Router.ts"],"names":[],"mappings":"AAKA,OAAO,eAAe,MAAM,0BAA0B,CAAA;AACtD,OAAO,MAAM,MAAM,iBAAiB,CAAA;AAQpC,OAAO,KAAK,MAAM,kBAAkB,CAAA;AAmJpC;;GAEG;AACH,MAAM,CAAC,OAAO,OAAO,MAAM;IAUzB;;;;OAIG;IACH,YAAY,MAAoB;QAC9B,IAAI,CAAC,MAAM,GAAG,MAAM,CAAA;QACpB,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,WAAW,EAAE,CAAC,CAAC,CAAC,IAAI,eAAe,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,IAAI,MAAM,CAAC,MAAM,CAAC,CAAA;IACrF,CAAC;IAED;;;OAGG;IACI,SAAS;QACd,OAAO,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE,CAAA;IAChC,CAAC;IAED;;;OAGG;IACK,WAAW;QACjB,OAAO,KAAK,CAAC,qBAAqB,EAAE,CAAA;IACtC,CAAC;CACF"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import Proxy from './container/Proxy.js';
|
|
2
|
+
import Server from './Server.js';
|
|
3
|
+
import { RouterConfig } from '../Router.js';
|
|
4
|
+
/**
|
|
5
|
+
* Represents a server container that extends the Server class and handles serverless events.
|
|
6
|
+
*/
|
|
7
|
+
export default class ContainerServer extends Server {
|
|
8
|
+
/**
|
|
9
|
+
* The proxy object used for handling requests and responses.
|
|
10
|
+
* @type {Proxy}
|
|
11
|
+
* @protected
|
|
12
|
+
*/
|
|
13
|
+
protected readonly proxy: Proxy;
|
|
14
|
+
/**
|
|
15
|
+
* Constructs a new instance of the Router class.
|
|
16
|
+
* @param {RouterConfig} config - The configuration object for the router.
|
|
17
|
+
* @returns None
|
|
18
|
+
*/
|
|
19
|
+
constructor(config: RouterConfig);
|
|
20
|
+
/**
|
|
21
|
+
* Returns a callable function that can be used to export data.
|
|
22
|
+
* @returns {CallableFunction} - A callable function that can be used to export data.
|
|
23
|
+
*/
|
|
24
|
+
getExport(): CallableFunction;
|
|
25
|
+
/**
|
|
26
|
+
* Starts the application by loading the proxy.
|
|
27
|
+
* @returns {Promise<void>} - A promise that resolves when the proxy is loaded.
|
|
28
|
+
*/
|
|
29
|
+
start(): Promise<void>;
|
|
30
|
+
/**
|
|
31
|
+
* Stops the execution of the program and unloads the proxy.
|
|
32
|
+
* @param {any} [err] - Optional error object to pass to the unload method.
|
|
33
|
+
* @returns {Promise<void>} - A promise that resolves once the proxy is unloaded.
|
|
34
|
+
*/
|
|
35
|
+
stop(err?: any): Promise<void>;
|
|
36
|
+
/**
|
|
37
|
+
* Listens for process events and handles them accordingly.
|
|
38
|
+
* @private
|
|
39
|
+
* @returns None
|
|
40
|
+
*/
|
|
41
|
+
private listenProcessEvents;
|
|
42
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import Proxy from './container/Proxy.js';
|
|
11
|
+
import Server from './Server.js';
|
|
12
|
+
/**
|
|
13
|
+
* Represents a server container that extends the Server class and handles serverless events.
|
|
14
|
+
*/
|
|
15
|
+
export default class ContainerServer extends Server {
|
|
16
|
+
/**
|
|
17
|
+
* Constructs a new instance of the Router class.
|
|
18
|
+
* @param {RouterConfig} config - The configuration object for the router.
|
|
19
|
+
* @returns None
|
|
20
|
+
*/
|
|
21
|
+
constructor(config) {
|
|
22
|
+
super(config);
|
|
23
|
+
this.proxy = new Proxy(config, this.handleServerlessEvent.bind(this));
|
|
24
|
+
this.listenProcessEvents();
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Returns a callable function that can be used to export data.
|
|
28
|
+
* @returns {CallableFunction} - A callable function that can be used to export data.
|
|
29
|
+
*/
|
|
30
|
+
getExport() {
|
|
31
|
+
// start server socket
|
|
32
|
+
this.start();
|
|
33
|
+
// return empty function, we are all done
|
|
34
|
+
return () => { };
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Starts the application by loading the proxy.
|
|
38
|
+
* @returns {Promise<void>} - A promise that resolves when the proxy is loaded.
|
|
39
|
+
*/
|
|
40
|
+
start() {
|
|
41
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
42
|
+
yield this.proxy.load();
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Stops the execution of the program and unloads the proxy.
|
|
47
|
+
* @param {any} [err] - Optional error object to pass to the unload method.
|
|
48
|
+
* @returns {Promise<void>} - A promise that resolves once the proxy is unloaded.
|
|
49
|
+
*/
|
|
50
|
+
stop(err) {
|
|
51
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
52
|
+
yield this.proxy.unload(err);
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Listens for process events and handles them accordingly.
|
|
57
|
+
* @private
|
|
58
|
+
* @returns None
|
|
59
|
+
*/
|
|
60
|
+
listenProcessEvents() {
|
|
61
|
+
// start process listeners
|
|
62
|
+
process.on('unhandledRejection', this.stop.bind(this)); // listen to exceptions
|
|
63
|
+
process.on('SIGINT', this.stop.bind(this)); // listen on SIGINT signal and gracefully stop the server
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
//# sourceMappingURL=ContainerServer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ContainerServer.js","sourceRoot":"","sources":["../../../../src/Server/lib/ContainerServer.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,KAAK,MAAM,sBAAsB,CAAA;AACxC,OAAO,MAAM,MAAM,aAAa,CAAA;AAGhC;;GAEG;AACH,MAAM,CAAC,OAAO,OAAO,eAAgB,SAAQ,MAAM;IAQjD;;;;OAIG;IACH,YAAY,MAAoB;QAC9B,KAAK,CAAC,MAAM,CAAC,CAAA;QACb,IAAI,CAAC,KAAK,GAAG,IAAI,KAAK,CAAC,MAAM,EAAE,IAAI,CAAC,qBAAqB,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAA;QACrE,IAAI,CAAC,mBAAmB,EAAE,CAAA;IAC5B,CAAC;IAED;;;OAGG;IACI,SAAS;QACd,sBAAsB;QACtB,IAAI,CAAC,KAAK,EAAE,CAAA;QACZ,yCAAyC;QACzC,OAAO,GAAG,EAAE,GAAE,CAAC,CAAA;IACjB,CAAC;IAED;;;OAGG;IACU,KAAK;;YAChB,MAAM,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,CAAA;QACzB,CAAC;KAAA;IAED;;;;OAIG;IACU,IAAI,CAAC,GAAS;;YACzB,MAAM,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;QAC9B,CAAC;KAAA;IAED;;;;OAIG;IACK,mBAAmB;QACzB,0BAA0B;QAC1B,OAAO,CAAC,EAAE,CAAC,oBAAoB,EAAE,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAA,CAAC,uBAAuB;QAC9E,OAAO,CAAC,EAAE,CAAC,QAAQ,EAAE,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAA,CAAC,yDAAyD;IACtG,CAAC;CACF"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { APIGatewayProxyEvent, Context } from 'aws-lambda';
|
|
2
|
+
import { RouterConfig } from '../Router.js';
|
|
3
|
+
import RouteResolver from '../RouteResolver.js';
|
|
4
|
+
/**
|
|
5
|
+
* Represents a server that handles serverless events and routes them to appropriate handlers.
|
|
6
|
+
*/
|
|
7
|
+
export default class Server {
|
|
8
|
+
/**
|
|
9
|
+
* The configuration object for the router.
|
|
10
|
+
* @readonly
|
|
11
|
+
* @type {RouterConfig}
|
|
12
|
+
*/
|
|
13
|
+
protected readonly config: RouterConfig;
|
|
14
|
+
/**
|
|
15
|
+
* A protected property that holds a RouteResolver object.
|
|
16
|
+
* The RouteResolver is responsible for resolving routes and returning the appropriate response.
|
|
17
|
+
* @type {RouteResolver}
|
|
18
|
+
*/
|
|
19
|
+
protected readonly routeResolver: RouteResolver;
|
|
20
|
+
/**
|
|
21
|
+
* Constructs a new instance of the Router class.
|
|
22
|
+
* @param {RouterConfig} config - The configuration object for the router.
|
|
23
|
+
* @returns None
|
|
24
|
+
*/
|
|
25
|
+
constructor(config: RouterConfig);
|
|
26
|
+
/**
|
|
27
|
+
* Returns a callable function that is bound to the `handleServerlessEvent` method of the current object.
|
|
28
|
+
* @returns {CallableFunction} - A callable function that is bound to the `handleServerlessEvent` method.
|
|
29
|
+
*/
|
|
30
|
+
getExport(): CallableFunction;
|
|
31
|
+
/**
|
|
32
|
+
* Handles a serverless event by executing a transaction and resolving the route based on the event.
|
|
33
|
+
* @param {APIGatewayProxyEvent} event - The serverless event object.
|
|
34
|
+
* @param {Context} context - The serverless context object.
|
|
35
|
+
* @returns None
|
|
36
|
+
*/
|
|
37
|
+
handleServerlessEvent(event: APIGatewayProxyEvent, context: Context): Promise<void>;
|
|
38
|
+
/**
|
|
39
|
+
* Parses the path parameters from the request URL based on the given route path.
|
|
40
|
+
* @param {Request<any>} req - The request object.
|
|
41
|
+
* @param {string} routePath - The route path pattern to match against.
|
|
42
|
+
* @returns None
|
|
43
|
+
*/
|
|
44
|
+
private parsePathParams;
|
|
45
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { pathToRegexp } from 'path-to-regexp';
|
|
11
|
+
import Response from '../../API/Response.js';
|
|
12
|
+
import Transaction from '../../BaseEvent/Transaction.js';
|
|
13
|
+
import Validator from '../../Validation/Validator.js';
|
|
14
|
+
import RouteResolver from '../RouteResolver.js';
|
|
15
|
+
/**
|
|
16
|
+
* Represents a server that handles serverless events and routes them to appropriate handlers.
|
|
17
|
+
*/
|
|
18
|
+
export default class Server {
|
|
19
|
+
/**
|
|
20
|
+
* Constructs a new instance of the Router class.
|
|
21
|
+
* @param {RouterConfig} config - The configuration object for the router.
|
|
22
|
+
* @returns None
|
|
23
|
+
*/
|
|
24
|
+
constructor(config) {
|
|
25
|
+
this.config = config;
|
|
26
|
+
this.routeResolver = new RouteResolver(config);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Returns a callable function that is bound to the `handleServerlessEvent` method of the current object.
|
|
30
|
+
* @returns {CallableFunction} - A callable function that is bound to the `handleServerlessEvent` method.
|
|
31
|
+
*/
|
|
32
|
+
getExport() {
|
|
33
|
+
return this.handleServerlessEvent.bind(this);
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Handles a serverless event by executing a transaction and resolving the route based on the event.
|
|
37
|
+
* @param {APIGatewayProxyEvent} event - The serverless event object.
|
|
38
|
+
* @param {Context} context - The serverless context object.
|
|
39
|
+
* @returns None
|
|
40
|
+
*/
|
|
41
|
+
handleServerlessEvent(event, context) {
|
|
42
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
43
|
+
// init transaction
|
|
44
|
+
yield new Transaction(event, context, this.config).execute((transaction) => __awaiter(this, void 0, void 0, function* () {
|
|
45
|
+
const request = transaction.request;
|
|
46
|
+
const route = this.routeResolver.resolveRoute(request.getMethod(), request.getPath());
|
|
47
|
+
if (route) {
|
|
48
|
+
transaction.logger.log('Router accepted route:', route.path);
|
|
49
|
+
// Validate input
|
|
50
|
+
if (route.inputSchema) {
|
|
51
|
+
const validationResp = Validator.validateSchema(request.getBody(), route.inputSchema);
|
|
52
|
+
if (validationResp && validationResp instanceof Response)
|
|
53
|
+
return validationResp;
|
|
54
|
+
}
|
|
55
|
+
// Validate query
|
|
56
|
+
if (route.querySchema) {
|
|
57
|
+
const validationResp = Validator.validateSchema(request.getQueryParams(), route.querySchema);
|
|
58
|
+
if (validationResp && validationResp instanceof Response)
|
|
59
|
+
return validationResp;
|
|
60
|
+
}
|
|
61
|
+
// parse before validating
|
|
62
|
+
for (const path of Array.isArray(route.path) ? route.path : [route.path]) {
|
|
63
|
+
this.parsePathParams(request, path);
|
|
64
|
+
}
|
|
65
|
+
// Validate path
|
|
66
|
+
if (route.pathSchema) {
|
|
67
|
+
const validationResp = Validator.validateSchema(request.getPathParams(), route.pathSchema);
|
|
68
|
+
if (validationResp && validationResp instanceof Response)
|
|
69
|
+
return validationResp;
|
|
70
|
+
}
|
|
71
|
+
// Continue to route handler
|
|
72
|
+
return yield route.handler(transaction);
|
|
73
|
+
}
|
|
74
|
+
//No route found :/
|
|
75
|
+
return new Response(404, { err: 'Route not found!' });
|
|
76
|
+
}));
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Parses the path parameters from the request URL based on the given route path.
|
|
81
|
+
* @param {Request<any>} req - The request object.
|
|
82
|
+
* @param {string} routePath - The route path pattern to match against.
|
|
83
|
+
* @returns None
|
|
84
|
+
*/
|
|
85
|
+
parsePathParams(req, routePath) {
|
|
86
|
+
const path = req.getPath();
|
|
87
|
+
const regex = pathToRegexp(routePath);
|
|
88
|
+
const result = regex.regexp.exec(path);
|
|
89
|
+
if (result)
|
|
90
|
+
req.setFixedPathParams(regex.keys, result);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
//# sourceMappingURL=Server.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Server.js","sourceRoot":"","sources":["../../../../src/Server/lib/Server.ts"],"names":[],"mappings":";;;;;;;;;AACA,OAAO,EAAE,YAAY,EAA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I,CAAC,CAAA;QACtC,IAAI,MAAM;YAAE,GAAG,CAAC,kBAAkB,CAAC,KAAK,CAAC,IAAI,EAAE,MAAM,CAAC,CAAA;IACxD,CAAC;CACF"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Request, Response } from 'express';
|
|
2
|
+
import Server from '../Server.js';
|
|
3
|
+
/**
|
|
4
|
+
* Creates an async function that handles serverless events and sends a response.
|
|
5
|
+
* @param {Server['handleServerlessEvent']} serverlessHandler - The serverless handler function.
|
|
6
|
+
* @returns {Function} - An async function that handles the request and sends a response.
|
|
7
|
+
*/
|
|
8
|
+
declare const _default: (serverlessHandler: Server["handleServerlessEvent"]) => (request: Request, res: Response) => Promise<void>;
|
|
9
|
+
export default _default;
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import GenericHandlerEvent from './GenericHandlerEvent.js';
|
|
11
|
+
import Globals from '../../../Globals.js';
|
|
12
|
+
import Utils from '../../../Util/Utils.js';
|
|
13
|
+
/**
|
|
14
|
+
* Creates an async function that handles serverless events and sends a response.
|
|
15
|
+
* @param {Server['handleServerlessEvent']} serverlessHandler - The serverless handler function.
|
|
16
|
+
* @returns {Function} - An async function that handles the request and sends a response.
|
|
17
|
+
*/
|
|
18
|
+
export default (serverlessHandler) => {
|
|
19
|
+
/**
|
|
20
|
+
* Handles an HTTP request by invoking a serverless function and processing the response.
|
|
21
|
+
* @param {Request} request - The HTTP request object.
|
|
22
|
+
* @param {Response} res - The HTTP response object.
|
|
23
|
+
* @returns None
|
|
24
|
+
*/
|
|
25
|
+
return (request, res) => __awaiter(void 0, void 0, void 0, function* () {
|
|
26
|
+
const startTime = Date.now();
|
|
27
|
+
try {
|
|
28
|
+
// Generate event with request stuff (http to serverless translation)
|
|
29
|
+
const event = new GenericHandlerEvent(request, serverlessHandler);
|
|
30
|
+
// Invoke
|
|
31
|
+
const invokationResp = yield event.invoke();
|
|
32
|
+
// Respond
|
|
33
|
+
processServerlessResponse(invokationResp, res);
|
|
34
|
+
}
|
|
35
|
+
catch (e) {
|
|
36
|
+
console.error('[Proxy] - Exception during execution!', e);
|
|
37
|
+
console.error(e.stack);
|
|
38
|
+
res.status(Globals.Resp_STATUSCODE_EXCEPTION).json(Object.assign(Object.assign({}, e), { err: Globals.Resp_MSG_EXCEPTION, errCode: Globals.Resp_CODE_EXCEPTION }));
|
|
39
|
+
}
|
|
40
|
+
console.debug(`[Proxy] - Request took ${Date.now() - startTime}ms`);
|
|
41
|
+
});
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* Processes the response received from a serverless function invocation and sends the appropriate response back to the client.
|
|
45
|
+
* @param {GenericHandlerEventResponse} invokation - The response object received from the serverless function invocation.
|
|
46
|
+
* @param {Response} res - The response object to send back to the client.
|
|
47
|
+
* @returns The response object to send back to the client.
|
|
48
|
+
*/
|
|
49
|
+
const processServerlessResponse = (invokation, res) => {
|
|
50
|
+
var _a, _b;
|
|
51
|
+
// translate answer to http layer
|
|
52
|
+
if (invokation && invokation.err) {
|
|
53
|
+
// err came
|
|
54
|
+
return res.json({ err: invokation.err }).status(400);
|
|
55
|
+
}
|
|
56
|
+
else if (!invokation || !invokation.data) {
|
|
57
|
+
// invalid response came
|
|
58
|
+
return res.status(Globals.Resp_STATUSCODE_INVALIDRESP).json({
|
|
59
|
+
err: Globals.Resp_MSG_INVALIDRESP,
|
|
60
|
+
errCode: Globals.Resp_CODE_INVALIDRESP,
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
// Check for headers
|
|
65
|
+
if (invokation.data.headers) {
|
|
66
|
+
for (const hKey of Object.keys(invokation.data.headers)) {
|
|
67
|
+
res.header(hKey, invokation.data.headers[hKey]);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
//
|
|
71
|
+
let response = res.status(invokation.data.statusCode);
|
|
72
|
+
// check for stream
|
|
73
|
+
if ((_b = (_a = invokation.data) === null || _a === void 0 ? void 0 : _a.body) === null || _b === void 0 ? void 0 : _b.pipe) {
|
|
74
|
+
invokation.data.body.pipe(response);
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
response = response.json(Utils.parseObjectNullIfEmpty(invokation.data.body) || {});
|
|
78
|
+
}
|
|
79
|
+
return response;
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
//# sourceMappingURL=GenericHandler.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"GenericHandler.js","sourceRoot":"","sources":["../../../../../src/Server/lib/container/GenericHandler.ts"],"names":[],"mappings":";;;;;;;;;AAEA,OAAO,mBAAoD,MAAM,0BAA0B,CAAA;AAC3F,OAAO,OAAO,MAAM,qBAAqB,CAAA;AACzC,OAAO,KAAK,MAAM,wBAAwB,CAAA;AAG1C;;;;GAIG;AACH,eAAe,CAAC,iBAAkD,EAAE,EAAE;IACpE;;;;;OAKG;IACH,OAAO,CAAO,OAAgB,EAAE,GAAa,EAAE,EAAE;QAC/C,MAAM,SAAS,GAAG,IAAI,CAAC,GAAG,EAAE,CAAA;QAC5B,IAAI,CAAC;YACH,qEAAqE;YACrE,MAAM,KAAK,GAAG,IAAI,mBAAmB,CAAC,OAAO,EAAE,iBAAiB,CAAC,CAAA;YACjE,SAAS;YACT,MAAM,cAAc,GAAG,MAAM,KAAK,CAAC,MAAM,EAAE,CAA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IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC;gBACxD,GAAG,CAAC,MAAM,CAAC,IAAI,EAAE,UAAU,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAA;YACjD,CAAC;QACH,CAAC;QACD,EAAE;QACF,IAAI,QAAQ,GAAG,GAAG,CAAC,MAAM,CAAC,UAAU,CAAC,IAAI,CAAC,UAAU,CAAC,CAAA;QACrD,mBAAmB;QACnB,IAAI,MAAA,MAAA,UAAU,CAAC,IAAI,0CAAE,IAAI,0CAAE,IAAI,EAAE,CAAC;YAChC,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA;QACrC,CAAC;aAAM,CAAC;YACN,QAAQ,GAAG,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,sBAAsB,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC,CAAA;QACpF,CAAC;QACD,OAAO,QAAQ,CAAA;IACjB,CAAC;AACH,CAAC,CAAA"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { Request } from 'express';
|
|
2
|
+
import Server from '../Server.js';
|
|
3
|
+
/**
|
|
4
|
+
* Represents the response object returned by a generic event handler.
|
|
5
|
+
* @typedef {Object} GenericHandlerEventResponse
|
|
6
|
+
* @property {Error | string} [err] - An optional error object or error message.
|
|
7
|
+
* @property {*} [data] - An optional data object.
|
|
8
|
+
*/
|
|
9
|
+
export type GenericHandlerEventResponse = {
|
|
10
|
+
err?: Error | string;
|
|
11
|
+
data?: any;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Represents a generic handler event for serverless functions.
|
|
15
|
+
*/
|
|
16
|
+
export default class GenericHandlerEvent {
|
|
17
|
+
/**
|
|
18
|
+
* Represents an HTTP request.
|
|
19
|
+
* @property {Request} request - The HTTP request object.
|
|
20
|
+
*/
|
|
21
|
+
request: Request;
|
|
22
|
+
/**
|
|
23
|
+
* The handler function for serverless events in a server.
|
|
24
|
+
* @param {Server['handleServerlessEvent']} serverlessHandler - The function that handles serverless events.
|
|
25
|
+
* @returns None
|
|
26
|
+
*/
|
|
27
|
+
serverlessHandler: Server['handleServerlessEvent'];
|
|
28
|
+
/**
|
|
29
|
+
* Constructs a new instance of the class.
|
|
30
|
+
* @param {Request} request - The request object.
|
|
31
|
+
* @param {Server['handleServerlessEvent']} serverlessHandler - The serverless handler function.
|
|
32
|
+
* @returns None
|
|
33
|
+
*/
|
|
34
|
+
constructor(request: Request, serverlessHandler: Server['handleServerlessEvent']);
|
|
35
|
+
/**
|
|
36
|
+
* Invokes the handler function asynchronously and returns a promise that resolves to a GenericHandlerEventResponse.
|
|
37
|
+
* @returns {Promise<GenericHandlerEventResponse>} A promise that resolves to a GenericHandlerEventResponse.
|
|
38
|
+
*/
|
|
39
|
+
invoke(): Promise<GenericHandlerEventResponse>;
|
|
40
|
+
/**
|
|
41
|
+
* Builds and returns an APIGatewayProxyEvent object based on the current request.
|
|
42
|
+
* @returns {APIGatewayProxyEvent} - The constructed APIGatewayProxyEvent object.
|
|
43
|
+
*/
|
|
44
|
+
private buildEvent;
|
|
45
|
+
/**
|
|
46
|
+
* Builds and returns a context object for an AWS Lambda function.
|
|
47
|
+
* @param {APIGatewayProxyEvent} event - The event object passed to the Lambda function.
|
|
48
|
+
* @param {(err?: Error | string, data?: any) => void} callback - The callback function to be called when the Lambda function completes.
|
|
49
|
+
* @returns {Context} - The context object for the Lambda function.
|
|
50
|
+
*/
|
|
51
|
+
private buildContext;
|
|
52
|
+
}
|