kool-koala 1.1.2 → 1.2.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/README.md CHANGED
@@ -3,4 +3,69 @@
3
3
  (\__/)
4
4
  (o.o ) KOOL KOALA
5
5
  (> < )
6
-
6
+
7
+ The purpose of this package is to make easy to create applications.
8
+
9
+ ## Prerequisites
10
+
11
+ The only prerequisites is to have the following dependencies installed in your project:
12
+
13
+ - koa
14
+ - koa-router
15
+ - typeorm
16
+
17
+ ## How to install
18
+
19
+ To use it in your project you only need to execute the following command in your project:
20
+
21
+ ```
22
+ npm i kool-koala
23
+ ```
24
+
25
+ ## Start an app
26
+
27
+ To start an application, you have to write only a few lines of code, like in the example below.
28
+
29
+ ```
30
+ import { Configuration, KoalApp } from 'kool-koala';
31
+
32
+ const configuration = new Configuration({
33
+ port: 8080,
34
+ });
35
+
36
+ KoalApp.getInstance(configuration).start((configuration) => {
37
+ console.log(`KoalApp is started.`);
38
+ });
39
+ ```
40
+
41
+ ## Controllers
42
+
43
+ To add endpoints to the application you have to create controllers and add them to the `controllers` attribute of the configuration.
44
+
45
+ A basic controller:
46
+
47
+ ```
48
+ import { ControllerBase, StatusCode } from "kool-koala";
49
+
50
+ export class TestController extends ControllerBase {
51
+ registerEndpoints(): void {
52
+ this.router.get(this.getApiUrl("/test"), (context, res) => {
53
+ context.body = "It is working!";
54
+ context.status = StatusCode.OK;
55
+ });
56
+ }
57
+ }
58
+ ```
59
+
60
+ To register this controller you have to modify the configuration of the application like this:
61
+
62
+ ```
63
+ import { TestController } from './test-controller';
64
+ ...
65
+ const configuration = new Configuration({
66
+ port: 8080,
67
+ controllers: [
68
+ TestController
69
+ ]
70
+ });
71
+ ```
@@ -1,10 +1,65 @@
1
1
  export declare enum StatusCode {
2
+ CONTINUE = 100,
3
+ SWITCHING_PROTOCOLS = 101,
4
+ PROCESSING = 102,
5
+ EARLY_HINTS = 103,
2
6
  OK = 200,
3
7
  CREATED = 201,
8
+ ACCEPTED = 202,
9
+ NON_AUTHORITATIVE_INFORMATION = 203,
10
+ NO_CONTENT = 204,
11
+ RESET_CONTENT = 205,
12
+ PARTIAL_CONTENT = 206,
13
+ MULTI_STATUS = 207,
14
+ ALREADY_REPORTED = 208,
15
+ IM_USED = 226,
16
+ MULTIPLE_CHOICES = 300,
17
+ MOVED_PERMANENTLY = 301,
18
+ FOUND = 302,
19
+ SEE_OTHER = 303,
20
+ NOT_MODIFIED = 304,
21
+ USE_PROXY = 305,
22
+ SWITCH_PROXY = 306,
23
+ TEMPORARY_REDIRECT = 307,
24
+ PERMANENT_REDIRECT = 308,
4
25
  BAD_REQUEST = 400,
5
- I_AM_A_TEAPOT = 418,
6
26
  UNAUTHORIZED = 401,
27
+ PAYMENT_REQUIRED = 402,
7
28
  FORBIDDEN = 403,
8
29
  NOT_FOUND = 404,
9
- INTERNAL_SERVER_ERROR = 500
30
+ METHOD_NOT_ALLOWED = 405,
31
+ NOT_ACCEPTABLE = 406,
32
+ PROXY_AUTHENTICATION_REQUIRED = 407,
33
+ REQUEST_TIMEOUT = 408,
34
+ CONFLICT = 409,
35
+ GONE = 410,
36
+ LENGTH_REQUIRED = 411,
37
+ PRECONDITION_FAILED = 412,
38
+ CONTENT_TOO_LARGE = 413,
39
+ URI_TOO_LONG = 414,
40
+ UNSUPPORTED_MEDIA_TYPE = 415,
41
+ RANGE_NOT_SATISFIABLE = 416,
42
+ EXPECTATION_FAILED = 417,
43
+ I_AM_A_TEAPOT = 418,
44
+ MISDIRECTED_REQUEST = 421,
45
+ UNPROCESSABLE_ENTITY = 422,
46
+ LOCKED = 423,
47
+ FAILED_DEPENDENCY = 424,
48
+ TOO_EARLY = 425,
49
+ UPGRADE_REQUIRED = 426,
50
+ PRECONDITION_REQUIRED = 428,
51
+ TOO_MANY_REQUESTS = 429,
52
+ REQUEST_HEADER_FIELDS_TOO_LARGE = 431,
53
+ UNAVAILABLE_FOR_LEGAL_REASONS = 451,
54
+ INTERNAL_SERVER_ERROR = 500,
55
+ NOT_IMPLEMENTED = 501,
56
+ BAD_GATEWAY = 502,
57
+ SERVICE_UNAVAILABLE = 503,
58
+ GATEWAY_TIMEOUT = 504,
59
+ HTTP_VERSION_NOT_SUPPORTED = 505,
60
+ VARIANT_ALSO_NEGOTIATES = 506,
61
+ INSUFFICIENT_STORAGE = 507,
62
+ LOOP_DETECTED = 508,
63
+ NOT_EXTENDED = 510,
64
+ NETWORK_AUTHENTICATION_REQUIRED = 511
10
65
  }
@@ -3,14 +3,69 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.StatusCode = void 0;
4
4
  var StatusCode;
