cc-api-next
Advanced tools
Comparing version 1.0.10 to 1.0.11
34
index.js
const fetch = require('isomorphic-unfetch'); | ||
const Cookies = require('universal-cookie'); | ||
class Api { | ||
constructor(props){ | ||
constructor(props) { | ||
this.url = props.url; | ||
@@ -18,3 +17,3 @@ this.secret = props.secret; | ||
setToken(token){ | ||
setToken(token) { | ||
this.headers['x-token'] = token; | ||
@@ -25,3 +24,3 @@ } | ||
if(config === undefined){ | ||
if (config === undefined) { | ||
config = data || {}; | ||
@@ -31,3 +30,5 @@ data = undefined; | ||
if(!config.method){ | ||
const { headers } = config; | ||
if (!config.method) { | ||
config.method = 'get'; | ||
@@ -41,15 +42,22 @@ } | ||
if(options.method !== 'GET'){ | ||
if (options.method !== 'GET') { | ||
options.body = JSON.stringify({ data }); | ||
} | ||
const handleResponse = response => { | ||
switch (headers['Content-Type']) { | ||
case 'application/octet-stream': | ||
case 'application/pdf': | ||
return response.blob(); | ||
default: | ||
return response.json(); | ||
} | ||
}; | ||
return fetch(`${this.url}${path}`, options) | ||
.then(response => { | ||
return response.json() | ||
.then(response => { | ||
return response; | ||
}); | ||
}).catch(e => { | ||
.then(handleResponse).catch(e => { | ||
return e; | ||
}); | ||
} | ||
@@ -89,4 +97,4 @@ | ||
module.exports = function(options) { | ||
module.exports = function (options) { | ||
return new Api(options); | ||
}; |
{ | ||
"name": "cc-api-next", | ||
"version": "1.0.10", | ||
"version": "1.0.11", | ||
"description": "cc api next", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
2708
74