UNPKG

4.96 kBJavaScriptView Raw
1import { __awaiter, __extends, __generator } from "tslib";
2import { assign, objectDefinedNotNull } from "./util.js";
3import { safeGlobal } from "./safe-global.js";
4export function mergeHeaders(target, source) {
5 if (objectDefinedNotNull(source)) {
6 var temp = new Request("", { headers: source });
7 temp.headers.forEach(function (value, name) {
8 target.append(name, value);
9 });
10 }
11}
12export function mergeOptions(target, source) {
13 if (objectDefinedNotNull(source)) {
14 var headers = assign(target.headers || {}, source.headers);
15 target = assign(target, source);
16 target.headers = headers;
17 }
18}
19/**
20 * Parses out the root of the request url to use as the resource when getting the token
21 *
22 * @param url The url to parse
23 */
24export function getADALResource(url) {
25 var u = new URL(url);
26 return u.protocol + "//" + u.hostname;
27}
28/**
29 * Makes requests using the global/window fetch API
30 */
31var FetchClient = /** @class */ (function () {
32 function FetchClient() {
33 }
34 FetchClient.prototype.fetch = function (url, options) {
35 return safeGlobal.fetch(url, options);
36 };
37 return FetchClient;
38}());
39export { FetchClient };
40/**
41 * Makes requests using the fetch API adding the supplied token to the Authorization header
42 */
43var BearerTokenFetchClient = /** @class */ (function (_super) {
44 __extends(BearerTokenFetchClient, _super);
45 function BearerTokenFetchClient(token) {
46 var _this = _super.call(this) || this;
47 _this.token = token;
48 return _this;
49 }
50 BearerTokenFetchClient.prototype.fetch = function (url, options) {
51 if (options === void 0) { options = {}; }
52 var headers = new Headers();
53 mergeHeaders(headers, options.headers);
54 headers.set("Authorization", "Bearer " + this.token);
55 options.headers = headers;
56 return _super.prototype.fetch.call(this, url, options);
57 };
58 return BearerTokenFetchClient;
59}(FetchClient));
60export { BearerTokenFetchClient };
61var LambdaFetchClient = /** @class */ (function (_super) {
62 __extends(LambdaFetchClient, _super);
63 function LambdaFetchClient(tokenFactory) {
64 var _this = _super.call(this, null) || this;
65 _this.tokenFactory = tokenFactory;
66 return _this;
67 }
68 /**
69 * Executes a fetch request using the supplied url and options
70 *
71 * @param url Absolute url of the request
72 * @param options Any options
73 */
74 LambdaFetchClient.prototype.fetch = function (url, options) {
75 return __awaiter(this, void 0, void 0, function () {
76 var _a;
77 return __generator(this, function (_b) {
78 switch (_b.label) {
79 case 0:
80 _a = this;
81 return [4 /*yield*/, this.tokenFactory({ url: url, options: options })];
82 case 1:
83 _a.token = _b.sent();
84 return [2 /*return*/, _super.prototype.fetch.call(this, url, options)];
85 }
86 });
87 });
88 };
89 return LambdaFetchClient;
90}(BearerTokenFetchClient));
91export { LambdaFetchClient };
92/**
93 * Client wrapping the aadTokenProvider available from SPFx >= 1.6
94 */
95var SPFxAdalClient = /** @class */ (function (_super) {
96 __extends(SPFxAdalClient, _super);
97 /**
98 *
99 * @param context provide the appropriate SPFx Context object
100 */
101 function SPFxAdalClient(context) {
102 var _this = _super.call(this, function (params) { return __awaiter(_this, void 0, void 0, function () {
103 var provider;
104 return __generator(this, function (_a) {
105 switch (_a.label) {
106 case 0: return [4 /*yield*/, context.aadTokenProviderFactory.getTokenProvider()];
107 case 1:
108 provider = _a.sent();
109 return [2 /*return*/, provider.getToken(getADALResource(params.url))];
110 }
111 });
112 }); }) || this;
113 _this.context = context;
114 return _this;
115 }
116 /**
117 * Gets an AAD token for the provided resource using the SPFx AADTokenProvider
118 *
119 * @param resource Resource for which a token is to be requested (ex: https://graph.microsoft.com)
120 */
121 SPFxAdalClient.prototype.getToken = function (resource) {
122 return __awaiter(this, void 0, void 0, function () {
123 var provider;
124 return __generator(this, function (_a) {
125 switch (_a.label) {
126 case 0: return [4 /*yield*/, this.context.aadTokenProviderFactory.getTokenProvider()];
127 case 1:
128 provider = _a.sent();
129 return [2 /*return*/, provider.getToken(resource)];
130 }
131 });
132 });
133 };
134 return SPFxAdalClient;
135}(LambdaFetchClient));
136export { SPFxAdalClient };
137//# sourceMappingURL=net.js.map
\No newline at end of file