1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | var typeis = require('type-is');
|
7 | var json = require('./json');
|
8 | var form = require('./form');
|
9 | var text = require('./text');
|
10 |
|
11 | var jsonTypes = ['json', 'application/*+json', 'application/csp-report'];
|
12 | var formTypes = ['urlencoded'];
|
13 | var textTypes = ['text'];
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | module.exports = function(req, opts){
|
29 | req = req.req || req;
|
30 | opts = opts || {};
|
31 |
|
32 |
|
33 | var jsonType = opts.jsonTypes || jsonTypes;
|
34 | if (typeis(req, jsonType)) return json(req, opts);
|
35 |
|
36 |
|
37 | var formType = opts.formTypes || formTypes;
|
38 | if (typeis(req, formType)) return form(req, opts);
|
39 |
|
40 |
|
41 | var textType = opts.textTypes || textTypes;
|
42 | if (typeis(req, textType)) return text(req, opts);
|
43 |
|
44 |
|
45 | var type = req.headers['content-type'] || '';
|
46 | var message = type ? 'Unsupported content-type: ' + type : 'Missing content-type';
|
47 | var err = new Error(message);
|
48 | err.status = 415;
|
49 | return Promise.reject(err);
|
50 | };
|