Skip to content

Commit

Permalink
Merge pull request #52 from moneytree/lint-error-fix
Browse files Browse the repository at this point in the history
fix(*): update for lint error and test failed
  • Loading branch information
mt-rory authored Nov 25, 2020
2 parents bc6bfe5 + 362f0b7 commit 02fcb53
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 53 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"@types/node-fetch": "^2.5.4",
"@types/qs": "^6.9.5",
"@types/url-safe-base64": "^1.1.0",
"@types/uuid": "^8.3.0",
"@typescript-eslint/eslint-plugin": "^4.5.0",
"@typescript-eslint/parser": "^4.5.0",
"conventional-changelog-cli": "^2.1.0",
Expand Down
3 changes: 0 additions & 3 deletions src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ describe('index', () => {
const options = instance.storedOptions;
const storedOptions = {
clientId: options.clientId,
codeVerifier: options.codeVerifier,
mode: options.mode,
redirectUri: options.redirectUri,
state: options.state,
Expand Down Expand Up @@ -81,12 +80,10 @@ describe('index', () => {
mtLinkSdk.init('clientId', {
mode: 'local',
state: 'state',
codeVerifier: 'codeVerifier',
});

expect(mtLinkSdk.storedOptions.mode).toBe('local');
expect(mtLinkSdk.storedOptions.state).toBe('state');
expect(mtLinkSdk.storedOptions.codeVerifier).toBe('codeVerifier');
});

test('invalid mode default to production', () => {
Expand Down
14 changes: 0 additions & 14 deletions src/api/__tests__/authorize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ describe('api', () => {
mockedStorage.set.mockClear();
open.mockClear();

const codeVerifier = 'codeVerifier';
const country = 'JP';
const scopes = 'points_read';
const cobrandClientId = 'cobrandClientId';
Expand All @@ -48,7 +47,6 @@ describe('api', () => {
const mtLinkSdk = new MtLinkSdk();
mtLinkSdk.init(clientId, {
redirectUri,
codeVerifier,
country,
scopes,
locale,
Expand All @@ -65,24 +63,18 @@ describe('api', () => {
response_type: 'code',
scope: scopes,
redirect_uri: redirectUri,
code_challenge: 'N1E4yRMD7xixn_oFyO_W3htYN3rY7-HMDKJe6z6r928',
code_challenge_method: 'S256',
country,
locale,
configs: generateConfigs(),
});
const url = `${MY_ACCOUNT_DOMAINS.production}/oauth/authorize?${query}`;
expect(open).toBeCalledWith(url, '_self');

expect(mockedStorage.set).toBeCalledTimes(1);
expect(mockedStorage.set).toBeCalledWith('codeVerifier', codeVerifier);
});

test('with options', () => {
mockedStorage.set.mockClear();
open.mockClear();

const codeVerifier = 'codeVerifier';
const state = 'state';
const country = 'JP';
const scopes = 'points_read';
Expand All @@ -92,7 +84,6 @@ describe('api', () => {

authorize(mtLinkSdk.storedOptions, {
state,
codeVerifier,
redirectUri,
country,
scopes,
Expand All @@ -105,17 +96,12 @@ describe('api', () => {
response_type: 'code',
scope: scopes,
redirect_uri: redirectUri,
code_challenge: 'N1E4yRMD7xixn_oFyO_W3htYN3rY7-HMDKJe6z6r928',
code_challenge_method: 'S256',
state,
country,
configs: generateConfigs(),
});
const url = `${MY_ACCOUNT_DOMAINS.production}/oauth/authorize?${query}`;
expect(open).toBeCalledWith(url, '_self');

expect(mockedStorage.set).toBeCalledTimes(2);
expect(mockedStorage.set).toBeCalledWith('codeVerifier', codeVerifier);
});

test('without window', () => {
Expand Down
14 changes: 0 additions & 14 deletions src/api/__tests__/onboard.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ describe('api', () => {
mockedStorage.set.mockClear();
open.mockClear();

const codeVerifier = 'codeVerifier';
const country = 'JP';
const scopes = 'points_read';
const cobrandClientId = 'cobrandClientId';
Expand All @@ -76,7 +75,6 @@ describe('api', () => {
const mtLinkSdk = new MtLinkSdk();
mtLinkSdk.init(clientId, {
redirectUri,
codeVerifier,
country,
scopes,
email,
Expand All @@ -94,24 +92,18 @@ describe('api', () => {
response_type: 'code',
scope: scopes,
redirect_uri: redirectUri,
code_challenge: 'N1E4yRMD7xixn_oFyO_W3htYN3rY7-HMDKJe6z6r928',
code_challenge_method: 'S256',
country,
locale,
configs: generateConfigs({ email }),
});
const url = `${MY_ACCOUNT_DOMAINS.production}/onboard?${query}`;
expect(open).toBeCalledWith(url, '_self');

expect(mockedStorage.set).toBeCalledTimes(1);
expect(mockedStorage.set).toBeCalledWith('codeVerifier', codeVerifier);
});

test('with options', () => {
mockedStorage.set.mockClear();
open.mockClear();

const codeVerifier = 'codeVerifier';
const state = 'state';
const country = 'JP';
const scopes = 'points_read';
Expand All @@ -121,7 +113,6 @@ describe('api', () => {

onboard(mtLinkSdk.storedOptions, {
state,
codeVerifier,
redirectUri,
country,
scopes,
Expand All @@ -135,17 +126,12 @@ describe('api', () => {
response_type: 'code',
scope: scopes,
redirect_uri: redirectUri,
code_challenge: 'N1E4yRMD7xixn_oFyO_W3htYN3rY7-HMDKJe6z6r928',
code_challenge_method: 'S256',
state,
country,
configs: generateConfigs({ email }),
});
const url = `${MY_ACCOUNT_DOMAINS.production}/onboard?${query}`;
expect(open).toBeCalledWith(url, '_self');

expect(mockedStorage.set).toBeCalledTimes(2);
expect(mockedStorage.set).toBeCalledWith('codeVerifier', codeVerifier);
});

test('without window', () => {
Expand Down
10 changes: 8 additions & 2 deletions src/api/__tests__/token-info.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import fetch from 'jest-fetch-mock';
import qs from 'qs';

import { MY_ACCOUNT_DOMAINS } from '../../server-paths';
import { MtLinkSdk } from '../..';
import { generateConfigs } from '../../helper';
import tokenInfo from '../token-info';

describe('api', () => {
Expand Down Expand Up @@ -39,15 +41,19 @@ describe('api', () => {
fetch.mockResponseOnce(JSON.stringify(response));

await tokenInfo(mtLinkSdk.storedOptions, token);
const query = qs.stringify({
client_id: clientId,
configs: generateConfigs(),
});

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

expect(fetch).toBeCalledTimes(1);
expect(fetch).toBeCalledWith(url, {
method: 'GET',
headers: {
Authorization: `Bearer ${token}`,
'x-api-key': clientId,
'API-Version': '1604911588',
},
});
});
Expand Down
5 changes: 4 additions & 1 deletion src/api/authorize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import { MY_ACCOUNT_DOMAINS } from '../server-paths';
import { StoredOptions, AuthorizeOptions } from '../typings';
import storage from '../storage';

export default function authorize(storedOptions: StoredOptions, options: AuthorizeOptions = {}): void {
export default function authorize(
storedOptions: StoredOptions,
options: AuthorizeOptions = {}
): void {
if (!window) {
throw new Error('[mt-link-sdk] `authorize` only works in the browser.');
}
Expand Down
12 changes: 2 additions & 10 deletions src/api/exchange-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,13 @@ export default async function exchangeToken(
storedOptions: StoredOptions,
options: ExchangeTokenOptions = {}
): Promise<string> {
const {
clientId,
redirectUri: defaultRedirectUri,
mode
} = storedOptions;
const { clientId, redirectUri: defaultRedirectUri, mode } = storedOptions;

if (!clientId) {
throw new Error('[mt-link-sdk] Make sure to call `init` before calling `exchangeToken`.');
}

const {
redirectUri = defaultRedirectUri,
code = getCode(),
codeVerifier,
} = options;
const { redirectUri = defaultRedirectUri, code = getCode(), codeVerifier } = options;

if (!code) {
throw new Error(
Expand Down
21 changes: 12 additions & 9 deletions src/api/token-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,20 @@ export default async function tokenInfo(

const queryString = stringify({
client_id: clientId,
configs: generateConfigs()
})
configs: generateConfigs(),
});

try {
const response = await fetch(`${MY_ACCOUNT_DOMAINS[mode]}/oauth/token/info.json?${queryString}`, {
method: 'GET',
headers: {
Authorization: `Bearer ${token}`,
'API-Version': '1604911588',
},
});
const response = await fetch(
`${MY_ACCOUNT_DOMAINS[mode]}/oauth/token/info.json?${queryString}`,
{
method: 'GET',
headers: {
Authorization: `Bearer ${token}`,
'API-Version': '1604911588',
},
}
);

const result = await response.json();

Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,11 @@
resolved "https://registry.yarnpkg.com/@types/url-safe-base64/-/url-safe-base64-1.1.0.tgz#90d35b9432164896a9d5b3d43bb88fb2ef86f2e2"
integrity sha512-cVZIEorjpbV8NgdodMXyLItd9q0wT4k075a2F5Ew5L/zwB80rLIGhb7Owo17le0hhnZcJXizxc7hHdTs1or51A==

"@types/uuid@^8.3.0":
version "8.3.0"
resolved "https://registry.yarnpkg.com/@types/uuid/-/uuid-8.3.0.tgz#215c231dff736d5ba92410e6d602050cce7e273f"
integrity sha512-eQ9qFW/fhfGJF8WKHGEHZEyVWfZxrT+6CLIJGBcZPfxUh/+BnEj+UCGYMlr9qZuX/2AltsvwrGqp0LhEW8D0zQ==

"@types/yargs-parser@*":
version "15.0.0"
resolved "https://registry.yarnpkg.com/@types/yargs-parser/-/yargs-parser-15.0.0.tgz#cb3f9f741869e20cce330ffbeb9271590483882d"
Expand Down

0 comments on commit 02fcb53

Please sign in to comment.