kool-koala 1.1.0 → 1.1.2

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.
@@ -0,0 +1,19 @@
1
+ import { DataSource, Repository } from "typeorm";
2
+ import { ControllerConstructor } from "../controllers";
3
+ import { AuthenticableEntity } from "../types";
4
+ export interface DatabaseConfigurationParamters {
5
+ dataSource: DataSource;
6
+ shouldRunMigrations: boolean;
7
+ }
8
+ export interface ConfigurationParameters<U extends AuthenticableEntity, P extends Record<string, string | number>> {
9
+ port: number;
10
+ controllers?: (ControllerConstructor)[];
11
+ database?: DatabaseConfigurationParamters;
12
+ jwt?: {
13
+ saltRounds: number;
14
+ secretKey: string;
15
+ };
16
+ userRepository?: Repository<U>;
17
+ permissionType?: P;
18
+ restPrefix?: string;
19
+ }
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/../src/common/configuration-parameters.ts"],"names":[],"mappings":"","file":"configuration-parameters.js","sourcesContent":["import { DataSource, Repository } from \"typeorm\";\nimport { ControllerConstructor } from \"../controllers\";\nimport { AuthenticableEntity } from \"../types\";\n\nexport interface DatabaseConfigurationParamters {\n dataSource: DataSource,\n shouldRunMigrations: boolean\n}\n\nexport interface ConfigurationParameters<\n U extends AuthenticableEntity,\n P extends Record<string, string | number>\n> {\n port: number,\n controllers: (ControllerConstructor)[],\n database?: DatabaseConfigurationParamters,\n jwt?: {\n saltRounds: number,\n secretKey: string\n },\n userRepository: Repository<U>,\n permissionType: P,\n restPrefix?: string,\n}"]}
1
+ {"version":3,"sources":["../src/../src/common/configuration-parameters.ts"],"names":[],"mappings":"","file":"configuration-parameters.js","sourcesContent":["import { DataSource, Repository } from \"typeorm\";\nimport { ControllerConstructor } from \"../controllers\";\nimport { AuthenticableEntity } from \"../types\";\n\nexport interface DatabaseConfigurationParamters {\n dataSource: DataSource,\n shouldRunMigrations: boolean\n}\n\nexport interface ConfigurationParameters<\n U extends AuthenticableEntity,\n P extends Record<string, string | number>\n> {\n port: number,\n controllers?: (ControllerConstructor)[],\n database?: DatabaseConfigurationParamters,\n jwt?: {\n saltRounds: number,\n secretKey: string\n },\n userRepository?: Repository<U>,\n permissionType?: P,\n restPrefix?: string,\n}"]}
@@ -0,0 +1,21 @@
1
+ import { Repository } from "typeorm";
2
+ import { ControllerConstructor } from "../controllers/controller-base";
3
+ import { AuthenticableEntity } from "../types/entities/authenticable-entity";
4
+ import { ConfigurationParameters, DatabaseConfigurationParamters } from "./configuration-parameters";
5
+ interface JwtParameters {
6
+ saltRounds: number;
7
+ secretKey: string;
8
+ }
9
+ export declare class Configuration<U extends AuthenticableEntity, P extends Record<string, string | number>> {
10
+ private parameters;
11
+ constructor(parameters: ConfigurationParameters<U, P>);
12
+ static instantiate<T extends AuthenticableEntity, Q extends Record<string, string | number>>(configurationParameters: ConfigurationParameters<T, Q>): Configuration<T, Q>;
13
+ getControllers(): ControllerConstructor[];
14
+ getDatabase(): DatabaseConfigurationParamters;
15
+ getPort(): number;
16
+ getJwtParameters(): JwtParameters;
17
+ getUserRepository(): Repository<U>;
18
+ getPermissionType(): P;
19
+ getRestPrefix(): string;
20
+ }
21
+ export {};
@@ -0,0 +1,4 @@
1
+ export * from './configuration';
2
+ export * from './configuration-parameters';
3
+ export * from './koalapp';
4
+ export * from './status-code';
@@ -0,0 +1,23 @@
1
+ import { Next, ParameterizedContext } from 'koa';
2
+ import { Configuration } from './configuration';
3
+ import { AuthenticableEntity } from '../types/entities/authenticable-entity';
4
+ import { DataSource } from 'typeorm';
5
+ import { AuthorizationService } from '../services';
6
+ export declare class KoalApp<U extends AuthenticableEntity, P extends Record<string, string | number>> {
7
+ private configuration;
8
+ private static instance;
9
+ private koa;
10
+ private routerService;
11
+ private databaseConnection;
12
+ private authorizationService;
13
+ private constructor();
14
+ static getInstance<T extends AuthenticableEntity, Q extends Record<string, string | number>>(configuration?: Configuration<T, Q>): KoalApp<T, Q>;
15
+ getConfiguration(): Configuration<U, P>;
16
+ getDatabaseConnection(): DataSource;
17
+ initialize(): Promise<void>;
18
+ registerStaticFileServerMiddleware(): void;
19
+ start(callback?: (configuration: Configuration<U, P>) => void): Promise<void>;
20
+ authorizationHeaderParser(context: ParameterizedContext, next: Next): Promise<void>;
21
+ errorHandler(context: ParameterizedContext, next: Next): Promise<void>;
22
+ getAuthorizationService(): AuthorizationService<U, P>;
23
+ }
@@ -0,0 +1,10 @@
1
+ export declare enum StatusCode {
2
+ OK = 200,
3
+ CREATED = 201,
4
+ BAD_REQUEST = 400,
5
+ I_AM_A_TEAPOT = 418,
6
+ UNAUTHORIZED = 401,
7
+ FORBIDDEN = 403,
8
+ NOT_FOUND = 404,
9
+ INTERNAL_SERVER_ERROR = 500
10
+ }
@@ -0,0 +1,8 @@
1
+ import Router from "koa-router";
2
+ export declare abstract class ControllerBase {
3
+ protected router: Router;
4
+ constructor(router: Router);
5
+ abstract registerEndpoints(): void;
6
+ protected getApiUrl(path: string): string;
7
+ }
8
+ export type ControllerConstructor = new (router: Router) => ControllerBase;
@@ -0,0 +1 @@
1
+ export * from './controller-base';
package/index.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ export * from './koalas';
2
+ export * from './common';
3
+ export * from './controllers';
4
+ export * from './services';
5
+ export * from './types';
@@ -0,0 +1 @@
1
+ export declare function printKoalaArt(): void;
package/package.json CHANGED
@@ -1,8 +1,9 @@
1
1
  {
2
2
  "name": "kool-koala",
3
- "version": "1.1.0",
3
+ "version": "1.1.2",
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
+ "types": "index.d.ts",
6
7
  "bin": {
7
8
  "kool-koala": "./bin/kool-koala.js"
8
9
  },
@@ -0,0 +1,5 @@
1
+ import { AuthenticableEntity } from '../types';
2
+ export declare class AuthorizationService<U extends AuthenticableEntity, P> {
3
+ constructor();
4
+ userHasRight(user: U, permission: P): Promise<void>;
5
+ }
@@ -11,20 +11,20 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.AuthorizationService = void 0;
13
13
  const authorization_error_1 = require("../types/errors/authorization-error");
14
+ const common_1 = require("../common");
14
15
  class AuthorizationService {
15
- constructor(userRepository) {
16
- this.userRepository = userRepository;
16
+ constructor() {
17
17
  }
18
18
  userHasRight(user, permission) {
19
19
  return __awaiter(this, void 0, void 0, function* () {
20
- user = yield this.userRepository.findOne({
20
+ user = (yield common_1.KoalApp.getInstance().getConfiguration().getUserRepository().findOne({
21
21
  where: {
22
22
  id: user.id
23
23
  },
24
24
  relations: {
25
25
  permissions: true
26
26
  }
27
- });
27
+ }));
28
28
  if (!user.permissions.some((p) => p.textId === permission)) {
29
29
  throw new authorization_error_1.AuthorizationError(user, permission);
30
30
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/../src/services/authorization-service.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,6EAAyE;AAGzE,MAAa,oBAAoB;IAI/B,YAAoB,cAA6B;QAA7B,mBAAc,GAAd,cAAc,CAAe;IACjD,CAAC;IACK,YAAY,CAAC,IAAO,EAAE,UAAa;;YACvC,IAAI,GAAG,MAAM,IAAI,CAAC,cAAc,CAAC,OAAO,CAAC;gBACvC,KAAK,EAAuB;oBAC1B,EAAE,EAAE,IAAI,CAAC,EAAE;iBACZ;gBACD,SAAS,EAA2B;oBAClC,WAAW,EAAE,IAAI;iBAClB;aACF,CAAC,CAAC;YACH,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,MAAM,KAAK,UAAU,CAAC,EAAE,CAAC;gBAC3D,MAAM,IAAI,wCAAkB,CAAC,IAAI,EAAE,UAAU,CAAC,CAAC;YACjD,CAAC;QACH,CAAC;KAAA;CACF;AAnBD,oDAmBC","file":"authorization-service.js","sourcesContent":["import { FindOptionsRelations, FindOptionsWhere, Repository } from 'typeorm';\nimport { AuthorizationError } from '../types/errors/authorization-error';\nimport { AuthenticableEntity } from '../types';\n\nexport class AuthorizationService<\n U extends AuthenticableEntity,\n P\n> {\n constructor(private userRepository: Repository<U>) {\n }\n async userHasRight(user: U, permission: P) {\n user = await this.userRepository.findOne({\n where: <FindOptionsWhere<U>>{\n id: user.id\n },\n relations: <FindOptionsRelations<U>>{\n permissions: true\n }\n });\n if (!user.permissions.some((p) => p.textId === permission)) {\n throw new AuthorizationError(user, permission);\n }\n }\n}"]}
1
+ {"version":3,"sources":["../src/../src/services/authorization-service.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,6EAAyE;AAEzE,sCAAoC;AAEpC,MAAa,oBAAoB;IAI/B;IACA,CAAC;IACK,YAAY,CAAC,IAAO,EAAE,UAAa;;YACvC,IAAI,IAAM,MAAM,gBAAO,CAAC,WAAW,EAAE,CAAC,gBAAgB,EAAE,CAAC,iBAAiB,EAAE,CAAC,OAAO,CAAC;gBACnF,KAAK,EAAuB;oBAC1B,EAAE,EAAE,IAAI,CAAC,EAAE;iBACZ;gBACD,SAAS,EAA2B;oBAClC,WAAW,EAAE,IAAI;iBAClB;aACF,CAAC,CAAA,CAAC;YACH,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,MAAM,KAAK,UAAU,CAAC,EAAE,CAAC;gBAC3D,MAAM,IAAI,wCAAkB,CAAC,IAAI,EAAE,UAAU,CAAC,CAAC;YACjD,CAAC;QACH,CAAC;KAAA;CACF;AAnBD,oDAmBC","file":"authorization-service.js","sourcesContent":["import { FindOptionsRelations, FindOptionsWhere, Repository } from 'typeorm';\nimport { AuthorizationError } from '../types/errors/authorization-error';\nimport { AuthenticableEntity } from '../types';\nimport { KoalApp } from '../common';\n\nexport class AuthorizationService<\n U extends AuthenticableEntity,\n P\n> {\n constructor() {\n }\n async userHasRight(user: U, permission: P) {\n user = <U>await KoalApp.getInstance().getConfiguration().getUserRepository().findOne({\n where: <FindOptionsWhere<U>>{\n id: user.id\n },\n relations: <FindOptionsRelations<U>>{\n permissions: true\n }\n });\n if (!user.permissions.some((p) => p.textId === permission)) {\n throw new AuthorizationError(user, permission);\n }\n }\n}"]}
@@ -0,0 +1,2 @@
1
+ export * from './authorization-service';
2
+ export * from './router-service';
@@ -0,0 +1,9 @@
1
+ import Router from "koa-router";
2
+ import { ControllerConstructor } from "../controllers/controller-base";
3
+ export declare class RouterService {
4
+ private controllers;
5
+ private router;
6
+ constructor(controllers: (ControllerConstructor)[]);
7
+ getRoutes(): Router.IMiddleware<any, {}>;
8
+ allowedMethods(): Router.IMiddleware<any, {}>;
9
+ }
@@ -7,9 +7,10 @@ exports.RouterService = void 0;
7
7
  const koa_router_1 = __importDefault(require("koa-router"));
8
8
  class RouterService {
9
9
  constructor(controllers) {
10
+ var _a;
10
11
  this.controllers = controllers;
11
12
  this.router = new koa_router_1.default();
12
- this.controllers.forEach((controllerType) => {
13
+ ((_a = this.controllers) !== null && _a !== void 0 ? _a : []).forEach((controllerType) => {
13
14
  console.log(`Register endpoints of '${controllerType.name}'`);
14
15
  const controller = new controllerType(this.router);
15
16
  controller.registerEndpoints();
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/../src/services/router-service.ts"],"names":[],"mappings":";;;;;;AAAA,4DAAgC;AAGhC,MAAa,aAAa;IAExB,YAAoB,WAAsC;QAAtC,gBAAW,GAAX,WAAW,CAA2B;QACxD,IAAI,CAAC,MAAM,GAAG,IAAI,oBAAM,EAAE,CAAC;QAC3B,IAAI,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC,cAAc,EAAE,EAAE;YAC1C,OAAO,CAAC,GAAG,CAAC,0BAA0B,cAAc,CAAC,IAAI,GAAG,CAAC,CAAC;YAC9D,MAAM,UAAU,GAAG,IAAI,cAAc,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;YACnD,UAAU,CAAC,iBAAiB,EAAE,CAAC;QACjC,CAAC,CAAC,CAAC;IACL,CAAC;IAED,SAAS;QACP,OAAO,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC;IAC9B,CAAC;IAED,cAAc;QACZ,OAAO,IAAI,CAAC,MAAM,CAAC,cAAc,EAAE,CAAC;IACtC,CAAC;CACF;AAlBD,sCAkBC","file":"router-service.js","sourcesContent":["import Router from \"koa-router\";\nimport { ControllerConstructor } from \"../controllers/controller-base\";\n\nexport class RouterService {\n private router: Router;\n constructor(private controllers: (ControllerConstructor)[]) {\n this.router = new Router();\n this.controllers.forEach((controllerType) => {\n console.log(`Register endpoints of '${controllerType.name}'`);\n const controller = new controllerType(this.router);\n controller.registerEndpoints();\n });\n }\n\n getRoutes() {\n return this.router.routes();\n }\n\n allowedMethods() {\n return this.router.allowedMethods();\n }\n}\n"]}
1
+ {"version":3,"sources":["../src/../src/services/router-service.ts"],"names":[],"mappings":";;;;;;AAAA,4DAAgC;AAGhC,MAAa,aAAa;IAExB,YAAoB,WAAsC;;QAAtC,gBAAW,GAAX,WAAW,CAA2B;QACxD,IAAI,CAAC,MAAM,GAAG,IAAI,oBAAM,EAAE,CAAC;QAC3B,CAAC,MAAA,IAAI,CAAC,WAAW,mCAAI,EAAE,CAAC,CAAC,OAAO,CAAC,CAAC,cAAc,EAAE,EAAE;YAClD,OAAO,CAAC,GAAG,CAAC,0BAA0B,cAAc,CAAC,IAAI,GAAG,CAAC,CAAC;YAC9D,MAAM,UAAU,GAAG,IAAI,cAAc,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;YACnD,UAAU,CAAC,iBAAiB,EAAE,CAAC;QACjC,CAAC,CAAC,CAAC;IACL,CAAC;IAED,SAAS;QACP,OAAO,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC;IAC9B,CAAC;IAED,cAAc;QACZ,OAAO,IAAI,CAAC,MAAM,CAAC,cAAc,EAAE,CAAC;IACtC,CAAC;CACF;AAlBD,sCAkBC","file":"router-service.js","sourcesContent":["import Router from \"koa-router\";\nimport { ControllerConstructor } from \"../controllers/controller-base\";\n\nexport class RouterService {\n private router: Router;\n constructor(private controllers: (ControllerConstructor)[]) {\n this.router = new Router();\n (this.controllers ?? []).forEach((controllerType) => {\n console.log(`Register endpoints of '${controllerType.name}'`);\n const controller = new controllerType(this.router);\n controller.registerEndpoints();\n });\n }\n\n getRoutes() {\n return this.router.routes();\n }\n\n allowedMethods() {\n return this.router.allowedMethods();\n }\n}\n"]}
@@ -0,0 +1,4 @@
1
+ import { HumanIdentifiableEntity } from "./human-identifiable-entity";
2
+ import { IdentifiableEntity } from "./identifiable-entity";
3
+ import { PermissionHolderEntity } from "./permission-holder-entity";
4
+ export type AuthenticableEntity = IdentifiableEntity & PermissionHolderEntity & HumanIdentifiableEntity;
@@ -0,0 +1,3 @@
1
+ export interface HumanIdentifiableEntity {
2
+ username: string;
3
+ }
@@ -0,0 +1,3 @@
1
+ export interface IdentifiableEntity {
2
+ id: number;
3
+ }
@@ -0,0 +1,6 @@
1
+ export * from './authenticable-entity';
2
+ export * from './human-identifiable-entity';
3
+ export * from './identifiable-entity';
4
+ export * from './permission-entity';
5
+ export * from './permission-holder-entity';
6
+ export * from './user-entity';
@@ -0,0 +1,4 @@
1
+ import { IdentifiableEntity } from "./identifiable-entity";
2
+ export interface PermissionEntity extends IdentifiableEntity {
3
+ textId: string;
4
+ }
@@ -0,0 +1,4 @@
1
+ import { PermissionEntity } from "./permission-entity";
2
+ export interface PermissionHolderEntity {
3
+ permissions: PermissionEntity[];
4
+ }
@@ -0,0 +1,4 @@
1
+ import { IdentifiableEntity } from "./identifiable-entity";
2
+ export interface UserEntity extends IdentifiableEntity {
3
+ username: string;
4
+ }
@@ -0,0 +1,5 @@
1
+ import { HumanIdentifiableEntity } from "../entities/human-identifiable-entity";
2
+ import { ErrorBase } from "./error-base";
3
+ export declare class AuthorizationError<U extends HumanIdentifiableEntity, PT> extends ErrorBase {
4
+ constructor(user: U, permission: PT);
5
+ }
@@ -0,0 +1,9 @@
1
+ import { StatusCode } from "../../common/status-code";
2
+ export declare class ErrorBase extends Error {
3
+ message: string;
4
+ protected statusCode: StatusCode;
5
+ protected error?: Error;
6
+ constructor(message: string, statusCode: StatusCode, error?: Error);
7
+ getStatusCode(): StatusCode;
8
+ getError(): Error | undefined;
9
+ }
@@ -0,0 +1,2 @@
1
+ export * from './authorization-error';
2
+ export * from './error-base';
@@ -0,0 +1,3 @@
1
+ export * from './entities';
2
+ export * from './errors';
3
+ export * from './responses';
@@ -0,0 +1,4 @@
1
+ export interface BaseResponse {
2
+ success: boolean;
3
+ message?: string;
4
+ }
@@ -0,0 +1 @@
1
+ export * from './base-response';