UNPKG

2.72 kBPlain TextView Raw
1/**
2 * Copyright 2018 Google Inc. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17import * as Debug from 'debug';
18import * as https from 'https';
19
20const name = 'actions-on-google';
21
22/** @hidden */
23export const debug = Debug(`${name}:debug`);
24
25/** @hidden */
26export const warn = Debug(`${name}:warn`);
27
28/** @hidden */
29// tslint:disable-next-line:no-console Allow console binding
30export const error = console.error.bind(console) as typeof console.error;
31
32/** @hidden */
33// tslint:disable-next-line:no-console Allow console binding
34export const info = console.log.bind(console) as typeof console.log;
35
36warn.log = error;
37debug.log = info;
38
39/** @hidden */
40export const deprecate = (feature: string, alternative: string) =>
41 info(`${feature} is *DEPRECATED*: ${alternative}`);
42
43/** @public */
44export interface JsonObject {
45 // tslint:disable-next-line:no-any JSON value can be anything
46 [key: string]: any;
47}
48
49/** @hidden */
50export const values = <T>(o: {[key: string]: T}) =>
51 Object.keys(o).map(k => o[k]);
52
53/** @hidden */
54export const clone = <T>(o: T): T => JSON.parse(JSON.stringify(o));
55
56/** @hidden */
57// tslint:disable-next-line:no-any root can be anything
58export const stringify = (root: any, ...exclude: string[]) => {
59 const excluded = new Set(exclude);
60 const filtered = Object.keys(root).reduce((o, k) => {
61 if (excluded.has(k)) {
62 o[k] = '[Excluded]';
63 return o;
64 }
65 const value = root[k];
66 try {
67 JSON.stringify(value);
68 o[k] = value;
69 return o;
70 } catch (e) {
71 const {message = ''} = e;
72 o[k] = message.includes('Converting circular structure to JSON')
73 ? '[Circular]'
74 : `[Stringify Error] ${e}`;
75 return o;
76 }
77 }, {} as typeof root);
78 return JSON.stringify(filtered, null, 2);
79};
80
81/** @hidden */
82export type ProtoAny<TType, TSpec> = {'@type': TType} & TSpec;
83
84/** @hidden */
85export const toArray = <T>(a: T | T[]) => (Array.isArray(a) ? a : [a]);
86
87/** @hidden */
88export interface ApiClientObjectMap<TValue> {
89 [key: string]: TValue;
90}
91
92// Bind this to https to ensure its not implementation dependent
93/** @hidden */
94export const request: typeof https.request = https.request.bind(https);