Skip to content

Commit 8aece4e

Browse files
author
Patrick Ammann
committed
feat: #1 port to typescript
1 parent ca51a6e commit 8aece4e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+1071
-627
lines changed

src/AccessTokenEvents.js renamed to src/AccessTokenEvents.ts

+14-9
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,30 @@
11
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
33

4-
import { Log } from './Log.js';
5-
import { Timer } from './Timer.js';
4+
import { Log } from './Log';
5+
import { Timer } from './Timer';
6+
import { User } from './User';
67

78
const DefaultAccessTokenExpiringNotificationTime = 60; // seconds
89

10+
export type AccessTokenCallback = (...ev: any[]) => void;
11+
912
export class AccessTokenEvents {
13+
private _accessTokenExpiringNotificationTime: number
14+
private _accessTokenExpiring: Timer
15+
private _accessTokenExpired: Timer
1016

1117
constructor({
1218
accessTokenExpiringNotificationTime = DefaultAccessTokenExpiringNotificationTime,
1319
accessTokenExpiringTimer = new Timer("Access token expiring"),
1420
accessTokenExpiredTimer = new Timer("Access token expired")
1521
} = {}) {
1622
this._accessTokenExpiringNotificationTime = accessTokenExpiringNotificationTime;
17-
1823
this._accessTokenExpiring = accessTokenExpiringTimer;
1924
this._accessTokenExpired = accessTokenExpiredTimer;
2025
}
2126

22-
load(container) {
27+
load(container: User) {
2328
// only register events if there's an access token and it has an expiration
2429
if (container.access_token && container.expires_in !== undefined) {
2530
let duration = container.expires_in;
@@ -31,7 +36,7 @@ export class AccessTokenEvents {
3136
if (expiring <= 0){
3237
expiring = 1;
3338
}
34-
39+
3540
Log.debug("AccessTokenEvents.load: registering expiring timer in:", expiring);
3641
this._accessTokenExpiring.init(expiring);
3742
}
@@ -57,17 +62,17 @@ export class AccessTokenEvents {
5762
this._accessTokenExpired.cancel();
5863
}
5964

60-
addAccessTokenExpiring(cb) {
65+
addAccessTokenExpiring(cb: AccessTokenCallback) {
6166
this._accessTokenExpiring.addHandler(cb);
6267
}
63-
removeAccessTokenExpiring(cb) {
68+
removeAccessTokenExpiring(cb: AccessTokenCallback) {
6469
this._accessTokenExpiring.removeHandler(cb);
6570
}
6671

67-
addAccessTokenExpired(cb) {
72+
addAccessTokenExpired(cb: AccessTokenCallback) {
6873
this._accessTokenExpired.addHandler(cb);
6974
}
70-
removeAccessTokenExpired(cb) {
75+
removeAccessTokenExpired(cb: AccessTokenCallback) {
7176
this._accessTokenExpired.removeHandler(cb);
7277
}
7378
}

src/CheckSessionIFrame.js renamed to src/CheckSessionIFrame.ts

+27-11
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,26 @@
11
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
33

4-
import { Log } from './Log.js';
4+
import { Log } from './Log';
55

66
const DefaultInterval = 2000;
77

88
export class CheckSessionIFrame {
9-
constructor(callback, client_id, url, interval, stopOnError = true) {
9+
private _callback: () => void;
10+
private _client_id: string;
11+
private _interval: number;
12+
private _stopOnError: boolean;
13+
private _frame_origin: string;
14+
private _frame: HTMLIFrameElement;
15+
private _boundMessageEvent: ((e: any) => void) | null;
16+
private _timer: number | null;
17+
private _session_state: any | null;
18+
19+
constructor(callback: () => void, client_id: string, url: string, interval?: number, stopOnError?: boolean) {
1020
this._callback = callback;
1121
this._client_id = client_id;
12-
this._url = url;
1322
this._interval = interval || DefaultInterval;
14-
this._stopOnError = stopOnError;
23+
this._stopOnError = stopOnError || true;
1524

1625
var idx = url.indexOf("/", url.indexOf("//") + 2);
1726
this._frame_origin = url.substr(0, idx);
@@ -22,13 +31,17 @@ export class CheckSessionIFrame {
2231
this._frame.style.visibility = "hidden";
2332
this._frame.style.position = "absolute";
2433
this._frame.style.display = "none";
25-
this._frame.width = 0;
26-
this._frame.height = 0;
27-
34+
this._frame.width = "0";
35+
this._frame.height = "0";
2836
this._frame.src = url;
37+
38+
this._boundMessageEvent = null;
39+
this._timer = null;
40+
2941
}
42+
3043
load() {
31-
return new Promise((resolve) => {
44+
return new Promise<void>((resolve) => {
3245
this._frame.onload = () => {
3346
resolve();
3447
}
@@ -38,7 +51,8 @@ export class CheckSessionIFrame {
3851
window.addEventListener("message", this._boundMessageEvent, false);
3952
});
4053
}
41-
_message(e) {
54+
55+
_message(e: any) {
4256
if (e.origin === this._frame_origin &&
4357
e.source === this._frame.contentWindow
4458
) {
@@ -58,7 +72,8 @@ export class CheckSessionIFrame {
5872
}
5973
}
6074
}
61-
start(session_state) {
75+
76+
start(session_state: any) {
6277
if (this._session_state !== session_state) {
6378
Log.debug("CheckSessionIFrame.start");
6479

@@ -67,9 +82,10 @@ export class CheckSessionIFrame {
6782
this._session_state = session_state;
6883

6984
let send = () => {
85+
this._frame.contentWindow &&
7086
this._frame.contentWindow.postMessage(this._client_id + " " + this._session_state, this._frame_origin);
7187
};
72-
88+
7389
// trigger now
7490
send();
7591

File renamed without changes.

src/CordovaIFrameNavigator.js renamed to src/CordovaIFrameNavigator.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
33

4-
import { CordovaPopupWindow } from './CordovaPopupWindow.js';
4+
import { CordovaPopupWindow } from './CordovaPopupWindow';
5+
import { INavigator } from './INavigator';
56

6-
export class CordovaIFrameNavigator {
7+
export class CordovaIFrameNavigator implements INavigator {
78

8-
prepare(params) {
9+
prepare(params: any) {
910
params.popupWindowFeatures = 'hidden=yes';
1011
let popup = new CordovaPopupWindow(params);
1112
return Promise.resolve(popup);

src/CordovaPopupNavigator.js renamed to src/CordovaPopupNavigator.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
33

4-
import { CordovaPopupWindow } from './CordovaPopupWindow.js';
4+
import { CordovaPopupWindow } from './CordovaPopupWindow';
5+
import { INavigator } from './INavigator';
56

6-
export class CordovaPopupNavigator {
7+
export class CordovaPopupNavigator implements INavigator {
78

8-
prepare(params) {
9+
prepare(params: any) {
910
let popup = new CordovaPopupWindow(params);
1011
return Promise.resolve(popup);
1112
}

src/CordovaPopupWindow.js renamed to src/CordovaPopupWindow.ts

+36-20
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,67 @@
11
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
33

4-
import { Log } from './Log.js';
4+
import { Log } from './Log';
5+
import { IWindow } from './IWindow';
56

67
const DefaultPopupFeatures = 'location=no,toolbar=no,zoom=no';
78
const DefaultPopupTarget = "_blank";
89

9-
export class CordovaPopupWindow {
10+
export class CordovaPopupWindow implements IWindow {
11+
private _promise: Promise<unknown>;
12+
private _resolve!: (value: unknown) => void;
13+
private _reject!: (reason?: any) => void;
14+
private features: string;
15+
private target: string;
16+
private redirect_uri: string;
17+
private _popup: any;
18+
private _exitCallbackEvent?: (message: any) => void;
19+
private _loadStartCallbackEvent?: (event: any) => void;
1020

11-
constructor(params) {
21+
constructor(params: any) {
1222
this._promise = new Promise((resolve, reject) => {
1323
this._resolve = resolve;
1424
this._reject = reject;
1525
});
1626

1727
this.features = params.popupWindowFeatures || DefaultPopupFeatures;
1828
this.target = params.popupWindowTarget || DefaultPopupTarget;
19-
29+
2030
this.redirect_uri = params.startUrl;
2131
Log.debug("CordovaPopupWindow.ctor: redirect_uri: " + this.redirect_uri);
2232
}
2333

24-
_isInAppBrowserInstalled(cordovaMetadata) {
34+
_isInAppBrowserInstalled(cordovaMetadata: any) {
2535
return ["cordova-plugin-inappbrowser", "cordova-plugin-inappbrowser.inappbrowser", "org.apache.cordova.inappbrowser"].some(function (name) {
2636
return cordovaMetadata.hasOwnProperty(name)
2737
})
2838
}
29-
30-
navigate(params) {
39+
40+
navigate(params: any) {
3141
if (!params || !params.url) {
3242
this._error("No url provided");
3343
} else {
44+
// @ts-ignore
3445
if (!window.cordova) {
35-
return this._error("cordova is undefined")
46+
this._error("cordova is undefined");
47+
return this.promise;
3648
}
37-
49+
50+
// @ts-ignore
3851
var cordovaMetadata = window.cordova.require("cordova/plugin_list").metadata;
3952
if (this._isInAppBrowserInstalled(cordovaMetadata) === false) {
40-
return this._error("InAppBrowser plugin not found")
53+
this._error("InAppBrowser plugin not found");
54+
return this.promise;
4155
}
56+
57+
// @ts-ignore
4258
this._popup = cordova.InAppBrowser.open(params.url, this.target, this.features);
4359
if (this._popup) {
4460
Log.debug("CordovaPopupWindow.navigate: popup successfully created");
45-
46-
this._exitCallbackEvent = this._exitCallback.bind(this);
61+
62+
this._exitCallbackEvent = this._exitCallback.bind(this);
4763
this._loadStartCallbackEvent = this._loadStartCallback.bind(this);
48-
64+
4965
this._popup.addEventListener("exit", this._exitCallbackEvent, false);
5066
this._popup.addEventListener("loadstart", this._loadStartCallbackEvent, false);
5167
} else {
@@ -59,22 +75,22 @@ export class CordovaPopupWindow {
5975
return this._promise;
6076
}
6177

62-
_loadStartCallback(event) {
78+
_loadStartCallback(event: any) {
6379
if (event.url.indexOf(this.redirect_uri) === 0) {
6480
this._success({ url: event.url });
65-
}
81+
}
6682
}
67-
_exitCallback(message) {
68-
this._error(message);
83+
_exitCallback(message: string) {
84+
this._error(message);
6985
}
70-
71-
_success(data) {
86+
87+
_success(data: any) {
7288
this._cleanup();
7389

7490
Log.debug("CordovaPopupWindow: Successful response from cordova popup window");
7591
this._resolve(data);
7692
}
77-
_error(message) {
93+
_error(message: string) {
7894
this._cleanup();
7995

8096
Log.error(message);

src/ErrorResponse.js renamed to src/ErrorResponse.ts

+14-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
33

4-
import { Log } from './Log.js';
4+
import { Log } from './Log';
55

66
export class ErrorResponse extends Error {
7-
constructor({error, error_description, error_uri, state, session_state}={}
8-
) {
9-
if (!error){
7+
public readonly name: string;
8+
9+
public readonly error: string;
10+
public readonly error_description: string;
11+
public readonly error_uri: string;
12+
13+
public readonly state: any;
14+
public readonly session_state?: string;
15+
16+
constructor({
17+
error, error_description, error_uri, state, session_state
18+
}: any) {
19+
if (!error) {
1020
Log.error("No error passed to ErrorResponse");
1121
throw new Error("error");
1222
}

src/Event.js renamed to src/Event.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,29 @@
11
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
33

4-
import { Log } from './Log.js';
4+
import { Log } from './Log';
55

66
export class Event {
7+
protected _name: string;
8+
private _callbacks: ((...ev: any[]) => void)[];
79

8-
constructor(name) {
10+
constructor(name: string) {
911
this._name = name;
1012
this._callbacks = [];
1113
}
1214

13-
addHandler(cb) {
15+
addHandler(cb: (...ev: any[]) => void) {
1416
this._callbacks.push(cb);
1517
}
1618

17-
removeHandler(cb) {
19+
removeHandler(cb: (...ev: any[]) => void) {
1820
var idx = this._callbacks.findIndex(item => item === cb);
1921
if (idx >= 0) {
2022
this._callbacks.splice(idx, 1);
2123
}
2224
}
2325

24-
raise(...params) {
26+
raise(...params: any[]) {
2527
Log.debug("Event: Raising event: " + this._name);
2628
for (let i = 0; i < this._callbacks.length; i++) {
2729
this._callbacks[i](...params);

0 commit comments

Comments
 (0)