Skip to content

Commit

Permalink
feat(sdk): return the full token from exchangeToken
Browse files Browse the repository at this point in the history
  • Loading branch information
mt-dfrey committed Oct 5, 2023
1 parent 1b75f52 commit 5131f12
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 21 deletions.
13 changes: 11 additions & 2 deletions src/__tests__/index.test.ts
Expand Up @@ -59,9 +59,18 @@ describe('index', () => {
expect(result5).toBeUndefined();
expect(requestLoginLink).toBeCalledWith(storedOptions, { loginLinkTo: 'settings' });

mocked(exchangeToken).mockResolvedValueOnce('test');
const token = {
access_token: 'access_token',
refresh_token: 'refresh_token',
expires_in: 3600,
token_type: 'bearer',
scope: 'guest_read',
created_at: Date.now(),
resource_server: 'jp-api'
};
mocked(exchangeToken).mockResolvedValueOnce(token);
const result6 = await instance.exchangeToken({ code: 'code' });
expect(result6).toBe('test');
expect(result6).toEqual(token);
expect(exchangeToken).toBeCalledWith(storedOptions, { code: 'code' });

// @ts-ignore: set tokenInfo with invalid type value
Expand Down
34 changes: 19 additions & 15 deletions src/api/__tests__/exchange-token.test.ts
Expand Up @@ -11,7 +11,15 @@ describe('api', () => {
const clientId = 'clientId';
const code = 'code';
const redirectUri = 'redirectUri';
const token = 'token';
const token = {
access_token: 'access_token',
refresh_token: 'refresh_token',
expires_in: 3600,
token_type: 'bearer',
scope: 'guest_read',
created_at: Date.now(),
resource_server: 'jp-api'
};
const state = 'state';

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

test('make request', async () => {
fetch.mockClear();

fetch.mockResponseOnce(JSON.stringify({ access_token: token }));
fetch.mockResponseOnce(JSON.stringify(token));

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

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

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

const code1 = 'code1';
const code2 = 'code2';

fetch.mockResponseOnce(JSON.stringify({ access_token: token }));
const code = 'realCode';

jest.spyOn(window, 'location', 'get').mockReturnValueOnce({
search: `?code=${code1}&code=${code2}`
search: `?code=otherCode&code=${code}`
} as typeof window.location);

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

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

expect(data.code).toBe(code2);
expect(data.code).toBe(code);
});

test('auto extract state from url query if no state was passed or set during init', async () => {
fetch.mockClear();

const state1 = 'state1';

fetch.mockResponseOnce(JSON.stringify({ access_token: token }));
fetch.mockResponseOnce(JSON.stringify(token));

jest.spyOn(window, 'location', 'get').mockReturnValueOnce({
search: `?state=${state1}&state=${state}`
search: `?state=otherState&state=${state}`
} as typeof window.location);

await expect(exchangeToken(mtLinkSdk.storedOptions, { code, redirectUri })).resolves.toBe(token);
const actual = await exchangeToken(mtLinkSdk.storedOptions, { code, redirectUri });

expect(actual).toEqual(token);
});

test('non browser environment will not auto extract code from url', async () => {
Expand Down
6 changes: 3 additions & 3 deletions src/api/exchange-token.ts
Expand Up @@ -2,7 +2,7 @@ import qs from 'qs';

import { generateSdkHeaderInfo } from '../helper';
import { MY_ACCOUNT_DOMAINS } from '../server-paths';
import { StoredOptions, ExchangeTokenOptions } from '../typings';
import { StoredOptions, ExchangeTokenOptions, Token } from '../typings';
import storage from '../storage';

function getCode(): string | undefined {
Expand All @@ -21,7 +21,7 @@ function getCode(): string | undefined {
export default async function exchangeToken(
storedOptions: StoredOptions,
options: ExchangeTokenOptions = {}
): Promise<string> {
): Promise<Token> {
const { clientId, redirectUri: defaultRedirectUri, mode } = storedOptions;

if (!clientId) {
Expand Down Expand Up @@ -64,7 +64,7 @@ export default async function exchangeToken(
throw new Error(result.error_description);
}

return result.access_token;
return result as Token;
} catch (error) {
throw new Error(`[mt-link-sdk] \`exchangeToken\` execution failed. ${error}`);
}
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Expand Up @@ -10,6 +10,7 @@ import requestLoginLink from './api/request-login-link';
import exchangeToken from './api/exchange-token';
import tokenInfo from './api/token-info';
import {
Token,
StoredOptions,
ServiceId,
LogoutOptions,
Expand Down Expand Up @@ -141,7 +142,7 @@ export class MtLinkSdk {
return requestLoginLink(this.storedOptions, options);
}

public exchangeToken(options?: ExchangeTokenOptions): Promise<string> {
public exchangeToken(options?: ExchangeTokenOptions): Promise<Token> {
return exchangeToken(this.storedOptions, options);
}

Expand Down
10 changes: 10 additions & 0 deletions src/typings.ts
Expand Up @@ -170,3 +170,13 @@ export interface TokenInfo {
lang: string;
};
}

export interface Token {
access_token: string;
refresh_token: string;
token_type: string;
created_at: number;
expires_in: number;
scope: string;
resource_server: string;
}

0 comments on commit 5131f12

Please sign in to comment.