1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | Object.defineProperty(exports, "__esModule", { value: true });
|
18 | exports.attach = void 0;
|
19 | const framework_1 = require("./framework");
|
20 | const common = require("./common");
|
21 |
|
22 | const create = (options) => ({
|
23 | frameworks: Object.assign({}, framework_1.builtin),
|
24 | handler: () => Promise.reject(new Error('StandardHandler not set')),
|
25 | use(plugin) {
|
26 | return plugin(this) || this;
|
27 | },
|
28 | debug: !!(options && options.debug),
|
29 | });
|
30 |
|
31 | const attach = (service, options) => {
|
32 | let app = Object.assign(create(options), service);
|
33 |
|
34 | const omni = (...args) => {
|
35 | for (const framework of common.values(app.frameworks)) {
|
36 | if (framework.check(...args)) {
|
37 | return framework.handle(app.handler)(...args);
|
38 | }
|
39 | }
|
40 | return app.handler(args[0], args[1]);
|
41 | };
|
42 | app = Object.assign(omni, app);
|
43 | const handler = app.handler.bind(app);
|
44 | const standard = async (body, headers, metadata) => {
|
45 | const log = app.debug ? common.info : common.debug;
|
46 | log('Request', common.stringify(body));
|
47 | log('Headers', common.stringify(headers));
|
48 | const response = await handler(body, headers, metadata);
|
49 | if (!response.headers) {
|
50 | response.headers = {};
|
51 | }
|
52 | response.headers['content-type'] = 'application/json;charset=utf-8';
|
53 | log('Response', common.stringify(response));
|
54 | return response;
|
55 | };
|
56 | app.handler = standard;
|
57 | return app;
|
58 | };
|
59 | exports.attach = attach;
|
60 |
|
\ | No newline at end of file |