Skip to content

Commit

Permalink
Fix update fails when nothing is changed (#166)
Browse files Browse the repository at this point in the history
Closes #141
  • Loading branch information
fzaninotto committed Sep 4, 2024
1 parent b282e86 commit 7c896f5
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,9 @@ export default (config: IDataProviderConfig): DataProvider => ({
const query = getQuery(primaryKey, id, resource, meta);
const url = `${config.apiUrl}/${resource}?${qs.stringify(query)}`;
const changedData = getChanges(data, previousData);
if (Object.keys(changedData).length === 0) {
return Promise.resolve({ data: { ...previousData } });
}
const metaSchema = params.meta?.schema;

const body = JSON.stringify({
Expand Down
2 changes: 1 addition & 1 deletion src/urlBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ export const getPrimaryKey = (
resource: string,
primaryKeys: Map<string, PrimaryKey>
) => {
return primaryKeys.get(resource) || ['id'];
return (primaryKeys && primaryKeys.get(resource)) || ['id'];
};

export const decodeId = (
Expand Down
11 changes: 11 additions & 0 deletions tests/dataProvider/update.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import qs from 'qs';
import { makeTestFromCase, Case } from './helper';
import dataProviderBuilder from '../../src';

describe('update specific', () => {
const method = 'update';
Expand Down Expand Up @@ -31,4 +32,14 @@ describe('update specific', () => {
];

cases.forEach(makeTestFromCase);

it('should not fail when no changes are requested', async () => {
const dataProvider = dataProviderBuilder({} as any);
const { data } = await dataProvider.update('posts', {
id: 123,
data: { foo: 'bar' },
previousData: { id: 123, foo: 'bar' },
});
expect(data).toEqual({ id: 123, foo: 'bar' });
});
});

0 comments on commit 7c896f5

Please sign in to comment.