-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
1,883 additions
and
344 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{ | ||
"env": { | ||
"commonjs": true, | ||
"es6": true, | ||
"node": true | ||
}, | ||
"extends": "eslint:recommended", | ||
"globals": { | ||
"Atomics": "readonly", | ||
"SharedArrayBuffer": "readonly" | ||
}, | ||
"parserOptions": { | ||
"ecmaVersion": 2018 | ||
}, | ||
"rules": { | ||
"indent": [ | ||
"error", | ||
2 | ||
], | ||
"linebreak-style": [ | ||
"error", | ||
"unix" | ||
], | ||
"quotes": [ | ||
"error", | ||
"single" | ||
], | ||
"semi": [ | ||
"error", | ||
"always" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,122 +1,122 @@ | ||
var request = require('request'), | ||
util = require('./util'); | ||
util = require('./util'); | ||
|
||
const BASE_URL = 'https://swapi.co/api/'; | ||
|
||
function sendRequest(options) { | ||
return new Promise((resolve, reject) => { | ||
request(options, (error, response, body) => { | ||
if (error || response.statusCode > 399) { | ||
return reject({error: response.statusCode}); | ||
return new Promise((resolve, reject) => { | ||
request(options, (error, response, body) => { | ||
if (error || response.statusCode > 399) { | ||
return reject({error: response.statusCode}); | ||
} | ||
|
||
var jsonBody; | ||
|
||
try { | ||
jsonBody = JSON.parse(body); | ||
} catch (e) { | ||
return reject({error: 'JSON parse error'}); | ||
} | ||
|
||
['nextPage', 'previousPage'].forEach((value) => { | ||
jsonBody[value] = (() => { | ||
return () => { | ||
var url = jsonBody[(value.indexOf('next') > -1) ? 'next' : 'previous']; | ||
if (url) { | ||
return makeRequest(url); | ||
} | ||
|
||
var jsonBody; | ||
return Promise.resolve(null); | ||
}; | ||
})(); | ||
}); | ||
|
||
Object.keys(jsonBody).forEach((value) => { | ||
if (typeof jsonBody[value] !== 'function') { | ||
jsonBody['get' + util.capitaliseFirstLetter(value)] = (() => { | ||
return () => { | ||
if (!Array.isArray(jsonBody[value])) { | ||
if (jsonBody[value].indexOf(BASE_URL) > -1) { | ||
return makeRequest(jsonBody[value]); | ||
} | ||
|
||
try { | ||
jsonBody = JSON.parse(body); | ||
} catch (e) { | ||
return reject({error: 'JSON parse error'}); | ||
} | ||
return Promise.resolve(jsonBody[value]); | ||
} | ||
|
||
['nextPage', 'previousPage'].forEach((value) => { | ||
jsonBody[value] = (() => { | ||
return () => { | ||
var url = jsonBody[(value.indexOf('next') > -1) ? 'next' : 'previous']; | ||
if (url) { | ||
return makeRequest(url); | ||
} | ||
|
||
return Promise.resolve(null); | ||
}; | ||
})(); | ||
}); | ||
|
||
Object.keys(jsonBody).forEach((value) => { | ||
if (typeof jsonBody[value] !== 'function') { | ||
jsonBody['get' + util.capitaliseFirstLetter(value)] = (() => { | ||
return () => { | ||
if (!Array.isArray(jsonBody[value])) { | ||
if (jsonBody[value].indexOf(BASE_URL) > -1) { | ||
return makeRequest(jsonBody[value]); | ||
} | ||
|
||
return Promise.resolve(jsonBody[value]); | ||
} | ||
|
||
var p = jsonBody[value].map((val) => { | ||
if (val.indexOf(BASE_URL) > -1) { | ||
return makeRequest(val); | ||
} | ||
return Promise.resolve(val); | ||
}); | ||
|
||
return Promise.all(p).then((v) => { | ||
return v; | ||
}); | ||
}; | ||
})(); | ||
var p = jsonBody[value].map((val) => { | ||
if (val.indexOf(BASE_URL) > -1) { | ||
return makeRequest(val); | ||
} | ||
}); | ||
return Promise.resolve(val); | ||
}); | ||
|
||
return Promise.all(p).then((v) => { | ||
return v; | ||
}); | ||
}; | ||
})(); | ||
} | ||
}); | ||
|
||
return resolve(jsonBody); | ||
}); | ||
return resolve(jsonBody); | ||
}); | ||
}); | ||
} | ||
|
||
function makeRequest(url) { | ||
var options = { | ||
url: (url.indexOf(BASE_URL) > -1) ? url : BASE_URL + url, | ||
headers: { | ||
'User-Agent': 'swapi-node', | ||
'SWAPI-Node-Version': require('../package.json').version, | ||
} | ||
}; | ||
var options = { | ||
url: (url.indexOf(BASE_URL) > -1) ? url : BASE_URL + url, | ||
headers: { | ||
'User-Agent': 'swapi-node', | ||
'SWAPI-Node-Version': require('../package.json').version, | ||
} | ||
}; | ||
|
||
return sendRequest(options); | ||
return sendRequest(options); | ||
} | ||
|
||
function parseOptions(options) { | ||
var id, opts; | ||
var opts; | ||
|
||
opts = options || {}; | ||
opts = options || {}; | ||
|
||
if (typeof opts === 'object') { | ||
return opts; | ||
} else { | ||
return { | ||
id: opts | ||
}; | ||
} | ||
if (typeof opts === 'object') { | ||
return opts; | ||
} else { | ||
return { | ||
id: opts | ||
}; | ||
} | ||
|
||
|
||
} | ||
|
||
module.exports = { | ||
get (url) { | ||
return makeRequest(url); | ||
}, | ||
getPerson (options) { | ||
var opts = parseOptions(options); | ||
return makeRequest('people' + (opts.id ? '/' + opts.id : '/')); | ||
}, | ||
getStarship (options) { | ||
var opts = parseOptions(options); | ||
return makeRequest('starships' + (opts.id ? '/' + opts.id : '/')); | ||
}, | ||
getVehicle (options) { | ||
var opts = parseOptions(options); | ||
return makeRequest('vehicles' + (opts.id ? '/' + opts.id : '/')); | ||
}, | ||
getFilm (options) { | ||
var opts = parseOptions(options); | ||
return makeRequest('films' + (opts.id ? '/' + opts.id : '/')); | ||
}, | ||
getSpecies (options) { | ||
var opts = parseOptions(options); | ||
return makeRequest('species' + (opts.id ? '/' + opts.id : '/')); | ||
}, | ||
getPlanets (options) { | ||
var opts = parseOptions(options); | ||
return makeRequest('planets' + (opts.id ? '/' + opts.id : '/')); | ||
} | ||
get (url) { | ||
return makeRequest(url); | ||
}, | ||
getPerson (options) { | ||
var opts = parseOptions(options); | ||
return makeRequest('people' + (opts.id ? '/' + opts.id : '/')); | ||
}, | ||
getStarship (options) { | ||
var opts = parseOptions(options); | ||
return makeRequest('starships' + (opts.id ? '/' + opts.id : '/')); | ||
}, | ||
getVehicle (options) { | ||
var opts = parseOptions(options); | ||
return makeRequest('vehicles' + (opts.id ? '/' + opts.id : '/')); | ||
}, | ||
getFilm (options) { | ||
var opts = parseOptions(options); | ||
return makeRequest('films' + (opts.id ? '/' + opts.id : '/')); | ||
}, | ||
getSpecies (options) { | ||
var opts = parseOptions(options); | ||
return makeRequest('species' + (opts.id ? '/' + opts.id : '/')); | ||
}, | ||
getPlanets (options) { | ||
var opts = parseOptions(options); | ||
return makeRequest('planets' + (opts.id ? '/' + opts.id : '/')); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
module.exports = { | ||
capitaliseFirstLetter (string) { | ||
return string.charAt(0).toUpperCase() + string.slice(1); | ||
} | ||
capitaliseFirstLetter (string) { | ||
return string.charAt(0).toUpperCase() + string.slice(1); | ||
} | ||
}; |
Oops, something went wrong.