-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
95 lines (87 loc) · 2.84 KB
/
index.js
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
class Router {
constructor(handlers) {
this.handlers = handlers;
this.init();
}
init() {
this.handlerKeys = Object.keys(this.handlers).reverse();
this.currentHandlerKey = null;
this.noHandler = true;
}
/**
* Call this function to jump to the next applicable handler
* @param handlerInput
* @return {Promise<*>} Returns false in case of no applicable handlers
*/
async next(handlerInput) {
if (this.handlerKeys.length === 0) {
if (this.noHandler) {
console.log('Router: no request handler found');
}
return false;
}
this.currentHandlerKey = this.handlerKeys.pop();
const handler = this.handlers[this.currentHandlerKey];
if (await handler.canHandle(handlerInput)) {
this.noHandler = false;
return await handler.handle(handlerInput);
} else {
return await this.next(handlerInput);
}
}
/**
* Restart routing from the first request handler
* @param handlerInput
* @return {Promise<*>}
*/
async restart(handlerInput) {
this.init();
return await this.next(handlerInput);
}
/**
* Jump to selected request handler
* @param handlerInput
* @param {string|symbol|int} key jump to the handler with selected key
* @param {boolean} check set to true if you need to test it with canHandle()
* @returns {Promise<boolean|*>}
*/
async jumpTo(handlerInput, key, check = false) {
this.init();
const index = this.handlerKeys.indexOf(key);
if (index === -1) {
console.log(`Router: no request handler found with key "${key}"`);
return false;
}
this.handlerKeys = this.handlerKeys.slice(0, index);
this.currentHandlerKey = key;
const handler = this.handlers[this.currentHandlerKey];
if (check && !await handler.canHandle(handlerInput)) {
return false;
}
this.noHandler = false;
return await handler.handle(handlerInput);
}
/**
* Export router to addRequestHandlers() function
* @return {{canHandle: (function(): boolean), handle: (function(*=): Promise<*>)}}
*/
handle() {
return {
canHandle: () => true,
handle: (handlerInput) => this.restart(handlerInput),
};
}
/**
* Inject the router to request attributes using addRequestInterceptors() function
*/
interceptor() {
return {
process: (handlerInput) => {
const attributes = handlerInput.attributesManager.getRequestAttributes();
attributes.router = this;
handlerInput.attributesManager.setRequestAttributes(attributes);
}
};
}
}
module.exports = Router;