Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// 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;
test: (contentType: string, content: string) => {
const doesContentTypeMatch = !!typeIs.is(contentType, [
'application/xml',
'application/*+xml',
'text/xml',
]);
const isContentXML = parser.validate(content) === true;
return doesContentTypeMatch || isContentXML;
},
validate: (expected: Result, output: Result) => {
is: function (types) {
let type = this.type
if (!types) return type || false
if (!Array.isArray(types)) types = [].slice.call(arguments)
return typeis(type, types)
},
Response.prototype.is = function (types) {
var type = this.headers['content-type'];
if (!type) return false;
if (!Array.isArray(types)) types = slice.call(arguments);
return typeis(type, types);
}
try{
self._doEncoding(options,response);
}catch(e){
log('error',e);
return options.callback(e,{options:options},options.release);
}
response.options = options;
if(options.method === 'HEAD' || !options.jQuery){
return options.callback(null,response,options.release);
}
var injectableTypes = ['html','xhtml','text/xml', 'application/xml', '+xml'];
if (!options.html && !typeis(contentType(response), injectableTypes)){
log('warn','response body is not HTML, skip injecting. Set jQuery to false to suppress this message');
return options.callback(null,response,options.release);
}
log('debug','Injecting');
self._inject(response, options, self._injected.bind(self));
};
return mimes.some(mimeA => isType(mime, mimeA) || isType(mimeA, mime))
}
is(...types: any[]) {
return typeIs(this.getHeader('content-type') as string, types);
}
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;
}
() =>
typeIs(response.headers.get('content-type') || '', ['application/json', 'application/*+json'])
? response.json()
: response.text(),
E.toError