@st-graphics/premium
Advanced tools
Comparing version 0.2.4 to 0.2.5
@@ -43,2 +43,4 @@ const inProduction = process.env.NODE_ENV === 'production' | ||
window.fetch = window.fetch || fetch | ||
function PremiumClient () {} | ||
@@ -71,3 +73,4 @@ | ||
const url = SERVICE_URL + '/get-session' | ||
return fetch(url, {withCredentials: true}) | ||
return window.fetch(url, {credentials: 'include'}) | ||
.then(function (res) { return res.json() }) | ||
} | ||
@@ -81,15 +84,16 @@ | ||
const url = SERVICE_URL + '/content?' + stringifyQuery(query) | ||
return fetch(url, {withCredentials: true}) | ||
return window.fetch(url, {credentials: 'include'}) | ||
.then(function (res) { return res.json() }) | ||
.then(function (res) { | ||
if (res.renewed) console.log('Credential renewed') | ||
return fetch(res.signed_url) | ||
return window.fetch(res.signed_url) | ||
.then(function (res) { return res.json() }) | ||
.then(function (json) { return Object.assign(json, {user: res.user}) }) | ||
}) | ||
.catch(function () { | ||
return self.fetchPublicContent(resourceKey) | ||
}) | ||
.catch(function () { return self.fetchPublicContent(resourceKey) }) | ||
} | ||
PremiumClient.prototype.fetchPublicContent = function (resourceKey) { | ||
return fetch(BUCKET_URL + resourceKey) | ||
return window.fetch(BUCKET_URL + resourceKey) | ||
.then(function (res) { return res.json() }) | ||
} | ||
@@ -109,9 +113,20 @@ | ||
}) | ||
xhr.withCredentials = options.withCredentials | ||
xhr.withCredentials = options.credentials === 'include' | ||
xhr.onreadystatechange = function () { | ||
if (xhr.readyState === 4) { | ||
try { | ||
const json = JSON.parse(xhr.response) | ||
if (xhr.status === 200) resolve(json) | ||
else reject(new Error(json.message)) | ||
const response = Object.create({ | ||
text () { | ||
return xhr.responseText | ||
}, | ||
json () { | ||
return JSON.parse(xhr.responseText) | ||
} | ||
}) | ||
response.ok = xhr.status === 200 | ||
response.status = xhr.status | ||
response.statusText = xhr.statusText | ||
response.url = xhr.responseURL | ||
response.headers = xhr.getAllResponseHeaders() | ||
resolve(response) | ||
} catch (err) { | ||
@@ -118,0 +133,0 @@ reject(err) |
{ | ||
"name": "@st-graphics/premium", | ||
"version": "0.2.4", | ||
"version": "0.2.5", | ||
"description": "ST Graphics Premium implementation", | ||
@@ -5,0 +5,0 @@ "main": "client/premium.js", |
57002
290
1