'f'
mh-two-thousand-and-two
2024-04-12 26f2711ef9461961fb953e2b497bd314ef95e345
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
'use strict';
 
/**
 * Module dependencies.
 */
 
const typeis = require('type-is');
const json = require('./json');
const form = require('./form');
const text = require('./text');
 
const jsonTypes = [ 'json', 'application/*+json', 'application/csp-report' ];
const formTypes = [ 'urlencoded' ];
const textTypes = [ 'text' ];
 
/**
 * Return a Promise which parses form and json requests
 * depending on the Content-Type.
 *
 * Pass a node request or an object with `.req`,
 * such as a koa Context.
 *
 * @param {Request} req
 * @param {Options} [opts]
 * @return {Function}
 * @api public
 */
 
module.exports = async function(req, opts) {
  req = req.req || req;
  opts = opts || {};
 
  // json
  const jsonType = opts.jsonTypes || jsonTypes;
  if (typeis(req, jsonType)) return json(req, opts);
 
  // form
  const formType = opts.formTypes || formTypes;
  if (typeis(req, formType)) return form(req, opts);
 
  // text
  const textType = opts.textTypes || textTypes;
  if (typeis(req, textType)) return text(req, opts);
 
  // invalid
  const type = req.headers['content-type'] || '';
  const message = type ? 'Unsupported content-type: ' + type : 'Missing content-type';
  const err = new Error(message);
  err.status = 415;
  throw err;
};