orbitjs / orbit

Composable data framework for ambitious web applications.
https://orbitjs.com
MIT License
2.33k stars 134 forks source link

Should we always need to response a document for `addRecord`? #943

Open towerhe opened 2 years ago

towerhe commented 2 years ago

As described in the JSONAPI spec:

7.1.2.3 204 No Content

If a POST request did include a Client-Generated ID and the requested resource has been created successfully, the server MUST return either a 201 Created status code and response document (as described above) or a 204 No Content status code with no response document.

I have APIs response 204. When I try to create my resources with addRecord, I got the following errors:

Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'data')
    at JSONAPIDocumentSerializer.deserialize

I checked through the codes of orbit.js, and I found the TransformRequestProcessors.addRecord() always needs a document in the response.

    const url =
      request.options?.url ??
      requestProcessor.urlBuilder.resourceURL(record.type);

    const details = await requestProcessor.fetch(url, settings);
    const document = details.document as ResourceDocument;
    requestProcessor.preprocessResponseDocument(document, request);

Is there a way to fulfill my requirements?

dgeb commented 2 years ago

@towerhe you're correct that a 204 is a valid response. I'll mark this as a bug and address it soon.