How to use the type-is.hasBody function in type-is

To help you get started, we’ve selected a few type-is examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github jamesshore / lets_code_javascript / node_modules / karma / node_modules / connect / node_modules / body-parser / lib / types / urlencoded.js View on Github external
return function urlencodedParser(req, res, next) {
    if (req._body) {
      return debug('body already parsed'), next()
    }

    req.body = req.body || {}

    // skip requests without bodies
    if (!typeis.hasBody(req)) {
      return debug('skip empty body'), next()
    }

    debug('content-type %j', req.headers['content-type'])

    // determine if request should be parsed
    if (!shouldParse(req)) {
      return debug('skip parsing'), next()
    }

    // assert charset
    var charset = getCharset(req) || 'utf-8'
    if (charset !== 'utf-8') {
      debug('invalid charset')
      next(createError(415, 'unsupported charset "' + charset.toUpperCase() + '"', {
        charset: charset
github useringo / ringo / build-server / node_modules / body-parser / lib / types / json.js View on Github external
return function jsonParser(req, res, next) {
    if (req._body) {
      return debug('body already parsed'), next()
    }

    req.body = req.body || {}

    // skip requests without bodies
    if (!typeis.hasBody(req)) {
      return debug('skip empty body'), next()
    }

    debug('content-type %j', req.headers['content-type'])

    // determine if request should be parsed
    if (!shouldParse(req)) {
      return debug('skip parsing'), next()
    }

    // assert charset per RFC 7159 sec 8.1
    var charset = getCharset(req) || 'utf-8'
    if (charset.substr(0, 4) !== 'utf-') {
      var err = new Error('unsupported charset "' + charset.toUpperCase() + '"')
      err.charset = charset
      err.status = 415
github strongloop / microgateway / lib / apim-context / index.js View on Github external
function parseRequestBody(ctx, req) {
  if (!typeis.hasBody(req)) {
    return;
  }

  // determine what data types the payload should be parsed as
  var targetContentTypes;
  if (ctx.get('request.content-type')) {
    targetContentTypes = [ ctx.get('request.content-type') ];
  } else {
    targetContentTypes = ctx.get('_.api.consumes');
  }

  var body = ctx.get('request.body'); // body should be Buffer
  if (_.isEmpty(targetContentTypes)) {
    logger.warn('Unable to determine payload data type, store payload as Buffer');
  } else if (body && !(Buffer.isBuffer(body) && body.length === 0)) {
    // we only need to parse the payload if the body is not empty
github useringo / ringo / build-server / node_modules / body-parser / lib / types / text.js View on Github external
return function textParser(req, res, next) {
    if (req._body) {
      return debug('body already parsed'), next()
    }

    req.body = req.body || {}

    // skip requests without bodies
    if (!typeis.hasBody(req)) {
      return debug('skip empty body'), next()
    }

    debug('content-type %j', req.headers['content-type'])

    // determine if request should be parsed
    if (!shouldParse(req)) {
      return debug('skip parsing'), next()
    }

    // get charset
    var charset = getCharset(req) || defaultCharset

    // read
    read(req, res, next, parse, debug, {
      encoding: charset,
github charlielin99 / Jobalytics / node_modules / body-parser / lib / types / json.js View on Github external
return function jsonParser (req, res, next) {
    if (req._body) {
      debug('body already parsed')
      next()
      return
    }

    req.body = req.body || {}

    // skip requests without bodies
    if (!typeis.hasBody(req)) {
      debug('skip empty body')
      next()
      return
    }

    debug('content-type %j', req.headers['content-type'])

    // determine if request should be parsed
    if (!shouldParse(req)) {
      debug('skip parsing')
      next()
      return
    }

    // assert charset per RFC 7159 sec 8.1
    var charset = getCharset(req) || 'utf-8'
github joshlobaptista / Barista-Fullstack / node_modules / body-parser / lib / types / text.js View on Github external
return function textParser (req, res, next) {
    if (req._body) {
      debug('body already parsed')
      next()
      return
    }

    req.body = req.body || {}

    // skip requests without bodies
    if (!typeis.hasBody(req)) {
      debug('skip empty body')
      next()
      return
    }

    debug('content-type %j', req.headers['content-type'])

    // determine if request should be parsed
    if (!shouldParse(req)) {
      debug('skip parsing')
      next()
      return
    }

    // get charset
    var charset = getCharset(req) || defaultCharset
github expressjs / body-parser / lib / types / raw.js View on Github external
return function rawParser (req, res, next) {
    if (req._body) {
      debug('body already parsed')
      next()
      return
    }

    req.body = req.body || {}

    // skip requests without bodies
    if (!typeis.hasBody(req)) {
      debug('skip empty body')
      next()
      return
    }

    debug('content-type %j', req.headers['content-type'])

    // determine if request should be parsed
    if (!shouldParse(req)) {
      debug('skip parsing')
      next()
      return
    }

    // read
    read(req, res, next, parse, debug, {
github elastic / timelion / node_modules / body-parser / lib / types / urlencoded.js View on Github external
return function urlencodedParser(req, res, next) {
    if (req._body) {
      return debug('body already parsed'), next()
    }

    req.body = req.body || {}

    // skip requests without bodies
    if (!typeis.hasBody(req)) {
      return debug('skip empty body'), next()
    }

    debug('content-type %j', req.headers['content-type'])

    // determine if request should be parsed
    if (!shouldParse(req)) {
      return debug('skip parsing'), next()
    }

    // assert charset
    var charset = getCharset(req) || 'utf-8'
    if (charset !== 'utf-8') {
      debug('invalid charset')
      next(createError(415, 'unsupported charset "' + charset.toUpperCase() + '"', {
        charset: charset
github dvonlehman / express-request-proxy / test / setup.js View on Github external
function maybeParseBody(req, res, next) {
    if (is.hasBody(req)) {
      switch (is(req, ['urlencoded', 'json'])) {
        case 'urlencoded':
          debug('parse api urlencoded body');
          return bodyParser.urlencoded({extended: false})(req, res, next);
        case 'json':
          debug('parse api json body');
          return bodyParser.json()(req, res, next);
        default:
          break;
      }
    }

    next();
  }
github adonisjs / adonis-framework / packages / node-req / src / Request / index.js View on Github external
Request.hasBody = function (request) {
  return is.hasBody(request)
}

type-is

Infer the content-type of a request.

MIT
Latest version published 5 years ago

Package Health Score

79 / 100
Full package analysis

Popular type-is functions