Skip to content

Commit ceb6143

Browse files
authored
Merge pull request #12 from ConnerTechnology/fix_lint_errors
Fix lint errors
2 parents b33a114 + 217ad02 commit ceb6143

8 files changed

Lines changed: 103 additions & 59 deletions

File tree

.eslintrc.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,33 @@ module.exports = {
44
env: {
55
browser: true,
66
es6: true,
7-
node: true,
7+
node: true
88
},
99
extends: [
1010
'airbnb-base',
1111
'plugin:@typescript-eslint/recommended',
1212
'prettier/@typescript-eslint',
13-
'plugin:prettier/recommended',
13+
'plugin:prettier/recommended'
1414
],
1515
parser: '@typescript-eslint/parser',
1616
parserOptions: {
1717
project: path.resolve(__dirname, './tsconfig.json')
1818
},
1919
plugins: [
20-
'@typescript-eslint',
20+
'@typescript-eslint'
2121
],
2222
rules: {
23-
'lines-between-class-members': 'off',
23+
'lines-between-class-members': 'off'
2424
},
2525
overrides: [
2626
{
2727
files: ['**/*.test.js', '**/*.test.ts'],
2828
rules: {
2929
// Import
3030
'import/no-extraneous-dependencies': 'off'
31-
}
31+
},
3232
env: {
33-
jest: true,
33+
jest: true
3434
}
3535
}
3636
],

.github/workflows/ci.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Lint and Test
2+
3+
on: [push]
4+
5+
jobs:
6+
lint:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- name: Checkout Repository
10+
uses: actions/checkout@v1
11+
- name: Install Node
12+
uses: actions/setup-node@v1
13+
- name: Install Dependencies
14+
run: npm install
15+
- name: Lint
16+
run: npm run lint
17+
test:
18+
runs-on: ubuntu-latest
19+
steps:
20+
- name: Checkout Repository
21+
uses: actions/checkout@v1
22+
- name: Install Node
23+
uses: actions/setup-node@v1
24+
- name: Install Dependencies
25+
run: npm install
26+
- name: Test
27+
run: npm test

.github/workflows/release.yml

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,15 @@ jobs:
88
release:
99
runs-on: ubuntu-latest
1010
steps:
11-
- uses: actions/checkout@v1
12-
- uses: actions/setup-node@v1
13-
- run: npm install
14-
- run: npm run build
15-
- uses: mikeal/merge-release@master
11+
- name: Checkout Repository
12+
uses: actions/checkout@v1
13+
- name: Install Node
14+
uses: actions/setup-node@v1
15+
- name: Install Dependencies
16+
run: npm install
17+
- name: Build
18+
run: npm run build
19+
- name: Release
20+
uses: mikeal/merge-release@master
1621
env:
1722
NPM_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}

.github/workflows/test.yml

