-
Notifications
You must be signed in to change notification settings - Fork 668
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: change all websocket method parameters to lower camel case (#…
…941) * add types for WebSocket Stream class constructor options * warn user when a parameter is passed in that matches the service option but not the user option BREAKING CHANGE: All parameters have been converted to their lower camel case version. BREAKING CHANGE: Support for the `token` parameter has been removed BREAKING CHANGE: Support for the `customization_id` parameter has been removed BREAKING CHANGE: Method `setAuthorizationHeaderToken` has been removed from the WebSocket Stream classes
- Loading branch information
Showing
10 changed files
with
506 additions
and
265 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/** | ||
* (C) Copyright IBM Corp. 2019. | ||
* | ||
* 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'); | ||
import extend = require('extend'); | ||
|
||
/** | ||
* 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; | ||
} | ||
|
||
/** | ||
* This function retrieves an access token and stores it in the | ||
* request header before calling the callback function. | ||
* | ||
* @param {object} options - the internal options of a websocket stream class | ||
* @param {Function} callback | ||
*/ | ||
export function setAuthorizationHeader(options: any, callback: Function): void { | ||
// assuming the token manger would fall under property 'tokenManager' | ||
// this will likely change with the new authenticators anyways | ||
if (options.tokenManager) { | ||
options.tokenManager.getToken((err, token) => { | ||
if (err) { | ||
return callback(err); | ||
} | ||
const authHeader = { authorization: 'Bearer ' + token }; | ||
options.headers = extend(options.headers, authHeader); | ||
callback(null); | ||
}); | ||
} else { | ||
callback(null); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.