rapidjs / rapid.js

An ORM-like Interface and a Router For Your API Requests
https://rapidjs.drewjbartlett.com
711 stars 45 forks source link

custom route for delete with data. Is it possible? #39

Open plandem opened 6 years ago

plandem commented 6 years ago

Hi

I tried, but it appends data into the URL. In my case it's not a valid way to call 'delete' api endpoint. How to achieve this - call custom route for delete with data?

drewjbartlett commented 6 years ago

Can you provide a code sample please? @plandem

plandem commented 6 years ago

What are you talking about? Only GET and HEAD can't have body, afaik (need to check RFC to be sure, but fetch works in that way). So there is no reason to restrict the DELETE to have a body.

drewjbartlett commented 6 years ago

Sorry, not sure I'm following your question. I thought you were asking how to append data but now you're saying it's not valid? Please elaborate a little more if you could so I can understand.

plandem commented 6 years ago

I asked how to use DELETE and send data same time? By the RFC standard DELETE can have non empty body.

plandem commented 6 years ago
const customRoutes = [
    {
        name: 'user-delete',
        url: '/users',
        type: 'delete'
    },
];

const rapid = new Rapid({ customRoutes });

//this one is not working right now, library sends empty body in case of 'DELETE'
rapid.route('user-delete', null, { idList: [1,2,3,4,5] });
drewjbartlett commented 6 years ago

Interesting. This may be a solution for the moment but I think I may want to add better support in v2 which is currently underway.

model.withParam('idList', [1, 2, 3, 4, 5]).route('user-delete') which will generate: api/users?idList%5B0%5D=1&idList%5B1%5D=2&idList%5B2%5D=3&idList%5B3%5D=4&idList%5B4%5D=5

plandem commented 6 years ago

Sorry, in my case it will not help, because server expects idList from body :(

plandem commented 6 years ago

Do you mean library can't do it? Because by RFC standard anything except HEAD and GET can have a body. E.g.: https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/DELETE

That's why I asked. Had not tried axios directly, maybe there is a problem from the axios side.

drewjbartlett commented 6 years ago

@plandem I will have to look into this more at another time. Sorry! Just swamped at the moment!