Skip to content

Commit 90addc0

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

Some content is hidden

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

51 files changed

+993
-582
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

+26-10
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,11 +31,15 @@ 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() {
3144
return new Promise((resolve) => {
3245
this._frame.onload = () => {
@@ -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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
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';
55

66
export class CordovaIFrameNavigator {
77

8-
prepare(params) {
8+
prepare(params: any) {
99
params.popupWindowFeatures = 'hidden=yes';
1010
let popup = new CordovaPopupWindow(params);
1111
return Promise.resolve(popup);

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
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';
55

66
export class CordovaPopupNavigator {
77

8-
prepare(params) {
8+
prepare(params: any) {
99
let popup = new CordovaPopupWindow(params);
1010
return Promise.resolve(popup);
1111
}

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

+30-17
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,64 @@
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 DefaultPopupFeatures = 'location=no,toolbar=no,zoom=no';
77
const DefaultPopupTarget = "_blank";
88

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

11-
constructor(params) {
20+
constructor(params: any) {
1221
this._promise = new Promise((resolve, reject) => {
1322
this._resolve = resolve;
1423
this._reject = reject;
1524
});
1625

1726
this.features = params.popupWindowFeatures || DefaultPopupFeatures;
1827
this.target = params.popupWindowTarget || DefaultPopupTarget;
19-
28+
2029
this.redirect_uri = params.startUrl;
2130
Log.debug("CordovaPopupWindow.ctor: redirect_uri: " + this.redirect_uri);
2231
}
2332

24-
_isInAppBrowserInstalled(cordovaMetadata) {
33+
_isInAppBrowserInstalled(cordovaMetadata: any) {
2534
return ["cordova-plugin-inappbrowser", "cordova-plugin-inappbrowser.inappbrowser", "org.apache.cordova.inappbrowser"].some(function (name) {
2635
return cordovaMetadata.hasOwnProperty(name)
2736
})
2837
}
29-
30-
navigate(params) {
38+
39+
navigate(params: any) {
3140
if (!params || !params.url) {
3241
this._error("No url provided");
3342
} else {
43+
// @ts-ignore
3444
if (!window.cordova) {
3545
return this._error("cordova is undefined")
3646
}
37-
47+
48+
// @ts-ignore
3849
var cordovaMetadata = window.cordova.require("cordova/plugin_list").metadata;
3950
if (this._isInAppBrowserInstalled(cordovaMetadata) === false) {
4051
return this._error("InAppBrowser plugin not found")
4152
}
53+
54+
// @ts-ignore
4255
this._popup = cordova.InAppBrowser.open(params.url, this.target, this.features);
4356
if (this._popup) {
4457
Log.debug("CordovaPopupWindow.navigate: popup successfully created");
45-
46-
this._exitCallbackEvent = this._exitCallback.bind(this);
58+
59+
this._exitCallbackEvent = this._exitCallback.bind(this);
4760
this._loadStartCallbackEvent = this._loadStartCallback.bind(this);
48-
61+
4962
this._popup.addEventListener("exit", this._exitCallbackEvent, false);
5063
this._popup.addEventListener("loadstart", this._loadStartCallbackEvent, false);
5164
} else {
@@ -59,22 +72,22 @@ export class CordovaPopupWindow {
5972
return this._promise;
6073
}
6174

62-
_loadStartCallback(event) {
75+
_loadStartCallback(event: any) {
6376
if (event.url.indexOf(this.redirect_uri) === 0) {
6477
this._success({ url: event.url });
65-
}
78+
}
6679
}
67-
_exitCallback(message) {
68-
this._error(message);
80+
_exitCallback(message: string) {
81+
this._error(message);
6982
}
70-
71-
_success(data) {
83+
84+
_success(data: any) {
7285
this._cleanup();
7386

7487
Log.debug("CordovaPopupWindow: Successful response from cordova popup window");
7588
this._resolve(data);
7689
}
77-
_error(message) {
90+
_error(message: string) {
7891
this._cleanup();
7992

8093
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);

src/Global.js

-55
This file was deleted.

0 commit comments

Comments
 (0)