Skip to content

Commit

Permalink
feat: Add new simplier get methods. Deprecate the old ones. (#100)
Browse files Browse the repository at this point in the history
fixes #16
  • Loading branch information
lholmquist committed Jul 9, 2020
1 parent 3ba7fc5 commit b29d1a4
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lib/swapi-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,31 +88,48 @@ function parseOptions (options = {}) {
};
}

const funcs = ['people', 'starships', 'vehicles', 'films', 'species', 'planets'].map((name => {
return { name, func: options => {
const options_ = parseOptions(options);
return makeRequest(name + (options_.id ? '/' + options_.id : '/'));
} };
})).reduce((map, object) => {
map[object.name] = object.func;
return map;
}, {});

module.exports = {
...funcs,
get (url) {
return makeRequest(url);
},
getPerson (options) {
deprecate('getPerson is deprecated. Use people instead');
const options_ = parseOptions(options);
return makeRequest('people' + (options_.id ? '/' + options_.id : '/'));
},
getStarship (options) {
deprecate('getPerson is deprecated. Use starships instead');
const options_ = parseOptions(options);
return makeRequest('starships' + (options_.id ? '/' + options_.id : '/'));
},
getVehicle (options) {
deprecate('getPerson is deprecated. Use vehicles instead');
const options_ = parseOptions(options);
return makeRequest('vehicles' + (options_.id ? '/' + options_.id : '/'));
},
getFilm (options) {
deprecate('getPerson is deprecated. Use films instead');
const options_ = parseOptions(options);
return makeRequest('films' + (options_.id ? '/' + options_.id : '/'));
},
getSpecies (options) {
deprecate('getPerson is deprecated. Use species instead');
const options_ = parseOptions(options);
return makeRequest('species' + (options_.id ? '/' + options_.id : '/'));
},
getPlanets (options) {
deprecate('getPerson is deprecated. Use planets instead');
const options_ = parseOptions(options);
return makeRequest('planets' + (options_.id ? '/' + options_.id : '/'));
}
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
"error",
"always"
],
"unicorn/no-reduce": [
"off"
],
"space-before-function-paren": [
"error",
"always"
Expand Down
78 changes: 78 additions & 0 deletions test/swapi-api-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,19 @@ test('GET People - Return a Promise', async t => {
t.end();
});

test('GET People - Return a Promise', async t => {
nock(`${BASE_URL}/`)
.matchHeader('User-Agent', 'swapi-node')
.matchHeader('SWAPI-Node-Version', version)
.get('/people/1')
.reply(200, {});

const request = swapi.people(1);

t.equal(request instanceof Promise, true, 'should return a Promise');
t.end();
});

test('GET People - using options', async t => {
nock(`${BASE_URL}/`)
.matchHeader('User-Agent', 'swapi-node')
Expand Down Expand Up @@ -73,6 +86,19 @@ test('GET films', async t => {
t.end();
});

test('GET films', async t => {
nock(`${BASE_URL}/`)
.matchHeader('User-Agent', 'swapi-node')
.matchHeader('SWAPI-Node-Version', version)
.get('/films/1')
.reply(200, {});

const request = swapi.films(1);

t.equal(request instanceof Promise, true, 'should return a Promise');
t.end();
});

test('GET films - with options', async t => {
nock(`${BASE_URL}/`)
.matchHeader('User-Agent', 'swapi-node')
Expand Down Expand Up @@ -113,6 +139,19 @@ test('GET starship', async t => {
t.end();
});

test('GET starship', async t => {
nock(`${BASE_URL}/`)
.matchHeader('User-Agent', 'swapi-node')
.matchHeader('SWAPI-Node-Version', version)
.get('/starships/1')
.reply(200, {});

const request = swapi.starships(1);

t.equal(request instanceof Promise, true, 'should return a Promise');
t.end();
});

test('GET starship - with options', async t => {
nock(`${BASE_URL}/`)
.matchHeader('User-Agent', 'swapi-node')
Expand Down Expand Up @@ -152,6 +191,19 @@ test('GET vehicles', async t => {
t.end();
});

test('GET vehicles', async t => {
nock(`${BASE_URL}/`)
.matchHeader('User-Agent', 'swapi-node')
.matchHeader('SWAPI-Node-Version', version)
.get('/vehicles/1')
.reply(200, {});

const request = swapi.vehicles(1);

t.equal(request instanceof Promise, true, 'should return a Promise');
t.end();
});

test('GET vehicles - with options', async t => {
nock(`${BASE_URL}/`)
.matchHeader('User-Agent', 'swapi-node')
Expand Down Expand Up @@ -192,6 +244,19 @@ test('GET species', async t => {
t.end();
});

test('GET species', async t => {
nock(`${BASE_URL}/`)
.matchHeader('User-Agent', 'swapi-node')
.matchHeader('SWAPI-Node-Version', version)
.get('/species/1')
.reply(200, {});

const request = swapi.species(1);

t.equal(request instanceof Promise, true, 'should return a Promise');
t.end();
});

test('GET species - with options', async t => {
nock(`${BASE_URL}/`)
.matchHeader('User-Agent', 'swapi-node')
Expand Down Expand Up @@ -232,6 +297,19 @@ test('GET planets', async t => {
t.end();
});

test('GET planets', async t => {
nock(`${BASE_URL}/`)
.matchHeader('User-Agent', 'swapi-node')
.matchHeader('SWAPI-Node-Version', version)
.get('/planets/1')
.reply(200, {});

const request = swapi.planets(1);

t.equal(request instanceof Promise, true, 'should return a Promise');
t.end();
});

test('GET planets - with options', async t => {
nock(`${BASE_URL}/`)
.matchHeader('User-Agent', 'swapi-node')
Expand Down

0 comments on commit b29d1a4

Please sign in to comment.