From 369f85e0c6113601cd69340ae4cc7b7fb6309279 Mon Sep 17 00:00:00 2001 From: Jeff Koch Date: Wed, 19 Jul 2017 11:33:46 -0700 Subject: [PATCH] BREAKING CHANGE: Remove ExternalApi's dependence on Customer (EN-1254) (#17) --- src/examples/get_external_apis.test.js | 4 ++-- src/mocks.js | 8 ++++---- src/resources/ExternalApi.js | 5 ++--- src/resources/ExternalApi.test.js | 10 +++++----- src/resources/ExternalApisContext.js | 6 ++---- src/resources/ExternalApisContext.test.js | 4 ++-- src/resources/index.js | 19 +++++++++++++++++++ src/resources/index.test.js | 9 +++++++++ 8 files changed, 45 insertions(+), 20 deletions(-) diff --git a/src/examples/get_external_apis.test.js b/src/examples/get_external_apis.test.js index 263aa54..ab65509 100644 --- a/src/examples/get_external_apis.test.js +++ b/src/examples/get_external_apis.test.js @@ -18,7 +18,7 @@ describe('When searching for external APIs by name', () => { it('should get a list of external APIs', () => { api.logIn({ username: 'charlie@example.com', password: 'securepassword' }); - const externalApisPromise = api.customer('SYNC').externalApis() + const externalApisPromise = api.externalApis() .withQuery('arr') // External APIs containing "arr" in their name .getPage() .then(page => page.list) @@ -39,7 +39,7 @@ describe('When retrieving an external API by ID', () => { it('should get an external API', () => { api.logIn({ username: 'charlie@example.com', password: 'securepassword' }); - const externalApiPromise = api.customer('SYNC').externalApi(1) + const externalApiPromise = api.externalApi(1) .fetch() .then(externalApi => externalApi); // Do things with external API diff --git a/src/mocks.js b/src/mocks.js index c4e0262..501cf99 100644 --- a/src/mocks.js +++ b/src/mocks.js @@ -78,14 +78,14 @@ export const externalApis = { Client.toBlob(externalApis.list), { headers: { - Link: '; rel="next", ; rel="last"', + Link: '; rel="next", ; rel="last"', }, }); const singleResponse = () => new Response(Client.toBlob(externalApis.getById(1))); fetchMock - .get(client.resolve('/1/SYNC/external_apis?page=1&perPage=10&q=arr&sort='), listResponse) - .get(client.resolve('/1/SYNC/external_apis/1'), singleResponse); + .get(client.resolve('/1/external_apis?page=1&perPage=10&q=arr&sort='), listResponse) + .get(client.resolve('/1/external_apis/1'), singleResponse); }, getById: id => externalApis.list.find(e => e.id === id), list: [ @@ -93,7 +93,7 @@ export const externalApis = { id: 1, name: 'arrivals', description: 'upcoming arrivals', - href: '/1/SYNC/external_apis/1', + href: '/1/external_apis/1', }, ], }; diff --git a/src/resources/ExternalApi.js b/src/resources/ExternalApi.js index 6db9ad4..344a72e 100644 --- a/src/resources/ExternalApi.js +++ b/src/resources/ExternalApi.js @@ -23,13 +23,12 @@ class ExternalApi extends Resource { /** * Makes a href for a given customer code and ID - * @param {string} customerCode Customer code * @param {Number} id External API ID * @returns {string} URI to instance of external API */ - static makeHref(customerCode, id) { + static makeHref(id) { return { - href: `/1/${customerCode}/external_apis/${id}`, + href: `/1/external_apis/${id}`, }; } diff --git a/src/resources/ExternalApi.test.js b/src/resources/ExternalApi.test.js index b92ddcd..bf593d9 100644 --- a/src/resources/ExternalApi.test.js +++ b/src/resources/ExternalApi.test.js @@ -10,9 +10,9 @@ chai.use(chaiAsPromised); describe('When instantiating an external API based on customer and ID', () => { const client = new Client(); - const externalApi = new ExternalApi(client, ExternalApi.makeHref('SYNC', 1)); + const externalApi = new ExternalApi(client, ExternalApi.makeHref(1)); - it('should set the href', () => externalApi.href.should.equal('/1/SYNC/external_apis/1')); + it('should set the href', () => externalApi.href.should.equal('/1/external_apis/1')); it('should not be hydrated', () => externalApi.hydrated.should.equal(false)); }); @@ -21,7 +21,7 @@ describe('When instantiating an external API based on an object', () => { const externalApi = new ExternalApi(client, mockExternalApis.getById(1)); it('should set the ID', () => externalApi.id.should.equal(1)); - it('should set the href', () => externalApi.href.should.equal('/1/SYNC/external_apis/1')); + it('should set the href', () => externalApi.href.should.equal('/1/external_apis/1')); it('should be hydrated', () => externalApi.hydrated.should.equal(true)); }); @@ -34,11 +34,11 @@ describe('When fetching an external API best on customer and ID', () => { let promise; beforeEach(() => { - promise = new ExternalApi(client, ExternalApi.makeHref('SYNC', 1)).fetch(); + promise = new ExternalApi(client, ExternalApi.makeHref(1)).fetch(); }); it('should resolve the promise', () => promise.should.be.fulfilled); it('should set the ID', () => promise.then(e => e.id).should.eventually.equal(1)); - it('should set the href', () => promise.then(e => e.href).should.eventually.equal('/1/SYNC/external_apis/1')); + it('should set the href', () => promise.then(e => e.href).should.eventually.equal('/1/external_apis/1')); it('should be hydrated', () => promise.then(p => p.hydrated).should.eventually.equal(true)); }); diff --git a/src/resources/ExternalApisContext.js b/src/resources/ExternalApisContext.js index e7909dd..d25383d 100644 --- a/src/resources/ExternalApisContext.js +++ b/src/resources/ExternalApisContext.js @@ -11,12 +11,10 @@ class ExternalApisContext extends PagedContext { /** * Creates a new route context * @param {Client} client Instance of pre-configured client - * @param {string} customerCode Customer code * @param {Object} params Object of querystring parameters to append to the URL */ - constructor(client, customerCode, params) { + constructor(client, params) { super(client, { ...params }); - this.code = customerCode; } /** @@ -41,7 +39,7 @@ class ExternalApisContext extends PagedContext { * @see ExternalApi */ getPage() { - return this.page(ExternalApi, `/1/${this.code}/external_apis`); + return this.page(ExternalApi, '/1/external_apis'); } } diff --git a/src/resources/ExternalApisContext.test.js b/src/resources/ExternalApisContext.test.js index 1934d16..1797f64 100644 --- a/src/resources/ExternalApisContext.test.js +++ b/src/resources/ExternalApisContext.test.js @@ -13,13 +13,13 @@ describe('When building a query for external APIs', () => { client.setAuthenticated(); beforeEach(() => fetchMock - .get(client.resolve('/1/SYNC/external_apis?page=9&perPage=27&q=valid&sort=first_valid asc,second_valid desc'), mockExternalApis.list) + .get(client.resolve('/1/external_apis?page=9&perPage=27&q=valid&sort=first_valid asc,second_valid desc'), mockExternalApis.list) .catch(503)); afterEach(fetchMock.restore); let promise; beforeEach(() => { - const externalApis = new ExternalApisContext(client, 'SYNC'); + const externalApis = new ExternalApisContext(client); promise = externalApis .withPage(9) .withPerPage(27) diff --git a/src/resources/index.js b/src/resources/index.js index 15172db..efe6603 100644 --- a/src/resources/index.js +++ b/src/resources/index.js @@ -2,6 +2,8 @@ import 'isomorphic-fetch'; import JWT from 'jwt-client'; import Resource from './Resource'; import Customer from './Customer'; +import ExternalApi from './ExternalApi'; +import ExternalApisContext from './ExternalApisContext'; import User from './User'; import Client from '../Client'; import { ForbiddenResponse } from '../responses'; @@ -193,6 +195,23 @@ class Track extends Resource { return this.resource(Customer, code); } + /** + * Gets a context for querying this customer's external APIs + * @returns {ExternalApisContext} Context for querying this customer's external APIs + */ + externalApis() { + return this.resource(ExternalApisContext); + } + + /** + * Gets an external API resource by id + * @param {Number} id Identity of the external API + * @returns {ExternalApi} ExternalApi resource + */ + externalApi(id) { + return this.resource(ExternalApi, ExternalApi.makeHref(id)); + } + /** * Gets a user resource. Defaults to user for current authenticated session. * @param {number|string} [id=me] User ID diff --git a/src/resources/index.test.js b/src/resources/index.test.js index 6a01e2d..8b37464 100644 --- a/src/resources/index.test.js +++ b/src/resources/index.test.js @@ -6,6 +6,8 @@ import { ForbiddenResponse } from '../responses'; import { resolveAt } from '../testutils'; import Client from '../Client'; import { charlie, users as mockUsers } from '../mocks'; +import ExternalApi from './ExternalApi'; +import ExternalApisContext from './ExternalApisContext'; chai.should(); chai.use(chaiAsPromised); @@ -237,3 +239,10 @@ describe('When getting a user', () => { ]).should.be.fulfilled; }); }); + +describe('When querying for external APIs', () => { + const api = new Track(); + + it('should allow external apis to be searched', () => api.externalApis().should.be.instanceOf(ExternalApisContext)); + it('should allo an external api to be retrieved', () => api.externalApi().should.be.instanceOf(ExternalApi)); +});