Lines changed: 0 additions & 13 deletions
This file was deleted.

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
{
22
"name": "ipdata",
3-
"version": "2.0.0-next.2",
3+
"version": "2.0.0",
44
"description": "JavaScript library to gather information for an ip using https://ipdata.co.",
55
"main": "./lib/index.js",
66
"scripts": {
77
"prebuild": "del lib",
88
"build": "tsc",
99
"lint": "eslint --fix \"src/**\"",
10-
"prepublishOnly": "npm run build",
1110
"test": "jest"
1211
},
1312
"repository": {

src/ipdata.ts

Lines changed: 56 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,48 @@
11
import isString from 'lodash/isString';
22
import isArray from 'lodash/isArray';
33
import isIP from 'is-ip';
4-
import axios, { AxiosError, AxiosResponse } from 'axios';
4+
import axios, { AxiosError } from 'axios';
55
import urljoin from 'url-join';
66
import LRU from 'lru-cache';
77

88
const CACHE_MAX = 4096; // max number of items
99
const CACHE_MAX_AGE = 1000 * 60 * 60 * 24; // 24 hours
1010
const cache = new LRU<string, LookupResponse>({ max: CACHE_MAX, maxAge: CACHE_MAX_AGE });
1111
const DEFAULT_IP = 'DEFAULT_IP';
12-
const VALID_FIELDS=['ip', 'is_eu', 'city', 'region', 'region_code', 'country_name', 'country_code', 'continent_name', 'continent_code', 'latitude', 'longitude', 'asn', 'organisation', 'postal', 'calling_code', 'flag', 'emoji_flag', 'emoji_unicode', 'carrier', 'languages', 'currency', 'time_zone', 'threat', 'count', 'status'];
12+
const VALID_FIELDS = [
13+
'ip',
14+
'is_eu',
15+
'city',
16+
'region',
17+
'region_code',
18+
'country_name',
19+
'country_code',
20+
'continent_name',
21+
'continent_code',
22+
'latitude',
23+
'longitude',
24+
'asn',
25+
'organisation',
26+
'postal',
27+
'calling_code',
28+
'flag',
29+
'emoji_flag',
30+
'emoji_unicode',
31+
'carrier',
32+
'languages',
33+
'currency',
34+
'time_zone',
35+
'threat',
36+
'count',
37+
'status',
38+
];
1339
const BASE_URL = 'https://api.ipdata.co/';
1440

15-
function isValidIP(ip: string) {
41+
function isValidIP(ip: string): boolean {
1642
return ip === DEFAULT_IP || isIP(ip);
1743
}
1844

19-
function isValidSelectField(field: string) {
45+
function isValidSelectField(field: string): boolean {
2046
const index = VALID_FIELDS.indexOf(field);
2147

2248
if (index === -1) {
@@ -26,27 +52,27 @@ function isValidSelectField(field: string) {
2652
return true;
2753
}
2854

29-
function isValidFields(fields: string[]) {
55+
function isValidFields(fields: string[]): boolean {
3056
if (!isArray(fields)) {
3157
throw new Error('Fields should be an array.');
3258
}
3359

34-
for (const field of fields) {
60+
fields.forEach(field => {
3561
const index = VALID_FIELDS.indexOf(field);
3662
if (index === -1) {
3763
throw new Error(`${field} is not a valid field.`);
3864
}
39-
}
65+
});
4066

4167
return true;
4268
}
4369

44-
export function clearCache(ip?: string) {
45-
if (isValidIP(ip)) {
46-
return cache.del(ip);
47-
}
48-
return cache.reset();
49-
}
70+
// export function clearCache(ip?: string): void {
71+
// if (isValidIP(ip)) {
72+
// return cache.del(ip);
73+
// }
74+
// return cache.reset();
75+
// }
5076

5177
export interface LookupResponse {
5278
ip: string;
@@ -101,15 +127,21 @@ export interface LookupResponse {
101127
}
102128

103129
export interface BulkLookupResponse {
104-
responses: LookupResponse[],
130+
responses: LookupResponse[];
105131
status: number;
106132
}
107133

134+
// eslint-disable-next-line @typescript-eslint/interface-name-prefix
135+
interface IPDataParams {
136+
'api-key': string;
137+
fields?: string;
138+
}
139+
108140
export default class IPData {
109141
apiKey?: string;
110142
useCache?: boolean;
111143

112-
constructor(apiKey: string, useCache: boolean = true) {
144+
constructor(apiKey: string, useCache = true) {
113145
if (!isString(apiKey)) {
114146
throw new Error('An API key is required.');
115147
}
@@ -119,7 +151,7 @@ export default class IPData {
119151
}
120152

121153
async lookup(ip?: string, selectField?: string, fields?: string[]): Promise<LookupResponse> {
122-
const params: any = { 'api-key': this.apiKey };
154+
const params: IPDataParams = { 'api-key': this.apiKey };
123155
let url = ip ? urljoin(BASE_URL, ip) : BASE_URL;
124156

125157
if (ip && !isValidIP(ip)) {
@@ -147,38 +179,37 @@ export default class IPData {
147179
let data = { ...response.data, status: response.status };
148180

149181
if (selectField) {
150-
data = { 'select_field': response.data, status: response.status };
182+
data = { [selectField]: response.data, status: response.status };
151183
}
152184

153-
if (this.useCache) {
185+
if ((!selectField || !fields) && this.useCache) {
154186
cache.set(ip || DEFAULT_IP, data);
155187
} else {
156188
return data;
157189
}
158190
} catch (e) {
159191
const { response } = e as AxiosError;
160192
if (response) {
161-
return { ...response.data, status: response.status }
162-
} else {
163-
throw e;
193+
return { ...response.data, status: response.status };
164194
}
195+
throw e;
165196
}
166197

167198
return cache.get(ip || DEFAULT_IP);
168199
}
169200

170201
async bulkLookup(ips: string[], fields?: string[]): Promise<BulkLookupResponse> {
171-
const params: any = { 'api-key': this.apiKey };
202+
const params: IPDataParams = { 'api-key': this.apiKey };
172203

173204
if (ips.length < 2) {
174205
throw new Error('Bulk Lookup requires more than 1 IP Address in the payload.');
175206
}
176207

177-
for (const ip of ips) {
208+
ips.forEach(ip => {
178209
if (!isValidIP(ip)) {
179210
throw new Error(`${ip} is an invalid IP address.`);
180211
}
181-
}
212+
});
182213

183214
if (fields && isValidFields(fields)) {
184215
params.fields = fields.join(',');
@@ -191,9 +222,8 @@ export default class IPData {
191222
const { response } = e as AxiosError;
192223
if (response) {
193224
return { ...response.data, status: response.status };
194-
} else {
195-
throw e;
196225
}
226+
throw e;
197227
}
198228
}
199229
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import IPData from './ipdata';
1+
import IPData from '../src/ipdata';
22

33
const API_KEY = 'test';
44

@@ -69,10 +69,10 @@ describe('lookup()', () => {
6969
await expect(ipdata.lookup(null, field)).rejects.toThrowError(`${field} is not a valid field.`);
7070
});
7171

72-
it('should return a response with only the field', async () => {
72+
it.only('should return a response with only the field', async () => {
7373
const field = 'is_eu';
7474
const info = await ipdata.lookup(null, field);
75-
expect(info).toHaveProperty('select_field', false);
75+
expect(info).toHaveProperty(field, false);
7676
expect(info).toHaveProperty('status');
7777
});
7878
});

tsconfig.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,5 @@
1313
},
1414
"include": [
1515
"src/**/*"
16-
],
17-
"exclude": [
18-
"node_modules",
19-
"**/*.test.ts"
2016
]
2117
}

0 commit comments

Comments
 (0)