@libertytools/libertyjs 1.0.2 → 1.0.3
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/package.json +32 -32
- package/readme.md +363 -358
- package/src/index.js +308 -308
package/readme.md
CHANGED
|
@@ -1,359 +1,364 @@
|
|
|
1
|
-
# LibertyJS
|
|
2
|
-
|
|
3
|
-
A lightweight SDK for the **ER:LC Private Server API**, designed to simplify requests, enforce rate limits, and provide structured error handling.
|
|
4
|
-
|
|
5
|
-
---
|
|
6
|
-
|
|
7
|
-
## Features
|
|
8
|
-
|
|
9
|
-
* Automatic **rate limit handling** (GET + POST)
|
|
10
|
-
* Built-in **API key validation**
|
|
11
|
-
* **403 fail-safe** (halts after repeated failures)
|
|
12
|
-
* Optional **webhook integration**
|
|
13
|
-
* Structured **error responses**
|
|
14
|
-
* Automatic **JSON handling for POST requests**
|
|
15
|
-
* Built-in **command validation + argument checking**
|
|
16
|
-
|
|
17
|
-
---
|
|
18
|
-
|
|
19
|
-
##
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
```
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
*
|
|
87
|
-
*
|
|
88
|
-
*
|
|
89
|
-
*
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
```
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
```
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
```
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
*
|
|
192
|
-
*
|
|
193
|
-
*
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
```
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
```
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
```
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
```
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
```
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
*
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
1
|
+
# LibertyJS
|
|
2
|
+
|
|
3
|
+
A lightweight SDK for the **ER:LC Private Server API**, designed to simplify requests, enforce rate limits, and provide structured error handling.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## Features
|
|
8
|
+
|
|
9
|
+
* Automatic **rate limit handling** (GET + POST)
|
|
10
|
+
* Built-in **API key validation**
|
|
11
|
+
* **403 fail-safe** (halts after repeated failures)
|
|
12
|
+
* Optional **webhook integration**
|
|
13
|
+
* Structured **error responses**
|
|
14
|
+
* Automatic **JSON handling for POST requests**
|
|
15
|
+
* Built-in **command validation + argument checking**
|
|
16
|
+
|
|
17
|
+
---
|
|
18
|
+
|
|
19
|
+
## Install using npm
|
|
20
|
+
```bash
|
|
21
|
+
npm i @libertytools/libertyjs
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Importing
|
|
25
|
+
|
|
26
|
+
```js
|
|
27
|
+
import LibertyJS from "@libertytools/libertyjs";
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
---
|
|
31
|
+
|
|
32
|
+
## Initialization
|
|
33
|
+
|
|
34
|
+
```js
|
|
35
|
+
const LJS = new LibertyJS({
|
|
36
|
+
SERVER_KEY: process.env.SERVER_KEY,
|
|
37
|
+
PRIVATE_SERVER_API: "https://api.policeroleplay.community/v2/", // optional
|
|
38
|
+
WEBHOOK_URL: process.env.WEBHOOK_URL, // optional
|
|
39
|
+
WEBHOOK_TOKEN: process.env.WEBHOOK_TOKEN // required if WEBHOOK_URL is used
|
|
40
|
+
});
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
---
|
|
44
|
+
|
|
45
|
+
## Configuration Options
|
|
46
|
+
|
|
47
|
+
| Option | Required | Description |
|
|
48
|
+
| -------------------- | -------- | ------------------------------------- |
|
|
49
|
+
| `SERVER_KEY` | ✅ | ER:LC private server API key |
|
|
50
|
+
| `PRIVATE_SERVER_API` | ❌ | Base API URL (defaults to PRC v2) |
|
|
51
|
+
| `WEBHOOK_URL` | ❌ | Webhook base URL |
|
|
52
|
+
| `WEBHOOK_TOKEN` | ⚠️ | Required if `WEBHOOK_URL` is provided |
|
|
53
|
+
|
|
54
|
+
---
|
|
55
|
+
|
|
56
|
+
# Methods
|
|
57
|
+
|
|
58
|
+
---
|
|
59
|
+
|
|
60
|
+
## `getPrivateServerAPI(options, includeInvalid?)`
|
|
61
|
+
|
|
62
|
+
Fetch data from the ER:LC Private Server API.
|
|
63
|
+
|
|
64
|
+
### Example
|
|
65
|
+
|
|
66
|
+
```js
|
|
67
|
+
const data = await LJS.getPrivateServerAPI([
|
|
68
|
+
"Players",
|
|
69
|
+
"Staff"
|
|
70
|
+
]);
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
---
|
|
74
|
+
|
|
75
|
+
### Parameters
|
|
76
|
+
|
|
77
|
+
| Parameter | Type | Description |
|
|
78
|
+
| ---------------- | ---------- | ----------------------------------- |
|
|
79
|
+
| `options` | `string[]` | List of data types to request |
|
|
80
|
+
| `includeInvalid` | `boolean` | Include invalid options in response |
|
|
81
|
+
|
|
82
|
+
---
|
|
83
|
+
|
|
84
|
+
### Valid Options
|
|
85
|
+
|
|
86
|
+
* Players
|
|
87
|
+
* Staff
|
|
88
|
+
* JoinLogs
|
|
89
|
+
* Queue
|
|
90
|
+
* KillLogs
|
|
91
|
+
* CommandLogs
|
|
92
|
+
* ModCalls
|
|
93
|
+
* EmergencyCalls
|
|
94
|
+
* Vehicles
|
|
95
|
+
|
|
96
|
+
---
|
|
97
|
+
|
|
98
|
+
### Behavior
|
|
99
|
+
|
|
100
|
+
* Automatically appends `/server`
|
|
101
|
+
* Builds query string internally
|
|
102
|
+
* Filters invalid options
|
|
103
|
+
* Waits automatically if rate-limited
|
|
104
|
+
|
|
105
|
+
---
|
|
106
|
+
|
|
107
|
+
### Example with Invalid Tracking
|
|
108
|
+
|
|
109
|
+
```js
|
|
110
|
+
const res = await LJS.getPrivateServerAPI(
|
|
111
|
+
["Players", "InvalidOption"],
|
|
112
|
+
true
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
console.log(res);
|
|
116
|
+
/*
|
|
117
|
+
{
|
|
118
|
+
data: {...},
|
|
119
|
+
invalidOptions: ["InvalidOption"]
|
|
120
|
+
}
|
|
121
|
+
*/
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
---
|
|
125
|
+
|
|
126
|
+
### Errors
|
|
127
|
+
|
|
128
|
+
#### Invalid Input
|
|
129
|
+
|
|
130
|
+
```js
|
|
131
|
+
{
|
|
132
|
+
error: "invalid_input",
|
|
133
|
+
message: "[LibertyJS.getPrivateServerAPI]: Options must be an array"
|
|
134
|
+
}
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
---
|
|
138
|
+
|
|
139
|
+
#### Forbidden (after 2 failures)
|
|
140
|
+
|
|
141
|
+
```js
|
|
142
|
+
{
|
|
143
|
+
error: "forbidden",
|
|
144
|
+
message: "[LibertyJS]: Received a 403 error 2 times, suspending API calls as the server key may be invalid"
|
|
145
|
+
}
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
---
|
|
149
|
+
|
|
150
|
+
#### API Error
|
|
151
|
+
|
|
152
|
+
```js
|
|
153
|
+
{
|
|
154
|
+
error: "api-error",
|
|
155
|
+
message: "[LibertyJS]: Encountered an error while attempting to fetch <url>",
|
|
156
|
+
apiResponse: { ... }
|
|
157
|
+
}
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
---
|
|
161
|
+
|
|
162
|
+
## `sendPrivateServerCommand(commands)`
|
|
163
|
+
|
|
164
|
+
Send one or more commands to the private server.
|
|
165
|
+
|
|
166
|
+
---
|
|
167
|
+
|
|
168
|
+
### Example
|
|
169
|
+
|
|
170
|
+
```js
|
|
171
|
+
const res = await LJS.sendPrivateServerCommand([
|
|
172
|
+
":h Hello world!",
|
|
173
|
+
":kick PlayerName Spamming"
|
|
174
|
+
]);
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
---
|
|
178
|
+
|
|
179
|
+
### Parameters
|
|
180
|
+
|
|
181
|
+
| Parameter | Type | Description |
|
|
182
|
+
| ---------- | ---------- | ------------------------ |
|
|
183
|
+
| `commands` | `string[]` | Array of command strings |
|
|
184
|
+
|
|
185
|
+
---
|
|
186
|
+
|
|
187
|
+
### Behavior
|
|
188
|
+
|
|
189
|
+
* Validates:
|
|
190
|
+
|
|
191
|
+
* Command existence
|
|
192
|
+
* Argument count
|
|
193
|
+
* Ignores:
|
|
194
|
+
|
|
195
|
+
* Invalid commands
|
|
196
|
+
* Incorrect argument usage
|
|
197
|
+
* Sends commands **sequentially**
|
|
198
|
+
* Tracks success/failure per command
|
|
199
|
+
|
|
200
|
+
---
|
|
201
|
+
|
|
202
|
+
### Response
|
|
203
|
+
|
|
204
|
+
```js
|
|
205
|
+
{
|
|
206
|
+
successes: 2,
|
|
207
|
+
failures: 0,
|
|
208
|
+
failureReasons: []
|
|
209
|
+
}
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
---
|
|
213
|
+
|
|
214
|
+
### Failure Example
|
|
215
|
+
|
|
216
|
+
```js
|
|
217
|
+
{
|
|
218
|
+
successes: 1,
|
|
219
|
+
failures: 1,
|
|
220
|
+
failureReasons: [
|
|
221
|
+
{
|
|
222
|
+
command: ":kick Player",
|
|
223
|
+
apiResponse: { ... }
|
|
224
|
+
}
|
|
225
|
+
]
|
|
226
|
+
}
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
---
|
|
230
|
+
|
|
231
|
+
### Errors
|
|
232
|
+
|
|
233
|
+
#### Invalid Input
|
|
234
|
+
|
|
235
|
+
```js
|
|
236
|
+
{
|
|
237
|
+
error: "invalid_input",
|
|
238
|
+
message: "[LibertyJS.sendPrivateServerCommand]: Options must be an array"
|
|
239
|
+
}
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
#### Empty Array
|
|
243
|
+
|
|
244
|
+
```js
|
|
245
|
+
{
|
|
246
|
+
error: "invalid_input",
|
|
247
|
+
message: "[LibertyJS.sendPrivateServerCommand]: Options must have at least one item"
|
|
248
|
+
}
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
---
|
|
252
|
+
|
|
253
|
+
# Webhook API
|
|
254
|
+
|
|
255
|
+
> Webhooks are accessed via a **nested object**, not top-level methods.
|
|
256
|
+
|
|
257
|
+
---
|
|
258
|
+
|
|
259
|
+
## `webhook.status()`
|
|
260
|
+
|
|
261
|
+
Check webhook health.
|
|
262
|
+
|
|
263
|
+
### Example
|
|
264
|
+
|
|
265
|
+
```js
|
|
266
|
+
const res = await LJS.webhook.status();
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
---
|
|
270
|
+
|
|
271
|
+
### Errors
|
|
272
|
+
|
|
273
|
+
#### Webhook Disabled
|
|
274
|
+
|
|
275
|
+
```js
|
|
276
|
+
{
|
|
277
|
+
error: "webhook_disabled",
|
|
278
|
+
message: "[LibertyJS.webhook.status]: Webhook is not configured"
|
|
279
|
+
}
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
---
|
|
283
|
+
|
|
284
|
+
## `webhook.events()`
|
|
285
|
+
|
|
286
|
+
Fetch webhook events.
|
|
287
|
+
|
|
288
|
+
### Example
|
|
289
|
+
|
|
290
|
+
```js
|
|
291
|
+
const events = await LJS.webhook.events();
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
---
|
|
295
|
+
|
|
296
|
+
### Errors
|
|
297
|
+
|
|
298
|
+
#### Webhook Disabled
|
|
299
|
+
|
|
300
|
+
```js
|
|
301
|
+
{
|
|
302
|
+
error: "webhook_disabled",
|
|
303
|
+
message: "[LibertyJS.webhook.events]: Webhook is not configured"
|
|
304
|
+
}
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
---
|
|
308
|
+
|
|
309
|
+
# Internal Behavior
|
|
310
|
+
|
|
311
|
+
---
|
|
312
|
+
|
|
313
|
+
## Rate Limiting
|
|
314
|
+
|
|
315
|
+
Tracked separately for GET and POST:
|
|
316
|
+
|
|
317
|
+
```js
|
|
318
|
+
{
|
|
319
|
+
get: { limit, remaining, reset },
|
|
320
|
+
post: { limit, remaining, reset }
|
|
321
|
+
}
|
|
322
|
+
```
|
|
323
|
+
|
|
324
|
+
---
|
|
325
|
+
|
|
326
|
+
### Details
|
|
327
|
+
|
|
328
|
+
* Uses headers:
|
|
329
|
+
|
|
330
|
+
* `X-RateLimit-Limit`
|
|
331
|
+
* `X-RateLimit-Remaining`
|
|
332
|
+
* `X-RateLimit-Reset`
|
|
333
|
+
* Automatically delays requests when:
|
|
334
|
+
|
|
335
|
+
```
|
|
336
|
+
remaining === 0 && currentTime < reset
|
|
337
|
+
```
|
|
338
|
+
|
|
339
|
+
* Logs:
|
|
340
|
+
|
|
341
|
+
```
|
|
342
|
+
[LibertyJS]: Rate limited (GET/POST). Waiting Xs
|
|
343
|
+
```
|
|
344
|
+
|
|
345
|
+
---
|
|
346
|
+
|
|
347
|
+
## Forbidden Protection
|
|
348
|
+
|
|
349
|
+
After **2 consecutive `403` responses**:
|
|
350
|
+
|
|
351
|
+
* All future API calls are blocked
|
|
352
|
+
* Prevents invalid API key spam
|
|
353
|
+
|
|
354
|
+
---
|
|
355
|
+
|
|
356
|
+
## Request Handling (`#fetchAPI`)
|
|
357
|
+
|
|
358
|
+
Handles:
|
|
359
|
+
|
|
360
|
+
* Authentication headers (`server-key`)
|
|
361
|
+
* JSON parsing (safe fallback to `null`)
|
|
362
|
+
* Rate limit tracking
|
|
363
|
+
* Error normalization
|
|
359
364
|
* Automatic JSON stringification for POST bodies
|