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