Skip to content

Commit

Permalink
Merge 77ebffa into 790965e
Browse files Browse the repository at this point in the history
  • Loading branch information
lholmquist committed Feb 11, 2019
2 parents 790965e + 77ebffa commit ba23b0a
Show file tree
Hide file tree
Showing 14 changed files with 1,974 additions and 410 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
node_modules/
/coverage
dist/
.nyc_output/
2 changes: 1 addition & 1 deletion example.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const swapi = require('./index.js');
const swapi = require('./dist/swapi-node');

swapi.getPerson().then((result) => {
console.log(result);
Expand Down
1 change: 0 additions & 1 deletion index.js

This file was deleted.

118 changes: 0 additions & 118 deletions lib/swapi-node.js

This file was deleted.

118 changes: 118 additions & 0 deletions lib/swapi-node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
'use strict';

import request from 'request';
import { capitaliseFirstLetter } from './util';

const BASE_URL = 'https://swapi.co/api/';

function sendRequest(options: any) {
return new Promise((resolve, reject) => {
request(options, (error: any, response: request.Response, body: any) => {
if (error || response.statusCode > 399) {
return reject({ error: response.statusCode });
}

let jsonBody: any;

try {
jsonBody = JSON.parse(body);
} catch (e) {
return reject({ error: 'JSON parse error' });
}

['nextPage', 'previousPage'].forEach((value) => {
jsonBody[value] = (() => {
return () => {
const 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' + capitaliseFirstLetter(value)] = (() => {
return () => {
if (!Array.isArray(jsonBody[value])) {
if (jsonBody[value].indexOf(BASE_URL) > -1) {
return makeRequest(jsonBody[value]);
}

return Promise.resolve(jsonBody[value]);
}

const p = jsonBody[value].map((val: string) => {
if (val.indexOf(BASE_URL) > -1) {
return makeRequest(val);
}
return Promise.resolve(val);
});

return Promise.all(p).then((v) => {
return v;
});
};
})();
}
});

return resolve(jsonBody);
});
});
}

function makeRequest(url: string): Promise<any> {
const options = {
headers: {
'SWAPI-Node-Version': require('../package.json').version,
'User-Agent': 'swapi-node'
},
url: (url.indexOf(BASE_URL) > -1) ? url : BASE_URL + url
};

return sendRequest(options);
}

function parseOptions(options: number | object) {
const opts = options || {};

if (typeof opts === 'object') {
return opts;
} else {
return {
id: opts
};
}
}

export function get(url: string): Promise<any> {
return makeRequest(url);
}

export function getPerson(options: number | object) {
const opts: any = parseOptions(options);
return makeRequest('people' + (opts.id ? '/' + opts.id : '/'));
}

export function getStarship(options: number | object) {
const opts: any = parseOptions(options);
return makeRequest('starships' + (opts.id ? '/' + opts.id : '/'));
}

export function getVehicle(options: number | object) {
const opts: any = parseOptions(options);
return makeRequest('vehicles' + (opts.id ? '/' + opts.id : '/'));
}
export function getFilm(options: number | object) {
const opts: any = parseOptions(options);
return makeRequest('films' + (opts.id ? '/' + opts.id : '/'));
}

export function getSpecies(options: number | object) {
const opts: any = parseOptions(options);
return makeRequest('species' + (opts.id ? '/' + opts.id : '/'));
}
5 changes: 0 additions & 5 deletions lib/util.js

This file was deleted.

3 changes: 3 additions & 0 deletions lib/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function capitaliseFirstLetter(str: string): string {
return str.charAt(0).toUpperCase() + str.slice(1);
}
Loading

0 comments on commit ba23b0a

Please sign in to comment.