Skip to content

Commit e48b16e

Browse files
committed
Fix openService/openServiceUrl option parameter typing
Previous release introduced a bug where mandatory required attributes for a view options became optional. This commit fixes the typing issue.
1 parent b66715d commit e48b16e

File tree

7 files changed

+207
-52
lines changed

7 files changed

+207
-52
lines changed

sample/src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import mtLinkSdk, {
66
OpenServicesConfigsOptions,
77
ServiceId,
88
LoginLinkTo,
9-
ServicesListType
9+
VaultViewServiceList
1010
} from '@moneytree/mt-link-javascript-sdk';
1111

1212
import elements from './elements';
@@ -134,12 +134,12 @@ elements.openServiceBtn.onclick = () => {
134134
view: 'services-list',
135135
type:
136136
(openServiceOptionsElms.type.options[openServiceOptionsElms.type.selectedIndex].value as Pick<
137-
ServicesListType,
137+
VaultViewServiceList,
138138
'type'
139139
>['type']) || undefined,
140140
group:
141141
(openServiceOptionsElms.group.options[openServiceOptionsElms.group.selectedIndex].value as Pick<
142-
ServicesListType,
142+
VaultViewServiceList,
143143
'group'
144144
>['group']) || undefined,
145145
search: openServiceOptionsElms.search.value || undefined

src/api/__tests__/open-service-url.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import qs from 'qs';
22

33
import { MY_ACCOUNT_DOMAINS, VAULT_DOMAINS, LINK_KIT_DOMAINS } from '../../server-paths';
4-
import { MtLinkSdk, ServiceId } from '../..';
4+
import { MtLinkSdk } from '../..';
55
import openServiceUrl from '../open-service-url';
66
import { generateConfigs } from '../../helper';
77

@@ -148,8 +148,8 @@ describe('api', () => {
148148
test('invalid service id', () => {
149149
expect(() => {
150150
// force cast invalid value so that we can use it for testing
151-
openServiceUrl(new MtLinkSdk().storedOptions, 'invalid' as ServiceId);
152-
}).toThrow('[mt-link-sdk] Invalid `serviceId` in `openServiceUrl/openService`, got: invalid');
151+
openServiceUrl(new MtLinkSdk().storedOptions, 'invalid' as 'vault');
152+
}).toThrow('[mt-link-sdk] Invalid `serviceId` in `openServiceUrl`, got: invalid');
153153
});
154154

155155
test('saml_subject_id is passed when initialized', () => {

src/api/__tests__/open-service.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import qs from 'qs';
22

33
import { MY_ACCOUNT_DOMAINS, VAULT_DOMAINS, LINK_KIT_DOMAINS } from '../../server-paths';
4-
import { MtLinkSdk, ServiceId } from '../..';
4+
import { MtLinkSdk } from '../..';
55
import openService from '../open-service';
66
import { generateConfigs } from '../../helper';
77

@@ -194,8 +194,8 @@ describe('api', () => {
194194
test('invalid service id', () => {
195195
expect(() => {
196196
// force cast invalid value so that we can use it for testing
197-
openService(new MtLinkSdk().storedOptions, 'invalid' as ServiceId);
198-
}).toThrow('[mt-link-sdk] Invalid `serviceId` in `openServiceUrl/openService`, got: invalid');
197+
openService(new MtLinkSdk().storedOptions, 'invalid' as 'myaccount');
198+
}).toThrow('[mt-link-sdk] Invalid `serviceId` in `openService`, got: invalid');
199199
});
200200

201201
test('saml_subject_id is passed when initialized', () => {

src/api/open-service-url.ts

Lines changed: 59 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,19 @@ import { generateConfigs, mergeConfigs } from '../helper';
44
import { MY_ACCOUNT_DOMAINS, VAULT_DOMAINS, LINK_KIT_DOMAINS } from '../server-paths';
55
import {
66
StoredOptions,
7-
ServiceId,
8-
ConnectionSettingType,
9-
ServiceConnectionType,
10-
ServicesListType,
7+
VaultViewConnectionSetting,
8+
VaultViewServiceConnection,
9+
VaultViewServiceList,
1110
OpenServiceUrlOptions,
12-
VaultOpenServiceOptions,
13-
MyAccountOpenServiceOptions
11+
VaultServiceTypes,
12+
MyAccountServiceTypes,
13+
LinkKitOpenServiceUrlOptions,
14+
MyAccountOpenServiceUrlOptions,
15+
ConfigsOptionsWithoutIsNewTab,
16+
VaultOpenServiceUrlViewServiceList,
17+
VaultOpenServiceUrlViewServiceConnection,
18+
VaultOpenServiceUrlViewConnectionSetting,
19+
VaultOpenServiceUrlViewCustomerSupport
1420
} from '../typings';
1521

1622
interface QueryData {
@@ -23,19 +29,53 @@ interface QueryData {
2329

2430
export default function openServiceUrl(
2531
storedOptions: StoredOptions,
26-
serviceId: ServiceId,
32+
serviceId: 'link-kit',
33+
options?: LinkKitOpenServiceUrlOptions
34+
): string;
35+
export default function openServiceUrl(
36+
storedOptions: StoredOptions,
37+
serviceId: 'myaccount',
38+
options?: MyAccountOpenServiceUrlOptions
39+
): string;
40+
export default function openServiceUrl(
41+
storedOptions: StoredOptions,
42+
serviceId: 'vault',
43+
options?: ConfigsOptionsWithoutIsNewTab
44+
): string;
45+
export default function openServiceUrl(
46+
storedOptions: StoredOptions,
47+
serviceId: 'vault',
48+
options?: VaultOpenServiceUrlViewServiceList
49+
): string;
50+
export default function openServiceUrl(
51+
storedOptions: StoredOptions,
52+
serviceId: 'vault',
53+
options?: VaultOpenServiceUrlViewServiceConnection
54+
): string;
55+
export default function openServiceUrl(
56+
storedOptions: StoredOptions,
57+
serviceId: 'vault',
58+
options?: VaultOpenServiceUrlViewConnectionSetting
59+
): string;
60+
export default function openServiceUrl(
61+
storedOptions: StoredOptions,
62+
serviceId: 'vault',
63+
options?: VaultOpenServiceUrlViewCustomerSupport
64+
): string;
65+
export default function openServiceUrl(
66+
storedOptions: StoredOptions,
67+
serviceId: 'myaccount' | 'vault' | 'link-kit',
2768
options: OpenServiceUrlOptions = {}
2869
): string {
2970
const { clientId, mode, cobrandClientId, locale, samlSubjectId } = storedOptions;
30-
const { view = '', ...rest } = options as VaultOpenServiceOptions | MyAccountOpenServiceOptions;
3171

3272
const getQueryValue = (needStringify = true): string | QueryData => {
3373
const query: QueryData = {
3474
client_id: clientId,
3575
cobrand_client_id: cobrandClientId,
3676
locale,
3777
saml_subject_id: samlSubjectId,
38-
configs: generateConfigs(mergeConfigs(storedOptions, rest))
78+
configs: generateConfigs(mergeConfigs(storedOptions, options))
3979
};
4080

4181
if (!needStringify) {
@@ -47,14 +87,16 @@ export default function openServiceUrl(
4787

4888
switch (serviceId) {
4989
case 'vault':
50-
if (!view) {
90+
let { view: vaultView } = options as VaultServiceTypes;
91+
92+
if (!vaultView) {
5193
return `${VAULT_DOMAINS[mode]}?${getQueryValue()}`;
5294
}
5395

54-
switch (view) {
96+
switch (vaultView) {
5597
case 'services-list':
5698
// eslint-disable-next-line no-case-declarations
57-
const { group, type, search } = options as ServicesListType;
99+
const { group, type, search } = options as VaultViewServiceList;
58100

59101
return `${VAULT_DOMAINS[mode]}/services?${stringify({
60102
...(getQueryValue(false) as QueryData),
@@ -65,13 +107,13 @@ export default function openServiceUrl(
65107

66108
case 'service-connection':
67109
// eslint-disable-next-line no-case-declarations
68-
const { entityKey } = options as ServiceConnectionType;
110+
const { entityKey } = options as VaultViewServiceConnection;
69111

70112
return `${VAULT_DOMAINS[mode]}/service/${entityKey}?${getQueryValue()}`;
71113

72114
case 'connection-setting':
73115
// eslint-disable-next-line no-case-declarations
74-
const { credentialId } = options as ConnectionSettingType;
116+
const { credentialId } = options as VaultViewConnectionSetting;
75117

76118
return `${VAULT_DOMAINS[mode]}/connection/${credentialId}?${getQueryValue()}`;
77119

@@ -81,12 +123,13 @@ export default function openServiceUrl(
81123
}
82124

83125
case 'myaccount':
84-
return `${MY_ACCOUNT_DOMAINS[mode]}/${view}?${getQueryValue()}`;
126+
let { view: myAccountView = '' } = options as MyAccountServiceTypes;
127+
return `${MY_ACCOUNT_DOMAINS[mode]}/${myAccountView}?${getQueryValue()}`;
85128

86129
case 'link-kit':
87130
return `${LINK_KIT_DOMAINS[mode]}?${getQueryValue()}`;
88131

89132
default:
90-
throw new Error(`[mt-link-sdk] Invalid \`serviceId\` in \`openServiceUrl/openService\`, got: ${serviceId}`);
133+
throw new Error(`[mt-link-sdk] Invalid \`serviceId\` in \`openServiceUrl\`, got: ${serviceId}`);
91134
}
92135
}

src/api/open-service.ts

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,49 @@
11
import { getIsTabValue, openWindow } from '../helper';
2-
import { StoredOptions, ServiceId, OpenServiceOptions } from '../typings';
2+
import {
3+
StoredOptions,
4+
ServiceId,
5+
OpenServiceOptions,
6+
LinkKitOpenServiceOptions,
7+
MyAccountOpenServiceOptions,
8+
VaultOpenServiceViewServiceList,
9+
VaultOpenServiceViewServiceConnection,
10+
VaultOpenServiceViewConnectionSetting,
11+
VaultOpenServiceViewCustomerSupport,
12+
ConfigsOptions
13+
} from '../typings';
314
import openServiceUrl from './open-service-url';
415

16+
export default function openService(
17+
storedOptions: StoredOptions,
18+
serviceId: 'link-kit',
19+
options?: LinkKitOpenServiceOptions
20+
): void;
21+
export default function openService(
22+
storedOptions: StoredOptions,
23+
serviceId: 'myaccount',
24+
options?: MyAccountOpenServiceOptions
25+
): void;
26+
export default function openService(storedOptions: StoredOptions, serviceId: 'vault', options?: ConfigsOptions): void;
27+
export default function openService(
28+
storedOptions: StoredOptions,
29+
serviceId: 'vault',
30+
options?: VaultOpenServiceViewServiceList
31+
): void;
32+
export default function openService(
33+
storedOptions: StoredOptions,
34+
serviceId: 'vault',
35+
options?: VaultOpenServiceViewServiceConnection
36+
): void;
37+
export default function openService(
38+
storedOptions: StoredOptions,
39+
serviceId: 'vault',
40+
options?: VaultOpenServiceViewConnectionSetting
41+
): void;
42+
export default function openService(
43+
storedOptions: StoredOptions,
44+
serviceId: 'vault',
45+
options?: VaultOpenServiceViewCustomerSupport
46+
): void;
547
export default function openService(
648
storedOptions: StoredOptions,
749
serviceId: ServiceId,
@@ -13,5 +55,17 @@ export default function openService(
1355

1456
const { isNewTab, ...restOptions } = options;
1557

16-
openWindow(openServiceUrl(storedOptions, serviceId, restOptions), getIsTabValue(isNewTab));
58+
switch (serviceId) {
59+
case 'myaccount':
60+
openWindow(openServiceUrl(storedOptions, 'myaccount', restOptions), getIsTabValue(isNewTab));
61+
break;
62+
case 'vault':
63+
openWindow(openServiceUrl(storedOptions, 'vault', restOptions), getIsTabValue(isNewTab));
64+
break;
65+
case 'link-kit':
66+
openWindow(openServiceUrl(storedOptions, 'link-kit', restOptions), getIsTabValue(isNewTab));
67+
break;
68+
default:
69+
throw new Error(`[mt-link-sdk] Invalid \`serviceId\` in \`openService\`, got: ${serviceId}`);
70+
}
1771
}

src/index.ts

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import onboardUrl from './api/onboard-url';
55
import logout from './api/logout';
66
import logoutUrl from './api/logout-url';
77
import openService from './api/open-service';
8-
import openServiceUrlApi from './api/open-service-url';
8+
import openServiceUrl from './api/open-service-url';
99
import requestLoginLink from './api/request-login-link';
1010
import exchangeToken from './api/exchange-token';
1111
import tokenInfo from './api/token-info';
@@ -26,11 +26,19 @@ import {
2626
OpenServiceUrlOptions,
2727
LinkKitOpenServiceUrlOptions,
2828
MyAccountOpenServiceUrlOptions,
29-
VaultOpenServiceUrlOptions,
30-
VaultOpenServiceOptions,
3129
LinkKitOpenServiceOptions,
3230
MyAccountOpenServiceOptions,
33-
OpenServiceOptions
31+
OpenServiceOptions,
32+
ConfigsOptions,
33+
ConfigsOptionsWithoutIsNewTab,
34+
VaultOpenServiceUrlViewServiceList,
35+
VaultOpenServiceUrlViewServiceConnection,
36+
VaultOpenServiceUrlViewConnectionSetting,
37+
VaultOpenServiceUrlViewCustomerSupport,
38+
VaultOpenServiceViewServiceList,
39+
VaultOpenServiceViewServiceConnection,
40+
VaultOpenServiceViewConnectionSetting,
41+
VaultOpenServiceViewCustomerSupport
3442
} from './typings';
3543

3644
export * from './typings';
@@ -88,16 +96,45 @@ export class MtLinkSdk {
8896

8997
public openService(serviceId: 'link-kit', options?: LinkKitOpenServiceOptions): void;
9098
public openService(serviceId: 'myaccount', options?: MyAccountOpenServiceOptions): void;
91-
public openService(serviceId: 'vault', options?: VaultOpenServiceOptions): void;
99+
public openService(serviceId: 'vault', options?: ConfigsOptions): void;
100+
public openService(serviceId: 'vault', options?: VaultOpenServiceViewServiceList): void;
101+
public openService(serviceId: 'vault', options?: VaultOpenServiceViewServiceConnection): void;
102+
public openService(serviceId: 'vault', options?: VaultOpenServiceViewConnectionSetting): void;
103+
public openService(serviceId: 'vault', options?: VaultOpenServiceViewCustomerSupport): void;
92104
public openService(serviceId: ServiceId, options?: OpenServiceOptions): void {
93-
openService(this.storedOptions, serviceId, options);
105+
switch (serviceId) {
106+
case 'myaccount':
107+
openService(this.storedOptions, 'myaccount', options);
108+
break;
109+
case 'vault':
110+
openService(this.storedOptions, 'vault', options);
111+
break;
112+
case 'link-kit':
113+
openService(this.storedOptions, 'link-kit', options);
114+
break;
115+
default:
116+
throw new Error(`[mt-link-sdk] Invalid \`serviceId\` in \`openService\`, got: ${serviceId}`);
117+
}
94118
}
95119

96120
public openServiceUrl(serviceId: 'link-kit', options?: LinkKitOpenServiceUrlOptions): string;
97121
public openServiceUrl(serviceId: 'myaccount', options?: MyAccountOpenServiceUrlOptions): string;
98-
public openServiceUrl(serviceId: 'vault', options?: VaultOpenServiceUrlOptions): string;
122+
public openServiceUrl(serviceId: 'vault', options?: ConfigsOptionsWithoutIsNewTab): string;
123+
public openServiceUrl(serviceId: 'vault', options?: VaultOpenServiceUrlViewServiceList): string;
124+
public openServiceUrl(serviceId: 'vault', options?: VaultOpenServiceUrlViewServiceConnection): string;
125+
public openServiceUrl(serviceId: 'vault', options?: VaultOpenServiceUrlViewConnectionSetting): string;
126+
public openServiceUrl(serviceId: 'vault', options?: VaultOpenServiceUrlViewCustomerSupport): string;
99127
public openServiceUrl(serviceId: ServiceId, options?: OpenServiceUrlOptions): string {
100-
return openServiceUrlApi(this.storedOptions, serviceId, options);
128+
switch (serviceId) {
129+
case 'myaccount':
130+
return openServiceUrl(this.storedOptions, 'myaccount', options);
131+
case 'vault':
132+
return openServiceUrl(this.storedOptions, 'vault', options);
133+
case 'link-kit':
134+
return openServiceUrl(this.storedOptions, 'link-kit', options);
135+
default:
136+
throw new Error(`[mt-link-sdk] Invalid \`serviceId\` in \`openServiceUrl\`, got: ${serviceId}`);
137+
}
101138
}
102139

103140
public requestLoginLink(options?: RequestLoginLinkOptions): Promise<void> {

0 commit comments

Comments
 (0)