Skip to content

Commit 5131f12

Browse files
committed
feat(sdk): return the full token from exchangeToken
1 parent 1b75f52 commit 5131f12

File tree

5 files changed

+45
-21
lines changed

5 files changed

+45
-21
lines changed

src/__tests__/index.test.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,18 @@ describe('index', () => {
5959
expect(result5).toBeUndefined();
6060
expect(requestLoginLink).toBeCalledWith(storedOptions, { loginLinkTo: 'settings' });
6161

62-
mocked(exchangeToken).mockResolvedValueOnce('test');
62+
const token = {
63+
access_token: 'access_token',
64+
refresh_token: 'refresh_token',
65+
expires_in: 3600,
66+
token_type: 'bearer',
67+
scope: 'guest_read',
68+
created_at: Date.now(),
69+
resource_server: 'jp-api'
70+
};
71+
mocked(exchangeToken).mockResolvedValueOnce(token);
6372
const result6 = await instance.exchangeToken({ code: 'code' });
64-
expect(result6).toBe('test');
73+
expect(result6).toEqual(token);
6574
expect(exchangeToken).toBeCalledWith(storedOptions, { code: 'code' });
6675

6776
// @ts-ignore: set tokenInfo with invalid type value

src/api/__tests__/exchange-token.test.ts

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,15 @@ describe('api', () => {
1111
const clientId = 'clientId';
1212
const code = 'code';
1313
const redirectUri = 'redirectUri';
14-
const token = 'token';
14+
const token = {
15+
access_token: 'access_token',
16+
refresh_token: 'refresh_token',
17+
expires_in: 3600,
18+
token_type: 'bearer',
19+
scope: 'guest_read',
20+
created_at: Date.now(),
21+
resource_server: 'jp-api'
22+
};
1523
const state = 'state';
1624

1725
const mtLinkSdk = new MtLinkSdk();
@@ -48,8 +56,7 @@ describe('api', () => {
4856

4957
test('make request', async () => {
5058
fetch.mockClear();
51-
52-
fetch.mockResponseOnce(JSON.stringify({ access_token: token }));
59+
fetch.mockResponseOnce(JSON.stringify(token));
5360

5461
await exchangeToken(mtLinkSdk.storedOptions, { code, codeVerifier: '' });
5562

@@ -94,36 +101,33 @@ describe('api', () => {
94101

95102
test('auto extract code from url query if no code was passed', async () => {
96103
fetch.mockClear();
104+
fetch.mockResponseOnce(JSON.stringify(token));
97105

98-
const code1 = 'code1';
99-
const code2 = 'code2';
100-
101-
fetch.mockResponseOnce(JSON.stringify({ access_token: token }));
106+
const code = 'realCode';
102107

103108
jest.spyOn(window, 'location', 'get').mockReturnValueOnce({
104-
search: `?code=${code1}&code=${code2}`
109+
search: `?code=otherCode&code=${code}`
105110
} as typeof window.location);
106111

107112
await exchangeToken(mtLinkSdk.storedOptions, { state });
108113

109114
const result = fetch.mock.calls[0][1] || {};
110115
const data = JSON.parse(result.body as string);
111116

112-
expect(data.code).toBe(code2);
117+
expect(data.code).toBe(code);
113118
});
114119

115120
test('auto extract state from url query if no state was passed or set during init', async () => {
116121
fetch.mockClear();
117-
118-
const state1 = 'state1';
119-
120-
fetch.mockResponseOnce(JSON.stringify({ access_token: token }));
122+
fetch.mockResponseOnce(JSON.stringify(token));
121123

122124
jest.spyOn(window, 'location', 'get').mockReturnValueOnce({
123-
search: `?state=${state1}&state=${state}`
125+
search: `?state=otherState&state=${state}`
124126
} as typeof window.location);
125127

126-
await expect(exchangeToken(mtLinkSdk.storedOptions, { code, redirectUri })).resolves.toBe(token);
128+
const actual = await exchangeToken(mtLinkSdk.storedOptions, { code, redirectUri });
129+
130+
expect(actual).toEqual(token);
127131
});
128132

129133
test('non browser environment will not auto extract code from url', async () => {

src/api/exchange-token.ts

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

33
import { generateSdkHeaderInfo } from '../helper';
44
import { MY_ACCOUNT_DOMAINS } from '../server-paths';
5-
import { StoredOptions, ExchangeTokenOptions } from '../typings';
5+
import { StoredOptions, ExchangeTokenOptions, Token } from '../typings';
66
import storage from '../storage';
77

88
function getCode(): string | undefined {
@@ -21,7 +21,7 @@ function getCode(): string | undefined {
2121
export default async function exchangeToken(
2222
storedOptions: StoredOptions,
2323
options: ExchangeTokenOptions = {}
24-
): Promise<string> {
24+
): Promise<Token> {
2525
const { clientId, redirectUri: defaultRedirectUri, mode } = storedOptions;
2626

2727
if (!clientId) {
@@ -64,7 +64,7 @@ export default async function exchangeToken(
6464
throw new Error(result.error_description);
6565
}
6666

67-
return result.access_token;
67+
return result as Token;
6868
} catch (error) {
6969
throw new Error(`[mt-link-sdk] \`exchangeToken\` execution failed. ${error}`);
7070
}

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import requestLoginLink from './api/request-login-link';
1010
import exchangeToken from './api/exchange-token';
1111
import tokenInfo from './api/token-info';
1212
import {
13+
Token,
1314
StoredOptions,
1415
ServiceId,
1516
LogoutOptions,
@@ -141,7 +142,7 @@ export class MtLinkSdk {
141142
return requestLoginLink(this.storedOptions, options);
142143
}
143144

144-
public exchangeToken(options?: ExchangeTokenOptions): Promise<string> {
145+
public exchangeToken(options?: ExchangeTokenOptions): Promise<Token> {
145146
return exchangeToken(this.storedOptions, options);
146147
}
147148

src/typings.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,13 @@ export interface TokenInfo {
170170
lang: string;
171171
};
172172
}
173+
174+
export interface Token {
175+
access_token: string;
176+
refresh_token: string;
177+
token_type: string;
178+
created_at: number;
179+
expires_in: number;
180+
scope: string;
181+
resource_server: string;
182+
}

0 commit comments

Comments
 (0)