Skip to content

Commit

Permalink
refactor!: remove options from tokenInfo
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `tokenInfo` API no longer accept options parameter as it provide no benefit.
  • Loading branch information
mt-max committed Nov 10, 2020
1 parent c7d5dc7 commit c918b9d
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 71 deletions.
2 changes: 0 additions & 2 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,6 @@ tokenInfo.isMtClient // for internal use
| Parameter | Type | Required | Default Value | Description |
| - | - | - | - | - |
| token | string | true | | Token you wish to get info for. |
| options | object | false | Value set during `init`. | Optional parameters. |
| options.redirectUri | string | false | Value set during `init`. | Make sure the value of `redirectUri` here is the same state value used during `authorize` or `onboard` call.<br /><br /><strong>NOTE:</strong> The SDK will throw an error if both this parameter and the default value from the [init options](?id=api-init_options) are undefined. |

### logout

Expand Down
1 change: 0 additions & 1 deletion src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ describe('index', () => {
mocked(tokenInfo).mockResolvedValueOnce('test');
const result7 = await instance.tokenInfo('test');
expect(result7).toBe('test');
expect(tokenInfo).toBeCalledWith(storedOptions, 'test', undefined);
});

test('mtLinkSdk', () => {
Expand Down
37 changes: 2 additions & 35 deletions src/api/__tests__/token-info.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import fetch from 'jest-fetch-mock';
import qs from 'qs';

import { MY_ACCOUNT_DOMAINS } from '../../server-paths';
import { MtLinkSdk } from '../..';
Expand Down Expand Up @@ -35,56 +34,24 @@ describe('api', () => {
);
});

test('redirectUri is required', async () => {
const instance = new MtLinkSdk();

await expect(tokenInfo(instance.storedOptions, token)).rejects.toThrow(
'[mt-link-sdk] Missing option `redirectUri` in `tokenInfo`, make sure to pass one via `tokenInfo` options or `init` options.'
);
});

test('make request', async () => {
fetch.mockClear();
fetch.mockResponseOnce(JSON.stringify(response));

await tokenInfo(mtLinkSdk.storedOptions, token);

const query = qs.stringify({
client_id: clientId,
redirect_uri: redirectUri,
response_type: 'token',
});

const url = `${MY_ACCOUNT_DOMAINS.production}/oauth/token/info.json?${query}`;
const url = `${MY_ACCOUNT_DOMAINS.production}/oauth/token/info.json`;

expect(fetch).toBeCalledTimes(1);
expect(fetch).toBeCalledWith(url, {
method: 'GET',
headers: {
Authorization: `Bearer ${token}`,
'x-api-key': clientId,
},
});
});

test('use option', async () => {
fetch.mockClear();
fetch.mockResponseOnce(JSON.stringify(response));

const newRedirectUri = 'newRedirectUri';

await tokenInfo(mtLinkSdk.storedOptions, token, { redirectUri: newRedirectUri });

const query = qs.stringify({
client_id: clientId,
redirect_uri: newRedirectUri,
response_type: 'token',
});

const url = `${MY_ACCOUNT_DOMAINS.production}/oauth/token/info.json?${query}`;

expect(fetch.mock.calls[0][0]).toBe(url);
});

test('failed to request', async () => {
const error = 'failed';

Expand Down
38 changes: 10 additions & 28 deletions src/api/token-info.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,24 @@
import qs from 'qs';

import { MY_ACCOUNT_DOMAINS } from '../server-paths';
import { StoredOptions, TokenInfoOptions, TokenInfo } from '../typings';
import { StoredOptions, TokenInfo } from '../typings';

export default async function tokenInfo(
storedOptions: StoredOptions,
token: string,
options: TokenInfoOptions = {}
token: string
): Promise<TokenInfo> {
const { clientId, redirectUri: defaultRedirectUri, mode } = storedOptions;
const { redirectUri = defaultRedirectUri } = options;
const { mode, clientId } = storedOptions;

if (!token) {
throw new Error('[mt-link-sdk] Missing parameter `token` in `tokenInfo`.');
}

if (!redirectUri) {
throw new Error(
'[mt-link-sdk] Missing option `redirectUri` in `tokenInfo`, make sure to pass one via `tokenInfo` options or `init` options.'
);
}

const queryParams = qs.stringify({
client_id: clientId, // TODO: test if we need this
redirect_uri: redirectUri,
response_type: 'token',
});

try {
const response = await fetch(
`${MY_ACCOUNT_DOMAINS[mode]}/oauth/token/info.json?${queryParams}`,
{
method: 'GET',
headers: {
Authorization: `Bearer ${token}`,
},
}
);
const response = await fetch(`${MY_ACCOUNT_DOMAINS[mode]}/oauth/token/info.json`, {
method: 'GET',
headers: {
Authorization: `Bearer ${token}`,
'x-api-key': clientId as string,
},
});

const result = await response.json();

Expand Down
5 changes: 2 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
InitOptions,
AuthorizeOptions,
ExchangeTokenOptions,
TokenInfoOptions,
RequestMagicLinkOptions,
TokenInfo,
Mode,
Expand Down Expand Up @@ -76,8 +75,8 @@ export class MtLinkSdk {
return exchangeToken(this.storedOptions, options);
}

public tokenInfo(token: string, options?: TokenInfoOptions): Promise<TokenInfo> {
return tokenInfo(this.storedOptions, token, options);
public tokenInfo(token: string): Promise<TokenInfo> {
return tokenInfo(this.storedOptions, token);
}
}

Expand Down
2 changes: 0 additions & 2 deletions src/typings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,6 @@ export interface RequestMagicLinkOptions extends ConfigsOptions {
magicLinkTo?: MagicLinkTo;
}

export type TokenInfoOptions = Omit<Omit<OAuthSharedParams, 'state'>, 'codeVerifier'>;

export interface TokenInfo {
guestUid: string;
resourceServer: string;
Expand Down

0 comments on commit c918b9d

Please sign in to comment.