5
5
  (function (StatusCode) {
6
+ StatusCode[StatusCode["CONTINUE"] = 100] = "CONTINUE";
7
+ StatusCode[StatusCode["SWITCHING_PROTOCOLS"] = 101] = "SWITCHING_PROTOCOLS";
8
+ StatusCode[StatusCode["PROCESSING"] = 102] = "PROCESSING";
9
+ StatusCode[StatusCode["EARLY_HINTS"] = 103] = "EARLY_HINTS";
6
10
  StatusCode[StatusCode["OK"] = 200] = "OK";
7
11
  StatusCode[StatusCode["CREATED"] = 201] = "CREATED";
12
+ StatusCode[StatusCode["ACCEPTED"] = 202] = "ACCEPTED";
13
+ StatusCode[StatusCode["NON_AUTHORITATIVE_INFORMATION"] = 203] = "NON_AUTHORITATIVE_INFORMATION";
14
+ StatusCode[StatusCode["NO_CONTENT"] = 204] = "NO_CONTENT";
15
+ StatusCode[StatusCode["RESET_CONTENT"] = 205] = "RESET_CONTENT";
16
+ StatusCode[StatusCode["PARTIAL_CONTENT"] = 206] = "PARTIAL_CONTENT";
17
+ StatusCode[StatusCode["MULTI_STATUS"] = 207] = "MULTI_STATUS";
18
+ StatusCode[StatusCode["ALREADY_REPORTED"] = 208] = "ALREADY_REPORTED";
19
+ StatusCode[StatusCode["IM_USED"] = 226] = "IM_USED";
20
+ StatusCode[StatusCode["MULTIPLE_CHOICES"] = 300] = "MULTIPLE_CHOICES";
21
+ StatusCode[StatusCode["MOVED_PERMANENTLY"] = 301] = "MOVED_PERMANENTLY";
22
+ StatusCode[StatusCode["FOUND"] = 302] = "FOUND";
23
+ StatusCode[StatusCode["SEE_OTHER"] = 303] = "SEE_OTHER";
24
+ StatusCode[StatusCode["NOT_MODIFIED"] = 304] = "NOT_MODIFIED";
25
+ StatusCode[StatusCode["USE_PROXY"] = 305] = "USE_PROXY";
26
+ StatusCode[StatusCode["SWITCH_PROXY"] = 306] = "SWITCH_PROXY";
27
+ StatusCode[StatusCode["TEMPORARY_REDIRECT"] = 307] = "TEMPORARY_REDIRECT";
28
+ StatusCode[StatusCode["PERMANENT_REDIRECT"] = 308] = "PERMANENT_REDIRECT";
8
29
  StatusCode[StatusCode["BAD_REQUEST"] = 400] = "BAD_REQUEST";
9
- StatusCode[StatusCode["I_AM_A_TEAPOT"] = 418] = "I_AM_A_TEAPOT";
10
30
  StatusCode[StatusCode["UNAUTHORIZED"] = 401] = "UNAUTHORIZED";
31
+ StatusCode[StatusCode["PAYMENT_REQUIRED"] = 402] = "PAYMENT_REQUIRED";
11
32
  StatusCode[StatusCode["FORBIDDEN"] = 403] = "FORBIDDEN";
12
33
  StatusCode[StatusCode["NOT_FOUND"] = 404] = "NOT_FOUND";
34
+ StatusCode[StatusCode["METHOD_NOT_ALLOWED"] = 405] = "METHOD_NOT_ALLOWED";
35
+ StatusCode[StatusCode["NOT_ACCEPTABLE"] = 406] = "NOT_ACCEPTABLE";
36
+ StatusCode[StatusCode["PROXY_AUTHENTICATION_REQUIRED"] = 407] = "PROXY_AUTHENTICATION_REQUIRED";
37
+ StatusCode[StatusCode["REQUEST_TIMEOUT"] = 408] = "REQUEST_TIMEOUT";
38
+ StatusCode[StatusCode["CONFLICT"] = 409] = "CONFLICT";
39
+ StatusCode[StatusCode["GONE"] = 410] = "GONE";
40
+ StatusCode[StatusCode["LENGTH_REQUIRED"] = 411] = "LENGTH_REQUIRED";
41
+ StatusCode[StatusCode["PRECONDITION_FAILED"] = 412] = "PRECONDITION_FAILED";
42
+ StatusCode[StatusCode["CONTENT_TOO_LARGE"] = 413] = "CONTENT_TOO_LARGE";
43
+ StatusCode[StatusCode["URI_TOO_LONG"] = 414] = "URI_TOO_LONG";
44
+ StatusCode[StatusCode["UNSUPPORTED_MEDIA_TYPE"] = 415] = "UNSUPPORTED_MEDIA_TYPE";
45
+ StatusCode[StatusCode["RANGE_NOT_SATISFIABLE"] = 416] = "RANGE_NOT_SATISFIABLE";
46
+ StatusCode[StatusCode["EXPECTATION_FAILED"] = 417] = "EXPECTATION_FAILED";
47
+ StatusCode[StatusCode["I_AM_A_TEAPOT"] = 418] = "I_AM_A_TEAPOT";
48
+ StatusCode[StatusCode["MISDIRECTED_REQUEST"] = 421] = "MISDIRECTED_REQUEST";
49
+ StatusCode[StatusCode["UNPROCESSABLE_ENTITY"] = 422] = "UNPROCESSABLE_ENTITY";
50
+ StatusCode[StatusCode["LOCKED"] = 423] = "LOCKED";
51
+ StatusCode[StatusCode["FAILED_DEPENDENCY"] = 424] = "FAILED_DEPENDENCY";
52
+ StatusCode[StatusCode["TOO_EARLY"] = 425] = "TOO_EARLY";
53
+ StatusCode[StatusCode["UPGRADE_REQUIRED"] = 426] = "UPGRADE_REQUIRED";
54
+ StatusCode[StatusCode["PRECONDITION_REQUIRED"] = 428] = "PRECONDITION_REQUIRED";
55
+ StatusCode[StatusCode["TOO_MANY_REQUESTS"] = 429] = "TOO_MANY_REQUESTS";
56
+ StatusCode[StatusCode["REQUEST_HEADER_FIELDS_TOO_LARGE"] = 431] = "REQUEST_HEADER_FIELDS_TOO_LARGE";
57
+ StatusCode[StatusCode["UNAVAILABLE_FOR_LEGAL_REASONS"] = 451] = "UNAVAILABLE_FOR_LEGAL_REASONS";
13
58
  StatusCode[StatusCode["INTERNAL_SERVER_ERROR"] = 500] = "INTERNAL_SERVER_ERROR";
59
+ StatusCode[StatusCode["NOT_IMPLEMENTED"] = 501] = "NOT_IMPLEMENTED";
60
+ StatusCode[StatusCode["BAD_GATEWAY"] = 502] = "BAD_GATEWAY";
61
+ StatusCode[StatusCode["SERVICE_UNAVAILABLE"] = 503] = "SERVICE_UNAVAILABLE";
62
+ StatusCode[StatusCode["GATEWAY_TIMEOUT"] = 504] = "GATEWAY_TIMEOUT";
63
+ StatusCode[StatusCode["HTTP_VERSION_NOT_SUPPORTED"] = 505] = "HTTP_VERSION_NOT_SUPPORTED";
64
+ StatusCode[StatusCode["VARIANT_ALSO_NEGOTIATES"] = 506] = "VARIANT_ALSO_NEGOTIATES";
65
+ StatusCode[StatusCode["INSUFFICIENT_STORAGE"] = 507] = "INSUFFICIENT_STORAGE";
66
+ StatusCode[StatusCode["LOOP_DETECTED"] = 508] = "LOOP_DETECTED";
67
+ StatusCode[StatusCode["NOT_EXTENDED"] = 510] = "NOT_EXTENDED";
68
+ StatusCode[StatusCode["NETWORK_AUTHENTICATION_REQUIRED"] = 511] = "NETWORK_AUTHENTICATION_REQUIRED";
14
69
  })(StatusCode || (exports.StatusCode = StatusCode = {}));
