Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trying out typescript #35

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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