Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'type-is' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
// 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();
};
};
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;
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
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
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
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,
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'
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
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, {
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