-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: remove specific handling for "watson-token" query params (#149
) We currently have a special hack to handle watson tokens in a unique way. This used to be required for Watson services but they have not used this method of authentication in years. This is supposed to be a service-agnostic package, so this commit refactors the code to avoid the specific handling. I also edited a jsdoc file (even though I don't think it is used) and added a test for this small module.
- Loading branch information
Showing
3 changed files
with
14 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,14 @@ | ||
/* eslint-disable prettier/prettier, arrow-body-style */ | ||
|
||
/** | ||
* Stringify query params, Watson-style | ||
* | ||
* Why? The server that processes auth tokens currently only accepts the *exact* string, even if it's invalid for a URL. | ||
* Properly url-encoding percent characters causes it to reject the token | ||
* So, this is a custom qs.stringify function that properly encodes everything except watson-token, passing it along verbatim | ||
* Lightweight implementation for stringify-ing query params | ||
* | ||
* @param {object<string, object>} queryParams | ||
* @return {String} | ||
*/ | ||
const stringify = (queryParams: Object): string => { | ||
return Object.keys(queryParams) | ||
.map((key) => { | ||
return ( | ||
`${key | ||
}=${ | ||
key === 'watson-token' | ||
? queryParams[key] | ||
: encodeURIComponent(queryParams[key])}` | ||
); // the server chokes if the token is correctly url-encoded | ||
}) | ||
const stringify = (queryParams: Object): string => | ||
Object.keys(queryParams) | ||
.map((key) => `${key}=${encodeURIComponent(queryParams[key])}`) | ||
.join('&'); | ||
}; | ||
|
||
export default { | ||
stringify | ||
stringify, | ||
}; |
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,8 @@ | ||
const { stringify } = require('../../dist/lib/querystring').default; | ||
|
||
describe('querystring', () => { | ||
it('should convert params to query string format', () => { | ||
const params = { foo: 'bar', baz: ['qux', 'quux'], corge: '' }; | ||
expect(stringify(params)).toBe('foo=bar&baz=qux%2Cquux&corge='); | ||
}); | ||
}); |