UNPKG

2.25 kBJavaScriptView Raw
1"use strict";
2/**
3 * Copyright 2018 Google Inc. All Rights Reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.attach = void 0;
19const framework_1 = require("./framework");
20const common = require("./common");
21/** @hidden */
22const 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/** @hidden */
31const attach = (service, options) => {
32 let app = Object.assign(create(options), service);
33 // tslint:disable-next-line:no-any automatically detect any inputs
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};
59exports.attach = attach;
60//# sourceMappingURL=assistant.js.map
\No newline at end of file