orbitjs / orbit

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

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

Open SafaAlfulaij opened 2 years ago

SafaAlfulaij commented 2 years ago

https://github.com/orbitjs/orbit/blob/a3e44597a43e58e1d788e473a9a24edd6bf747e0/packages/%40orbit/jsonapi/src/lib/transform-requests.ts#L160-L164

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 2 years ago

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.

SafaAlfulaij commented 2 years ago

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