Skip to content

Commit 87102c7

Browse files
committed
run prettier
1 parent 298e313 commit 87102c7

File tree

2 files changed

+79
-76
lines changed

2 files changed

+79
-76
lines changed
Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,61 @@
1-
import * as dotenv from "dotenv"
2-
import { IPBogon, IPinfoResProxy } from "../src/common"
3-
import IPinfoResProxyWrapper from "../src/ipinfoResProxyWrapper"
1+
import * as dotenv from "dotenv";
2+
import { IPBogon, IPinfoResProxy } from "../src/common";
3+
import IPinfoResProxyWrapper from "../src/ipinfoResProxyWrapper";
44

5-
const testIfTokenIsSet = process.env.IPINFO_TOKEN ? test : test.skip
5+
const testIfTokenIsSet = process.env.IPINFO_TOKEN ? test : test.skip;
66

77
beforeAll(() => {
8-
dotenv.config()
9-
})
8+
dotenv.config();
9+
});
1010

1111
describe("IPinfoResProxyWrapper", () => {
1212
testIfTokenIsSet("lookupIp", async () => {
13-
const wrapper = new IPinfoResProxyWrapper(process.env.IPINFO_TOKEN!)
13+
const wrapper = new IPinfoResProxyWrapper(process.env.IPINFO_TOKEN!);
1414

1515
for (let i = 0; i < 5; i++) {
16-
const data = (await wrapper.lookupIp("139.5.0.122")) as IPinfoResProxy
17-
18-
expect(data.ip).toEqual("139.5.0.122")
19-
expect(data.service).toBeDefined()
20-
expect(typeof data.service).toBe("string")
21-
expect(data.last_seen).toBeDefined()
22-
expect(typeof data.last_seen).toBe("string")
23-
expect(data.percent_days_seen).toBeDefined()
24-
expect(typeof data.percent_days_seen).toBe("number")
16+
const data = (await wrapper.lookupIp(
17+
"139.5.0.122"
18+
)) as IPinfoResProxy;
19+
20+
expect(data.ip).toEqual("139.5.0.122");
21+
expect(data.service).toBeDefined();
22+
expect(typeof data.service).toBe("string");
23+
expect(data.last_seen).toBeDefined();
24+
expect(typeof data.last_seen).toBe("string");
25+
expect(data.percent_days_seen).toBeDefined();
26+
expect(typeof data.percent_days_seen).toBe("number");
2527
}
26-
})
28+
});
2729

2830
testIfTokenIsSet("isBogon", async () => {
29-
const wrapper = new IPinfoResProxyWrapper(process.env.IPINFO_TOKEN!)
31+
const wrapper = new IPinfoResProxyWrapper(process.env.IPINFO_TOKEN!);
3032

31-
const data = (await wrapper.lookupIp("198.51.100.1")) as IPBogon
32-
expect(data.ip).toEqual("198.51.100.1")
33-
expect(data.bogon).toEqual(true)
34-
})
33+
const data = (await wrapper.lookupIp("198.51.100.1")) as IPBogon;
34+
expect(data.ip).toEqual("198.51.100.1");
35+
expect(data.bogon).toEqual(true);
36+
});
3537

3638
test("Error is thrown for invalid token", async () => {
37-
const wrapper = new IPinfoResProxyWrapper("invalid-token")
38-
await expect(wrapper.lookupIp("1.2.3.4")).rejects.toThrow()
39-
})
39+
const wrapper = new IPinfoResProxyWrapper("invalid-token");
40+
await expect(wrapper.lookupIp("1.2.3.4")).rejects.toThrow();
41+
});
4042

4143
test("Error is thrown when response cannot be parsed as JSON", async () => {
42-
const baseUrlWithUnparseableResponse = "https://ipinfo.io/developers#"
43-
const wrapper = new IPinfoResProxyWrapper("token", undefined, undefined, baseUrlWithUnparseableResponse)
44+
const baseUrlWithUnparseableResponse = "https://ipinfo.io/developers#";
45+
const wrapper = new IPinfoResProxyWrapper(
46+
"token",
47+
undefined,
48+
undefined,
49+
baseUrlWithUnparseableResponse
50+
);
4451

45-
await expect(wrapper.lookupIp("1.2.3.4")).rejects.toThrow()
52+
await expect(wrapper.lookupIp("1.2.3.4")).rejects.toThrow();
4653

4754
const result = await wrapper
4855
.lookupIp("1.2.3.4")
4956
.then((_) => "parseable")
50-
.catch((_) => "unparseable")
57+
.catch((_) => "unparseable");
5158

52-
expect(result).toEqual("unparseable")
53-
})
54-
})
59+
expect(result).toEqual("unparseable");
60+
});
61+
});

src/ipinfoResProxyWrapper.ts

Lines changed: 40 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
1-
import fetch from "node-fetch"
2-
import type { RequestInit, Response } from "node-fetch"
3-
import Cache from "./cache/cache"
4-
import LruCache from "./cache/lruCache"
5-
import ApiLimitError from "./errors/apiLimitError"
6-
import { isInSubnet } from "subnet-check"
1+
import type { RequestInit, Response } from "node-fetch";
2+
import Cache from "./cache/cache";
3+
import LruCache from "./cache/lruCache";
4+
import ApiLimitError from "./errors/apiLimitError";
5+
import { isInSubnet } from "subnet-check";
76
import {
87
REQUEST_TIMEOUT_DEFAULT,
98
CACHE_VSN,
109
HOST_RES_PROXY,
1110
BOGON_NETWORKS,
1211
IPinfoResProxy,
1312
IPBogon
14-
} from "./common"
15-
import VERSION from "./version"
13+
} from "./common";
14+
import VERSION from "./version";
1615

