-
Notifications
You must be signed in to change notification settings - Fork 0
/
BaseApiAction.ts
71 lines (67 loc) · 1.77 KB
/
BaseApiAction.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { IDataBase } from '@grandlinex/core';
import {
ActionTypes,
IBaseAction,
IBaseCache,
IBaseClient,
IBaseKernelModule,
IBasePresenter,
IKernel,
} from '../lib/index.js';
import BaseAction from './BaseAction.js';
export default abstract class BaseApiAction<
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 BaseAction<K, T, P, C, E>
implements IBaseAction<K, T, P, C, E>
{
exmod: undefined | IBaseKernelModule<K>;
type: ActionTypes;
constructor(
type: ActionTypes,
chanel: string,
module: IBaseKernelModule<K, T, P, C, E>,
extMod?: IBaseKernelModule<K>,
) {
super(chanel, module);
this.exmod = extMod;
this.type = type;
}
register(): void {
let endpoint: IBasePresenter | null;
if (this.exmod) {
endpoint = this.exmod.getPresenter();
} else {
endpoint = this.getModule().getPresenter();
}
if (endpoint) {
this.debug(`register ${this.type} ${this.getName()}`);
const app = endpoint.getApp();
switch (this.type) {
case 'POST':
app.post(this.getName(), this.secureHandler);
break;
case 'USE':
app.use(this.getName(), this.secureHandler);
break;
case 'PATCH':
app.patch(this.getName(), this.secureHandler);
break;
case 'DELETE':
app.delete(this.getName(), this.secureHandler);
break;
case 'GET':
default:
app.get(this.getName(), this.secureHandler);
break;
}
} else {
this.error(`on register -> ${this.getName()}`);
this.error(`No Endpoint found`);
}
}
}