-
Notifications
You must be signed in to change notification settings - Fork 29
/
event.ts
103 lines (81 loc) · 3.06 KB
/
event.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import get from 'lodash.get';
import { CEventType, CAppTicket } from '@node-sdk/consts';
import { Cache, Logger, LoggerLevel } from '@node-sdk/typings';
import { internalCache } from '@node-sdk/utils';
import { defaultLogger } from '@node-sdk/logger/default-logger';
import { LoggerProxy } from '@node-sdk/logger/logger-proxy';
import { IHandles } from '@node-sdk/code-gen/events-template';
import RequestHandle from './request-handle';
const CAppTicketHandle = 'app_ticket';
export class EventDispatcher {
verificationToken: string = '';
encryptKey: string = '';
requestHandle?: RequestHandle;
handles: Map<string, Function> = new Map();
cache: Cache;
logger: Logger;
constructor(params: {
verificationToken?: string;
encryptKey?: string;
cache?: Cache;
logger?: Logger;
loggerLevel?: LoggerLevel;
}) {
const { encryptKey, verificationToken } = params;
this.encryptKey = encryptKey || '';
this.verificationToken = verificationToken || '';
this.logger = new LoggerProxy(
params.loggerLevel || LoggerLevel.info,
params.logger || defaultLogger
);
this.requestHandle = new RequestHandle({
encryptKey,
verificationToken,
logger: this.logger,
});
this.cache = params.cache || internalCache;
this.registerAppTicketHandle();
this.logger.info('event-dispatch is ready');
}
private registerAppTicketHandle() {
this.register({
app_ticket: async (data) => {
const { app_ticket, app_id } = data;
if (app_ticket) {
await this.cache.set(CAppTicket, app_ticket, undefined ,{
namespace: app_id
});
this.logger.debug('set app ticket');
} else {
this.logger.warn('response not include app ticket');
}
},
});
}
register<T={}>(handles: IHandles & T) {
Object.keys(handles).forEach((key) => {
if (this.handles.has(key) && key !== CAppTicketHandle) {
this.logger.error(`this ${key} handle is registered`);
}
this.handles.set(key, handles[key]);
this.logger.debug(`register ${key} handle`);
});
return this;
}
async invoke(data, params?: { needCheck?: boolean }) {
const needCheck = get(params, 'needCheck', true);
if (needCheck && !this.requestHandle?.checkIsEventValidated(data)) {
this.logger.warn('verification failed event');
return undefined;
}
const targetData = this.requestHandle?.parse(data);
const type = targetData[CEventType];
if (this.handles.has(type)) {
const ret = await this.handles.get(type)!(targetData);
this.logger.debug(`execute ${type} handle`);
return ret;
}
this.logger.warn(`no ${type} handle`);
return `no ${type} event handle`;
}
}