Skip to content

Commit

Permalink
Merge pull request #34 from adlawson/adlawson/csrf-cookie
Browse files Browse the repository at this point in the history
Send cookies with API request
  • Loading branch information
adlawson authored Aug 15, 2017
2 parents 8f8fea8 + 5091350 commit af9c42f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
},
"dependencies": {
"dustjs-linkedin": "~2.7.0",
"request": "~2.78"
"promise-polyfill": "^6.0.2",
"url-polyfill": "^1.0.7",
"whatwg-fetch": "^2.0.3"
},
"devDependencies": {
"babel-cli": "^6.0.0",
Expand Down
41 changes: 28 additions & 13 deletions tracklist.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@

'use strict';

require('whatwg-fetch'); // Patches window.fetch
require('url-polyfill'); // Patches window.URL

const dust = require('dustjs-linkedin');
const request = require('request');
const Promise = require('promise-polyfill');
const browser = require('./browser');

main();
Expand Down Expand Up @@ -41,18 +44,30 @@ function main() {
}

function fetchData(location, fn) {
request({
"uri": "/player/details",
"baseUrl": location.protocol + "//" + location.hostname,
"qs": { "key": location.pathname },
"json": true
}, (error, response, data) => {
if (!error && response.statusCode === 200 && data.cloudcast.sections.length > 0) {
fn(insertTrackNumber(data));
} else {
console.error(error);
}
});
let url = new URL(`${location.protocol}//${location.hostname}/player/details/`);
url.searchParams.append('key', location.pathname);
fetch(url, { credentials: 'include' })
.then(rejectFailed)
.then(response => response.json())
.then(rejectEmpty)
.then(data => fn(insertTrackNumber(data)))
.catch(err => console.warn("Tracklist unavailable", err));
}

function rejectEmpty(data) {
if (data.cloudcast.sections.length > 0) {
return Promise.resolve(data);
} else {
return Promise.reject(new ReferenceError("No tracklist returned"));
}
}

function rejectFailed(response) {
if (response.status >= 200 && response.status < 300) {
return Promise.resolve(response);
} else {
return Promise.reject(new Error(response.statusText));
}
}

function insertTrackNumber(data) {
Expand Down

0 comments on commit af9c42f

Please sign in to comment.