15
70
 
16
71
  //# sourceMappingURL=status-code.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/../src/common/status-code.ts"],"names":[],"mappings":";;;AAAA,IAAY,UASX;AATD,WAAY,UAAU;IACpB,yCAAQ,CAAA;IACR,mDAAa,CAAA;IACb,2DAAiB,CAAA;IACjB,+DAAmB,CAAA;IACnB,6DAAkB,CAAA;IAClB,uDAAe,CAAA;IACf,uDAAe,CAAA;IACf,+EAA2B,CAAA;AAC7B,CAAC,EATW,UAAU,0BAAV,UAAU,QASrB","file":"status-code.js","sourcesContent":["export enum StatusCode {\n OK = 200,\n CREATED = 201,\n BAD_REQUEST = 400,\n I_AM_A_TEAPOT = 418,\n UNAUTHORIZED = 401,\n FORBIDDEN = 403,\n NOT_FOUND = 404,\n INTERNAL_SERVER_ERROR = 500,\n}"]}
1
+ {"version":3,"sources":["../src/../src/common/status-code.ts"],"names":[],"mappings":";;;AAAA,IAAY,UAgEX;AAhED,WAAY,UAAU;IACpB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B,CAAA;IAClB,mGAAqC,CAAA;AACvC,CAAC,EAhEW,UAAU,0BAAV,UAAU,QAgErB","file":"status-code.js","sourcesContent":["export enum StatusCode {\n CONTINUE = 100,\n SWITCHING_PROTOCOLS = 101,\n PROCESSING = 102,\n EARLY_HINTS = 103,\n OK = 200,\n CREATED = 201,\n ACCEPTED = 202,\n NON_AUTHORITATIVE_INFORMATION = 203,\n NO_CONTENT = 204,\n RESET_CONTENT = 205,\n PARTIAL_CONTENT = 206,\n MULTI_STATUS = 207,\n ALREADY_REPORTED = 208,\n IM_USED = 226,\n MULTIPLE_CHOICES = 300,\n MOVED_PERMANENTLY = 301,\n FOUND = 302,\n SEE_OTHER = 303,\n NOT_MODIFIED = 304,\n USE_PROXY = 305,\n SWITCH_PROXY = 306,\n TEMPORARY_REDIRECT = 307,\n PERMANENT_REDIRECT = 308,\n BAD_REQUEST = 400,\n UNAUTHORIZED = 401,\n PAYMENT_REQUIRED = 402,\n FORBIDDEN = 403,\n NOT_FOUND = 404,\n METHOD_NOT_ALLOWED = 405,\n NOT_ACCEPTABLE = 406,\n PROXY_AUTHENTICATION_REQUIRED = 407,\n REQUEST_TIMEOUT = 408,\n CONFLICT = 409,\n GONE = 410,\n LENGTH_REQUIRED = 411,\n PRECONDITION_FAILED = 412,\n CONTENT_TOO_LARGE = 413,\n URI_TOO_LONG = 414,\n UNSUPPORTED_MEDIA_TYPE = 415,\n RANGE_NOT_SATISFIABLE = 416,\n EXPECTATION_FAILED = 417,\n I_AM_A_TEAPOT = 418,\n MISDIRECTED_REQUEST = 421,\n UNPROCESSABLE_ENTITY = 422,\n LOCKED = 423,\n FAILED_DEPENDENCY = 424,\n TOO_EARLY = 425,\n UPGRADE_REQUIRED = 426,\n PRECONDITION_REQUIRED = 428,\n TOO_MANY_REQUESTS = 429,\n REQUEST_HEADER_FIELDS_TOO_LARGE = 431,\n UNAVAILABLE_FOR_LEGAL_REASONS = 451,\n INTERNAL_SERVER_ERROR = 500,\n NOT_IMPLEMENTED = 501,\n BAD_GATEWAY = 502,\n SERVICE_UNAVAILABLE = 503,\n GATEWAY_TIMEOUT = 504,\n HTTP_VERSION_NOT_SUPPORTED = 505,\n VARIANT_ALSO_NEGOTIATES = 506,\n INSUFFICIENT_STORAGE = 507,\n LOOP_DETECTED = 508,\n NOT_EXTENDED = 510,\n NETWORK_AUTHENTICATION_REQUIRED = 511,\n}"]}
@@ -1 +1,2 @@
1
1
  export * from './controller-base';
