Skip to content

Commit 1b9f67c

Browse files
committed
refactor(params): separate params into two groups
1 parent eb3ef1e commit 1b9f67c

File tree

2 files changed

+35
-39
lines changed

2 files changed

+35
-39
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ Config properties:
5353
response_type, // string; // Tells the authorization server which grant to execute.
5454
scope, // string[]; // A list of permissions that the application requires.
5555
redirectUri, // string; // URI to return the user to after authorization is complete.
56-
continueTo, // string; // [optional] Parameter appended as `continue` to the `redirectUri`.
5756
locale, // string; // [optional] To force the display to a specific language (e.g.: en-AU)
5857
state, // string; // [optional] An opaque value, used for security purposes. If this request parameter is set in the request, then it is returned to the application as part of the redirect_uri.
5958
appToken, // string; // [optional] The Access Token granted through oauth

src/index.ts

Lines changed: 35 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as qs from 'qs';
22

33
import { DOMAIN, MY_ACCOUNT, VAULT } from './endpoints';
44

5-
interface Config {
5+
interface IConfig {
66
clientId: string;
77
scope: string[];
88
isTestEnvironment?: boolean;
@@ -13,54 +13,48 @@ interface Config {
1313
state?: string;
1414
}
1515

16-
interface Params {
16+
interface IParams {
17+
client_id: string;
18+
locale?: string;
19+
}
20+
21+
interface IOauthParams {
1722
client_id: string;
1823
redirect_uri: string;
1924
response_type: string;
2025
scope: string;
21-
continue?: string;
22-
locale?: string;
2326
state?: string;
2427
}
2528

26-
interface Domains {
29+
interface IDomains {
2730
vault: string;
2831
myaccount: string;
2932
}
3033

31-
interface VaultOptions {
34+
interface IVaultOptions {
3235
backTo?: string;
3336
newTab?: boolean;
3437
}
3538

36-
interface MyAccountOptions {
39+
interface IMyAccountOptions {
3740
backTo?: string;
3841
newTab?: boolean;
3942
email?: string;
4043
authPage?: string;
4144
showAuthToggle?: boolean;
4245
}
4346

44-
function removeOAuth2Params(params: Params) {
45-
const validParams = { ...params };
46-
delete validParams.scope;
47-
delete validParams.redirect_uri;
48-
delete validParams.response_type;
49-
delete validParams.state;
50-
51-
return validParams;
52-
}
53-
54-
function encodeConfigWithParams(params: Params, configs: { [k: string]: string | boolean | undefined }) {
47+
function encodeConfigWithParams(params: any, configs: { [k: string]: string | boolean | undefined }) {
5548
const endcodedConfigs = qs.stringify(configs, { delimiter: ';', encode: false });
5649
return qs.stringify({ ...params, configs: endcodedConfigs });
5750
}
5851

5952
class LinkSDK {
60-
private domains: Domains;
61-
private params: Params;
53+
private domains: IDomains;
54+
private params: IParams;
55+
private oauthParams: IOauthParams;
6256

63-
init(config: Config): void {
57+
init(config: IConfig): void {
6458
if (!config.clientId) {
6559
throw new Error('Need a clientId to initialise');
6660
}
@@ -75,12 +69,15 @@ class LinkSDK {
7569
} = config;
7670

7771
this.params = {
78-
continue: config.continueTo,
72+
client_id: clientId,
73+
locale
74+
};
75+
76+
this.oauthParams = {
7977
client_id: clientId,
8078
redirect_uri: redirectUri,
8179
response_type: responseType,
8280
scope: scope.join(' '),
83-
locale,
8481
state
8582
};
8683

@@ -92,26 +89,27 @@ class LinkSDK {
9289
}
9390

9491
// Open My Account to authorize application to use MtLink API
95-
authorize(options: MyAccountOptions = {}): void {
92+
authorize(options: IMyAccountOptions = {}): void {
9693
const { newTab = false, email, authPage, backTo, showAuthToggle } = options;
9794

98-
const params = encodeConfigWithParams(this.params, {
99-
email,
100-
sdk_platform: 'js',
101-
sdk_version: VERSION,
102-
auth_action: authPage,
103-
back_to: backTo,
104-
show_auth_toggle: showAuthToggle
105-
});
95+
const params = encodeConfigWithParams(
96+
{ ...this.oauthParams, ...this.params }, {
97+
email,
98+
sdk_platform: 'js',
99+
sdk_version: VERSION,
100+
auth_action: authPage,
101+
back_to: backTo,
102+
show_auth_toggle: showAuthToggle
103+
}
104+
);
106105

107106
window.open(`https://${this.domains.myaccount}/${MY_ACCOUNT.PATHS.OAUTH}?${params}`, newTab ? '_blank' : '_self');
108107
}
109108

110109
// Open the Vault page
111-
openVault(options: VaultOptions = {}): void {
110+
openVault(options: IVaultOptions = {}): void {
112111
const { newTab = false, backTo = location.href } = options;
113-
const validParams = removeOAuth2Params(this.params);
114-
const params = encodeConfigWithParams(validParams, {
112+
const params = encodeConfigWithParams(this.params, {
115113
sdk_platform: 'js',
116114
sdk_version: VERSION,
117115
back_to: backTo
@@ -121,11 +119,10 @@ class LinkSDK {
121119
}
122120

123121
// Open the Guest settings page
124-
openSettings(options: MyAccountOptions = {}): void {
122+
openSettings(options: IMyAccountOptions = {}): void {
125123
const { newTab = false, backTo = location.href } = options;
126124

127-
const validParams = removeOAuth2Params(this.params);
128-
const params = encodeConfigWithParams(validParams, {
125+
const params = encodeConfigWithParams(this.params, {
129126
sdk_platform: 'js',
130127
sdk_version: VERSION,
131128
back_to: backTo

0 commit comments

Comments
 (0)