1 | import { __awaiter, __extends, __generator } from "tslib";
|
2 | import { assign, objectDefinedNotNull } from "./util.js";
|
3 | import { safeGlobal } from "./safe-global.js";
|
4 | export 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 | }
|
12 | export 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 |
|
21 |
|
22 |
|
23 |
|
24 | export function getADALResource(url) {
|
25 | var u = new URL(url);
|
26 | return u.protocol + "//" + u.hostname;
|
27 | }
|
28 |
|
29 |
|
30 |
|
31 | var FetchClient = (function () {
|
32 | function FetchClient() {
|
33 | }
|
34 | FetchClient.prototype.fetch = function (url, options) {
|
35 | return safeGlobal.fetch(url, options);
|
36 | };
|
37 | return FetchClient;
|
38 | }());
|
39 | export { FetchClient };
|
40 |
|
41 |
|
42 |
|
43 | var BearerTokenFetchClient = (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));
|
60 | export { BearerTokenFetchClient };
|
61 | var LambdaFetchClient = (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 |
|
70 |
|
71 |
|
72 |
|
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 , this.tokenFactory({ url: url, options: options })];
|
82 | case 1:
|
83 | _a.token = _b.sent();
|
84 | return [2 , _super.prototype.fetch.call(this, url, options)];
|
85 | }
|
86 | });
|
87 | });
|
88 | };
|
89 | return LambdaFetchClient;
|
90 | }(BearerTokenFetchClient));
|
91 | export { LambdaFetchClient };
|
92 |
|
93 |
|
94 |
|
95 | var SPFxAdalClient = (function (_super) {
|
96 | __extends(SPFxAdalClient, _super);
|
97 | |
98 |
|
99 |
|
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 , context.aadTokenProviderFactory.getTokenProvider()];
|
107 | case 1:
|
108 | provider = _a.sent();
|
109 | return [2 , provider.getToken(getADALResource(params.url))];
|
110 | }
|
111 | });
|
112 | }); }) || this;
|
113 | _this.context = context;
|
114 | return _this;
|
115 | }
|
116 | |
117 |
|
118 |
|
119 |
|
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 , this.context.aadTokenProviderFactory.getTokenProvider()];
|
127 | case 1:
|
128 | provider = _a.sent();
|
129 | return [2 , provider.getToken(resource)];
|
130 | }
|
131 | });
|
132 | });
|
133 | };
|
134 | return SPFxAdalClient;
|
135 | }(LambdaFetchClient));
|
136 | export { SPFxAdalClient };
|
137 |
|
\ | No newline at end of file |