-
-
Notifications
You must be signed in to change notification settings - Fork 16.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for auto-generated Accept-Patch header #2878
Comments
@Risto-Stevcev Could you write something like this (EDIT: you can't): let allowPatch = function(req, res, next) {
if (router.methods.patch)
res.set('Allow-Patch', 'application/json')
next()
}
router.use(allowPatch) I'm not sure whether It feels like a better API would allow you to both define middleware (for very general or very dynamic situations) but also specify something directly on the route, like: router.patch('/some-resource', function(req, res, next) {
...
}, 'application/json');
router.patch('/some-resource', function(req, res, next) {
...
}, ['application/json', 'application/example']);
router.patch('/some-resource', function(req, res, next) {
...
}, function(...) {
...
}); I should mention, I haven't thought through the feasibility of those ideas. Jut thinking in "nice api" mode righ tnow. |
|
@Risto-Stevcev Apologies, my mistake then. I didn't have a chance to go in and check it for real, only by inspection. |
@Risto-Stevcev I realised my mistake: |
OK, this seems to work: var express = require('express');
var app = express();
var router = express.Router();
router.patch('*', function(req, res, next) {
res.set('Allow-Patch', 'application/json');
next();
});
router.get('/a', function(req, res, next) { res.write('{}'); res.end(); });
router.patch('/a', function(req, res, next) { res.write('{}'); res.end(); });
router.patch('/b', function(req, res, next) { res.write('{}'); res.end(); });
app.use('/', router);
app.listen(3000); Header not set for: |
@tunniclm That doesn't work either, because it affects the Something needs to change with how express does this, because there needs to be support from the library end for this. |
@Risto-Stevcev Darn, yes, you're right. So, you totally can already do something like the api I mentioned before. But that wouldn't be automatic like you wanted. function allow_patch(type) {
return function(req, res, next) {
res.set('Allow-Patch', type);
next();
}
}
router.get('/a', function(req, res, next) { res.write('{}'); res.end(); });
router.patch('/a', allow_patch('application/json'), function(req, res, next) { res.write('{}'); res.end(); });
router.patch('/b', allow_patch('application/json'), function(req, res, next) { res.write('{}'); res.end(); }); Maybe with #2828 you could do something like: router.with(function(req, res, next) {
if (/PATCH/.test(req.get('Allow')))
res.set('Allow-Patch', 'application/json')
next()
} ...assuming these |
@tunniclm |
@demalus let acceptPatch = function(req, res, next) {
if (/PATCH/.test(req.get('Allow')))
res.set('Accept-Patch', 'application/json')
next()
}
router.use(acceptPatch) // won't work because req.get('Allow') hasn't been auto-generated yet |
@Risto-Stevcev It sounds like |
Hopefully it would |
I just looked up how it sends the options - looks like it wraps the done callback and will send the options response if nothing else responded. Instead of testing any method against function allow_patch(type) {
return function(req, res, next) {
if(req.method === 'OPTIONS') {
res.set('Allow-Patch', type);
}
next();
}
}
router.patch('*', allow_patch('application/json')); |
@demalus |
The auto-generated
OPTIONS
response works well, but it there currently isn't any option that you can set to include Accept-Patch for the media type(s) that your API'sPATCH
request accepts.Creating a middleware function like this doesn't work because the middleware executes before the auto-generated
OPTIONS
response:The text was updated successfully, but these errors were encountered: