Skip to content

avoid using @id parameter. Fixes #11 #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ import { AbstractClient } from 'rest-client-sdk';

class SomeEntityClient extends AbstractClient {
getPathBase() {
return '/v2/some_entities';
return '/v2/some_entities'; // this is the URI used for querying
}

getEntityURI(entity) {
return `${this.getPathBase}/${entity.id}`; // this will be the URI used by update / delete script
}

getName() {
Expand Down Expand Up @@ -65,6 +69,8 @@ const clients = {
const sdk = new RestClientSdk(tokenStorage, config, clients);
```

### Make calls
#### Find
You can now call the clients this way:
```js
sdk.someEntity.find(8); // will find the entity with id 8. ie. /v2/some_entities/8
Expand All @@ -74,6 +80,13 @@ sdk.someEntity.findAll(); // will find all entities. ie. /v2/some_entities
sdk.someEntity.findBy({ foo: 'bar' }); // will find all entities for the request: /v2/some_entities?foo=bar
```

#### Update / delete
```js
sdk.someEntity.update(entity);

sdk.someEntity.delete(entity);
```

### Custom entity factory
You can inject a custom entity factory to the SDK. All entities will be send to the entityFactory.

Expand Down
6 changes: 6 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# v0.13.x
## breaking changes

- Do not depend on `@id` anymore: The `AbstractClient` need to implements a `getEntityURI(entity)` and return an query string from it. This is not a great pattern, but it will do for now.


# v0.12.x
## breaking changes

Expand Down
9 changes: 7 additions & 2 deletions src/client/AbstractClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ class AbstractClient {
You must implement "getPathBase" method.`);
}

getEntityURI(entity) {
throw new Error(`AbstractClient::getEntityURI can not be called directly.
You must implement "getEntityURI" method.`);
}

getName() {
throw new Error(`AbstractClient::getName can not be called directly.
You must implement "getName" method.`);
Expand Down Expand Up @@ -58,7 +63,7 @@ class AbstractClient {
}

update(entity, queryParam = {}) {
const url = new URI(entity.get('@id'));
const url = new URI(this.getEntityURI(entity));
url.addSearch(queryParam);

return this.createEntityFromJsonResponse(
Expand All @@ -71,7 +76,7 @@ class AbstractClient {
}

delete(entity) {
const url = entity.get('@id');
const url = this.getEntityURI(entity);
return this.authorizedFetch(url, {
method: 'DELETE',
});
Expand Down
61 changes: 61 additions & 0 deletions test/client/AbstractClient_specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,25 @@ class SomeTestClient extends AbstractClient {
getName() {
return 'SomeClient';
}

getEntityURI(entity) {
return entity.get('@id');
}
}

class NoAtIdClient extends AbstractClient {
getPathBase(pathParameters) {
return '/v2/no-at-id';
}

getName() {
return 'NoAtIdClient';
}

getEntityURI(entity) {
const uri = `${this.getPathBase()}/${entity.get('id')}`;
return uri;
}
}

class DefaultParametersTestClient extends AbstractClient {
Expand All @@ -35,6 +54,10 @@ class DefaultParametersTestClient extends AbstractClient {
getName() {
return 'DefaultParamTest';
}

getEntityURI(entity) {
return entity.get('@id');
}
}

const SomeSdk = new RestClientSdk(
Expand All @@ -43,6 +66,7 @@ const SomeSdk = new RestClientSdk(
{
test: SomeTestClient,
defParam: DefaultParametersTestClient,
noAtId: NoAtIdClient,
}
);
SomeSdk.tokenStorage.generateToken();
Expand Down Expand Up @@ -252,6 +276,8 @@ describe('Test Client', () => {
});

describe('Test errors', () => {
afterEach(fetchMock.restore);

it('handle 401 and 403 errors', () => {
fetchMock
.mock(/400$/, 400)
Expand All @@ -274,3 +300,38 @@ describe('Test errors', () => {
]);
});
});

describe('Update and delete function trigger the good urls', () => {
afterEach(fetchMock.restore);

it('handle updating and deleting entities with @ids', () => {
fetchMock
.mock(() => true, {
'@id': '/v2/test/8',
foo: 'bar',
})
.getMock()
;

const data = Map({
'@id': '/v2/test/8',
foo: 'foo',
});

const dataNoArobase = Map({
id: 9,
foo: 'foo',
});

return Promise.all([
SomeSdk.test.update(data),
SomeSdk.noAtId.update(dataNoArobase),
])
.then(() => {
const url1 = fetchMock.calls().matched[0][0];
expect(url1).to.equals('https://api.me/v2/test/8');
const url2 = fetchMock.calls().matched[1][0];
expect(url2).to.equals('https://api.me/v2/no-at-id/9');
});
});
});