Wikodit / js-data-jsonapi-light

JSData adapter which serialize and deserialize JSONApi response and requests.
MIT License
8 stars 4 forks source link

Custom actions support #27

Open Tronix117 opened 6 years ago

Tronix117 commented 6 years ago

Add support for deserialize/serialize for custom actions:

store.defineMapper('school', {
  endpoint: 'schools',
});

addAction('createTeacherReport', {
  pathname: 'teachers',
  method: 'POST'
})(store.getMapper('school'));

store.getMapper('school').createTeacherReport(1234, {
  data: { someAttribute: 123 }
}).then(function (response) {
  console.log('response', response.data);
});

See if in the then there should be response or directly data, in which case we can use raw: true to get the response.

phortx commented 6 years ago

Are custom actions possible currently? If yes, how? If not, how can I help?

Tronix117 commented 6 years ago

Custom actions are working, but the adapter is not plugged into those, so it means no serialization/deserialization from/to jsonapi.

I didn't put much effort into it, because in a way custom actions are not standard calls specified by JSONApi, so the format can be anything.

In any case, we should be able to give a configuration option to the addAction to say, we want it to go through the serialization/deserialization.

I will not have time to implement before january, so if need it, you can still submit a PR.


There is a workaround though:

If you want to call a GET on /something/page/current and expect one result, do

store.getMapper('xxx').find('current', {
  endpoint: 'something/page'
})

Will work just fine, and goes through jsonapi serialization deserialization

phortx commented 6 years ago

Sounds good. I'd love to send you a PR but I don't think I'm qualified to do a good contribution on this project due to the lack of any deeper knowledge of the adapter, so I won't to probably :)

Thanks for your help!