diff --git a/src/api/Api.js b/src/api/index.js similarity index 100% rename from src/api/Api.js rename to src/api/index.js diff --git a/src/auth/Auth.js b/src/auth/Auth.js deleted file mode 100644 index 1da5c40..0000000 --- a/src/auth/Auth.js +++ /dev/null @@ -1,26 +0,0 @@ -import ClientCredentialsFlow from './ClientCredentialsFlow'; -import TokenFetcher from './TokenFetcher'; - -/** - * Implements various KKBOX OAuth 2.0 authorization flows. - */ -export default class Auth { - /** - * Initialize the Auth object with client id and client secret. - * - * @param {string} clientID - * @param {string} clientSecret - * @example new Auth(clientID, clientSecret); - */ - constructor(clientID, clientSecret) { - /** - * @type {TokenFetcher} - */ - this.tokenFetcher = new TokenFetcher(clientID, clientSecret); - - /** - * @type {ClientCredentialsFlow} - */ - this.clientCredentialsFlow = new ClientCredentialsFlow(this.tokenFetcher); - } -} diff --git a/src/auth/ClientCredentialsFlow.js b/src/auth/ClientCredentialsFlow.js deleted file mode 100644 index ffb301a..0000000 --- a/src/auth/ClientCredentialsFlow.js +++ /dev/null @@ -1,28 +0,0 @@ -/** - * Implements the client credentials flow. Used for accessing APIs that don't need any KKBOX - * user's personal data. - * @see https://docs-en.kkbox.codes/docs/getting-started - */ -export default class ClientCredentialsFlow { - /** - * @ignore - */ - constructor(token) { - /** - * @ignore - */ - this.token = token; - } - - /** - * Fetch access token. - * - * @return {Promise} - * @example auth.clientCredentialsFlow.fetchAccessToken(); - */ - fetchAccessToken() { - return this.token.fetchAccessToken({ - grant_type: 'client_credentials' - }); - } -} diff --git a/src/auth/TokenFetcher.js b/src/auth/TokenFetcher.js deleted file mode 100644 index a16a0d5..0000000 --- a/src/auth/TokenFetcher.js +++ /dev/null @@ -1,42 +0,0 @@ -import axios from 'axios'; -import { authError } from '../catchError'; -import querystring from 'querystring'; -import { Token as ENDPOINT_TOKEN } from '../Endpoint'; - -/** - * Fetches access token. - */ -export default class TokenFetcher { - /** - * @ignore - */ - constructor(clientID, clientSecret) { - /** - * @ignore - */ - this.clientID = clientID; - - /** - * @ignore - */ - this.clientSecret = clientSecret; - } - - /** - * Fetches access token. - * - * @param {object} params - Form data. - * @return {Promise} - */ - fetchAccessToken(params) { - return axios - .post(ENDPOINT_TOKEN, querystring.stringify(params), { - auth: { - username: this.clientID, - password: this.clientSecret - }, - headers: {} - }) - .catch(authError); - } -} diff --git a/src/auth/index.js b/src/auth/index.js new file mode 100644 index 0000000..fde197f --- /dev/null +++ b/src/auth/index.js @@ -0,0 +1,42 @@ +import axios from 'axios'; +import { authError } from '../catchError'; +import { Token as ENDPOINT_TOKEN } from '../Endpoint'; + +/** + * Implements various KKBOX OAuth 2.0 authorization flows. + */ +export default class Auth { + /** + * Initialize the Auth object with client id and client secret. + * + * @param {string} clientID + * @param {string} clientSecret + * @example new Auth(clientID, clientSecret); + */ + constructor(clientID, clientSecret) { + this.clientID = clientID; + this.clientSecret = clientSecret; + } + + /** + * Fetch access token. + * + * @return {Promise} + * @example auth.fetchAccessToken(); + */ + fetchAccessToken() { + return axios({ + method: 'POST', + url: ENDPOINT_TOKEN, + headers: { + 'content-type': 'application/x-www-form-urlencoded' + }, + data: 'grant_type=client_credentials', + auth: { + username: this.clientID, + password: this.clientSecret + } + }) + .catch(authError); + } +} diff --git a/src/index.js b/src/index.js index 7127840..02cd786 100644 --- a/src/index.js +++ b/src/index.js @@ -1,2 +1,2 @@ -export { default as Auth } from './auth/Auth'; -export { default as Api } from './api/Api'; +export { default as Auth } from './auth'; +export { default as Api } from './api'; diff --git a/src/test/apitest.js b/src/test/apitest.js index 9666f78..1e79bd5 100644 --- a/src/test/apitest.js +++ b/src/test/apitest.js @@ -12,19 +12,17 @@ import MoodStationFetcher from '../api/MoodStationFetcher'; import GenreStationFetcher from '../api/GenreStationFetcher'; import ChartFetcher from '../api/ChartFetcher'; import TrackFetcher from '../api/TrackFetcher'; -import TokenFetcher from '../auth/TokenFetcher'; -import ClientCredentials from '../auth/ClientCredentialsFlow'; +import Auth from '../auth/'; import { kkbox_sdk } from '../../client_secrets.json'; const CLIENT_ID = kkbox_sdk.client_id; const CLIENT_SECRET = kkbox_sdk.client_secret; describe('Api Begin to Test', () => { - describe('ClientCredentials', () => { + describe('Auth', () => { describe('#fetch the access token()', () => { it('should fetch access token', () => { - const tokenFetcher = new TokenFetcher(CLIENT_ID, CLIENT_SECRET); - const clientCredentials = new ClientCredentials(tokenFetcher); - return clientCredentials.fetchAccessToken().then( + const auth = new Auth(CLIENT_ID, CLIENT_SECRET); + return auth.fetchAccessToken().then( response => { const access_token = response.data.access_token; access_token.should.be.ok; diff --git a/src/test/authtest.js b/src/test/authtest.js index 50d12af..6ac69b7 100644 --- a/src/test/authtest.js +++ b/src/test/authtest.js @@ -1,18 +1,16 @@ import should from 'should'; -import TokenFetcher from '../auth/TokenFetcher'; -import ClientCredentialsFlow from '../auth/ClientCredentialsFlow'; +import Auth from '../auth/'; import { kkbox_sdk } from '../../client_secrets.json'; const CLIENT_ID = kkbox_sdk.client_id; const CLIENT_SECRET = kkbox_sdk.client_secret; describe('Auth Begin to Test', () => { - const tokenFetcher = new TokenFetcher(CLIENT_ID, CLIENT_SECRET); + const auth = new Auth(CLIENT_ID, CLIENT_SECRET); describe('clientCredentialsFlow', () => { describe('#fetchAccessToken()', () => { it('should get access token', () => { - const clientCredentialsFlow = new ClientCredentialsFlow(tokenFetcher); - return clientCredentialsFlow.fetchAccessToken().then(response => { + return auth.fetchAccessToken().then(response => { const access_token = response.data.access_token; access_token.should.be.ok; }); diff --git a/src/test/sdktest.js b/src/test/sdktest.js index 0167c7b..d725a8f 100644 --- a/src/test/sdktest.js +++ b/src/test/sdktest.js @@ -9,7 +9,7 @@ describe('SDK Begin to Test', () => { describe('Auth', () => { it('should get access token', () => { const auth = new Auth(CLIENT_ID, CLIENT_SECRET); - return auth.clientCredentialsFlow.fetchAccessToken().then(response => { + return auth.fetchAccessToken().then(response => { describe('Api With Fake Token', () => { const api = new Api('FAKE_TOKEN', 'HK'); describe('Property checking', () => {