Skip to content

Commit ead20b5

Browse files
refactoring; fixing tests for power pages #157
1 parent 0c24941 commit ead20b5

File tree

4 files changed

+20
-12
lines changed

4 files changed

+20
-12
lines changed

src/client/RequestClient.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -93,19 +93,11 @@ export class RequestClient {
9393
}
9494

9595
if (token) {
96-
if (!request.headers) {
97-
request.headers = {};
98-
}
99-
request.headers["Authorization"] = "Bearer " + (token.hasOwnProperty("accessToken") ? (token as AccessToken).accessToken : token);
96+
request.headers!["Authorization"] = "Bearer " + (token.hasOwnProperty("accessToken") ? (token as AccessToken).accessToken : token);
10097
}
10198

10299
if (Utility.isRunningWithinPortals()) {
103-
try {
104-
const token = await (window as any).shell.getTokenDeferred();
105-
request.headers["__RequestVerificationToken"] = token;
106-
} catch (error) {
107-
throw new Error("Unable to fetch token from shell. Error: " + (error as Error)?.message);
108-
}
100+
request.headers!["__RequestVerificationToken"] = await global.window.shell!.getTokenDeferred();
109101
}
110102

111103
const url = request.apiConfig ? request.apiConfig.url : config.dataApi.url;

src/types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,11 @@ export declare namespace Core {
167167
async?: boolean;
168168
}
169169
}
170+
171+
declare global {
172+
interface Window {
173+
shell?: {
174+
getTokenDeferred: () => Promise<string>;
175+
};
176+
}
177+
}

src/utils/Utility.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ export class Utility {
162162
* @returns {boolean}
163163
*/
164164
static isRunningWithinPortals(): boolean {
165-
return global.DWA_BROWSER ? !!(global.window as any).shell : false;
165+
return global.DWA_BROWSER ? !!global.window.shell : false;
166166
}
167167

168168
static isObject(obj: any): boolean {

tests/xhr.spec.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,11 @@ describe("xhr -", () => {
126126
};
127127

128128
//@ts-ignore
129-
global.window.shell = {};
129+
global.window.shell = {
130+
getTokenDeferred: async () => {
131+
return "token";
132+
},
133+
};
130134
//@ts-ignore
131135
global.window.location = {};
132136
//@ts-ignore
@@ -177,6 +181,10 @@ describe("xhr -", () => {
177181
expect(requests[0].requestBody).to.be.undefined;
178182
});
179183

184+
it("sends the correct __RequestVerificationToken header", function () {
185+
expect(requests[0].requestHeaders["__RequestVerificationToken"]).to.be.eq("token");
186+
});
187+
180188
it("sends the correct If-Match header", function () {
181189
expect(requests[0].requestHeaders["If-Match"]).to.be.undefined;
182190
});

0 commit comments

Comments
 (0)