UNPKG

1.33 kBJavaScriptView Raw
1'use strict';var _typeof='function'==typeof Symbol&&'symbol'==typeof Symbol.iterator?function(a){return typeof a}:function(a){return a&&'function'==typeof Symbol&&a.constructor===Symbol&&a!==Symbol.prototype?'symbol':typeof a},_require=require('url'),URL=_require.URL,centra=require('centra'),phin=async function(a){if('string'!=typeof a&&!a.hasOwnProperty('url'))throw new Error('Missing url option from options for request method.');var b=centra('object'===('undefined'==typeof a?'undefined':_typeof(a))?a.url:a,a.method||'GET');a.headers&&b.header(a.headers),a.stream&&b.stream(),a.timeout&&b.timeout(a.timeout),a.data&&b.body(a.data),a.form&&b.body(a.form,'form'),a.compression&&b.compress(),'object'===_typeof(a.core)&&Object.keys(a.core).forEach(function(d){b.option(d,a.core[d])});var c=await b.send();return c.headers.hasOwnProperty('location')&&a.followRedirects?(a.url=new URL(c.headers.location,a.url).toString(),await phin(a)):a.stream?(c.stream=c,c):(c.coreRes.body=c.body,a.parse&&'json'===a.parse?(c.coreRes.body=await c.json(),c.coreRes):c.coreRes)};phin.promisified=phin,phin.unpromisified=function(a,b){phin(a).then(function(c){b&&b(null,c)}).catch(function(c){b&&b(c,null)})},phin.defaults=function(a){return async function(b){return await phin(Object.assign(a,'string'==typeof b?{url:b}:b))}},module.exports=phin;
\No newline at end of file