1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | import * as Debug from 'debug';
|
18 | import * as https from 'https';
|
19 |
|
20 | const name = 'actions-on-google';
|
21 |
|
22 |
|
23 | export const debug = Debug(`${name}:debug`);
|
24 |
|
25 |
|
26 | export const warn = Debug(`${name}:warn`);
|
27 |
|
28 |
|
29 |
|
30 | export const error = console.error.bind(console) as typeof console.error;
|
31 |
|
32 |
|
33 |
|
34 | export const info = console.log.bind(console) as typeof console.log;
|
35 |
|
36 | warn.log = error;
|
37 | debug.log = info;
|
38 |
|
39 |
|
40 | export const deprecate = (feature: string, alternative: string) =>
|
41 | info(`${feature} is *DEPRECATED*: ${alternative}`);
|
42 |
|
43 |
|
44 | export interface JsonObject {
|
45 |
|
46 | [key: string]: any;
|
47 | }
|
48 |
|
49 |
|
50 | export const values = <T>(o: {[key: string]: T}) =>
|
51 | Object.keys(o).map(k => o[k]);
|
52 |
|
53 |
|
54 | export const clone = <T>(o: T): T => JSON.parse(JSON.stringify(o));
|
55 |
|
56 |
|
57 |
|
58 | export 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 |
|
82 | export type ProtoAny<TType, TSpec> = {'@type': TType} & TSpec;
|
83 |
|
84 |
|
85 | export const toArray = <T>(a: T | T[]) => (Array.isArray(a) ? a : [a]);
|
86 |
|
87 |
|
88 | export interface ApiClientObjectMap<TValue> {
|
89 | [key: string]: TValue;
|
90 | }
|
91 |
|
92 |
|
93 |
|
94 | export const request: typeof https.request = https.request.bind(https);
|