Skip to content

Commit

Permalink
BREAKING CHANGE: Remove ExternalApi's dependence on Customer (EN-1254) (
Browse files Browse the repository at this point in the history
  • Loading branch information
JRKoch authored and thzinc committed Jul 19, 2017
1 parent 72ae36e commit 369f85e
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/examples/get_external_apis.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('When searching for external APIs by name', () => {
it('should get a list of external APIs', () => {
api.logIn({ username: '[email protected]', 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)
Expand All @@ -39,7 +39,7 @@ describe('When retrieving an external API by ID', () => {
it('should get an external API', () => {
api.logIn({ username: '[email protected]', password: 'securepassword' });

const externalApiPromise = api.customer('SYNC').externalApi(1)
const externalApiPromise = api.externalApi(1)
.fetch()
.then(externalApi => externalApi); // Do things with external API

Expand Down
8 changes: 4 additions & 4 deletions src/mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,22 +78,22 @@ export const externalApis = {
Client.toBlob(externalApis.list),
{
headers: {
Link: '</1/SYNC/external_apis?page=1&perPage=10&q=arr&sort=>; rel="next", </1/SYNC/external_apis?page=1&perPage=10&q=arr&sort=>; rel="last"',
Link: '</1/external_apis?page=1&perPage=10&q=arr&sort=>; rel="next", </1/external_apis?page=1&perPage=10&q=arr&sort=>; 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: [
{
id: 1,
name: 'arrivals',
description: 'upcoming arrivals',
href: '/1/SYNC/external_apis/1',
href: '/1/external_apis/1',
},
],
};
Expand Down
5 changes: 2 additions & 3 deletions src/resources/ExternalApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
};
}

Expand Down
10 changes: 5 additions & 5 deletions src/resources/ExternalApi.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
});

Expand All @@ -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));
});

Expand All @@ -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));
});
6 changes: 2 additions & 4 deletions src/resources/ExternalApisContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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');
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/resources/ExternalApisContext.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 19 additions & 0 deletions src/resources/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions src/resources/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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));
});

0 comments on commit 369f85e

Please sign in to comment.