-
Notifications
You must be signed in to change notification settings - Fork 10
/
util.ts
41 lines (38 loc) · 1.26 KB
/
util.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
import { QueryStringParameters } from './type-definitions/additions';
import { OAuthError } from './errors';
/**
* Generates the query string parameters out of the specified object which contains the parameters
*
* @export
* @param {IQueryStringParameters} queryStringParameters Object which contains the query keys and values
* @returns {string} The query string to add to the endpoint url
*/
export function resolveQueryStringParameters(queryStringParameters: QueryStringParameters): string {
let queryString = '?';
const end = Object.keys(queryStringParameters).length;
let count = 0;
for (const key in queryStringParameters) {
if (queryStringParameters.hasOwnProperty(key)) {
count++;
const value = queryStringParameters[key];
if (count !== end) {
queryString = queryString.concat(key, '=', value, '&');
} else {
queryString = queryString.concat(key, '=', value);
}
}
}
return queryString;
}
/**
* Checks for existing of an oauth access token.
* Throws an OauthError if not present.
*
* @export
* @param {string} [oauthAccesstoken]
*/
export function checkOauthToken(oauthAccesstoken?: string) {
if (!oauthAccesstoken) {
throw new OAuthError('You have to use OAuth to access this endpoint.');
}
}