@grandlinex/kernel 1.0.2 → 1.1.1
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/dist/cjs/KernelModule.js +1 -1
- package/dist/cjs/actions/ApiAuthTestAction.d.ts +2 -4
- package/dist/cjs/actions/ApiAuthTestAction.js +3 -9
- package/dist/cjs/actions/ApiVersionAction.d.ts +2 -4
- package/dist/cjs/actions/ApiVersionAction.js +3 -10
- package/dist/cjs/actions/GetTokenAction.d.ts +8 -9
- package/dist/cjs/actions/GetTokenAction.js +18 -36
- package/dist/cjs/annotation/index.d.ts +13 -0
- package/dist/cjs/annotation/index.js +21 -0
- package/dist/cjs/classes/BaseAction.d.ts +5 -0
- package/dist/cjs/classes/BaseAction.js +140 -1
- package/dist/cjs/classes/BaseApiAction.js +1 -1
- package/dist/cjs/classes/RouteApiAction.d.ts +6 -0
- package/dist/cjs/classes/RouteApiAction.js +25 -0
- package/dist/cjs/classes/index.d.ts +2 -1
- package/dist/cjs/classes/index.js +3 -1
- package/dist/cjs/index.d.ts +1 -0
- package/dist/cjs/index.js +1 -0
- package/dist/cjs/lib/express.d.ts +4 -1
- package/dist/cjs/modules/crypto/CryptoClient.d.ts +3 -2
- package/dist/cjs/modules/crypto/CryptoClient.js +2 -1
- package/dist/mjs/KernelModule.js +2 -2
- package/dist/mjs/actions/ApiAuthTestAction.d.ts +2 -4
- package/dist/mjs/actions/ApiAuthTestAction.js +4 -10
- package/dist/mjs/actions/ApiVersionAction.d.ts +2 -4
- package/dist/mjs/actions/ApiVersionAction.js +4 -11
- package/dist/mjs/actions/GetTokenAction.d.ts +8 -9
- package/dist/mjs/actions/GetTokenAction.js +19 -37
- package/dist/mjs/annotation/index.d.ts +13 -0
- package/dist/mjs/annotation/index.js +16 -0
- package/dist/mjs/classes/BaseAction.d.ts +5 -0
- package/dist/mjs/classes/BaseAction.js +140 -1
- package/dist/mjs/classes/BaseApiAction.js +1 -1
- package/dist/mjs/classes/RouteApiAction.d.ts +6 -0
- package/dist/mjs/classes/RouteApiAction.js +19 -0
- package/dist/mjs/classes/index.d.ts +2 -1
- package/dist/mjs/classes/index.js +2 -1
- package/dist/mjs/index.d.ts +1 -0
- package/dist/mjs/index.js +1 -0
- package/dist/mjs/lib/express.d.ts +4 -1
- package/dist/mjs/modules/crypto/CryptoClient.d.ts +3 -2
- package/dist/mjs/modules/crypto/CryptoClient.js +2 -1
- package/package.json +13 -12
- package/tsconfig-cjs.json +0 -92
package/dist/cjs/KernelModule.js
CHANGED
|
@@ -15,7 +15,7 @@ class KernelModule extends BaseKernelModule_js_1.default {
|
|
|
15
15
|
this.addAction(new ApiVersionAction_js_1.default(this), new ApiAuthTestAction_js_1.default(this), new GetTokenAction_js_1.default(this));
|
|
16
16
|
}
|
|
17
17
|
async initModule() {
|
|
18
|
-
this.addService(new core_1.
|
|
18
|
+
this.addService(new core_1.BackgroundService(this));
|
|
19
19
|
const endpoint = new KernelEndpoint_js_1.default('api', this, this.getKernel().getAppServerPort());
|
|
20
20
|
this.setPresenter(endpoint);
|
|
21
21
|
await this.getKernel().triggerEvent('load');
|
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { BaseApiAction, JwtToken } from '../classes/index.js';
|
|
1
|
+
import { JwtToken, RouteApiAction } from '../classes/index.js';
|
|
3
2
|
import { XActionEvent } from '../lib/express.js';
|
|
4
|
-
export default class ApiAuthTestAction extends
|
|
5
|
-
constructor(module: IBaseKernelModule<any, any, any, any>);
|
|
3
|
+
export default class ApiAuthTestAction extends RouteApiAction {
|
|
6
4
|
handler({ data, res }: XActionEvent<JwtToken>): Promise<void>;
|
|
7
5
|
}
|
|
@@ -5,17 +5,11 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
|
|
|
5
5
|
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
6
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
7
|
};
|
|
8
|
-
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
-
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
-
};
|
|
11
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
9
|
const swagger_mate_1 = require("@grandlinex/swagger-mate");
|
|
13
10
|
const index_js_1 = require("../classes/index.js");
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
super('GET', '/test/auth', module);
|
|
17
|
-
this.handler = this.handler.bind(this);
|
|
18
|
-
}
|
|
11
|
+
const index_js_2 = require("../annotation/index.js");
|
|
12
|
+
let ApiAuthTestAction = class ApiAuthTestAction extends index_js_1.RouteApiAction {
|
|
19
13
|
async handler({ data, res }) {
|
|
20
14
|
this.debug(data.userid);
|
|
21
15
|
res.status(200).send("It work's");
|
|
@@ -32,6 +26,6 @@ ApiAuthTestAction = __decorate([
|
|
|
32
26
|
},
|
|
33
27
|
},
|
|
34
28
|
}),
|
|
35
|
-
|
|
29
|
+
(0, index_js_2.Route)('GET', '/test/auth')
|
|
36
30
|
], ApiAuthTestAction);
|
|
37
31
|
exports.default = ApiAuthTestAction;
|
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { BaseApiAction } from '../classes/index.js';
|
|
1
|
+
import { RouteApiAction } from '../classes/index.js';
|
|
3
2
|
import { XActionEvent } from '../lib/express.js';
|
|
4
|
-
export default class ApiVersionAction extends
|
|
5
|
-
constructor(module: IBaseKernelModule);
|
|
3
|
+
export default class ApiVersionAction extends RouteApiAction {
|
|
6
4
|
handler({ res, extension }: XActionEvent): Promise<void>;
|
|
7
5
|
}
|
|
@@ -5,18 +5,11 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
|
|
|
5
5
|
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
6
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
7
|
};
|
|
8
|
-
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
-
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
-
};
|
|
11
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
9
|
const swagger_mate_1 = require("@grandlinex/swagger-mate");
|
|
13
10
|
const index_js_1 = require("../classes/index.js");
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
super('GET', '/version', module);
|
|
17
|
-
this.handler = this.handler.bind(this);
|
|
18
|
-
this.setMode(index_js_1.ActionMode.DMZ);
|
|
19
|
-
}
|
|
11
|
+
const index_js_2 = require("../annotation/index.js");
|
|
12
|
+
let ApiVersionAction = class ApiVersionAction extends index_js_1.RouteApiAction {
|
|
20
13
|
async handler({ res, extension }) {
|
|
21
14
|
extension.done();
|
|
22
15
|
res.status(200).send({ api: this.getKernel().getApiVersion() });
|
|
@@ -41,6 +34,6 @@ ApiVersionAction = __decorate([
|
|
|
41
34
|
},
|
|
42
35
|
},
|
|
43
36
|
}),
|
|
44
|
-
|
|
37
|
+
(0, index_js_2.Route)('GET', '/version', index_js_1.ActionMode.DMZ)
|
|
45
38
|
], ApiVersionAction);
|
|
46
39
|
exports.default = ApiVersionAction;
|
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { BaseApiAction, JwtToken } from '../classes/index.js';
|
|
1
|
+
import { JwtToken, RouteApiAction } from '../classes/index.js';
|
|
3
2
|
import { XActionEvent } from '../lib/express.js';
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
handler({ req, res, extension, }: XActionEvent<JwtToken>): Promise<void>;
|
|
3
|
+
type SchemaType = {
|
|
4
|
+
username: string;
|
|
5
|
+
token: string;
|
|
6
|
+
};
|
|
7
|
+
export default class GetTokenAction extends RouteApiAction {
|
|
8
|
+
handler({ req, res, extension, body, }: XActionEvent<JwtToken, SchemaType>): Promise<void>;
|
|
11
9
|
}
|
|
10
|
+
export {};
|
|
@@ -5,33 +5,26 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
|
|
|
5
5
|
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
6
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
7
|
};
|
|
8
|
-
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
-
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
-
};
|
|
11
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
9
|
const swagger_mate_1 = require("@grandlinex/swagger-mate");
|
|
13
10
|
const index_js_1 = require("../classes/index.js");
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
11
|
+
const index_js_2 = require("../annotation/index.js");
|
|
12
|
+
const schema = {
|
|
13
|
+
type: 'object',
|
|
14
|
+
properties: {
|
|
15
|
+
username: {
|
|
16
|
+
type: 'string',
|
|
17
|
+
},
|
|
18
|
+
token: {
|
|
19
|
+
type: 'string',
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
required: ['username', 'token'],
|
|
23
|
+
};
|
|
24
|
+
let GetTokenAction = class GetTokenAction extends index_js_1.RouteApiAction {
|
|
25
|
+
async handler({ req, res, extension, body, }) {
|
|
25
26
|
const cc = this.getKernel().getCryptoClient();
|
|
26
|
-
|
|
27
|
-
res.status(400).send('no token');
|
|
28
|
-
return;
|
|
29
|
-
}
|
|
30
|
-
if (!req.body.username) {
|
|
31
|
-
res.status(401).send('no username');
|
|
32
|
-
return;
|
|
33
|
-
}
|
|
34
|
-
const { username, token } = req.body;
|
|
27
|
+
const { username, token } = body;
|
|
35
28
|
const valid = await extension.timing.startFunc('validation', () => cc.apiTokenValidation(username, token, 'api'));
|
|
36
29
|
if (valid.valid && valid.userId) {
|
|
37
30
|
const jwt = await cc.jwtGenerateAccessToken({
|
|
@@ -53,18 +46,7 @@ GetTokenAction = __decorate([
|
|
|
53
46
|
operationId: 'getToken',
|
|
54
47
|
summary: 'Get API token',
|
|
55
48
|
tags: ['kernel'],
|
|
56
|
-
requestBody: swagger_mate_1.SPathUtil.jsonBody(
|
|
57
|
-
type: 'object',
|
|
58
|
-
properties: {
|
|
59
|
-
username: {
|
|
60
|
-
type: 'string',
|
|
61
|
-
},
|
|
62
|
-
token: {
|
|
63
|
-
type: 'string',
|
|
64
|
-
},
|
|
65
|
-
},
|
|
66
|
-
required: ['username', 'token'],
|
|
67
|
-
}),
|
|
49
|
+
requestBody: swagger_mate_1.SPathUtil.jsonBody(schema),
|
|
68
50
|
responses: swagger_mate_1.SPathUtil.jsonResponse('200', {
|
|
69
51
|
type: 'object',
|
|
70
52
|
properties: {
|
|
@@ -77,6 +59,6 @@ GetTokenAction = __decorate([
|
|
|
77
59
|
},
|
|
78
60
|
},
|
|
79
61
|
}),
|
|
80
|
-
|
|
62
|
+
(0, index_js_2.Route)('POST', '/token', index_js_1.ActionMode.DMZ, schema)
|
|
81
63
|
], GetTokenAction);
|
|
82
64
|
exports.default = GetTokenAction;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import 'reflect-metadata';
|
|
2
|
+
import { ObjectLike } from '@grandlinex/core';
|
|
3
|
+
import { SSchemaEl } from '@grandlinex/swagger-mate';
|
|
4
|
+
import { ActionTypes } from '../lib/index.js';
|
|
5
|
+
import { ActionMode } from '../classes/index.js';
|
|
6
|
+
export type RouteMeta = {
|
|
7
|
+
type: ActionTypes;
|
|
8
|
+
path: string;
|
|
9
|
+
mode?: ActionMode;
|
|
10
|
+
schema?: SSchemaEl;
|
|
11
|
+
};
|
|
12
|
+
export declare const Route: (type: ActionTypes, path: string, mode?: ActionMode, schema?: SSchemaEl) => ClassDecorator;
|
|
13
|
+
export declare function getRouteMeta<T extends ObjectLike>(target: T): RouteMeta | undefined;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Route = void 0;
|
|
4
|
+
exports.getRouteMeta = getRouteMeta;
|
|
5
|
+
require("reflect-metadata");
|
|
6
|
+
const routeKey = Symbol('route');
|
|
7
|
+
const Route = (type, path, mode, schema) => {
|
|
8
|
+
return (target) => {
|
|
9
|
+
const metadata = {
|
|
10
|
+
type,
|
|
11
|
+
path,
|
|
12
|
+
mode,
|
|
13
|
+
schema,
|
|
14
|
+
};
|
|
15
|
+
Reflect.defineMetadata(routeKey, metadata, target.prototype);
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
exports.Route = Route;
|
|
19
|
+
function getRouteMeta(target) {
|
|
20
|
+
return Reflect.getMetadata(routeKey, target.constructor.prototype);
|
|
21
|
+
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { CoreAction, IDataBase } from '@grandlinex/core';
|
|
2
|
+
import { ErrorType, SSchemaEl } from '@grandlinex/swagger-mate';
|
|
2
3
|
import { IBaseAction, IBaseCache, IBaseClient, IBaseKernelModule, IBasePresenter, IKernel } from '../lib/index.js';
|
|
3
4
|
import { XActionEvent, XRequest, XResponse } from '../lib/express.js';
|
|
4
5
|
export declare enum ActionMode {
|
|
@@ -9,8 +10,12 @@ export declare enum ActionMode {
|
|
|
9
10
|
export default abstract class BaseAction<K extends IKernel = IKernel, T extends IDataBase<any, any> | null = any, P extends IBaseClient | null = any, C extends IBaseCache | null = any, E extends IBasePresenter | null = any> extends CoreAction<K, T, P, C, E> implements IBaseAction<K, T, P, C, E> {
|
|
10
11
|
mode: ActionMode;
|
|
11
12
|
forceDebug: boolean;
|
|
13
|
+
schema: SSchemaEl | null;
|
|
12
14
|
constructor(chanel: string, module: IBaseKernelModule<K, T, P, C, E>);
|
|
13
15
|
abstract handler(event: XActionEvent): Promise<void>;
|
|
16
|
+
static validateSchema(error: ErrorType, schema: SSchemaEl, key: string, field: any, required?: boolean): void;
|
|
17
|
+
bodyValidation<A>(req: XRequest): A | ErrorType | null;
|
|
18
|
+
static sendError(res: XResponse, code: number, error: Partial<ErrorType>): void;
|
|
14
19
|
secureHandler(req: XRequest, res: XResponse, next: () => void): Promise<void>;
|
|
15
20
|
setMode(mode: ActionMode): void;
|
|
16
21
|
abstract register(): void;
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ActionMode = void 0;
|
|
4
4
|
const core_1 = require("@grandlinex/core");
|
|
5
|
+
const swagger_mate_1 = require("@grandlinex/swagger-mate");
|
|
5
6
|
const index_js_1 = require("./timing/index.js");
|
|
6
7
|
const BaseUserAgent_js_1 = require("./BaseUserAgent.js");
|
|
7
8
|
var ActionMode;
|
|
@@ -16,6 +17,102 @@ class BaseAction extends core_1.CoreAction {
|
|
|
16
17
|
this.secureHandler = this.secureHandler.bind(this);
|
|
17
18
|
this.mode = ActionMode.DEFAULT;
|
|
18
19
|
this.forceDebug = false;
|
|
20
|
+
this.schema = null;
|
|
21
|
+
}
|
|
22
|
+
static validateSchema(error, schema, key, field, required = true) {
|
|
23
|
+
if ((0, swagger_mate_1.isSwaggerRef)(schema)) {
|
|
24
|
+
error.field?.push({
|
|
25
|
+
key,
|
|
26
|
+
message: `Ref schema body validation is not supported yet`,
|
|
27
|
+
});
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
if (!required && (field === undefined || field === null)) {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
switch (schema?.type) {
|
|
34
|
+
case 'boolean':
|
|
35
|
+
if (typeof field !== 'boolean') {
|
|
36
|
+
error.field?.push({
|
|
37
|
+
key,
|
|
38
|
+
message: `must be a boolean`,
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
break;
|
|
42
|
+
case 'integer':
|
|
43
|
+
case 'number':
|
|
44
|
+
if (typeof field !== 'number') {
|
|
45
|
+
error.field?.push({
|
|
46
|
+
key,
|
|
47
|
+
message: `must be a number`,
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
break;
|
|
51
|
+
case 'string':
|
|
52
|
+
if (typeof field !== 'string') {
|
|
53
|
+
error.field?.push({
|
|
54
|
+
key,
|
|
55
|
+
message: `must be a string`,
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
break;
|
|
59
|
+
case 'array':
|
|
60
|
+
if (!Array.isArray(field)) {
|
|
61
|
+
error.field?.push({
|
|
62
|
+
key,
|
|
63
|
+
message: `must be a array`,
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
if (schema.items) {
|
|
67
|
+
field.forEach((it, id) => {
|
|
68
|
+
this.validateSchema(error, schema.items, `${key}[${id}]`, it);
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
break;
|
|
72
|
+
case 'object':
|
|
73
|
+
if (typeof field !== 'object') {
|
|
74
|
+
error.field?.push({
|
|
75
|
+
key,
|
|
76
|
+
message: `must be a object`,
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
if (schema.properties) {
|
|
80
|
+
Object.entries(schema.properties).forEach(([k, s]) => {
|
|
81
|
+
this.validateSchema(error, s, `${key}.${k}`, field[k], schema.required ? schema.required.includes(k) : false);
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
break;
|
|
85
|
+
case undefined:
|
|
86
|
+
default:
|
|
87
|
+
error.field?.push({
|
|
88
|
+
key,
|
|
89
|
+
message: `Schema type is not defined or not supported`,
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
bodyValidation(req) {
|
|
94
|
+
if (!this.schema) {
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
if (!req.body) {
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
const error = {
|
|
101
|
+
type: 'error',
|
|
102
|
+
global: [],
|
|
103
|
+
field: [],
|
|
104
|
+
};
|
|
105
|
+
BaseAction.validateSchema(error, this.schema, 'body', req.body);
|
|
106
|
+
if (error.field.length > 0 || error.global.length > 0) {
|
|
107
|
+
return error;
|
|
108
|
+
}
|
|
109
|
+
return req.body;
|
|
110
|
+
}
|
|
111
|
+
static sendError(res, code, error) {
|
|
112
|
+
res.status(code).send({
|
|
113
|
+
type: 'error',
|
|
114
|
+
...error,
|
|
115
|
+
});
|
|
19
116
|
}
|
|
20
117
|
async secureHandler(req, res, next) {
|
|
21
118
|
const extension = this.initExtension(res);
|
|
@@ -34,6 +131,18 @@ class BaseAction extends core_1.CoreAction {
|
|
|
34
131
|
if (this.mode === ActionMode.DMZ) {
|
|
35
132
|
auth.stop();
|
|
36
133
|
try {
|
|
134
|
+
let body = null;
|
|
135
|
+
if (this.schema) {
|
|
136
|
+
body = this.bodyValidation(req);
|
|
137
|
+
}
|
|
138
|
+
if ((0, swagger_mate_1.isErrorType)(body)) {
|
|
139
|
+
res.status(400).send(body);
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
if (this.schema && body === null) {
|
|
143
|
+
res.sendStatus(400);
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
37
146
|
await this.handler({
|
|
38
147
|
res,
|
|
39
148
|
req,
|
|
@@ -41,6 +150,8 @@ class BaseAction extends core_1.CoreAction {
|
|
|
41
150
|
data: null,
|
|
42
151
|
extension,
|
|
43
152
|
agent: new BaseUserAgent_js_1.BaseUserAgent(req),
|
|
153
|
+
body,
|
|
154
|
+
sendError: (code, error) => BaseAction.sendError(res, code, error),
|
|
44
155
|
});
|
|
45
156
|
}
|
|
46
157
|
catch (e) {
|
|
@@ -56,6 +167,18 @@ class BaseAction extends core_1.CoreAction {
|
|
|
56
167
|
auth.stop();
|
|
57
168
|
if (dat && typeof dat !== 'number') {
|
|
58
169
|
try {
|
|
170
|
+
let body = null;
|
|
171
|
+
if (this.schema) {
|
|
172
|
+
body = this.bodyValidation(req);
|
|
173
|
+
}
|
|
174
|
+
if ((0, swagger_mate_1.isErrorType)(body)) {
|
|
175
|
+
res.status(400).send(body);
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
if (this.schema && body === null) {
|
|
179
|
+
res.sendStatus(400);
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
59
182
|
await this.handler({
|
|
60
183
|
res,
|
|
61
184
|
req,
|
|
@@ -63,6 +186,8 @@ class BaseAction extends core_1.CoreAction {
|
|
|
63
186
|
data: dat,
|
|
64
187
|
extension,
|
|
65
188
|
agent: new BaseUserAgent_js_1.BaseUserAgent(req),
|
|
189
|
+
body,
|
|
190
|
+
sendError: (code, error) => BaseAction.sendError(res, code, error),
|
|
66
191
|
});
|
|
67
192
|
}
|
|
68
193
|
catch (e) {
|
|
@@ -75,6 +200,18 @@ class BaseAction extends core_1.CoreAction {
|
|
|
75
200
|
}
|
|
76
201
|
else if (this.mode === ActionMode.DMZ_WITH_USER) {
|
|
77
202
|
try {
|
|
203
|
+
let body = null;
|
|
204
|
+
if (this.schema) {
|
|
205
|
+
body = this.bodyValidation(req);
|
|
206
|
+
}
|
|
207
|
+
if ((0, swagger_mate_1.isErrorType)(body)) {
|
|
208
|
+
res.status(400).send(body);
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
if (this.schema && body === null) {
|
|
212
|
+
res.sendStatus(400);
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
78
215
|
await this.handler({
|
|
79
216
|
res,
|
|
80
217
|
req,
|
|
@@ -82,6 +219,8 @@ class BaseAction extends core_1.CoreAction {
|
|
|
82
219
|
data: null,
|
|
83
220
|
extension,
|
|
84
221
|
agent: new BaseUserAgent_js_1.BaseUserAgent(req),
|
|
222
|
+
body,
|
|
223
|
+
sendError: (code, error) => BaseAction.sendError(res, code, error),
|
|
85
224
|
});
|
|
86
225
|
}
|
|
87
226
|
catch (e) {
|
|
@@ -96,7 +235,7 @@ class BaseAction extends core_1.CoreAction {
|
|
|
96
235
|
res.sendStatus(dat);
|
|
97
236
|
}
|
|
98
237
|
else {
|
|
99
|
-
res.
|
|
238
|
+
res.sendStatus(401);
|
|
100
239
|
}
|
|
101
240
|
}
|
|
102
241
|
setMode(mode) {
|
|
@@ -16,7 +16,7 @@ class BaseApiAction extends BaseAction_js_1.default {
|
|
|
16
16
|
endpoint = this.exmod.getPresenter();
|
|
17
17
|
}
|
|
18
18
|
else {
|
|
19
|
-
endpoint = this.getModule().getPresenter();
|
|
19
|
+
endpoint = this.getKernel().getModule().getPresenter();
|
|
20
20
|
}
|
|
21
21
|
if (endpoint) {
|
|
22
22
|
this.debug(`register ${this.type} ${this.getName()}`);
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { IDataBase } from '@grandlinex/core';
|
|
2
|
+
import { IBaseAction, IBaseCache, IBaseClient, IBaseKernelModule, IBasePresenter, IKernel } from '../lib/index.js';
|
|
3
|
+
import BaseApiAction from './BaseApiAction.js';
|
|
4
|
+
export default abstract class RouteApiAction<K extends IKernel = IKernel, T extends IDataBase<any, any> | null = any, P extends IBaseClient | null = any, C extends IBaseCache | null = any, E extends IBasePresenter | null = any> extends BaseApiAction<K, T, P, C, E> implements IBaseAction<K, T, P, C, E> {
|
|
5
|
+
constructor(module: IBaseKernelModule<K, T, P, C, E>, extMod?: IBaseKernelModule<K>);
|
|
6
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const index_js_1 = require("../annotation/index.js");
|
|
7
|
+
const BaseApiAction_js_1 = __importDefault(require("./BaseApiAction.js"));
|
|
8
|
+
class RouteApiAction extends BaseApiAction_js_1.default {
|
|
9
|
+
constructor(module, extMod) {
|
|
10
|
+
super('GET', 'action', module, extMod);
|
|
11
|
+
this.exmod = extMod;
|
|
12
|
+
const meta = (0, index_js_1.getRouteMeta)(this);
|
|
13
|
+
if (!meta) {
|
|
14
|
+
throw this.lError('No route meta found for action');
|
|
15
|
+
}
|
|
16
|
+
const { type, path, mode, schema } = meta;
|
|
17
|
+
this.type = type;
|
|
18
|
+
this.channel = path;
|
|
19
|
+
if (mode) {
|
|
20
|
+
this.setMode(mode);
|
|
21
|
+
}
|
|
22
|
+
this.schema = schema ?? null;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
exports.default = RouteApiAction;
|
|
@@ -3,9 +3,10 @@ import BaseAction, { ActionMode } from './BaseAction.js';
|
|
|
3
3
|
import BaseEndpoint, { keepRawBody } from './BaseEndpoint.js';
|
|
4
4
|
import BaseKernelModule from './BaseKernelModule.js';
|
|
5
5
|
import BaseApiAction from './BaseApiAction.js';
|
|
6
|
+
import RouteApiAction from './RouteApiAction.js';
|
|
6
7
|
import BaseAuthProvider from './BaseAuthProvider.js';
|
|
7
8
|
export * from './BaseAction.js';
|
|
8
9
|
export * from './BaseUserAgent.js';
|
|
9
10
|
export * from './BaseAuthProvider.js';
|
|
10
11
|
export * from './timing/index.js';
|
|
11
|
-
export { BaseLoopService, BaseAuthProvider, BaseKernelModule, BaseService, BaseApiAction, BaseEndpoint, BaseElement, BaseCache, BaseAction, BaseClient, BaseBridge, keepRawBody, ActionMode, };
|
|
12
|
+
export { BaseLoopService, BaseAuthProvider, BaseKernelModule, BaseService, BaseApiAction, BaseEndpoint, BaseElement, RouteApiAction, BaseCache, BaseAction, BaseClient, BaseBridge, keepRawBody, ActionMode, };
|
|
@@ -39,7 +39,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
39
39
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
40
|
};
|
|
41
41
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
|
-
exports.ActionMode = exports.keepRawBody = exports.BaseBridge = exports.BaseClient = exports.BaseAction = exports.BaseCache = exports.BaseElement = exports.BaseEndpoint = exports.BaseApiAction = exports.BaseService = exports.BaseKernelModule = exports.BaseAuthProvider = exports.BaseLoopService = void 0;
|
|
42
|
+
exports.ActionMode = exports.keepRawBody = exports.BaseBridge = exports.BaseClient = exports.BaseAction = exports.BaseCache = exports.RouteApiAction = exports.BaseElement = exports.BaseEndpoint = exports.BaseApiAction = exports.BaseService = exports.BaseKernelModule = exports.BaseAuthProvider = exports.BaseLoopService = void 0;
|
|
43
43
|
const core_1 = require("@grandlinex/core");
|
|
44
44
|
Object.defineProperty(exports, "BaseBridge", { enumerable: true, get: function () { return core_1.CoreBridge; } });
|
|
45
45
|
Object.defineProperty(exports, "BaseCache", { enumerable: true, get: function () { return core_1.CoreCache; } });
|
|
@@ -57,6 +57,8 @@ const BaseKernelModule_js_1 = __importDefault(require("./BaseKernelModule.js"));
|
|
|
57
57
|
exports.BaseKernelModule = BaseKernelModule_js_1.default;
|
|
58
58
|
const BaseApiAction_js_1 = __importDefault(require("./BaseApiAction.js"));
|
|
59
59
|
exports.BaseApiAction = BaseApiAction_js_1.default;
|
|
60
|
+
const RouteApiAction_js_1 = __importDefault(require("./RouteApiAction.js"));
|
|
61
|
+
exports.RouteApiAction = RouteApiAction_js_1.default;
|
|
60
62
|
const BaseAuthProvider_js_1 = __importDefault(require("./BaseAuthProvider.js"));
|
|
61
63
|
exports.BaseAuthProvider = BaseAuthProvider_js_1.default;
|
|
62
64
|
__exportStar(require("./BaseAction.js"), exports);
|
package/dist/cjs/index.d.ts
CHANGED
|
@@ -6,6 +6,7 @@ import Kernel from './Kernel.js';
|
|
|
6
6
|
import KernelModule from './KernelModule.js';
|
|
7
7
|
export * from './actions/index.js';
|
|
8
8
|
export * from './api/index.js';
|
|
9
|
+
export * from './annotation/index.js';
|
|
9
10
|
export * from './classes/index.js';
|
|
10
11
|
export * from './modules/crypto/index.js';
|
|
11
12
|
export * from './lib/index.js';
|
package/dist/cjs/index.js
CHANGED
|
@@ -28,6 +28,7 @@ const KernelModule_js_1 = __importDefault(require("./KernelModule.js"));
|
|
|
28
28
|
exports.KernelModule = KernelModule_js_1.default;
|
|
29
29
|
__exportStar(require("./actions/index.js"), exports);
|
|
30
30
|
__exportStar(require("./api/index.js"), exports);
|
|
31
|
+
__exportStar(require("./annotation/index.js"), exports);
|
|
31
32
|
__exportStar(require("./classes/index.js"), exports);
|
|
32
33
|
__exportStar(require("./modules/crypto/index.js"), exports);
|
|
33
34
|
__exportStar(require("./lib/index.js"), exports);
|
|
@@ -1,15 +1,18 @@
|
|
|
1
1
|
import express from 'express';
|
|
2
|
+
import { ErrorType } from '@grandlinex/swagger-mate';
|
|
2
3
|
import { BaseUserAgent, IExtensionInterface, JwtToken } from '../classes/index.js';
|
|
3
4
|
export type XRequest = express.Request & {
|
|
4
5
|
rawBody?: string | null;
|
|
5
6
|
};
|
|
6
7
|
export type XResponse = express.Response;
|
|
7
8
|
export type XNextFc = express.NextFunction;
|
|
8
|
-
export type XActionEvent<G = JwtToken | null> = {
|
|
9
|
+
export type XActionEvent<G = JwtToken | null, B = any> = {
|
|
9
10
|
req: XRequest;
|
|
10
11
|
res: XResponse;
|
|
11
12
|
next: XNextFc;
|
|
12
13
|
data: G;
|
|
13
14
|
extension: IExtensionInterface;
|
|
14
15
|
agent: BaseUserAgent;
|
|
16
|
+
body: B;
|
|
17
|
+
sendError: (code: number, error: Partial<ErrorType>) => void;
|
|
15
18
|
};
|
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
import { CoreCryptoClient } from '@grandlinex/core';
|
|
2
|
+
import { type StringValue } from 'ms';
|
|
2
3
|
import { ICClient, IKernel } from '../../lib/index.js';
|
|
3
4
|
import { IAuthProvider, JwtExtend, JwtToken } from '../../classes/index.js';
|
|
4
5
|
import { XRequest } from '../../lib/express.js';
|
|
5
6
|
export default class CryptoClient<T extends JwtExtend = JwtExtend> extends CoreCryptoClient implements ICClient<T> {
|
|
6
7
|
protected authProvider: IAuthProvider<T> | null;
|
|
7
8
|
protected kernel: IKernel;
|
|
8
|
-
protected expiresIn:
|
|
9
|
+
protected expiresIn: StringValue;
|
|
9
10
|
constructor(key: string, kernel: IKernel);
|
|
10
11
|
setAuthProvider(provider: IAuthProvider<T>): boolean;
|
|
11
12
|
jwtVerifyAccessToken(token: string): Promise<JwtToken<T> | number>;
|
|
12
13
|
jwtDecodeAccessToken(token: string): JwtToken<T> | null;
|
|
13
|
-
jwtGenerateAccessToken(data: JwtToken<T>, extend?: Record<string, any>, expire?:
|
|
14
|
+
jwtGenerateAccessToken(data: JwtToken<T>, extend?: Record<string, any>, expire?: StringValue | number): Promise<string>;
|
|
14
15
|
apiTokenValidation(username: string, token: string, requestType: string): Promise<{
|
|
15
16
|
valid: boolean;
|
|
16
17
|
userId: string | null;
|
|
@@ -40,7 +40,8 @@ class CryptoClient extends core_1.CoreCryptoClient {
|
|
|
40
40
|
super(kernel, key);
|
|
41
41
|
this.kernel = kernel;
|
|
42
42
|
this.authProvider = null;
|
|
43
|
-
this.expiresIn = kernel.getConfigStore().get('JWT_EXPIRE') ||
|
|
43
|
+
this.expiresIn = (kernel.getConfigStore().get('JWT_EXPIRE') ||
|
|
44
|
+
'1 days');
|
|
44
45
|
}
|
|
45
46
|
setAuthProvider(provider) {
|
|
46
47
|
if (this.authProvider) {
|
package/dist/mjs/KernelModule.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { BackgroundService } from '@grandlinex/core';
|
|
2
2
|
import BaseKernelModule from './classes/BaseKernelModule.js';
|
|
3
3
|
import KernelEndpoint from './api/KernelEndpoint.js';
|
|
4
4
|
import ApiVersionAction from './actions/ApiVersionAction.js';
|
|
@@ -10,7 +10,7 @@ export default class KernelModule extends BaseKernelModule {
|
|
|
10
10
|
this.addAction(new ApiVersionAction(this), new ApiAuthTestAction(this), new GetTokenAction(this));
|
|
11
11
|
}
|
|
12
12
|
async initModule() {
|
|
13
|
-
this.addService(new
|
|
13
|
+
this.addService(new BackgroundService(this));
|
|
14
14
|
const endpoint = new KernelEndpoint('api', this, this.getKernel().getAppServerPort());
|
|
15
15
|
this.setPresenter(endpoint);
|
|
16
16
|
await this.getKernel().triggerEvent('load');
|
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { BaseApiAction, JwtToken } from '../classes/index.js';
|
|
1
|
+
import { JwtToken, RouteApiAction } from '../classes/index.js';
|
|
3
2
|
import { XActionEvent } from '../lib/express.js';
|
|
4
|
-
export default class ApiAuthTestAction extends
|
|
5
|
-
constructor(module: IBaseKernelModule<any, any, any, any>);
|
|
3
|
+
export default class ApiAuthTestAction extends RouteApiAction {
|
|
6
4
|
handler({ data, res }: XActionEvent<JwtToken>): Promise<void>;
|
|
7
5
|
}
|
|
@@ -4,16 +4,10 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
|
|
|
4
4
|
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
5
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
6
|
};
|
|
7
|
-
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
8
|
-
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
|
-
};
|
|
10
7
|
import { SPath, SPathUtil } from '@grandlinex/swagger-mate';
|
|
11
|
-
import {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
super('GET', '/test/auth', module);
|
|
15
|
-
this.handler = this.handler.bind(this);
|
|
16
|
-
}
|
|
8
|
+
import { RouteApiAction } from '../classes/index.js';
|
|
9
|
+
import { Route } from '../annotation/index.js';
|
|
10
|
+
let ApiAuthTestAction = class ApiAuthTestAction extends RouteApiAction {
|
|
17
11
|
async handler({ data, res }) {
|
|
18
12
|
this.debug(data.userid);
|
|
19
13
|
res.status(200).send("It work's");
|
|
@@ -30,6 +24,6 @@ ApiAuthTestAction = __decorate([
|
|
|
30
24
|
},
|
|
31
25
|
},
|
|
32
26
|
}),
|
|
33
|
-
|
|
27
|
+
Route('GET', '/test/auth')
|
|
34
28
|
], ApiAuthTestAction);
|
|
35
29
|
export default ApiAuthTestAction;
|