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

Return reserved #31

Merged
merged 3 commits into from
Apr 28, 2020
Merged
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
18 changes: 14 additions & 4 deletions source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import isIp from "is-ip"
interface IpApiData {
ip: string
city: string
reserved: boolean
region: string
region_code: string
country: string
Expand Down Expand Up @@ -61,6 +62,12 @@ declare namespace ipLocation {
inEu: boolean
}
}

export interface ReservedData {
reserved: boolean
}

export type ReturnType = (LocationData & ReservedData) | ReservedData
}

/**
Expand All @@ -76,17 +83,20 @@ const ipLocation = require("ip-location");
})();
```
*/
async function ipLocation(ip: string): Promise<ipLocation.LocationData> {
async function ipLocation(ip: string): Promise<ipLocation.ReturnType> {
if (typeof ip !== "string" || !isIp.v4(ip)) {
throw new TypeError("A valid ipv4 address must be provided!")
}

const { latitude, longitude, city, region, region_code, country_name, country_code, country_code_iso3, country_capital, country_tld, country_population, country_calling_code, continent_code, in_eu, postal, timezone, utc_offset, currency, currency_name, languages, country_area }: IpApiData = await ky(`https://ipapi.co/${ip}/json/`).json()
const { latitude, longitude, city, reserved, region, region_code, country_name, country_code, country_code_iso3, country_capital, country_tld, country_population, country_calling_code, continent_code, in_eu, postal, timezone, utc_offset, currency, currency_name, languages, country_area }: IpApiData = await ky(`https://ipapi.co/${ip}/json/`).json()

return {
return reserved ? {
reserved
} : {
latitude,
longitude,
city,
reserved: Boolean(reserved),
region: {
name: region,
code: region_code
Expand All @@ -109,7 +119,7 @@ async function ipLocation(ip: string): Promise<ipLocation.LocationData> {
name: currency_name,
code: currency
},
languages: languages.split(",")
languages: languages ? languages.split(",") : []
},
continent: {
code: continent_code,
Expand Down
1 change: 1 addition & 0 deletions test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ test("main", async t => {
latitude: -33.8591,
longitude: 151.2002,
city: "Sydney",
reserved: false,
region: { name: "New South Wales", code: "NSW" },
country: {
name: "Australia",
Expand Down