forked from watson-developer-cloud/node-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebsocket-utils.ts
75 lines (70 loc) · 2.91 KB
/
websocket-utils.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
/**
* (C) Copyright IBM Corp. 2019, 2020.
*
* 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 camelcase = require('camelcase');
/**
* To adhere to our Node style guideline, we expose lowerCamelCase parameters to the user. However, the
* service expects different case conventions so we have to serialize the user-provided params. We do this
* by passing in the user params with the allowed params, looking for the camelcase version of each allowed
* param, and creating an object with the correct keys.
*
* @param {object} options - the user-provided options, with lower camel case parameters
* @param {string[]} allowedParams - array of the parameter names that the service allows
* @returns {object}
*/
export function processUserParameters(options: any, allowedParams: string[]): any {
const processedOptions = {};
// look for the camelcase version of each parameter - that is what we expose to the user
allowedParams.forEach(param => {
const keyName = camelcase(param);
if (options[keyName] !== undefined) {
processedOptions[param] = options[keyName];
} else if (options[param] !== undefined) {
// if the user used the service property name, warn them and give them the name to use
console.warn(`Unrecognized parameter: "${param}". Did you mean "${keyName}"?`);
}
});
return processedOptions;
}
/**
* Pulls the transaction ID from the connection headers and returns it in a Promise.
* This function is used by the RecognizeStream and the SynthesizeStream - they both expose
* a method called `getTransactionId` that relies on this code to read the ID from the
* connection.
*
* @param {object} streamContext - the context (i.e. "this") of the invoking stream class
* @returns {Promise<string>} - Resolves with the transaction ID as a string
*/
export function extractTransactionId(streamContext: any) {
return new Promise<string>((resolve, reject) => {
if (
streamContext.socket &&
streamContext.socket._client &&
streamContext.socket._client.response &&
streamContext.socket._client.response.headers
) {
resolve(
(streamContext.socket._client.response.headers['x-global-transaction-id'] as string)
);
} else {
streamContext.on('open', () =>
resolve(
(streamContext.socket._client.response.headers['x-global-transaction-id'] as string)
)
);
streamContext.on('error', reject);
}
});
}