Skip to content

Commit

Permalink
fix: use patch by default
Browse files Browse the repository at this point in the history
  • Loading branch information
soyuka committed Nov 9, 2024
1 parent b89e124 commit e28a691
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions src/hydra/dataProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ function normalizeHydraKey(json: JsonLdObj, key: string): JsonLdObj {
* GET_LIST => GET http://my.api.url/posts
* GET_MANY => GET http://my.api.url/posts/123, GET http://my.api.url/posts/456, GET http://my.api.url/posts/789
* GET_ONE => GET http://my.api.url/posts/123
* UPDATE => PUT http://my.api.url/posts/123
* UPDATE => PATCH http://my.api.url/posts/123
*/
function dataProvider(
factoryParams: HydraDataProviderFactoryParams,
Expand Down Expand Up @@ -253,13 +253,10 @@ function dataProvider(
};

const transformReactAdminDataToRequestBody = (
resource: string,
apiResource: undefined | Resource,
data: Record<string, unknown> | XMLHttpRequestBodyInit,
extraInformation: { hasFileField?: boolean },
): Promise<XMLHttpRequestBodyInit> => {
const apiResource = apiSchema.resources.find(
({ name }) => resource === name,
);
if (undefined === apiResource) {
return Promise.resolve(data as XMLHttpRequestBodyInit);
}
Expand Down Expand Up @@ -366,12 +363,23 @@ function dataProvider(
if (typeof params.meta === 'object') {
extraInformation = params.meta;
}
const updateHttpMethod = extraInformation.hasFileField ? 'POST' : 'PUT';

const apiResource = apiSchema.resources.find(
({ name }) => resource === name,
);

let updateHttpMethod = 'POST';

if (!extraInformation.hasFileField) {
updateHttpMethod =
apiResource?.operations?.find((operation) => operation.type === 'edit')
?.method ?? 'PUT';
}

switch (type) {
case CREATE:
return transformReactAdminDataToRequestBody(
resource,
apiResource,
(params as CreateParams).data,
extraInformation,
).then((body) => ({
Expand Down Expand Up @@ -481,13 +489,17 @@ function dataProvider(

case UPDATE:
return transformReactAdminDataToRequestBody(
resource,
apiResource,
(params as UpdateParams).data,
extraInformation,
).then((body) => ({
options: {
body,
method: updateHttpMethod,
headers:
updateHttpMethod === 'PATCH'
? { 'content-type': 'application/merge-patch+json' }
: {},
},
url,
}));
Expand Down

0 comments on commit e28a691

Please sign in to comment.