How to use type-is - 10 common examples

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 eggjs / egg-security / lib / middlewares / csrf.js View on Github external
// ensure csrf token exists
    if (utils.checkCsrfType(options.type).shouldCheckCtoken) {
      ctx.ensureCsrfSecret();
    }

    // ignore requests: get, head, options and trace
    const method = ctx.method;
    if (method === 'GET' ||
      method === 'HEAD' ||
      method === 'OPTIONS' ||
      method === 'TRACE') {
      return next();
    }

    if (options.ignoreJSON && typeis.is(ctx.get('content-type'), 'json')) {
      return next();
    }

    const body = ctx.request.body || {};
    debug('%s %s, got %j', ctx.method, ctx.url, body);
    ctx.assertCsrf();
    return next();
  };
};
github ynnjs / ynn / lib / rsc.js View on Github external
options.headers[ name.toLowerCase() ] = headers[ name ];
        }

        // params can be set to null and to set full options for request in options
        if( params ) {

            params = Object.assign( {}, config.params || {}, api.params || {}, params );

            if( options.method === 'GET' || options.method === 'HEAD' ) {
                options.qs = Object.assign( rscqs( this ), params );
            } else {
                const ct = options.headers[ 'content-type' ];

                if( typeis.is( ct, 'application/json' ) ) {
                    options.body = params;
                } else if( typeis.is( ct, 'multipart/form-data' ) ) {
                    options.formData = params;
                } else if( typeis.is( ct, 'multipart/related' ) ) {
                    options.multipart = params;
                } else if( typeis.is( ct, 'text/xml' ) ) {
                    options.body = params.body;
                } else {
                    options.form = params;
                }
                options.qs = rscqs( this );
            }
        }
        if( opt.qs ) {
            Object.assign( options.qs, opt.qs );
        }
        options.json = is.undefined( opt.json ) ? true : opt.json;
        return options;
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

type-is

Infer the content-type of a request.

MIT
Latest version published 6 years ago

Package Health Score

82 / 100
Full package analysis

Popular type-is functions