17-
const clientUserAgent = `IPinfoClient/nodejs/${VERSION}`
16+
const clientUserAgent = `IPinfoClient/nodejs/${VERSION}`;
1817

1918
export default class IPinfoResProxyWrapper {
2019
private token: string;
@@ -24,7 +23,7 @@ export default class IPinfoResProxyWrapper {
2423

2524
/**
2625
* Creates IPinfoResProxyWrapper object to communicate with the IPinfo Res Proxy API.
27-
*
26+
*
2827
* @param token Token string provided by IPinfo for the registered user.
2928
* @param cache An implementation of IPCache interface, or LruCache if not specified.
3029
* @param timeout Request timeout in milliseconds, or 5000ms if not specified. 0 disables the timeout.
@@ -34,19 +33,19 @@ export default class IPinfoResProxyWrapper {
3433
token: string,
3534
cache?: Cache,
3635
timeout?: number,
37-
baseUrl?: string,
36+
baseUrl?: string
3837
) {
39-
this.token = token
40-
this.cache = cache || new LruCache()
38+
this.token = token;
39+
this.cache = cache || new LruCache();
4140
this.timeout =
4241
timeout === null || timeout === undefined
4342
? REQUEST_TIMEOUT_DEFAULT
44-
: timeout
45-
this.baseUrl = baseUrl || `https://${HOST_RES_PROXY}`
43+
: timeout;
44+
this.baseUrl = baseUrl || `https://${HOST_RES_PROXY}`;
4645
}
4746

4847
public static cacheKey(k: string): string {
49-
return `${k}:${CACHE_VSN}`
48+
return `${k}:${CACHE_VSN}`;
5049
}
5150

5251
public async fetchApi(
@@ -58,75 +57,72 @@ export default class IPinfoResProxyWrapper {
5857
Authorization: `Bearer ${this.token}`,
5958
"Content-Type": "application/json",
6059
"User-Agent": clientUserAgent
61-
}
60+
};
6261

6362
const request = Object.assign(
6463
{
6564
timeout: this.timeout,
6665
method: "GET",
67-
compress: false,
66+
compress: false
6867
},
6968
init,
70-
{ headers: Object.assign(headers, init.headers) },
71-
)
69+
{ headers: Object.assign(headers, init.headers) }
70+
);
7271

7372
const url = [this.baseUrl, path].join(
74-
!this.baseUrl.endsWith("/") && !path.startsWith("/")
75-
? "/"
76-
: ""
77-
)
73+
!this.baseUrl.endsWith("/") && !path.startsWith("/") ? "/" : ""
74+
);
7875

7976
return fetch(url, request).then((response: Response) => {
8077
if (response.status === 429) {
81-
throw new ApiLimitError()
78+
throw new ApiLimitError();
8279
}
8380

8481
if (response.status >= 400) {
85-
throw new Error(
82+
throw new Error(
8683
`Received an error from the IPinfo API ` +
87-
`(using authorization ${headers["Authorization"]}) ` +
88-
`${response.status} ${response.statusText} ${response.url}`
89-
)
84+
`(using authorization ${headers["Authorization"]}) ` +
85+
`${response.status} ${response.statusText} ${response.url}`
86+
);
9087
}
9188

92-
return response
93-
})
89+
return response;
90+
});
9491
}
9592

9693
public async lookupIp(
9794
ip: string | undefined = undefined
9895
): Promise<IPinfoResProxy | IPBogon> {
9996
if (ip && this.isBogon(ip)) {
100-
return { ip, bogon: true }
97+
return { ip, bogon: true };
10198
}
10299

103100
if (!ip) {
104-
ip = "me"
101+
ip = "me";
105102
}
106103

107-
const data = await this.cache.get(IPinfoResProxyWrapper.cacheKey(ip))
104+
const data = await this.cache.get(IPinfoResProxyWrapper.cacheKey(ip));
108105

109106
if (data) {
110-
return data
107+
return data;
111108
}
112109

113-
return this.fetchApi(ip)
114-
.then(async (response) => {
115-
const ipinfo = (await response.json()) as IPinfoResProxy
116-
this.cache.set(IPinfoResProxyWrapper.cacheKey(ip), ipinfo)
110+
return this.fetchApi(ip).then(async (response) => {
111+
const ipinfo = (await response.json()) as IPinfoResProxy;
112+
this.cache.set(IPinfoResProxyWrapper.cacheKey(ip), ipinfo);
117113

118-
return ipinfo
119-
})
114+
return ipinfo;
115+
});
120116
}
121117

122118
private isBogon(ip: string): boolean {
123119
if (ip != "") {
124120
for (let network of BOGON_NETWORKS) {
125121
if (isInSubnet(ip, network)) {
126-
return true
122+
return true;
127123
}
128124
}
129125
}
130-
return false
126+
return false;
131127
}
132-
}
128+
}

0 commit comments

Comments
 (0)