kool-koala 1.6.1 → 1.7.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.
@@ -1,10 +1,11 @@
1
1
  import { ControllerConstructor } from "../controllers";
2
- import { AuthenticableEntity } from "../types";
2
+ import { AuthenticableEntity, Context } from "../types";
3
3
  import { StringEnum } from "../types/common/string-enum";
4
4
  import { JwtConfigurationParameters } from "./jwt-configuration-parameters";
5
5
  import { DatabaseConfigurationParamters } from "./database-configuration-paramters";
6
6
  import { RepositoryBase } from "../database";
7
7
  import { StaticFilesConfigurationParameters } from "./static-files-configuration-parameters";
8
+ import { Next } from "koa";
8
9
  export interface ConfigurationParameters<U extends AuthenticableEntity = AuthenticableEntity, P extends StringEnum = {}> {
9
10
  port: number;
10
11
  controllers?: (ControllerConstructor)[];
@@ -14,4 +15,5 @@ export interface ConfigurationParameters<U extends AuthenticableEntity = Authent
14
15
  permissionType?: P;
15
16
  restPrefix?: string;
16
17
  staticFiles?: StaticFilesConfigurationParameters[];
18
+ middlewares?: ((context: Context, next: Next) => Promise<void>)[];
17
19
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/../src/common/configuration-parameters.ts"],"names":[],"mappings":"","file":"configuration-parameters.js","sourcesContent":["import { ControllerConstructor } from \"../controllers\";\nimport { AuthenticableEntity } from \"../types\";\nimport { StringEnum } from \"../types/common/string-enum\";\nimport { JwtConfigurationParameters } from \"./jwt-configuration-parameters\";\nimport { DatabaseConfigurationParamters } from \"./database-configuration-paramters\";\nimport { RepositoryBase } from \"../database\";\nimport { StaticFilesConfigurationParameters } from \"./static-files-configuration-parameters\";\n\nexport interface ConfigurationParameters<\n U extends AuthenticableEntity = AuthenticableEntity,\n P extends StringEnum = {}\n> {\n port: number,\n controllers?: (ControllerConstructor)[],\n database?: DatabaseConfigurationParamters,\n jwt?: JwtConfigurationParameters,\n userRepository?: RepositoryBase<U>,\n permissionType?: P,\n restPrefix?: string,\n staticFiles?: StaticFilesConfigurationParameters[]\n}\n"]}
1
+ {"version":3,"sources":["../src/../src/common/configuration-parameters.ts"],"names":[],"mappings":"","file":"configuration-parameters.js","sourcesContent":["import { ControllerConstructor } from \"../controllers\";\nimport { AuthenticableEntity, Context } from \"../types\";\nimport { StringEnum } from \"../types/common/string-enum\";\nimport { JwtConfigurationParameters } from \"./jwt-configuration-parameters\";\nimport { DatabaseConfigurationParamters } from \"./database-configuration-paramters\";\nimport { RepositoryBase } from \"../database\";\nimport { StaticFilesConfigurationParameters } from \"./static-files-configuration-parameters\";\nimport { Next } from \"koa\";\n\nexport interface ConfigurationParameters<\n U extends AuthenticableEntity = AuthenticableEntity,\n P extends StringEnum = {}\n> {\n port: number,\n controllers?: (ControllerConstructor)[],\n database?: DatabaseConfigurationParamters,\n jwt?: JwtConfigurationParameters,\n userRepository?: RepositoryBase<U>,\n permissionType?: P,\n restPrefix?: string,\n staticFiles?: StaticFilesConfigurationParameters[]\n middlewares?: ((context: Context, next: Next) => Promise<void>)[];\n}\n"]}
@@ -14,4 +14,5 @@ export declare class Configuration<U extends AuthenticableEntity, P extends Stri
14
14
  getPermissionType(): P;
15
15
  getRestPrefix(): string;
16
16
  getStaticFilesConfiguration(): import("./static-files-configuration-parameters").StaticFilesConfigurationParameters[];
