Skip to content

Commit

Permalink
fix(api-core): remove/delete should not assume data payloads
Browse files Browse the repository at this point in the history
BREAKING CHANGE: previously remove/delete would assume that data was being passed in the body of the request if the first param of the message signature was NOT a string or number. Now, the method assumes a config object is passed in instead of data. This allows the developers to pass in params or data as they see fit.
  • Loading branch information
Rob McGuinness committed Jan 4, 2018
1 parent 5cd35bf commit e45a9f6
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 45 deletions.
14 changes: 6 additions & 8 deletions packages/api-core/src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,30 +234,28 @@ export default class AvApi {
return this.request(config, this.afterUpdate || this.afterPut);
}

put(id, data, config) {
return this.update(id, data, config);
put(...args) {
return this.update(args);
}

// delete request
remove(id, config) {
let data;
if (typeof id !== 'string' && typeof id !== 'number') {
data = id;
config = id;
id = '';
}
config = this.config(config);
config.method = 'DELETE';
config.url = this.getUrl(config, id);
config.data = data;

const beforeFunc = this.beforeRemove || this.beforeDelete;
if (beforeFunc) {
config.data = beforeFunc(config.data);
config = beforeFunc(config);
}
return this.request(config, this.afterRemove || this.afterDelete);
}

delete(id, config) {
return this.remove(id, config);
delete(...args) {
return this.remove(args);
}
}
4 changes: 2 additions & 2 deletions packages/api-core/src/resources/notifications.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import AvApi from '../api';

export default class AvNotification extends AvApi {
export default class AvNotifications extends AvApi {
constructor(http, promise, config = {}) {
const options = Object.assign(
{
Expand All @@ -14,6 +14,6 @@ export default class AvNotification extends AvApi {

deleteByTopic(topic) {
const params = Object.assign({}, { topicId: topic });
return this.remove(Object.assign({}, { params }));
return this.remove({ params });
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import AvNotification from '../notification';
import AvNotification from '../notifications';

const mockHttp = jest.fn(() => Promise.resolve({}));

Expand All @@ -15,7 +15,7 @@ describe('AvNotification', () => {
expect(api).toBeDefined();
});

test('deleteByTopic() should call query with topic added to params.topicId', () => {
test('deleteByTopic() should call remove with topic added to params.topicId', () => {
api = new AvNotification(mockHttp, Promise, {});
api.remove = jest.fn();

Expand Down
54 changes: 21 additions & 33 deletions packages/api-core/src/tests/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -719,46 +719,34 @@ describe('AvApi', () => {
expect(api.request).toHaveBeenLastCalledWith(expectedConfig, undefined);
});

test('remove() should set first argument as data if not string or number', () => {
test('remove() should set first argument as config if not string or number', () => {
const config = {
testValue: 'test',
};
const data = {
testData: 'data',
};
const expectedConfig = Object.assign(
{
method: 'DELETE',
url: testUrl,
method: 'DELETE',
url: testUrl,
data: {
a: '1',
b: '2',
},
config,
{ data }
);
api.remove(data, config);
expect(api.getUrl).toHaveBeenLastCalledWith(expectedConfig, '');
expect(api.request).toHaveBeenLastCalledWith(expectedConfig, undefined);
};
api.remove(config);
expect(api.getUrl).toHaveBeenLastCalledWith(config, '');
expect(api.request).toHaveBeenLastCalledWith(config, undefined);
});

test('remove() should run data through beforeRemove if defined', () => {
test('remove() should call beforeRemove() if defined', () => {
const config = {
testValue: 'test',
};
const data = {
testData: 'data',
};
const expectedConfig = Object.assign(
{
method: 'DELETE',
url: testUrl,
method: 'DELETE',
url: testUrl,
data: {
a: '1',
b: '2',
},
config,
{ data }
);
};
api.beforeRemove = jest.fn(thisData => thisData);
api.remove(data, config);
expect(api.getUrl).toHaveBeenLastCalledWith(expectedConfig, '');
expect(api.request).toHaveBeenLastCalledWith(expectedConfig, undefined);
expect(api.beforeRemove).toHaveBeenLastCalledWith(data);
api.remove(config);
expect(api.getUrl).toHaveBeenLastCalledWith(config, '');
expect(api.request).toHaveBeenLastCalledWith(config, undefined);
expect(api.beforeRemove).toHaveBeenLastCalledWith(config);
});
});
});

0 comments on commit e45a9f6

Please sign in to comment.