UNPKG

2.31 kBJavaScriptView Raw
1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT License. See License.txt in the project root for license information.
3import { HttpHeaders } from "../httpHeaders";
4/**
5 * Authenticates to a service using an API key.
6 */
7var ApiKeyCredentials = /** @class */ (function () {
8 /**
9 * @constructor
10 * @param {object} options Specifies the options to be provided for auth. Either header or query needs to be provided.
11 */
12 function ApiKeyCredentials(options) {
13 if (!options || (options && !options.inHeader && !options.inQuery)) {
14 throw new Error("options cannot be null or undefined. Either \"inHeader\" or \"inQuery\" property of the options object needs to be provided.");
15 }
16 this.inHeader = options.inHeader;
17 this.inQuery = options.inQuery;
18 }
19 /**
20 * Signs a request with the values provided in the inHeader and inQuery parameter.
21 *
22 * @param {WebResource} webResource The WebResource to be signed.
23 * @returns {Promise<WebResource>} The signed request object.
24 */
25 ApiKeyCredentials.prototype.signRequest = function (webResource) {
26 if (!webResource) {
27 return Promise.reject(new Error("webResource cannot be null or undefined and must be of type \"object\"."));
28 }
29 if (this.inHeader) {
30 if (!webResource.headers) {
31 webResource.headers = new HttpHeaders();
32 }
33 for (var headerName in this.inHeader) {
34 webResource.headers.set(headerName, this.inHeader[headerName]);
35 }
36 }
37 if (this.inQuery) {
38 if (!webResource.url) {
39 return Promise.reject(new Error("url cannot be null in the request object."));
40 }
41 if (webResource.url.indexOf("?") < 0) {
42 webResource.url += "?";
43 }
44 for (var key in this.inQuery) {
45 if (!webResource.url.endsWith("?")) {
46 webResource.url += "&";
47 }
48 webResource.url += key + "=" + this.inQuery[key];
49 }
50 }
51 return Promise.resolve(webResource);
52 };
53 return ApiKeyCredentials;
54}());
55export { ApiKeyCredentials };
56//# sourceMappingURL=apiKeyCredentials.js.map
\No newline at end of file