orbitjs/orbit

[JSON:API] Some custom fetch settings are overridden by defaults

SafaAlfulaij opened this issue · 2 comments

const settings = {
...requestProcessor.buildFetchSettings(request),
method: 'PATCH',
json: requestDoc
};

I have several cases that I need to call custom endpoints to act on records. These endpoints doesn't comply fully with JSON:API, but I'd like Orbit to call them since they return proper formatted JSON:API responses that I would like Orbit to process (add to cache, etc).

Currently:

{
  "data": {
    "id": "1",
    "type": "planet",
    "attributes": {
      "name": "Custom Planet",
      "oxygen": 0.3
    }
  }
}

Example:

remote.update(
  (t) => t.updateRecord({"type": "planet", "id": "1"}),
  {
    sources: {
        remote: {
          settings: {
              method: "POST",
          },
          url: `/api/planets/1/purify-air/`,
        },
    },
  }
);

(Just an example, purify-air can change a lot of other things based on different planet conditions, so can't simply say replaceAttribute(..., "oxygen", 1))
Return:

{
  "data": {
    "id": "1",
    "type": "planet",
    "attributes": {
      "name": "Custom Planet",
      "oxygen": 1
    }
  }
}

Note that I'm using POST instead of PATCH for this custom endpoint, and would like Orbit to treat it as updateRecord/PATCH

dgeb commented

We should probably reverse the order of these settings, so that custom settings can always be used to override defaults. For example:

const settings = { 
   method: 'PATCH', 
   json: requestDoc,
   ...requestProcessor.buildFetchSettings(request)
};

Of course, we'll need some tests to ensure that this doesn't have unintended side effects.

Exactly. I'm not sure though why it was not like that when you wrote it :)