This repository has been archived by the owner on May 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 193
/
assistant.ts
104 lines (94 loc) · 2.87 KB
/
assistant.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
104
/**
* Copyright 2018 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {
OmniHandler,
StandardHandler,
BuiltinFrameworks,
builtin,
} from './framework';
import * as common from './common';
/** @public */
export type AppHandler = OmniHandler & BaseApp;
/** @public */
export interface AppOptions {
/** @public */
debug?: boolean;
}
/** @hidden */
export interface ServiceBaseApp {
/** @public */
handler: StandardHandler;
}
/** @public */
export interface Plugin<TService, TPlugin> {
/** @public */
<TApp>(app: AppHandler & TService & TApp):
| (AppHandler & TService & TApp & TPlugin)
| void;
}
/** @public */
export interface BaseApp extends ServiceBaseApp {
/** @public */
frameworks: BuiltinFrameworks;
/** @public */
use<TService, TPlugin>(plugin: Plugin<TService, TPlugin>): this & TPlugin;
/** @public */
debug: boolean;
}
/** @hidden */
const create = (options?: AppOptions): BaseApp => ({
frameworks: Object.assign({}, builtin),
handler: () => Promise.reject(new Error('StandardHandler not set')),
use(plugin) {
return plugin(this) || this;
},
debug: !!(options && options.debug),
});
/** @hidden */
export const attach = <TService>(
service: TService,
options?: AppOptions
): AppHandler & TService => {
let app: (BaseApp & TService) | (AppHandler & TService) = Object.assign(
create(options),
service
);
// tslint:disable-next-line:no-any automatically detect any inputs
const omni: OmniHandler = (...args: any[]) => {
for (const framework of common.values(app.frameworks)) {
if (framework.check(...args)) {
return framework.handle(app.handler)(...args);
}
}
return app.handler(args[0], args[1]);
};
app = Object.assign(omni, app);
const handler: typeof app.handler = app.handler.bind(app);
const standard: StandardHandler = async (body, headers, metadata) => {
const log = app.debug ? common.info : common.debug;
log('Request', common.stringify(body));
log('Headers', common.stringify(headers));
const response = await handler(body, headers, metadata);
if (!response.headers) {
response.headers = {};
}
response.headers['content-type'] = 'application/json;charset=utf-8';
log('Response', common.stringify(response));
return response;
};
app.handler = standard;
return app as AppHandler & TService;
};