abuseipdb-client 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,16 @@
1
+ Copyright (c) 2022 Arthur Melo <contact@arthurmelo.com>.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
4
+ documentation files (the "Software"), to deal in the Software without restriction, including without
5
+ limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
6
+ Software, and to permit persons to whom the Software is furnished to do so, subject to the following
7
+ conditions:
8
+
9
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions
10
+ of the Software.
11
+
12
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
13
+ TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
14
+ THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
15
+ CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
16
+ DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,189 @@
1
+ <br>
2
+
3
+ <p align="center">
4
+ <img src="./public/logo.svg" height="140">
5
+ </p>
6
+ <h1 align="center">
7
+ abuseipdb-client
8
+ </h1>
9
+
10
+ ![License](https://img.shields.io/github/license/arthur-melo/abuseipdb-client)
11
+
12
+ Unofficial [AbuseIPDB](https://www.abuseipdb.com/) Node.js client library.
13
+
14
+ ## Features
15
+
16
+ - Implements [API v2](https://docs.abuseipdb.com/)
17
+ - Built with [TypeScript](https://www.typescriptlang.org/)
18
+ - Runtime type checking (Using [Zod](https://zod.dev/))
19
+ - Promise API
20
+ - ESM and CJS builds
21
+
22
+ ## Table of Contents
23
+
24
+ 1. [Installation](#installation)
25
+ 2. [Usage](#usage)
26
+ 3. [About](#about)
27
+ 4. [API](#api)
28
+ 5. [Examples](#examples)
29
+ 6. [Running tests](#running-tests)
30
+ 7. [Browser Support](#browser-support)
31
+ 8. [License](#license)
32
+
33
+ ## Installation
34
+
35
+ ```bash
36
+ $ npm install -S abuseipdb-client
37
+ ```
38
+
39
+ ## Usage
40
+
41
+ ```typescript
42
+ import { AbuseIPDBClient } from 'abuseipdb-client';
43
+
44
+ const client = new AbuseIPDBClient('API_KEY');
45
+
46
+ const response = await client.check('127.0.0.1', { maxAgeInDays: 15 });
47
+
48
+ console.log(response);
49
+ ```
50
+
51
+ <details open>
52
+ <summary>Output</summary>
53
+
54
+ ```typescript
55
+ {
56
+ headers: {
57
+ url: 'https://api.abuseipdb.com/api/v2/check?ipAddress=127.0.0.1&maxAgeInDays=15',
58
+ status: 200,
59
+ statusText: 'OK',
60
+ 'x-ratelimit-limit': '3000',
61
+ 'x-ratelimit-remaining': '2999'
62
+ },
63
+ result: {
64
+ data: {
65
+ ipAddress: '127.0.0.1',
66
+ isPublic: false,
67
+ ipVersion: 4,
68
+ // ...
69
+ }
70
+ }
71
+ }
72
+ ```
73
+
74
+ </details>
75
+
76
+ ## About
77
+
78
+ This library wraps API responses into a single object, providing a standard structure.
79
+
80
+ ```typescript
81
+ const response = await client.check('127.0.0.1');
82
+
83
+ const { headers, result, error } = response;
84
+ ```
85
+
86
+ The `headers` structure contains the [AbuseIPDB HTTP headers](https://docs.abuseipdb.com/#api-daily-rate-limits) plus some of the [Fetch Response](https://developer.mozilla.org/en-US/docs/Web/API/Response) information.
87
+
88
+ ```typescript
89
+ headers: {
90
+ url: string;
91
+ status: string;
92
+ statusText: string;
93
+ 'x-ratelimit-limit': string;
94
+ 'x-ratelimit-remaining': string;
95
+ 'retry-after'?: string;
96
+ 'x-ratelimit-reset'?: string;
97
+ }
98
+ ```
99
+
100
+ The `error` structure wraps a [JSON-API compliant object](https://jsonapi.org/format/#error-objects), as defined by the [docs](https://docs.abuseipdb.com/#error-handling).
101
+
102
+ ```typescript
103
+ error?: {
104
+ errors?: [
105
+ {
106
+ detail?: string;
107
+ status?: number;
108
+ source?: {
109
+ parameter: string
110
+ }
111
+ }
112
+ ]
113
+ }
114
+ ```
115
+
116
+ The `result` structure wraps the API endpoint response.
117
+
118
+ ```typescript
119
+ result?: APIResponse<T>
120
+ ```
121
+
122
+ The `headers` object is always populated with the data returned from the API. `result` and `error` are <u>exclusive</u>, either one of them is defined for a given response.
123
+
124
+ ```typescript
125
+ const response = await client.check('127.0.0.1');
126
+
127
+ const { headers, result, error } = response;
128
+
129
+ if (error) {
130
+ // API returned some error message.
131
+ console.log(error);
132
+ }
133
+
134
+ if (result) {
135
+ // API returned a result response.
136
+ console.log(result);
137
+ }
138
+
139
+ // Headers are defined either way.
140
+ console.log(headers);
141
+ ```
142
+
143
+ A more detailed explanation can be found at: [examples/abstraction.ts](https://github.com/arthur-melo/abuseipdb-client/examples/abstraction.ts).
144
+
145
+ ## API
146
+
147
+ See [API Docs](https://arthur-melo.github.io/abuseipdb-client/)
148
+
149
+ ## Examples
150
+
151
+ See [Examples](https://github.com/arthur-melo/abuseipdb-client/examples/)
152
+
153
+ ## Running tests
154
+
155
+ In order to run tests, you will need to create an API Key from AbuseIPDB's dashboard.
156
+
157
+ Then, clone this repo:
158
+
159
+ ```bash
160
+ $ git clone https://github.com/arthur-melo/abuseipdb-client
161
+ ```
162
+
163
+ Copy the `.env.example` file to `.env`, modifying it with your generated API Key.
164
+
165
+ Install dependencies:
166
+
167
+ ```bash
168
+ $ npm install
169
+ ```
170
+
171
+ And run the tests:
172
+
173
+ ```bash
174
+ $ npm run test
175
+ ```
176
+
177
+ ## Browser Support
178
+
179
+ This project was built using libraries that allow browser usage, but as of this commit, AbuseIPDB doesn't support [CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS).
180
+
181
+ This limits how you can interact with the API, given that you need a proxy server in order to contact the service.
182
+
183
+ I'm not generating browser bundles at the moment, but I decided to keep the supporting code nevertheless.
184
+
185
+ I'll keep an eye whether the team behind AbuseIPDB adds support for this use case in the future.
186
+
187
+ ## License
188
+
189
+ abuseipdb-client is released under the [MIT license](https://github.com/arthur-melo/abuseipdb-client/LICENSE).