devour-client icon indicating copy to clipboard operation
devour-client copied to clipboard

Resource type is pluralized

Open tomers opened this issue 8 years ago • 2 comments

I created the following model:

jsonApiInstance.define('screen', {
  name: ''
}

When I send patch request, I get the following error:

:5000/screens/3:1 PATCH http://localhost:5000/screens/3 409 (CONFLICT)
minilog.js?2831:17 devour  Error: Request failed with status code 409
    at createError (createError.js?16d0:16)
    at settle (settle.js?db52:18)
    at XMLHttpRequest.handleLoad (xhr.js?ec6c:77)
actions.js?e1db:23 
{type: {…}}
type
:
{title: "Incorrect type", detail: "Invalid type. Expected "screen"."}
__proto__
:
Object

This can be resolved by explicitly specifying type in define's options argument:

jsonApiInstance.define('screen', {
  name: ''
},
{
  type: 'screen'
})

The cause for this issue is in devour/src/middleware/json-api/_serialize.js file:

function resource (modelName, item) {
  // ...
  let typeName = options.type || this.pluralize(modelName)
  // ...
}

I think it is more correct to use this.pluralize.singular for this case.

tomers avatar May 02 '18 12:05 tomers

I am seeing the same issue. Is pluralizing the resource part of the jsonapi spec?

shrop avatar Jul 07 '18 03:07 shrop

I was able to take care of this by setting up the devour object with the pluralize option set to false

const jsonApi = new JsonApi({apiUrl:'https://site/jsonapi', pluralize: false});

shrop avatar Jul 07 '18 04:07 shrop