Skip to content
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

fix: use patch by default #589

Merged
merged 1 commit into from
Nov 9, 2024
Merged
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
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
Loading