devour-client
devour-client copied to clipboard
Ignore null parameters
It would be nice to be able to set null for the value of include for example, and have the resulting request just not include that param. That way it would be possible to concisely decide to specify an include or not.
I just ended up creating the following middleware:
this.jsonApi.insertMiddlewareBefore('axios-request', {
name: 'ignore-null-params',
req: (payload) => {
if (payload.req.params) {
Object.entries(payload.req.params).forEach(([key, value]) => {
if (!value) {
delete payload.req.params[key]
}
})
}
return payload
}
})
I still think this would be nice as default behavior though. Would like to hear others' opinions.
I don't think this can be universal, particularly with relationships, where 'null' means "break this relationship".
What is your use case for this?
Basically it makes logic for including params dynamically easier. Example: https://github.com/matthewdias/kitsu-slack/blob/master/src/kitsu.js#L321
What I've noticed is that sending null actually just appends an empty string, so currently without this middleware, this line would become ?include=&fields=.... Unless i'm mistaken, atm you have to send null as a string for the url to be properly built. This code only affects url params, so the functionality of breaking relationships, which is sent in the request body, should be unaffected.
AAAH, right, that makes sense.