Skip to content

Instantly share code, notes, and snippets.

@ctaggart
Last active December 12, 2016 06:07
Show Gist options
  • Save ctaggart/eff0ec89d2ed4b7f657a61a627edbfa7 to your computer and use it in GitHub Desktop.
Save ctaggart/eff0ec89d2ed4b7f657a61a627edbfa7 to your computer and use it in GitHub Desktop.
import * as mocha from "mocha";
import { assert } from "chai";
import fetch = require("node-fetch");
async function getGeoIP() {
const res = await fetch("http://freegeoip.net/json/");
const json = await res.json();
return json as FreeGeoIP;
}
describe("fetch tests", () => {
it("get ip", done => {
getGeoIP().then(geoip => {
console.log(geoip);
console.log("ip is " + geoip.ip);
assert.isString(geoip.ip);
assert.isString(geoip.country_code);
done();
});
});
});
interface FreeGeoIP {
ip: string;
country_code: string;
country_name: string;
region_code: string;
region_name: string;
city: string;
zip_code: string;
time_zone: string;
latitude: number;
longitude: number;
metro_code: number;
}
#!/bin/sh -e
npm install --save-dev @types/node
# unit testing
npm install --save-dev chai
npm install --save-dev mocha
npm install --save-dev @types/chai
npm install --save-dev @types/mocha
# fetch
npm install --save node-fetch
# download fetch.d.ts & streams.d.ts
# https://github.com/Microsoft/TypeScript/pull/12493
PS C:\Users\c\ts\fetch> tsc
PS C:\Users\c\ts\fetch> mocha .\fetch-tests.js
fetch tests
{ ip: '23.16.226.33',
country_code: 'CA',
country_name: 'Canada',
region_code: 'BC',
region_name: 'British Columbia',
city: 'Revelstoke',
zip_code: '',
time_zone: 'America/Vancouver',
latitude: 51,
longitude: -118.1833,
metro_code: 0 }
ip is 23.16.226.33
√ get ip (304ms)
1 passing (317ms)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment