Skip to content

Commit 239704f

Browse files
committed
fix(encoding): fix enconding issue and some typecript typing tweaks
1 parent db2bed4 commit 239704f

1 file changed

Lines changed: 52 additions & 29 deletions

File tree

src/index.ts

Lines changed: 52 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,61 @@ import { DOMAIN, MY_ACCOUNT, VAULT } from './endpoints';
33

44
interface Config {
55
clientId: string;
6-
isTestEnvironment?: boolean;
76
scope: string[];
7+
isTestEnvironment?: boolean;
88
redirectUri?: string;
99
continueTo?: string;
10-
responseType?: string;
10+
responseType?: 'code' | 'token';
1111
locale?: string;
1212
state?: string;
1313
}
1414

15-
interface Params {
15+
export interface Params {
1616
client_id: string;
1717
redirect_uri: string;
18-
continue: string;
1918
response_type: string;
2019
scope: string;
21-
back?: string;
20+
continue?: string;
2221
locale?: string;
2322
state?: string;
2423
}
2524

25+
export type Domains = {
26+
vault: string;
27+
myaccount: string;
28+
};
29+
2630
interface VaultOptions {
2731
backTo?: string;
2832
newTab?: boolean;
2933
}
3034

31-
interface MyaccountOptions {
35+
export interface MyAccountOptions {
3236
backTo?: string;
3337
newTab?: boolean;
3438
email?: string;
3539
authPage?: string;
40+
showAuthToggle?: boolean;
41+
}
42+
43+
function removeOAuth2Params(params: Params) {
44+
const validParams = { ...params };
45+
delete validParams.scope;
46+
delete validParams.redirect_uri;
47+
delete validParams.response_type;
48+
delete validParams.state;
49+
50+
return validParams;
3651
}
3752

38-
const encodeConfig = (configs: object): string => {
39-
// My Account separate key/values using semicolon
40-
return encodeURIComponent(qs.stringify(configs, { delimiter: ';' }));
53+
function encodeConfigWithParams(params: Params, configs: { [k: string]: string | boolean | undefined }) {
54+
const endcodedConfigs = qs.stringify(configs, { delimiter: ';', encode: false });
55+
return qs.stringify({ ...params, configs: endcodedConfigs });
4156
}
4257

4358
class LinkSDK {
44-
private domains: { [name: string]: string }
45-
private params: Params
59+
private domains: Domains;
60+
private params: Params;
4661

4762
init(config: Config): void {
4863
if (!config.clientId) {
@@ -52,17 +67,16 @@ class LinkSDK {
5267
const {
5368
clientId,
5469
redirectUri = `${location.protocol}//${location.host}/callback`,
55-
continueTo = '',
5670
responseType = 'token',
5771
scope = [],
5872
locale,
5973
state
6074
} = config;
6175

6276
this.params = {
77+
continue: config.continueTo,
6378
client_id: clientId,
6479
redirect_uri: redirectUri,
65-
continue: continueTo,
6680
response_type: responseType,
6781
scope: scope.join(' '),
6882
locale,
@@ -77,37 +91,46 @@ class LinkSDK {
7791
}
7892

7993
// Open My Account to authorize application to use MtLink API
80-
authorize(options: MyaccountOptions = {}): void {
81-
const { newTab = false, backTo, email, authPage } = options;
82-
const { PATHS: { OAUTH }} = MY_ACCOUNT;
83-
const configs = {
94+
authorize(options: MyAccountOptions = {}): void {
95+
const { newTab = false, email, authPage, backTo, showAuthToggle } = options;
96+
97+
const params = encodeConfigWithParams(this.params, {
8498
sdk_platform: 'js',
85-
email,
99+
email: email ? encodeURIComponent(email) : undefined,
86100
auth_action: authPage,
87101
back_to: backTo,
88-
show_auth_toggle: true
89-
};
90-
const params = qs.stringify({ ...this.params, configs: encodeConfig(configs) }, { encode: false });
91-
window.open(`https://${this.domains.myaccount}/${OAUTH}?${params}`, newTab ? '_blank' : '_self');
102+
show_auth_toggle: showAuthToggle
103+
});
104+
105+
window.open(`https://${this.domains.myaccount}/${MY_ACCOUNT.PATHS.OAUTH}?${params}`, newTab ? '_blank' : '_self');
92106
}
93107

94108
// Open the Vault page
95109
openVault(options: VaultOptions = {}): void {
96110
const { newTab = false, backTo = location.href } = options;
97-
const params = qs.stringify({ ...this.params, back_to: backTo });
111+
const validParams = removeOAuth2Params(this.params);
112+
const params = encodeConfigWithParams(validParams, {
113+
sdk_platform: 'js',
114+
back_to: backTo
115+
});
116+
98117
window.open(`https://${this.domains.vault}?${params}`, newTab ? '_blank' : '_self');
99118
}
100119

101120
// Open the Guest settings page
102-
openSettings(options: MyaccountOptions = {}): void {
121+
openSettings(options: MyAccountOptions = {}): void {
103122
const { newTab = false, backTo = location.href } = options;
104-
const { PATHS: { SETTINGS }} = MY_ACCOUNT;
105-
const configs = {
123+
124+
const validParams = removeOAuth2Params(this.params);
125+
const params = encodeConfigWithParams(validParams, {
106126
sdk_platform: 'js',
107127
back_to: backTo
108-
};
109-
const params = qs.stringify({ ...this.params, configs: encodeConfig(configs) }, { encode: false });
110-
window.open(`https://${this.domains.myaccount}?${params}/${SETTINGS}`, newTab ? '_blank' : '_self');
128+
});
129+
130+
window.open(
131+
`https://${this.domains.myaccount}/${MY_ACCOUNT.PATHS.SETTINGS}?${params}`,
132+
newTab ? '_blank' : '_self'
133+
);
111134
}
112135
}
113136

0 commit comments

Comments
 (0)