17
+ getMiddlewares(): ((context: import("..").Context, next: import("koa").Next) => Promise<void>)[];
17
18
  }
@@ -33,6 +33,9 @@ class Configuration {
33
33
  getStaticFilesConfiguration() {
34
34
  return this.parameters.staticFiles;
35
35
  }
36
+ getMiddlewares() {
37
+ return this.parameters.middlewares;
38
+ }
36
39
  }
37
40
  exports.Configuration = Configuration;
38
41
 
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/../src/common/configuration.ts"],"names":[],"mappings":";;;AAKA,MAAa,aAAa;IAIxB,YACU,UAAyC;QAAzC,eAAU,GAAV,UAAU,CAA+B;IAC/C,CAAC;IACL,MAAM,CAAC,WAAW,CAGhB,uBAAsD;QACtD,OAAO,IAAI,aAAa,CACtB,uBAAuB,CACxB,CAAC;IACJ,CAAC;IACD,cAAc;QACZ,OAAO,IAAI,CAAC,UAAU,CAAC,WAAW,CAAC;IACrC,CAAC;IACD,WAAW;QACT,OAAO,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC;IAClC,CAAC;IACD,OAAO;QACL,OAAO,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC;IAC9B,CAAC;IACD,gBAAgB;QACd,OAAO,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC;IAC7B,CAAC;IACD,iBAAiB;QACf,OAAO,IAAI,CAAC,UAAU,CAAC,cAAc,CAAC;IACxC,CAAC;IACD,iBAAiB;QACf,OAAO,IAAI,CAAC,UAAU,CAAC,cAAc,CAAC;IACxC,CAAC;IACD,aAAa;;QACX,OAAO,MAAA,IAAI,CAAC,UAAU,CAAC,UAAU,mCAAI,MAAM,CAAC;IAC9C,CAAC;IACD,2BAA2B;QACzB,OAAO,IAAI,CAAC,UAAU,CAAC,WAAW,CAAC;IACrC,CAAC;CACF;AAvCD,sCAuCC","file":"configuration.js","sourcesContent":["import { AuthenticableEntity } from \"../types/entities/authenticable-entity\";\nimport { ConfigurationParameters } from \"./configuration-parameters\";\nimport { StringEnum } from \"../types/common/string-enum\";\nimport { JwtConfigurationParameters } from \"./jwt-configuration-parameters\";\n\nexport class Configuration<\n U extends AuthenticableEntity,\n P extends StringEnum\n> {\n constructor(\n private parameters: ConfigurationParameters<U, P>\n ) { }\n static instantiate<\n T extends AuthenticableEntity,\n Q extends StringEnum\n >(configurationParameters: ConfigurationParameters<T, Q>): Configuration<T, Q> {\n return new Configuration<T, Q>(\n configurationParameters\n );\n }\n getControllers() {\n return this.parameters.controllers;\n }\n getDatabase() {\n return this.parameters.database;\n }\n getPort(): number {\n return this.parameters.port;\n }\n getJwtParameters(): JwtConfigurationParameters {\n return this.parameters.jwt;\n }\n getUserRepository() {\n return this.parameters.userRepository;\n }\n getPermissionType() {\n return this.parameters.permissionType;\n }\n getRestPrefix() {\n return this.parameters.restPrefix ?? '/api';\n }\n getStaticFilesConfiguration() {\n return this.parameters.staticFiles;\n }\n}\n"]}
1
+ {"version":3,"sources":["../src/../src/common/configuration.ts"],"names":[],"mappings":";;;AAKA,MAAa,aAAa;IAIxB,YACU,UAAyC;QAAzC,eAAU,GAAV,UAAU,CAA+B;IAC/C,CAAC;IACL,MAAM,CAAC,WAAW,CAGhB,uBAAsD;QACtD,OAAO,IAAI,aAAa,CACtB,uBAAuB,CACxB,CAAC;IACJ,CAAC;IACD,cAAc;QACZ,OAAO,IAAI,CAAC,UAAU,CAAC,WAAW,CAAC;IACrC,CAAC;IACD,WAAW;QACT,OAAO,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC;IAClC,CAAC;IACD,OAAO;QACL,OAAO,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC;IAC9B,CAAC;IACD,gBAAgB;QACd,OAAO,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC;IAC7B,CAAC;IACD,iBAAiB;QACf,OAAO,IAAI,CAAC,UAAU,CAAC,cAAc,CAAC;IACxC,CAAC;IACD,iBAAiB;QACf,OAAO,IAAI,CAAC,UAAU,CAAC,cAAc,CAAC;IACxC,CAAC;IACD,aAAa;;QACX,OAAO,MAAA,IAAI,CAAC,UAAU,CAAC,UAAU,mCAAI,MAAM,CAAC;IAC9C,CAAC;IACD,2BAA2B;QACzB,OAAO,IAAI,CAAC,UAAU,CAAC,WAAW,CAAC;IACrC,CAAC;IACD,cAAc;QACZ,OAAO,IAAI,CAAC,UAAU,CAAC,WAAW,CAAC;IACrC,CAAC;CACF;AA1CD,sCA0CC","file":"configuration.js","sourcesContent":["import { AuthenticableEntity } from \"../types/entities/authenticable-entity\";\nimport { ConfigurationParameters } from \"./configuration-parameters\";\nimport { StringEnum } from \"../types/common/string-enum\";\nimport { JwtConfigurationParameters } from \"./jwt-configuration-parameters\";\n\nexport class Configuration<\n U extends AuthenticableEntity,\n P extends StringEnum\n> {\n constructor(\n private parameters: ConfigurationParameters<U, P>\n ) { }\n static instantiate<\n T extends AuthenticableEntity,\n Q extends StringEnum\n >(configurationParameters: ConfigurationParameters<T, Q>): Configuration<T, Q> {\n return new Configuration<T, Q>(\n configurationParameters\n );\n }\n getControllers() {\n return this.parameters.controllers;\n }\n getDatabase() {\n return this.parameters.database;\n }\n getPort(): number {\n return this.parameters.port;\n }\n getJwtParameters(): JwtConfigurationParameters {\n return this.parameters.jwt;\n }\n getUserRepository() {\n return this.parameters.userRepository;\n }\n getPermissionType() {\n return this.parameters.permissionType;\n }\n getRestPrefix() {\n return this.parameters.restPrefix ?? '/api';\n }\n getStaticFilesConfiguration() {\n return this.parameters.staticFiles;\n }\n getMiddlewares() {\n return this.parameters.middlewares;\n }\n}\n"]}
@@ -24,8 +24,9 @@ export declare class KoalApp<U extends AuthenticableEntity, P extends StringEnum
24
24
  setupDatabaseConnection(): Promise<void>;