2
+ export * from './rest-controller-base';
@@ -15,5 +15,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./controller-base"), exports);
18
+ __exportStar(require("./rest-controller-base"), exports);
18
19
 
19
20
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/../src/controllers/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,oDAAkC","file":"index.js","sourcesContent":["export * from './controller-base';"]}
1
+ {"version":3,"sources":["../src/../src/controllers/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,oDAAkC;AAClC,yDAAuC","file":"index.js","sourcesContent":["export * from './controller-base';\nexport * from './rest-controller-base';"]}
@@ -0,0 +1,34 @@
1
+ import { ParameterizedContext } from "koa";
2
+ import { RepositoryBase } from "../database/repositories";
3
+ import { IdentifiableEntity } from "../types";
4
+ import { ControllerBase } from "./controller-base";
5
+ import { DeepPartial } from "typeorm";
6
+ export declare abstract class RestControllerBase<T extends IdentifiableEntity, RouteType extends Record<string, string>, PermissionType extends Record<string, string>, CreateRequest extends any = Omit<T, 'id'>, EditRequest = T> extends ControllerBase {
7
+ abstract getEndpoint(): RouteType[keyof RouteType];
8
+ abstract getRepository(): RepositoryBase<T>;
9
+ registerEndpoints(): void;
10
+ protected getAuthorizationService(): import("..").AuthorizationService<import("../types").AuthenticableEntity, Record<string, string | number>>;
11
+ listEntities(context: ParameterizedContext): Promise<void>;
12
+ viewEntity(context: ParameterizedContext): Promise<void>;
13
+ createEntity(context: ParameterizedContext): Promise<T>;
14
+ editEntity(context: ParameterizedContext): Promise<void>;
15
+ deleteEntity(context: ParameterizedContext): Promise<void>;
16
+ protected delete(entity: T): Promise<void>;
17
+ getEntities(context: ParameterizedContext): Promise<T[]>;
18
+ getById(context: ParameterizedContext): Promise<T>;
19
+ protected getListPermission(): PermissionType | undefined;
20
+ protected getViewPermission(): PermissionType | undefined;
21
+ protected getCreatePermission(): PermissionType | undefined;
22
+ protected getEditPermission(): PermissionType | undefined;
23
+ protected getDeletePermission(): PermissionType | undefined;
24
+ protected shouldRegisterListEndpoint(): boolean;
25
+ protected shouldRegisterViewEndpoint(): boolean;
26
+ protected shouldRegisterCreateEndpoint(): boolean;
27
+ protected shouldRegisterEditEndpoint(): boolean;
28
+ protected shouldRegisterDeleteEndpoint(): boolean;
29
+ protected entityCanBeCreated(body: CreateRequest): Promise<string | null>;
30
+ protected entityCanBeEdited(entity: T, body: EditRequest): Promise<string | null>;
31
+ protected entityCanBeDeleted(entity: T): Promise<string | null>;
32
+ protected modifyEntityForCreate(body: CreateRequest): Promise<DeepPartial<T>>;
33
+ protected modifyEntityForUpdate(entity: T, body: EditRequest): Promise<T>;
34
+ }
@@ -0,0 +1,190 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.RestControllerBase = void 0;
13
+ const types_1 = require("../types");
14
+ const controller_base_1 = require("./controller-base");
15
+ const common_1 = require("../common");
16
+ class RestControllerBase extends controller_base_1.ControllerBase {
17
+ registerEndpoints() {
18
+ if (this.shouldRegisterListEndpoint()) {
19
+ this.router.get(this.getApiUrl(this.getEndpoint()), this.listEntities.bind(this));
20
+ }
21
+ if (this.shouldRegisterViewEndpoint()) {
22
+ this.router.get(this.getApiUrl(`${this.getEndpoint()}/:id`), this.viewEntity.bind(this));
23
+ }
24
+ if (this.shouldRegisterCreateEndpoint()) {
25
+ this.router.post(this.getApiUrl(this.getEndpoint()), this.createEntity.bind(this));
26
+ }
27
+ if (this.shouldRegisterEditEndpoint()) {
28
+ this.router.put(this.getApiUrl(`${this.getEndpoint()}/:id`), this.editEntity.bind(this));
29
+ }
30
+ if (this.shouldRegisterDeleteEndpoint()) {
31
+ this.router.delete(this.getApiUrl(`${this.getEndpoint()}/:id`), this.deleteEntity.bind(this));
32
+ }
33
+ }
34
+ getAuthorizationService() {
35
+ return common_1.KoalApp.getInstance().getAuthorizationService();
36
+ }
37
+ listEntities(context) {
38
+ return __awaiter(this, void 0, void 0, function* () {
39
+ if (this.getListPermission() !== undefined) {
40
+ yield this.getAuthorizationService().userHasRight(context.state.user, this.getListPermission());
41
+ }
42
+ context.body = yield this.getEntities(context);
43
+ });
44
+ }
45
+ viewEntity(context) {
46
+ return __awaiter(this, void 0, void 0, function* () {
47
+ if (this.getViewPermission() !== undefined) {
48
+ yield this.getAuthorizationService().userHasRight(context.state.user, this.getViewPermission());
49
+ }
50
+ const entity = yield this.getById(context);
51
+ if (entity) {
52
+ context.body = entity;
53
+ }
54
+ else {
55
+ context.status = common_1.StatusCode.NOT_FOUND;
56
+ }
57
+ });
58
+ }
59
+ createEntity(context) {
60
+ return __awaiter(this, void 0, void 0, function* () {
61
+ if (this.getCreatePermission() !== undefined) {
62
+ yield this.getAuthorizationService().userHasRight(context.state.user, this.getCreatePermission());
63
+ }
64
+ const errorMessage = yield this.entityCanBeCreated(context.request.body);
65
+ if (errorMessage !== null) {
66
+ throw new types_1.ErrorBase(errorMessage, common_1.StatusCode.BAD_REQUEST);
67
+ }
68
+ const entity = yield this.getRepository().save(yield this.modifyEntityForCreate(context.request.body));
69
+ context.status = common_1.StatusCode.CREATED;
70
+ context.body = {
71
+ success: true,
72
+ entity
73
+ };
74
+ return entity;
75
+ });
76
+ }
77
+ editEntity(context) {
78
+ return __awaiter(this, void 0, void 0, function* () {
79
+ if (this.getEditPermission() !== undefined) {
80
+ yield this.getAuthorizationService().userHasRight(context.state.user, this.getEditPermission());
81
+ }
82
+ const entity = yield this.getById(context);
83
+ if (entity) {
84
+ const errorMessage = yield this.entityCanBeEdited(entity, context.request.body);
85
+ if (errorMessage !== null) {
86
+ throw new types_1.ErrorBase(errorMessage, common_1.StatusCode.BAD_REQUEST);
87
+ }
88
+ yield this.getRepository().save(yield this.modifyEntityForUpdate(entity, context.request.body));
89
+ context.status = common_1.StatusCode.OK;
90
+ context.body = {
91
+ success: true
92
+ };
93
+ }
94
+ else {
95
+ context.status = common_1.StatusCode.NOT_FOUND;
96
+ }
97
+ });
98
+ }
99
+ deleteEntity(context) {
100
+ return __awaiter(this, void 0, void 0, function* () {
101
+ if (this.getDeletePermission() !== undefined) {
102
+ yield this.getAuthorizationService().userHasRight(context.state.user, this.getDeletePermission());
103
+ }
104
+ const entity = yield this.getById(context);
105
+ if (entity) {
106
+ const errorMessage = yield this.entityCanBeDeleted(entity);
107
+ if (errorMessage !== null) {
108
+ throw new types_1.ErrorBase(errorMessage, common_1.StatusCode.BAD_REQUEST);
109
+ }
110
+ yield this.delete(entity);
111
+ context.status = common_1.StatusCode.OK;
112
+ context.body = {
113
+ success: true
114
+ };
115
+ }
116
+ else {
117
+ context.status = common_1.StatusCode.NOT_FOUND;
118
+ }
119
+ });
120
+ }
121
+ delete(entity) {
122
+ return __awaiter(this, void 0, void 0, function* () {
123
+ yield this.getRepository().delete(entity);
124
+ });
125
+ }
126
+ getEntities(context) {
127
+ return this.getRepository().getAll();
128
+ }
129
+ getById(context) {
130
+ return this.getRepository().getById(context.params.id);
131
+ }
132
+ getListPermission() {
133
+ return undefined;
134
+ }
135
+ getViewPermission() {
136
+ return undefined;
137
+ }
138
+ getCreatePermission() {
139
+ return undefined;
140
+ }
141
+ getEditPermission() {
142
+ return undefined;
143
+ }
144
+ getDeletePermission() {
145
+ return undefined;
146
+ }
147
+ shouldRegisterListEndpoint() {
148
+ return true;
149
+ }
150
+ shouldRegisterViewEndpoint() {
151
+ return true;
152
+ }
153
+ shouldRegisterCreateEndpoint() {
154
+ return true;
155
+ }
156
+ shouldRegisterEditEndpoint() {
157
+ return true;
158
+ }
159
+ shouldRegisterDeleteEndpoint() {
160
+ return true;
161
+ }
162
+ entityCanBeCreated(body) {
163
+ return __awaiter(this, void 0, void 0, function* () {
164
+ return null;
165
+ });
166
+ }
167
+ entityCanBeEdited(entity, body) {
168
+ return __awaiter(this, void 0, void 0, function* () {
169
+ return null;
170
+ });
171
+ }
172
+ entityCanBeDeleted(entity) {
173
+ return __awaiter(this, void 0, void 0, function* () {
174
+ return null;
175
+ });
176
+ }
177
+ modifyEntityForCreate(body) {
178
+ return __awaiter(this, void 0, void 0, function* () {
179
+ return body;
180
+ });
181
+ }
182
+ modifyEntityForUpdate(entity, body) {
183
+ return __awaiter(this, void 0, void 0, function* () {
184
+ return entity;
185
+ });
186
+ }
187
+ }
188
+ exports.RestControllerBase = RestControllerBase;
189
+
190
+ //# sourceMappingURL=rest-controller-base.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/../src/controllers/rest-controller-base.ts"],"names":[],"mappings":";;;;;;;;;;;;AAEA,oCAAuE;AACvE,uDAAmD;AACnD,sCAAgD;AAGhD,MAAsB,kBAAsM,SAAQ,gCAAc;IAGhP,iBAAiB;QACf,IAAI,IAAI,CAAC,0BAA0B,EAAE,EAAE,CAAC;YACtC,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,WAAW,EA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;IACnB,CAAC;IACS,iBAAiB;QACzB,OAAO,SAAS,CAAC;IACnB,CAAC;IACS,mBAAmB;QAC3B,OAAO,SAAS,CAAC;IACnB,CAAC;IACS,iBAAiB;QACzB,OAAO,SAAS,CAAC;IACnB,CAAC;IACS,mBAAmB;QAC3B,OAAO,SAAS,CAAC;IACnB,CAAC;IAES,0BAA0B;QAClC,OAAO,IAAI,CAAC;IACd,CAAC;IACS,0BAA0B;QAClC,OAAO,IAAI,CAAC;IACd,CAAC;IACS,4BAA4B;QACpC,OAAO,IAAI,CAAC;IACd,CAAC;IACS,0BAA0B;QAClC,OAAO,IAAI,CAAC;IACd,CAAC;IACS,4BAA4B;QACpC,OAAO,IAAI,CAAC;IACd,CAAC;IACe,kBAAkB,CAAC,IAAmB;;YACpD,OAAO,IAAI,CAAC;QACd,CAAC;KAAA;IACe,iBAAiB,CAAC,MAAS,EAAE,IAAiB;;YAC5D,OAAO,IAAI,CAAC;QACd,CAAC;KAAA;IACe,kBAAkB,CAAC,MAAS;;YAC1C,OAAO,IAAI,CAAC;QACd,CAAC;KAAA;IACe,qBAAqB,CAAC,IAAmB;;YACvD,OAAO,IAAsB,CAAC;QAChC,CAAC;KAAA;IACe,qBAAqB,CAAC,MAAS,EAAE,IAAiB;;YAChE,OAAO,MAAM,CAAC;QAChB,CAAC;KAAA;CACF;AAzJD,gDAyJC","file":"rest-controller-base.js","sourcesContent":["import { ParameterizedContext } from \"koa\";\nimport { RepositoryBase } from \"../database/repositories\";\nimport { BaseResponse, ErrorBase, IdentifiableEntity } from \"../types\";\nimport { ControllerBase } from \"./controller-base\";\nimport { KoalApp, StatusCode } from \"../common\";\nimport { DeepPartial } from \"typeorm\";\n\nexport abstract class RestControllerBase<T extends IdentifiableEntity, RouteType extends Record<string, string>, PermissionType extends Record<string, string>, CreateRequest extends any = Omit<T, 'id'>, EditRequest = T> extends ControllerBase {\n abstract getEndpoint(): RouteType[keyof RouteType];\n abstract getRepository(): RepositoryBase<T>;\n registerEndpoints(): void {\n if (this.shouldRegisterListEndpoint()) {\n this.router.get(this.getApiUrl(this.getEndpoint()), this.listEntities.bind(this));\n }\n if (this.shouldRegisterViewEndpoint()) {\n this.router.get(this.getApiUrl(`${this.getEndpoint()}/:id`), this.viewEntity.bind(this));\n }\n if (this.shouldRegisterCreateEndpoint()) {\n this.router.post(this.getApiUrl(this.getEndpoint()), this.createEntity.bind(this));\n }\n if (this.shouldRegisterEditEndpoint()) {\n this.router.put(this.getApiUrl(`${this.getEndpoint()}/:id`), this.editEntity.bind(this));\n }\n if (this.shouldRegisterDeleteEndpoint()) {\n this.router.delete(this.getApiUrl(`${this.getEndpoint()}/:id`), this.deleteEntity.bind(this));\n }\n }\n protected getAuthorizationService() {\n return KoalApp.getInstance().getAuthorizationService();\n }\n async listEntities(context: ParameterizedContext): Promise<void> {\n if (this.getListPermission() !== undefined) {\n await this.getAuthorizationService().userHasRight(context.state.user, this.getListPermission());\n }\n context.body = await this.getEntities(context);\n }\n async viewEntity(context: ParameterizedContext): Promise<void> {\n if (this.getViewPermission() !== undefined) {\n await this.getAuthorizationService().userHasRight(context.state.user, this.getViewPermission());\n }\n const entity = await this.getById(context);\n if (entity) {\n context.body = entity;\n }\n else {\n context.status = StatusCode.NOT_FOUND;\n }\n }\n async createEntity(context: ParameterizedContext): Promise<T> {\n if (this.getCreatePermission() !== undefined) {\n await this.getAuthorizationService().userHasRight(context.state.user, this.getCreatePermission());\n }\n const errorMessage = await this.entityCanBeCreated(context.request.body as CreateRequest);\n if (errorMessage !== null) {\n throw new ErrorBase(errorMessage, StatusCode.BAD_REQUEST);\n }\n const entity = await this.getRepository().save(await this.modifyEntityForCreate(context.request.body as CreateRequest));\n context.status = StatusCode.CREATED;\n context.body = {\n success: true,\n entity\n };\n return entity;\n }\n async editEntity(context: ParameterizedContext): Promise<void> {\n if (this.getEditPermission() !== undefined) {\n await this.getAuthorizationService().userHasRight(context.state.user, this.getEditPermission());\n }\n const entity = await this.getById(context);\n if (entity) {\n const errorMessage = await this.entityCanBeEdited(entity, context.request.body as EditRequest);\n if (errorMessage !== null) {\n throw new ErrorBase(errorMessage, StatusCode.BAD_REQUEST);\n }\n await this.getRepository().save(await this.modifyEntityForUpdate(entity, context.request.body as EditRequest));\n context.status = StatusCode.OK\n context.body = <BaseResponse>{\n success: true\n };\n }\n else {\n context.status = StatusCode.NOT_FOUND;\n }\n }\n async deleteEntity(context: ParameterizedContext) {\n if (this.getDeletePermission() !== undefined) {\n await this.getAuthorizationService().userHasRight(context.state.user, this.getDeletePermission());\n }\n const entity = await this.getById(context);\n if (entity) {\n const errorMessage = await this.entityCanBeDeleted(entity)\n if (errorMessage !== null) {\n throw new ErrorBase(errorMessage, StatusCode.BAD_REQUEST);\n }\n await this.delete(entity);\n context.status = StatusCode.OK;\n context.body = <BaseResponse>{\n success: true\n };\n }\n else {\n context.status = StatusCode.NOT_FOUND;\n }\n }\n protected async delete(entity: T) {\n await this.getRepository().delete(entity);\n }\n getEntities(context: ParameterizedContext): Promise<T[]> {\n return this.getRepository().getAll();\n }\n getById(context: ParameterizedContext): Promise<T> {\n return this.getRepository().getById(context.params.id);\n }\n\n protected getListPermission(): PermissionType | undefined {\n return undefined;\n }\n protected getViewPermission(): PermissionType | undefined {\n return undefined;\n }\n protected getCreatePermission(): PermissionType | undefined {\n return undefined;\n }\n protected getEditPermission(): PermissionType | undefined {\n return undefined;\n }\n protected getDeletePermission(): PermissionType | undefined {\n return undefined;\n }\n\n protected shouldRegisterListEndpoint(): boolean {\n return true;\n }\n protected shouldRegisterViewEndpoint(): boolean {\n return true;\n }\n protected shouldRegisterCreateEndpoint(): boolean {\n return true;\n }\n protected shouldRegisterEditEndpoint(): boolean {\n return true;\n }\n protected shouldRegisterDeleteEndpoint(): boolean {\n return true;\n }\n protected async entityCanBeCreated(body: CreateRequest): Promise<string | null> {\n return null;\n }\n protected async entityCanBeEdited(entity: T, body: EditRequest): Promise<string | null> {\n return null;\n }\n protected async entityCanBeDeleted(entity: T): Promise<string | null> {\n return null;\n }\n protected async modifyEntityForCreate(body: CreateRequest): Promise<DeepPartial<T>> {\n return body as DeepPartial<T>;\n }\n protected async modifyEntityForUpdate(entity: T, body: EditRequest): Promise<T> {\n return entity;\n }\n}"]}
@@ -0,0 +1 @@
1
+ export * from './repositories';
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./repositories"), exports);
18
+
19
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/../src/database/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,iDAA+B","file":"index.js","sourcesContent":["export * from './repositories';\n"]}
@@ -0,0 +1 @@
1
+ export * from './repository-base';
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./repository-base"), exports);
18
+
19
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/../src/database/repositories/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,oDAAkC","file":"index.js","sourcesContent":["export * from './repository-base';\n"]}
@@ -0,0 +1,16 @@
1
+ import { DeepPartial, FindManyOptions, FindOneOptions, FindOptionsRelations, Repository } from "typeorm";
2
+ import { IdentifiableEntity } from "../../types";
3
+ export declare abstract class RepositoryBase<T extends IdentifiableEntity> {
4
+ private _repository;
5
+ constructor();
6
+ getRepository(): Repository<T>;
7
+ abstract getEntityType(): new () => T;
8
+ getAll(): Promise<T[]>;
9
+ getWhere(options: FindManyOptions<T>): Promise<T[]>;
10
+ getOneWhere(options: FindOneOptions<T>): Promise<T>;
11
+ getById(id: number, relations?: FindOptionsRelations<T>): Promise<T>;
12
+ save(entity: T | DeepPartial<T>): Promise<DeepPartial<T> & T>;
13
+ find(where: FindManyOptions<T>): Promise<T[]>;
14
+ findOne(where: FindOneOptions<T>): Promise<T>;
15
+ delete(entity: T): Promise<import("typeorm").DeleteResult>;
16
+ }
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RepositoryBase = void 0;
4
+ const common_1 = require("../../common");
5
+ class RepositoryBase {
6
+ constructor() {
7
+ this._repository = common_1.KoalApp.getInstance().getDatabaseConnection().getRepository(this.getEntityType());
8
+ }
9
+ getRepository() {
10
+ return this._repository;
11
+ }
12
+ getAll() {
13
+ return this._repository.find();
14
+ }
15
+ getWhere(options) {
16
+ return this._repository.find(options);
17
+ }
18
+ getOneWhere(options) {
19
+ return this._repository.findOne(options);
20
+ }
21
+ getById(id, relations) {
22
+ return this.getRepository().findOne({
23
+ where: {
24
+ id
25
+ },
26
+ relations
27
+ });
28
+ }
29
+ save(entity) {
30
+ return this.getRepository().save(entity);
31
+ }
32
+ find(where) {
33
+ return this.getRepository().find(where);
34
+ }
35
+ findOne(where) {
36
+ return this.getRepository().findOne(where);
37
+ }
38
+ delete(entity) {
39
+ return this.getRepository().delete(entity.id);
40
+ }
41
+ }
42
+ exports.RepositoryBase = RepositoryBase;
43
+
44
+ //# sourceMappingURL=repository-base.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/../src/database/repositories/repository-base.ts"],"names":[],"mappings":";;;AAEA,yCAAuC;AAEvC,MAAsB,cAAc;IAElC;QACE,IAAI,CAAC,WAAW,GAAG,gBAAO,CAAC,WAAW,EAAE,CAAC,qBAAqB,EAAE,CAAC,aAAa,CAAC,IAAI,CAAC,aAAa,EAAE,CAAC,CAAC;IACvG,CAAC;IACD,aAAa;QACX,OAAO,IAAI,CAAC,WAAW,CAAC;IAC1B,CAAC;IAED,MAAM;QACJ,OAAO,IAAI,CAAC,WAAW,CAAC,IAAI,EAAE,CAAC;IACjC,CAAC;IACD,QAAQ,CAAC,OAA2B;QAClC,OAAO,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IACxC,CAAC;IACD,WAAW,CAAC,OAA0B;QACpC,OAAO,IAAI,CAAC,WAAW,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;IAC3C,CAAC;IACD,OAAO,CAAC,EAAU,EAAE,SAAmC;QACrD,OAAO,IAAI,CAAC,aAAa,EAAE,CAAC,OAAO,CAAoB;YACrD,KAAK,EAAE;gBACL,EAAE;aACH;YACD,SAAS;SACV,CAAC,CAAC;IACL,CAAC;IACD,IAAI,CAAC,MAA0B;QAC7B,OAAO,IAAI,CAAC,aAAa,EAAE,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IAC3C,CAAC;IACD,IAAI,CAAC,KAAyB;QAC5B,OAAO,IAAI,CAAC,aAAa,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IAC1C,CAAC;IACD,OAAO,CAAC,KAAwB;QAC9B,OAAO,IAAI,CAAC,aAAa,EAAE,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;IAC7C,CAAC;IACD,MAAM,CAAC,MAAS;QACd,OAAO,IAAI,CAAC,aAAa,EAAE,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;IAChD,CAAC;CACF;AAtCD,wCAsCC","file":"repository-base.js","sourcesContent":["import { DeepPartial, FindManyOptions, FindOneOptions, FindOptionsRelations, Repository } from \"typeorm\";\nimport { IdentifiableEntity } from \"../../types\";\nimport { KoalApp } from \"../../common\";\n\nexport abstract class RepositoryBase<T extends IdentifiableEntity> {\n private _repository: Repository<T>;\n constructor() {\n this._repository = KoalApp.getInstance().getDatabaseConnection().getRepository(this.getEntityType());\n }\n getRepository() {\n return this._repository;\n }\n abstract getEntityType(): new () => T;\n getAll() {\n return this._repository.find();\n }\n getWhere(options: FindManyOptions<T>) {\n return this._repository.find(options);\n }\n getOneWhere(options: FindOneOptions<T>) {\n return this._repository.findOne(options);\n }\n getById(id: number, relations?: FindOptionsRelations<T>) {\n return this.getRepository().findOne(<FindOneOptions<T>>{\n where: {\n id\n },\n relations\n });\n }\n save(entity: T | DeepPartial<T>) {\n return this.getRepository().save(entity);\n }\n find(where: FindManyOptions<T>) {\n return this.getRepository().find(where);\n }\n findOne(where: FindOneOptions<T>) {\n return this.getRepository().findOne(where);\n }\n delete(entity: T) {\n return this.getRepository().delete(entity.id);\n }\n}\n"]}
package/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export * from './koalas';
2
2
  export * from './common';
3
3
  export * from './controllers';
4
+ export * from './database';
4
5
  export * from './services';
5
6
  export * from './types';
package/index.js CHANGED
@@ -21,6 +21,7 @@ if (require.main === module) {
21
21
  __exportStar(require("./koalas"), exports);
22
22
  __exportStar(require("./common"), exports);
23
23
  __exportStar(require("./controllers"), exports);
24
+ __exportStar(require("./database"), exports);
24
25
  __exportStar(require("./services"), exports);
25
26
  __exportStar(require("./types"), exports);
26
27
 
package/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,qCAAyC;AAEzC,IAAI,OAAO,CAAC,IAAI,KAAK,MAAM,EAAE,CAAC;IAC5B,IAAA,sBAAa,GAAE,CAAC;AAClB,CAAC;AAED,2CAAyB;AAEzB,2CAAyB;AACzB,gDAA8B;AAC9B,6CAA2B;AAC3B,0CAAwB","file":"index.js","sourcesContent":["import { printKoalaArt } from \"./koalas\";\n\nif (require.main === module) {\n printKoalaArt();\n}\n\nexport * from './koalas';\n\nexport * from './common';\nexport * from './controllers';\nexport * from './services';\nexport * from './types';\n"]}
1
+ {"version":3,"sources":["../src/../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,qCAAyC;AAEzC,IAAI,OAAO,CAAC,IAAI,KAAK,MAAM,EAAE,CAAC;IAC5B,IAAA,sBAAa,GAAE,CAAC;AAClB,CAAC;AAED,2CAAyB;AAEzB,2CAAyB;AACzB,gDAA8B;AAC9B,6CAA2B;AAC3B,6CAA2B;AAC3B,0CAAwB","file":"index.js","sourcesContent":["import { printKoalaArt } from \"./koalas\";\n\nif (require.main === module) {\n printKoalaArt();\n}\n\nexport * from './koalas';\n\nexport * from './common';\nexport * from './controllers';\nexport * from './database';\nexport * from './services';\nexport * from './types';\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kool-koala",
3
- "version": "1.1.2",
3
+ "version": "1.2.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",
@@ -40,10 +40,6 @@
40
40
  "@types/gulp-rename": "^2.0.6",
41
41
  "@types/gulp-sourcemaps": "^0.0.38",
42
42
  "@types/jsonwebtoken": "^9.0.9",
43
- "@types/koa": "^2.15.0",
44
- "@types/koa-bodyparser": "^4.3.12",
45
- "@types/koa-router": "^7.4.8",
46
- "@types/koa-static": "^4.0.4",
47
43
  "@types/yargs": "^17.0.33",
48
44
  "commitizen": "^4.3.1",
49
45
  "cz-git": "^1.11.1",
@@ -69,6 +65,10 @@
69
65
  "typeorm": "^0.3.21"
70
66
  },
71
67
  "dependencies": {
68
+ "@types/koa": "^2.15.0",
69
+ "@types/koa-bodyparser": "^4.3.12",
70
+ "@types/koa-router": "^7.4.8",
71
+ "@types/koa-static": "^4.0.4",
72
72
  "jsonwebtoken": "^9.0.2",
73
73
  "koa-bodyparser": "^4.4.1",
74
74
  "koa-static": "^5.0.0"