25
25
  setupErrorHandler(): void;
26
26
  setupTransaction(): void;
27
- registerStaticFileServerMiddleware(): void;
28
27
  registerEndpoints(): void;
28
+ registerStaticFileServerMiddleware(): void;
29
+ registerMiddlewaresFromConfiguration(): void;
29
30
  start(callback?: (configuration: Configuration<U, P>) => void): Promise<void>;
30
31
  stop(): Promise<void>;
31
32
  authorizationHeaderParser(context: Context, next: Next): Promise<void>;
package/common/koalapp.js CHANGED
@@ -66,10 +66,12 @@ class KoalApp {
66
66
  console.log("Authorization header parser initialized.");
67
67
  this.koa.use((0, koa_bodyparser_1.default)());
68
68
  console.log("Body parser initialized.");
69
- this.registerStaticFileServerMiddleware();
70
- console.log("Static file server initialized.");
69
+ this.registerMiddlewaresFromConfiguration();
70
+ console.log("Middlewares registered from configuration.");
71
71
  this.registerEndpoints();
72
72
  console.log("Endpoints registered.");
73
+ this.registerStaticFileServerMiddleware();
74
+ console.log("Static file server initialized.");
73
75
  }
74
76
  catch (error) {
75
77
  console.log("Error during database initialization...", error);
@@ -91,6 +93,12 @@ class KoalApp {
91
93
  setupTransaction() {
92
94
  this.koa.use(transaction_middleware_1.transactionMiddleware);
93
95
  }
96
+ registerEndpoints() {
97
+ this.routerService = new router_service_1.RouterService(this.configuration.getControllers());
98
+ this.koa
99
+ .use(this.routerService.getRoutes())
100
+ .use(this.routerService.allowedMethods());
101
+ }
94
102
  registerStaticFileServerMiddleware() {
95
103
  if (!this.configuration.getStaticFilesConfiguration()) {
96
104
  return;
@@ -101,9 +109,8 @@ class KoalApp {
101
109
  this.koa.use((0, koa_static_1.default)(staticFilesPath));
102
110
  this.koa.use((ctx, next) => __awaiter(this, void 0, void 0, function* () {
103
111
  var _a, _b;
104
- console.log(`Request received for static file: ${ctx.request.path}`);
105
112
  const requestPath = ctx.request.path;
106
- if (!requestPath.startsWith(this.configuration.getRestPrefix()) && !/\.[a-z]+$/.test(requestPath)) {
113
+ if (!requestPath.startsWith(this.configuration.getRestPrefix()) && /\.[a-z]+$/.test(requestPath)) {
107
114
  ctx.type = (_a = staticFilesConfig.defaultFileMimeType) !== null && _a !== void 0 ? _a : 'html';
108
115
  ctx.body = fs_1.default.createReadStream(path_1.default.join(staticFilesPath, (_b = staticFilesConfig.defaultFile) !== null && _b !== void 0 ? _b : 'index.html'));
109
116
  }
@@ -113,11 +120,10 @@ class KoalApp {
113
120
  }));
114
121
  }
115
122
  }
116
- registerEndpoints() {
117
- this.routerService = new router_service_1.RouterService(this.configuration.getControllers());
118
- this.koa
119
- .use(this.routerService.getRoutes())
120
- .use(this.routerService.allowedMethods());
123
+ registerMiddlewaresFromConfiguration() {
124
+ for (const middleware of this.configuration.getMiddlewares() || []) {
125
+ this.koa.use(middleware);
126
+ }
121
127
  }
122
128
  start(callback) {
123
129
  return __awaiter(this, void 0, void 0, function* () {
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/../src/common/koalapp.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,8CAAgC;AAEhC,+DAA2D;AAG3D,+CAA2C;AAC3C,gEAA+B;AAC/B,oEAAwC;AACxC,oCAAmD;AACnD,4DA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AAM,CAAC,KAAK,EAAE,IAAI,CAAC,aAAa,CAAC,gBAAgB,EAAE,CAAC,SAAS,CAAC,CAAC;oBAC7G,MAAM,IAAI,EAAE,CAAC;gBACf,CAAC;gBAAC,OAAO,KAAK,EAAE,CAAC;oBACf,OAAO,CAAC,MAAM,GAAG,wBAAU,CAAC,YAAY,CAAC;oBACzC,OAAO,CAAC,IAAI,GAAG;wBACb,OAAO,EAAE,KAAK;wBACd,OAAO,EAAE,eAAe;qBACzB,CAAA;gBACH,CAAC;YACH,CAAC;iBACI,CAAC;gBACJ,MAAM,IAAI,EAAE,CAAC;YACf,CAAC;QACH,CAAC;KAAA;IAEK,YAAY,CAAC,OAAgB,EAAE,IAAU;;YAC7C,IAAI,CAAC;gBACH,MAAM,IAAI,EAAE,CAAC;YACf,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,IAAI,KAAK,YAAY,iBAAS,EAAE,CAAC;oBAC/B,OAAO,CAAC,MAAM,GAAG,KAAK,CAAC,aAAa,EAAE,CAAC;oBACvC,OAAO,CAAC,IAAI,GAAiB;wBAC3B,OAAO,EAAE,KAAK;wBACd,OAAO,EAAE,KAAK,CAAC,OAAO;qBACvB,CAAC;gBACJ,CAAC;qBACI,CAAC;oBACJ,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;oBACnB,OAAO,CAAC,MAAM,GAAG,wBAAU,CAAC,qBAAqB,CAAC;oBAClD,OAAO,CAAC,IAAI,GAAiB;wBAC3B,OAAO,EAAE,KAAK;qBACf,CAAC;gBACJ,CAAC;YACH,CAAC;QACH,CAAC;KAAA;IAED,uBAAuB;QACrB,OAAO,IAAI,CAAC,oBAAoB,CAAC;IACnC,CAAC;CACF;AAtLD,0BAsLC","file":"koalapp.js","sourcesContent":["import Koa, { Next } from 'koa';\nimport { Configuration } from './configuration';\nimport { RouterService } from '../services/router-service';\nimport { AuthenticableEntity } from '../types/entities/authenticable-entity';\nimport { DataSource } from 'typeorm';\nimport { StatusCode } from './status-code';\nimport jwt from 'jsonwebtoken';\nimport bodyParser from 'koa-bodyparser';\nimport { BaseResponse, ErrorBase } from '../types';\nimport serve from \"koa-static\";\nimport path from \"path\";\nimport fs from \"fs\";\nimport { AuthorizationService } from '../services';\nimport { StringEnum } from '../types/common/string-enum';\nimport { errorHandlerMiddleware } from '../middlewares/error-handler-middleware';\nimport { transactionMiddleware } from '../middlewares/transaction-middleware';\nimport { Server } from 'http';\nimport { Context } from '../types/common/context';\n\nexport class KoalApp<\n U extends AuthenticableEntity,\n P extends StringEnum\n> {\n private static instance: KoalApp<any, any>;\n\n private koa = new Koa();\n private routerService: RouterService;\n private databaseConnection: DataSource;\n\n private server: Server;\n\n private authorizationService: AuthorizationService<U, P>;\n\n private constructor(private configuration: Configuration<U, P>) {\n }\n\n public static getInstance<\n T extends AuthenticableEntity,\n Q extends StringEnum\n >(configuration?: Configuration<T, Q>): KoalApp<T, Q> {\n if (!KoalApp.instance) {\n if (!configuration) {\n throw new Error(\"Configuration is required\");\n }\n KoalApp.instance = new KoalApp(configuration!);\n }\n return KoalApp.instance;\n }\n\n public static async resetInstance() {\n await KoalApp.getInstance().getDatabaseConnection().destroy();\n KoalApp.instance = undefined;\n }\n\n public getConfiguration(): Configuration<U, P> {\n return this.configuration;\n }\n\n public getRouterService(): RouterService {\n return this.routerService;\n }\n\n public getDatabaseConnection(): DataSource {\n return this.databaseConnection;\n }\n async initialize() {\n try {\n await this.setupDatabaseConnection();\n console.log(\"Database connection initialized.\");\n this.setupErrorHandler();\n console.log(\"Error handler initialized.\");\n this.setupTransaction();\n console.log(\"Transaction starter middleware initialized.\");\n this.koa.use(this.authorizationHeaderParser.bind(this));\n console.log(\"Authorization header parser initialized.\");\n this.koa.use(bodyParser());\n console.log(\"Body parser initialized.\");\n this.registerStaticFileServerMiddleware();\n console.log(\"Static file server initialized.\");\n this.registerEndpoints();\n console.log(\"Endpoints registered.\");\n } catch (error) {\n console.log(\"Error during database initialization...\", error);\n throw new Error('Error during application intialization...');\n }\n }\n async setupDatabaseConnection() {\n if (this.configuration.getDatabase()) {\n this.databaseConnection = await this.configuration.getDatabase().dataSource.initialize();\n await this.databaseConnection.runMigrations();\n }\n }\n setupErrorHandler() {\n this.koa.use(errorHandlerMiddleware);\n }\n setupTransaction() {\n this.koa.use(transactionMiddleware);\n }\n\n registerStaticFileServerMiddleware() {\n if (!this.configuration.getStaticFilesConfiguration()) {\n return;\n }\n for (const staticFilesConfig of this.configuration.getStaticFilesConfiguration()) {\n const staticFilesPath = path.isAbsolute(staticFilesConfig.folder) ? staticFilesConfig.folder : path.join(path.dirname(require.main.filename), staticFilesConfig.folder);\n console.log(`Serving files from: ${staticFilesPath}`);\n this.koa.use(serve(staticFilesPath));\n this.koa.use(async (ctx, next) => {\n console.log(`Request received for static file: ${ctx.request.path}`);\n const requestPath = ctx.request.path;\n\n if (!requestPath.startsWith(this.configuration.getRestPrefix()) && !/\\.[a-z]+$/.test(requestPath)) {\n ctx.type = staticFilesConfig.defaultFileMimeType ?? 'html';\n ctx.body = fs.createReadStream(path.join(staticFilesPath, staticFilesConfig.defaultFile ?? 'index.html'));\n } else {\n await next();\n }\n });\n }\n }\n\n registerEndpoints() {\n this.routerService = new RouterService(\n this.configuration.getControllers()\n );\n this.koa\n .use(this.routerService.getRoutes())\n .use(this.routerService.allowedMethods());\n }\n\n async start(callback?: (configuration: Configuration<U, P>) => void) {\n this.server = this.koa.listen(this.configuration.getPort(), () => {\n if (callback) {\n callback(this.configuration);\n }\n });\n }\n\n stop() {\n return new Promise<void>((resolve, reject) => {\n if (this.server) {\n this.server.close((error) => {\n if (!error) {\n resolve();\n }\n else {\n reject();\n }\n });\n this.server = undefined;\n }\n else {\n reject();\n }\n });\n }\n\n async authorizationHeaderParser(context: Context, next: Next) {\n const authHeader = context.headers.authorization;\n if (authHeader) {\n const token = authHeader.split(' ')[1];\n try {\n context.state.user = <AuthenticableEntity>jwt.verify(token, this.configuration.getJwtParameters().secretKey);\n await next();\n } catch (error) {\n context.status = StatusCode.UNAUTHORIZED;\n context.body = {\n success: false,\n message: 'Invalid token'\n }\n }\n }\n else {\n await next();\n }\n }\n\n async errorHandler(context: Context, next: Next) {\n try {\n await next();\n } catch (error) {\n if (error instanceof ErrorBase) {\n context.status = error.getStatusCode();\n context.body = <BaseResponse>{\n success: false,\n message: error.message\n };\n }\n else {\n console.log(error);\n context.status = StatusCode.INTERNAL_SERVER_ERROR;\n context.body = <BaseResponse>{\n success: false\n };\n }\n }\n }\n\n getAuthorizationService() {\n return this.authorizationService;\n }\n}"]}
1
+ {"version":3,"sources":["../src/../src/common/koalapp.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,8CAAgC;AAEhC,+DAA2D;AAG3D,+CAA2C;AAC3C,gEAA+B;AAC/B,oEAAwC;AACxC,oCAAmD;AACnD,4DAA+B;AAC/B,gDAAwB;AACxB,4CAAoB;AAGpB,sFAAiF;AACjF,kFAA8E;AAI9E,MAAa,OAAO;IAclB,YAA4B,aAAkC;QAAlC,kBAAa,GAAb,aAAa,CAAqB;QARtD,QAAG,GAAG,IAAI,aAAG,EAAE,CAAC;IASxB,CAAC;IAEM,MAAM,CAAC,WAAW,CAGvB,aAAmC;QACnC,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE,CAAC;YACtB,IAAI,CAAC,aAAa,EAAE,CAAC;gBACnB,MAAM,IAAI,KAAK,CAAC,2BAA2B,CAAC,CAAC;YAC/C,CAAC;YACD,OAAO,CAAC,QAAQ,GAAG,IAAI,OAAO,CAAC,aAAc,CAAC,CAAC;QACjD,CAAC;QACD,OAAO,OAAO,CAAC,QAAQ,CAAC;IAC1B,CAAC;IAEM,MAAM,CAAO,aAAa;;YAC/B,MAAM,OAAO,CAAC,WAAW,EAAE,CAAC,qBAAqB,EAAE,CAAC,OAAO,EAAE,CAAC;YAC9D,OAAO,CAAC,QAAQ,GAAG,SAAS,CAAC;QAC/B,CAAC;KAAA;IAEM,gBAAgB;QACrB,OAAO,IAAI,CAAC,aAAa,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IAAI,KAAK,YAAY,iBAAS,EAAE,CAAC;oBAC/B,OAAO,CAAC,MAAM,GAAG,KAAK,CAAC,aAAa,EAAE,CAAC;oBACvC,OAAO,CAAC,IAAI,GAAiB;wBAC3B,OAAO,EAAE,KAAK;wBACd,OAAO,EAAE,KAAK,CAAC,OAAO;qBACvB,CAAC;gBACJ,CAAC;qBACI,CAAC;oBACJ,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;oBACnB,OAAO,CAAC,MAAM,GAAG,wBAAU,CAAC,qBAAqB,CAAC;oBAClD,OAAO,CAAC,IAAI,GAAiB;wBAC3B,OAAO,EAAE,KAAK;qBACf,CAAC;gBACJ,CAAC;YACH,CAAC;QACH,CAAC;KAAA;IAED,uBAAuB;QACrB,OAAO,IAAI,CAAC,oBAAoB,CAAC;IACnC,CAAC;CACF;AA7LD,0BA6LC","file":"koalapp.js","sourcesContent":["import Koa, { Next } from 'koa';\nimport { Configuration } from './configuration';\nimport { RouterService } from '../services/router-service';\nimport { AuthenticableEntity } from '../types/entities/authenticable-entity';\nimport { DataSource } from 'typeorm';\nimport { StatusCode } from './status-code';\nimport jwt from 'jsonwebtoken';\nimport bodyParser from 'koa-bodyparser';\nimport { BaseResponse, ErrorBase } from '../types';\nimport serve from \"koa-static\";\nimport path from \"path\";\nimport fs from \"fs\";\nimport { AuthorizationService } from '../services';\nimport { StringEnum } from '../types/common/string-enum';\nimport { errorHandlerMiddleware } from '../middlewares/error-handler-middleware';\nimport { transactionMiddleware } from '../middlewares/transaction-middleware';\nimport { Server } from 'http';\nimport { Context } from '../types/common/context';\n\nexport class KoalApp<\n U extends AuthenticableEntity,\n P extends StringEnum\n> {\n private static instance: KoalApp<any, any>;\n\n private koa = new Koa();\n private routerService: RouterService;\n private databaseConnection: DataSource;\n\n private server: Server;\n\n private authorizationService: AuthorizationService<U, P>;\n\n private constructor(private configuration: Configuration<U, P>) {\n }\n\n public static getInstance<\n T extends AuthenticableEntity,\n Q extends StringEnum\n >(configuration?: Configuration<T, Q>): KoalApp<T, Q> {\n if (!KoalApp.instance) {\n if (!configuration) {\n throw new Error(\"Configuration is required\");\n }\n KoalApp.instance = new KoalApp(configuration!);\n }\n return KoalApp.instance;\n }\n\n public static async resetInstance() {\n await KoalApp.getInstance().getDatabaseConnection().destroy();\n KoalApp.instance = undefined;\n }\n\n public getConfiguration(): Configuration<U, P> {\n return this.configuration;\n }\n\n public getRouterService(): RouterService {\n return this.routerService;\n }\n\n public getDatabaseConnection(): DataSource {\n return this.databaseConnection;\n }\n async initialize() {\n try {\n await this.setupDatabaseConnection();\n console.log(\"Database connection initialized.\");\n this.setupErrorHandler();\n console.log(\"Error handler initialized.\");\n this.setupTransaction();\n console.log(\"Transaction starter middleware initialized.\");\n this.koa.use(this.authorizationHeaderParser.bind(this));\n console.log(\"Authorization header parser initialized.\");\n this.koa.use(bodyParser());\n console.log(\"Body parser initialized.\");\n this.registerMiddlewaresFromConfiguration();\n console.log(\"Middlewares registered from configuration.\");\n this.registerEndpoints();\n console.log(\"Endpoints registered.\");\n this.registerStaticFileServerMiddleware();\n console.log(\"Static file server initialized.\");\n } catch (error) {\n console.log(\"Error during database initialization...\", error);\n throw new Error('Error during application intialization...');\n }\n }\n async setupDatabaseConnection() {\n if (this.configuration.getDatabase()) {\n this.databaseConnection = await this.configuration.getDatabase().dataSource.initialize();\n await this.databaseConnection.runMigrations();\n }\n }\n setupErrorHandler() {\n this.koa.use(errorHandlerMiddleware);\n }\n setupTransaction() {\n this.koa.use(transactionMiddleware);\n }\n\n registerEndpoints() {\n this.routerService = new RouterService(\n this.configuration.getControllers()\n );\n this.koa\n .use(this.routerService.getRoutes())\n .use(this.routerService.allowedMethods());\n }\n\n registerStaticFileServerMiddleware() {\n if (!this.configuration.getStaticFilesConfiguration()) {\n return;\n }\n for (const staticFilesConfig of this.configuration.getStaticFilesConfiguration()) {\n const staticFilesPath = path.isAbsolute(staticFilesConfig.folder) ? staticFilesConfig.folder : path.join(path.dirname(require.main.filename), staticFilesConfig.folder);\n console.log(`Serving files from: ${staticFilesPath}`);\n this.koa.use(serve(staticFilesPath));\n this.koa.use(async (ctx, next) => {\n const requestPath = ctx.request.path;\n\n if (!requestPath.startsWith(this.configuration.getRestPrefix()) && /\\.[a-z]+$/.test(requestPath)) {\n ctx.type = staticFilesConfig.defaultFileMimeType ?? 'html';\n ctx.body = fs.createReadStream(path.join(staticFilesPath, staticFilesConfig.defaultFile ?? 'index.html'));\n } else {\n await next();\n }\n });\n }\n }\n\n registerMiddlewaresFromConfiguration() {\n for (const middleware of this.configuration.getMiddlewares() || []) {\n this.koa.use(middleware);\n }\n }\n\n async start(callback?: (configuration: Configuration<U, P>) => void) {\n this.server = this.koa.listen(this.configuration.getPort(), () => {\n if (callback) {\n callback(this.configuration);\n }\n });\n }\n\n stop() {\n return new Promise<void>((resolve, reject) => {\n if (this.server) {\n this.server.close((error) => {\n if (!error) {\n resolve();\n }\n else {\n reject();\n }\n });\n this.server = undefined;\n }\n else {\n reject();\n }\n });\n }\n\n async authorizationHeaderParser(context: Context, next: Next) {\n const authHeader = context.headers.authorization;\n if (authHeader) {\n const token = authHeader.split(' ')[1];\n try {\n context.state.user = <AuthenticableEntity>jwt.verify(token, this.configuration.getJwtParameters().secretKey);\n await next();\n } catch (error) {\n context.status = StatusCode.UNAUTHORIZED;\n context.body = {\n success: false,\n message: 'Invalid token'\n }\n }\n }\n else {\n await next();\n }\n }\n\n async errorHandler(context: Context, next: Next) {\n try {\n await next();\n } catch (error) {\n if (error instanceof ErrorBase) {\n context.status = error.getStatusCode();\n context.body = <BaseResponse>{\n success: false,\n message: error.message\n };\n }\n else {\n console.log(error);\n context.status = StatusCode.INTERNAL_SERVER_ERROR;\n context.body = <BaseResponse>{\n success: false\n };\n }\n }\n }\n\n getAuthorizationService() {\n return this.authorizationService;\n }\n}"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kool-koala",
3
- "version": "1.6.1",
3
+ "version": "1.7.0",
4
4
  "description": "Full-stack framework to create NodeJS applications on server side and Angular applications on the client side with ease.",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",