@cybearl/cypack 1.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/backend.d.ts ADDED
@@ -0,0 +1,1193 @@
1
+ import pino from 'pino';
2
+ import { NextApiRequest, NextApiResponse } from 'next';
3
+ import { Session } from 'next-auth';
4
+
5
+ /**
6
+ * The type of the benchmark function result.
7
+ */
8
+ type BenchmarkResult = {
9
+ operationsPerSecond: number;
10
+ avgExecutionTime: number;
11
+ operations: number;
12
+ };
13
+ /**
14
+ * An object containing multiple benchmark results, ordered by functions.
15
+ */
16
+ type BenchmarkResults = {
17
+ [fn: string]: BenchmarkResult;
18
+ };
19
+ /**
20
+ * A class that provides a simple way to benchmark functions.
21
+ */
22
+ declare class Bench {
23
+ /**
24
+ * The duration of the benchmark in milliseconds.
25
+ */
26
+ benchmarkDuration: number;
27
+ /**
28
+ * Stores the results of the benchmark.
29
+ */
30
+ results: BenchmarkResults;
31
+ /**
32
+ * Creates a new benchmarking instance.
33
+ * @param benchmarkDuration The duration of the benchmark in milliseconds (optional, defaults to 256ms).
34
+ */
35
+ constructor(benchmarkDuration?: number);
36
+ /**
37
+ * The main headless benchmarking function.
38
+ *
39
+ * Does not print anything to the console but returns the number of iterations per second
40
+ * and the logs themselves for further formatting, such as console coloring depending on the results.
41
+ * @param fn The function to run.
42
+ * @param name The name of the function to run.
43
+ * @returns The benchmark result (operations per second, average execution time, and total operations).
44
+ */
45
+ benchmark: (fn: () => unknown, name: string) => void;
46
+ /**
47
+ * Formats and prints multiple benchmark results.
48
+ * @param category The category of the benchmark results (optional, defaults to `RESULTS`).
49
+ * @param clear Whether to clear the results object for the next benchmark (optional, defaults to `true`).
50
+ */
51
+ print: (category?: string, clear?: boolean) => void;
52
+ }
53
+
54
+ /**
55
+ * The type for the CGAS status string, it can either be:
56
+ * - `enabled`: The application is enabled and available to the public.
57
+ * - `disabled`: The application is disabled and not available to the public.
58
+ * - `in-maintenance`: The application is in maintenance mode and not available to the public.
59
+ * - `in-development`: The application is in development mode and not available to the public.
60
+ */
61
+ type CGASStatusString = "enabled" | "disabled" | "in-maintenance" | "in-development"
62
+
63
+ /**
64
+ * The Cybearl General API System (CGAS) status response.
65
+ *
66
+ * About the status of the application (allows to enable/disable the application),
67
+ * it can either be:
68
+ * - `enabled`: The application is enabled and available to the public.
69
+ * - `disabled`: The application is disabled and not available to the public.
70
+ * - `in-maintenance`: The application is in maintenance mode and not available to the public.
71
+ * - `in-development`: The application is in development mode and not available to the public.
72
+ */
73
+ type CGASStatus = {
74
+ status: CGASStatusString
75
+ marker: string
76
+ timestamp: string
77
+ version: {
78
+ raw: string
79
+ formatted: `v${string}` | "unavailable"
80
+ }
81
+ message: string
82
+ }
83
+
84
+ /**
85
+ * Returns the formatted status of the application based on the different parameters.
86
+ * This function should be executed within the CGAS API endpoint to return the status.
87
+ * @param status The status of the application.
88
+ * @param marker The marker of the application.
89
+ * @param version The version of the application (optional).
90
+ * @param message The message to display (optional).
91
+ * @param markerOnly Whether to only return the marker (optional, defaults to false).
92
+ * @returns The formatted status of the application.
93
+ */
94
+ declare function generateCGASStatus(status: CGASStatusString, marker: string, version: string | undefined, message?: string, markerOnly?: boolean): CGASStatus | string;
95
+
96
+ /**
97
+ * A type for a single bit (`0` or `1`).
98
+ */
99
+ type Bit = 0 | 1;
100
+ /**
101
+ * A type for the endianness of a buffer.
102
+ */
103
+ type Endianness = "LE" | "BE";
104
+ /**
105
+ * A type for the encoding of a string.
106
+ */
107
+ type StringEncoding = "utf8" | "hex";
108
+ /**
109
+ * `CyBuffer` is a helper class that indirectly extends the Uint8Array class with additional methods
110
+ * similar to the Buffer class in Node.js, while also providing multiple utility methods
111
+ * linked to advanced cryptography and binary data manipulation.
112
+ */
113
+ declare class CyBuffer {
114
+ /**
115
+ * The platform's endianness which all related methods will use by default,
116
+ * in order to normalize the endianness parameter.
117
+ */
118
+ readonly platformEndianness: Endianness;
119
+ /**
120
+ * The ArrayBuffer instance referenced by the buffer.
121
+ */
122
+ readonly arrayBuffer: ArrayBuffer;
123
+ /**
124
+ * The Uint8Array instance that acts as a bridge between the buffer and the array buffer.
125
+ */
126
+ readonly array: Uint8Array;
127
+ /**
128
+ * The offset in bytes of the buffer.
129
+ */
130
+ readonly offset: number;
131
+ /**
132
+ * The length in bytes of the buffer.
133
+ */
134
+ readonly length: number;
135
+ /**
136
+ * Creates a new `CyBuffer` instance based on a length or an input.
137
+ * @param length The length of the buffer to create.
138
+ * @param endianness The endianness to use (optional, defaults to the platform's endianness).
139
+ * @param options The options to use (optional).
140
+ * - `arrayBuffer`: The array buffer to use.
141
+ * - `offset`: The offset in bytes to start reading from (optional, defaults to 0).
142
+ * - `length`: The length in bytes to read (optional, defaults to the input length).
143
+ */
144
+ constructor(length: number, options?: {
145
+ arrayBuffer: ArrayBuffer;
146
+ offset?: number;
147
+ length?: number;
148
+ });
149
+ /**
150
+ * ============
151
+ * SIGNATURES
152
+ * ============
153
+ */
154
+ /**
155
+ * Signature for the `[]` operator.
156
+ */
157
+ [index: number]: number;
158
+ /**
159
+ * ==================
160
+ * INTERNAL METHODS
161
+ * ==================
162
+ */
163
+ /**
164
+ * Get the platform endianness.
165
+ * @returns The platform endianness.
166
+ */
167
+ getPlatformEndianness: () => Endianness;
168
+ /**
169
+ * Normalizes the endianness parameter.
170
+ *
171
+ * By default, the extended Uint8Array is written for Little Endian, to keep it consistent,
172
+ * if the platform is big endian the endianness parameter is reversed.
173
+ *
174
+ * - Little endian platform:
175
+ * - `LE` read from left to right.
176
+ * - `BE` read from right to left.
177
+ * - Big endian platform:
178
+ * - `LE` read from right to left.
179
+ * - `BE` read from left to right.
180
+ */
181
+ normalizeEndianness: (endianness: Endianness) => Endianness;
182
+ /**
183
+ * Checks the offset and length for validity.
184
+ *
185
+ * **Note:** This method is used internally by the read/write methods.
186
+ * @param offset The offset to check.
187
+ * @param length The length to check.
188
+ * @throws If the offset or length are invalid.
189
+ * @returns The current buffer instance.
190
+ */
191
+ check: (offset: number, length: number) => this;
192
+ /**
193
+ * ================
194
+ * STATIC METHODS
195
+ * ================
196
+ */
197
+ /**
198
+ * Creates a new `CyBuffer` instance of the specified length, initially filled with zeros.
199
+ * @param length The length of the buffer.
200
+ * @param fillWith The value to initially fill the buffer with (optional).
201
+ * @returns A new `CyBuffer` instance.
202
+ */
203
+ static alloc: (length: number, fillWith?: number) => CyBuffer;
204
+ /**
205
+ * Creates a new `CyBuffer` instance from an hexadecimal string (supports `0x` prefix).
206
+ *
207
+ * **Note:** There is no `endianness` parameter here as there is no concept of word size,
208
+ * the order will simply follow the string.
209
+ * @param value The hexadecimal string to create the buffer from.
210
+ * @returns A new `CyBuffer` instance.
211
+ */
212
+ static fromHexString: (value: string) => CyBuffer;
213
+ /**
214
+ * Creates a new `CyBuffer` instance from an UTF-8 string.
215
+ * @param value The UTF-8 string to create the buffer from.
216
+ * @returns A new `CyBuffer` instance.
217
+ */
218
+ static fromUtf8String: (value: string) => CyBuffer;
219
+ /**
220
+ * Creates a new `CyBuffer` instance from a string using a specific encoding.
221
+ * @param value The string to create the buffer from.
222
+ * @param encoding The encoding to use (optional, defaults to "utf8").
223
+ * @returns A new `CyBuffer` instance.
224
+ */
225
+ static fromString: (value: string, encoding?: StringEncoding) => CyBuffer;
226
+ /**
227
+ * Creates a new `CyBuffer` instance from an array of bits.
228
+ * @param array The array of bits to create the buffer from.
229
+ * @param msbFirst Whether to write the bits from the most significant bit (optional, defaults to `true`).
230
+ * @returns A new `CyBuffer` instance.
231
+ */
232
+ static fromBits: (array: Bit[], msbFirst?: boolean) => CyBuffer;
233
+ /**
234
+ * Creates a new `CyBuffer` instance from a Uint8Array.
235
+ * @param array The Uint8Array to create the buffer from.
236
+ * @returns A new `CyBuffer` instance.
237
+ */
238
+ static fromUint8Array: (array: Uint8Array) => CyBuffer;
239
+ /**
240
+ * Creates a new `CyBuffer` instance from a Uint16Array.
241
+ * @param array The Uint16Array to create the buffer from.
242
+ * @returns A new `CyBuffer` instance.
243
+ */
244
+ static fromUint16Array: (array: Uint16Array) => CyBuffer;
245
+ /**
246
+ * Creates a new `CyBuffer` instance from a Uint32Array.
247
+ * @param array The Uint32Array to create the buffer from.
248
+ * @returns A new `CyBuffer` instance.
249
+ */
250
+ static fromUint32Array: (array: Uint32Array) => CyBuffer;
251
+ /**
252
+ * Creates a new `CyBuffer` instance from a big integer.
253
+ * @param value The big integer to create the buffer from.
254
+ * @param endianness The endianness to use (optional, defaults to the platform's endianness).
255
+ * @returns A new `CyBuffer` instance.
256
+ */
257
+ static fromBigInt: (value: bigint, endianness?: Endianness) => CyBuffer;
258
+ /**
259
+ * Creates a new `CyBuffer` instance from a range of numbers between 0 and 255.
260
+ * @param start The start of the range, inclusive.
261
+ * @param end The end of the range, exclusive.
262
+ * @returns A new `CyBuffer` instance.
263
+ */
264
+ static fromRange: (start: number, end: number) => CyBuffer;
265
+ /**
266
+ * ===========
267
+ * ACCESSORS
268
+ * ===========
269
+ */
270
+ /**
271
+ * The proxy that allows to access/assign values via the [] operator.
272
+ * Note that both the getter and setter are safe and never throw.
273
+ * @returns The proxy returned by the constructor.
274
+ */
275
+ private get _proxy();
276
+ /**
277
+ * The symbol iterator of the buffer, allowing to iterate over the buffer
278
+ * using `for..of` loops and returning bytes.
279
+ * @returns A new generator yielding the bytes of the buffer.
280
+ */
281
+ [Symbol.iterator](): Generator<number>;
282
+ /**
283
+ * Iterates over the buffer and yields the index and value of each byte as an array.
284
+ * @returns The generator yielding the index and value of each byte.
285
+ */
286
+ entries(): Generator<[number, number]>;
287
+ /**
288
+ * ===============
289
+ * WRITE METHODS
290
+ * ===============
291
+ */
292
+ /**
293
+ * Writes an hexadecimal string to the buffer (supports `0x` prefix).
294
+ *
295
+ * **Note:** There is no `endianness` parameter here as there is no concept of word size,
296
+ * the order will simply follow the string.
297
+ * @param value The hexadecimal string to write to the buffer.
298
+ * @param offset The offset to start writing at (optional, defaults to 0).
299
+ * @param length The length to write (optional, defaults to the value length).
300
+ * @returns The current buffer instance.
301
+ */
302
+ writeHexString: (value: string, offset?: number, length?: number) => this;
303
+ /**
304
+ * Writes an UTF-8 string to the buffer.
305
+ * @param value The UTF-8 string to write to the buffer.
306
+ * @param offset The offset to start writing at (optional, defaults to 0).
307
+ * @param length The length to write (optional, defaults to the value length).
308
+ * @returns The current buffer instance.
309
+ */
310
+ writeUtf8String: (value: string, offset?: number, length?: number) => this;
311
+ /**
312
+ * Writes a string to the buffer using a specific encoding.
313
+ *
314
+ * **Notes:**
315
+ * - The `hex` encoding supports `0x` prefix but there's no `endianness` parameter here,
316
+ * it follows the order of the string.
317
+ * @param value The string to write to the buffer.
318
+ * @param encoding The encoding to use (optional, defaults to "utf8").
319
+ * @param offset The offset to start writing at (optional, defaults to 0).
320
+ * @param length The length to write (optional, defaults to the value length).
321
+ * @returns The current buffer instance.
322
+ */
323
+ writeString: (value: string, encoding?: StringEncoding, offset?: number, length?: number) => this;
324
+ /**
325
+ * Writes a single bit to the buffer.
326
+ * @param value The value to write (`0` or `1`).
327
+ * @param bitOffset The offset to read from **as a number of bits** (optional, defaults to 0).
328
+ * @param msbFirst Whether to write the bit to the most significant bit (optional, defaults to `true`).
329
+ * @param check Whether to enable the overall check (optional, defaults to `true`).
330
+ * @returns The current buffer instance.
331
+ */
332
+ writeBit: (value: Bit, bitOffset?: number, msbFirst?: boolean, check?: boolean) => this;
333
+ /**
334
+ * Writes a single byte to the buffer.
335
+ * @param value The value to write.
336
+ * @param offset The offset to write to (optional, defaults to 0).
337
+ * @param check Whether to enable the overall check (optional, defaults to `true`).
338
+ * @returns The current buffer instance.
339
+ */
340
+ writeUint8: (value: number, offset?: number, check?: boolean) => this;
341
+ /**
342
+ * **[LITTLE ENDIAN]** Writes a single 16-bit value to the buffer.
343
+ * @param value The value to write.
344
+ * @param offset The offset to write to (optional, defaults to 0).
345
+ * @param verifyAlignment Whether to verify that the offset is aligned to 2 bytes (optional, defaults to `true`).
346
+ * @param check Whether to enable the overall check (optional, defaults to `true`).
347
+ * @returns The current buffer instance.
348
+ */
349
+ writeUint16LE: (value: number, offset?: number, verifyAlignment?: boolean, check?: boolean) => this;
350
+ /**
351
+ * **[BIG ENDIAN]** Writes a single 16-bit value to the buffer.
352
+ * @param value The value to write.
353
+ * @param offset The offset to write to (optional, defaults to 0).
354
+ * @param verifyAlignment Whether to verify that the offset is aligned to 2 bytes (optional, defaults to `true`).
355
+ * @param check Whether to enable the overall check (optional, defaults to `true`).
356
+ * @returns The current buffer instance.
357
+ */
358
+ writeUint16BE: (value: number, offset?: number, verifyAlignment?: boolean, check?: boolean) => this;
359
+ /**
360
+ * Writes a single 16-bit value to the buffer.
361
+ * @param value The value to write.
362
+ * @param offset The offset to write to (optional, defaults to 0).
363
+ * @param endianness The endianness to use (optional, defaults to the platform's endianness).
364
+ * @param verifyAlignment Whether to verify that the offset is aligned to 2 bytes (optional, defaults to `true`).
365
+ * @param check Whether to enable the overall check (optional, defaults to `true`).
366
+ * @returns The current buffer instance.
367
+ */
368
+ writeUint16: (value: number, offset?: number, endianness?: Endianness, verifyAlignment?: boolean, check?: boolean) => this;
369
+ /**
370
+ * **[LITTLE ENDIAN]** Writes a single 32-bit word to the buffer.
371
+ * @param value The value to write.
372
+ * @param offset The offset to write to (optional, defaults to 0).
373
+ * @param verifyAlignment Whether to verify that the offset is aligned to 4 bytes (optional, defaults to `true`).
374
+ * @param check Whether to enable the overall check (optional, defaults to `true`).
375
+ * @returns The current buffer instance.
376
+ */
377
+ writeUint32LE: (value: number, offset?: number, verifyAlignment?: boolean, check?: boolean) => this;
378
+ /**
379
+ * **[BIG ENDIAN]** Writes a single 32-bit word to the buffer.
380
+ * @param value The value to write.
381
+ * @param offset The offset to write to (optional, defaults to 0).
382
+ * @param verifyAlignment Whether to verify that the offset is aligned to 4 bytes (optional, defaults to `true`).
383
+ * @param check Whether to enable the overall check (optional, defaults to `true`).
384
+ * @returns The current buffer instance.
385
+ */
386
+ writeUint32BE: (value: number, offset?: number, verifyAlignment?: boolean, check?: boolean) => this;
387
+ /**
388
+ * Writes a single 32-bit word to the buffer.
389
+ * @param value The value to write.
390
+ * @param offset The offset to write to (optional, defaults to 0).
391
+ * @param endianness The endianness to use (optional, defaults to the platform's endianness).
392
+ * @param verifyAlignment Whether to verify that the offset is aligned to 4 bytes (optional, defaults to `true`).
393
+ * @param check Whether to enable the overall check (optional, defaults to `true`).
394
+ * @returns The current buffer instance.
395
+ */
396
+ writeUint32: (value: number, offset?: number, endianness?: Endianness, verifyAlignment?: boolean, check?: boolean) => this;
397
+ /**
398
+ * Writes an array of bits to the buffer.
399
+ * @param array The array of bits to write to the buffer.
400
+ * @param bitOffset The offset to start writing at **as a number of bits** (optional, defaults to 0).
401
+ * @param bitLength The length to write **as a number of bits** (optional, defaults to the array length).
402
+ * @param msbFirst Whether to write the bits from the most significant bit (optional, defaults to `true`).
403
+ * @returns The current buffer instance.
404
+ */
405
+ writeBits: (array: Bit[], bitOffset?: number, bitLength?: number, msbFirst?: boolean) => this;
406
+ /**
407
+ * Writes a Uint8Array to the buffer.
408
+ * @param array The Uint8Array to write to the buffer.
409
+ * @param offset The offset to start writing at (optional, defaults to 0).
410
+ * @param length The length to write (optional, defaults to the array length).
411
+ * @param arrayOffset The offset to start reading from in the array (optional, defaults to 0).
412
+ * @returns The current buffer instance.
413
+ */
414
+ writeUint8Array: (array: Uint8Array, offset?: number, length?: number, arrayOffset?: number) => this;
415
+ /**
416
+ * Writes a Uint16Array to the buffer.
417
+ * @param array The Uint16Array to write to the buffer.
418
+ * @param offset The offset to start writing at (optional, defaults to 0).
419
+ * @param length The length to write (optional, defaults to the array length).
420
+ * @param arrayOffset The offset to start reading from in the array (optional, defaults to 0).
421
+ * @param endianness The endianness to use (optional, defaults to the platform's endianness).
422
+ * @param verifyAlignment Whether to verify that the offset is aligned to 2 bytes (optional, defaults to `true`).
423
+ * @returns The current buffer instance.
424
+ */
425
+ writeUint16Array: (array: Uint16Array, offset?: number, length?: number, arrayOffset?: number, endianness?: Endianness, verifyAlignment?: boolean) => this;
426
+ /**
427
+ * Writes a Uint32Array to the buffer.
428
+ * @param array The Uint16Array to write to the buffer.
429
+ * @param offset The offset to start writing at (optional, defaults to 0).
430
+ * @param length The length to write (optional, defaults to the array length).
431
+ * @param arrayOffset The offset to start reading from in the array (optional, defaults to 0).
432
+ * @param endianness The endianness to use (optional, defaults to the platform's endianness).
433
+ * @param verifyAlignment Whether to verify that the offset is aligned to 2 bytes (optional, defaults to `true`).
434
+ * @returns The current buffer instance.
435
+ */
436
+ writeUint32Array: (array: Uint32Array, offset?: number, length?: number, arrayOffset?: number, endianness?: Endianness, verifyAlignment?: boolean) => this;
437
+ /**
438
+ * **[LITTLE ENDIAN]** Writes a big integer of any size to the buffer.
439
+ *
440
+ * **No alignment is required, the length is automatically calculated.**
441
+ * @param value The big integer to write to the buffer.
442
+ * @param offset The offset to start writing at (optional, defaults to 0).
443
+ * @param length The length to write (optional, defaults to the array length).
444
+ * @returns The current buffer instance.
445
+ */
446
+ writeBigIntLE: (value: bigint, offset?: number, length?: number) => this;
447
+ /**
448
+ * **[BIG ENDIAN]** Writes a big integer of any size to the buffer.
449
+ *
450
+ * **No alignment is required, the length is automatically calculated.**
451
+ * @param value The big integer to write to the buffer.
452
+ * @param offset The offset to start writing at (optional, defaults to 0).
453
+ * @param length The length to write (optional, defaults to the array length).
454
+ * @returns The current buffer instance.
455
+ */
456
+ writeBigIntBE: (value: bigint, offset?: number, length?: number) => this;
457
+ /**
458
+ * Writes a big integer of any size to the buffer.
459
+ *
460
+ * **No alignment is required, the length is automatically calculated.**
461
+ * @param value The big integer to write to the buffer.
462
+ * @param offset The offset to start writing at (optional, defaults to 0).
463
+ * @param length The length to write (optional, defaults to the array length).
464
+ * @param endianness The endianness to use (optional, defaults to the platform's endianness).
465
+ * @returns The current buffer instance.
466
+ */
467
+ writeBigInt: (value: bigint, offset?: number, length?: number, endianness?: Endianness) => this;
468
+ /**
469
+ * Writes a range of numbers between 0 and 255 to the buffer.
470
+ * @param start The start of the range, inclusive.
471
+ * @param end The end of the range, exclusive.
472
+ * @param offset The offset to start writing at (optional, defaults to 0).
473
+ * @returns The current buffer instance.
474
+ */
475
+ writeRange: (start: number, end: number, offset?: number) => this;
476
+ /**
477
+ * ==============
478
+ * READ METHODS
479
+ * ==============
480
+ *
481
+ * Notes:
482
+ * - All read methods have the capability to disable the overall check.
483
+ * - All of the "endianness sensitive" methods are wrapped within a single
484
+ * method with an optional endianness parameter.
485
+ */
486
+ /**
487
+ * **[LITTLE ENDIAN]** Reads a part of the buffer and returns it as an hexadecimal string (always uppercase).
488
+ * @param offset The offset to start reading from (optional, defaults to 0).
489
+ * @param length The length to read (optional, defaults to the buffer length - offset).
490
+ * @param check Whether to enable the overall check (optional, defaults to `true`).
491
+ * @returns The hexadecimal string.
492
+ */
493
+ readHexStringLE: (offset?: number, length?: number, check?: boolean) => string;
494
+ /**
495
+ * **[BIG ENDIAN]** Reads a part of the buffer and returns it as an hexadecimal string (always uppercase).
496
+ * @param offset The offset to start reading from (optional, defaults to 0).
497
+ * @param length The length to read (optional, defaults to the buffer length - offset).
498
+ * @param check Whether to enable the overall check (optional, defaults to `true`).
499
+ * @returns The hexadecimal string.
500
+ */
501
+ readHexStringBE: (offset?: number, length?: number, check?: boolean) => string;
502
+ /**
503
+ * Reads a part of the buffer and returns it as an hexadecimal string (always uppercase).
504
+ * @param offset The offset to start reading from (optional, defaults to 0).
505
+ * @param length The length to read (optional, defaults to the buffer length - offset).
506
+ * @param endianness The endianness to use (optional, defaults to the platform's endianness).'
507
+ * @param check Whether to enable the overall check (optional, defaults to `true`).
508
+ * @returns The hexadecimal string.
509
+ */
510
+ readHexString: (offset?: number, length?: number, endianness?: Endianness, check?: boolean) => string;
511
+ /**
512
+ * Reads a part of the buffer and returns it as an UTF-8 string.
513
+ * @param offset The offset to start reading from (optional, defaults to 0).
514
+ * @param length The length to read (optional, defaults to the buffer length - offset).
515
+ * @param check Whether to enable the overall check (optional, defaults to `true`).
516
+ * @returns The UTF-8 string.
517
+ */
518
+ readUtf8String: (offset?: number, length?: number, check?: boolean) => string;
519
+ /**
520
+ * Read a single bit from the buffer.
521
+ *
522
+ * **Note:** The offset is in bits, not bytes in contrast to the other methods.
523
+ * @param bitOffset The offset to read from **as a number of bits** (optional, defaults to 0).
524
+ * @param msbFirst Whether to read the bit from the most significant bit (optional, defaults to `true`).
525
+ * @param check Whether to enable the overall check (optional, defaults to `true`).
526
+ * @returns The bit as 0 or 1.
527
+ */
528
+ readBit: (bitOffset?: number, msbFirst?: boolean, check?: boolean) => Bit;
529
+ /**
530
+ * Reads a single byte from the buffer.
531
+ *
532
+ * Equivalent to using the index signature (square bracket notation).
533
+ * @param offset The offset to start reading from (optional, defaults to 0).
534
+ * @param check Whether to enable the overall check (optional, defaults to `true`).
535
+ * @returns The byte as number.
536
+ */
537
+ readUint8: (offset?: number, check?: boolean) => number;
538
+ /**
539
+ * **[LITTLE ENDIAN]** Reads a single 16-bit value from the buffer.
540
+ * @param offset The offset to read from (optional, defaults to 0).
541
+ * @param verifyAlignment Whether to verify that the offset is aligned to 2 bytes (optional, defaults to `true`).
542
+ * @param check Whether to enable the overall check (optional, defaults to `true`).
543
+ * @returns The 16-bit value.
544
+ */
545
+ readUint16LE: (offset?: number, verifyAlignment?: boolean, check?: boolean) => number;
546
+ /**
547
+ * **[BIG ENDIAN]** Reads a single 16-bit value from the buffer.
548
+ * @param offset The offset to read from (optional, defaults to 0).
549
+ * @param verifyAlignment Whether to verify that the offset is aligned to 2 bytes (optional, defaults to `true`).
550
+ * @param check Whether to enable the overall check (optional, defaults to `true`).
551
+ * @returns The 16-bit value.
552
+ */
553
+ readUint16BE: (offset?: number, verifyAlignment?: boolean, check?: boolean) => number;
554
+ /**
555
+ * Reads a single 16-bit value from the buffer.
556
+ * @param offset The offset to read from (optional, defaults to 0).
557
+ * @param endianness Whether to read the value in Little Endian (optional, defaults to `false`).
558
+ * @param verifyAlignment Whether to verify that the offset is aligned to 2 bytes (optional, defaults to `true`).
559
+ * @param check Whether to enable the overall check (optional, defaults to `true`).
560
+ * @returns The 16-bit value.
561
+ */
562
+ readUint16: (offset?: number, endianness?: Endianness, verifyAlignment?: boolean, check?: boolean) => number;
563
+ /**
564
+ * **[LITTLE ENDIAN]** Reads a single 32-bit word from the buffer.
565
+ * @param offset The offset to read from (optional, defaults to 0).
566
+ * @param verifyAlignment Whether to verify that the offset is aligned to 4 bytes (optional, defaults to `true`).
567
+ * @param check Whether to enable the overall check (optional, defaults to `true`).
568
+ */
569
+ readUint32LE: (offset?: number, verifyAlignment?: boolean, check?: boolean) => number;
570
+ /**
571
+ * **[BIG ENDIAN]** Reads a single 32-bit word from the buffer.
572
+ * @param offset The offset to read from (optional, defaults to 0).
573
+ * @param verifyAlignment Whether to verify that the offset is aligned to 4 bytes (optional, defaults to `true`).
574
+ * @param check Whether to enable the overall check (optional, defaults to `true`).
575
+ */
576
+ readUint32BE: (offset?: number, verifyAlignment?: boolean, check?: boolean) => number;
577
+ /**
578
+ * Reads a single 32-bit word from the buffer.
579
+ * @param offset The offset to read from (optional, defaults to 0).
580
+ * @param endianness Whether to read the value in Little Endian (optional, defaults to `false`).
581
+ * @param verifyAlignment Whether to verify that the offset is aligned to 4 bytes (optional, defaults to `true`).
582
+ * @param check Whether to enable the overall check (optional, defaults to `true`).
583
+ */
584
+ readUint32: (offset?: number, endianness?: Endianness, verifyAlignment?: boolean, check?: boolean) => number;
585
+ /**
586
+ * Reads a part of the buffer and return it as a bit array.
587
+ *
588
+ * **Note:** The offset is in bits, not bytes in contrast to the other methods.
589
+ * @param bitOffset The offset to read from **as a number of bits** (optional, defaults to 0).
590
+ * @param bitLength The length to read **as a number of bits** (optional, defaults to the buffer length - offset).
591
+ * @param msbFirst Whether to read the bits from the most significant bit (optional, defaults to `true`).
592
+ * @param check Whether to enable the overall check (optional, defaults to `true`).
593
+ * @returns The bit array.
594
+ */
595
+ readBits: (offset?: number, length?: number, msbFirst?: boolean, check?: boolean) => Bit[];
596
+ /**
597
+ * Reads a part of the buffer and return it as a Uint8Array.
598
+ * @param offset The offset to start reading from (optional, defaults to 0).
599
+ * @param length The length to read (optional, defaults to the buffer length - offset).
600
+ * @param check Whether to enable the overall check (optional, defaults to `true`).
601
+ * @returns The Uint8Array.
602
+ */
603
+ readUint8Array: (offset?: number, length?: number, check?: boolean) => Uint8Array;
604
+ /**
605
+ * Reads a part of the buffer and return it as a Uint16Array.
606
+ * @param offset The offset to start reading from (optional, defaults to 0).
607
+ * @param length The length to read (optional, defaults to the buffer length - offset).
608
+ * @returns The Uint16Array.
609
+ */
610
+ readUint16Array: (offset?: number, length?: number, check?: boolean) => Uint16Array;
611
+ /**
612
+ * Reads a part of the buffer and return it as a Uint32Array.
613
+ * @param offset The offset to start reading from (optional, defaults to 0).
614
+ * @param length The length to read (optional, defaults to the buffer length - offset).
615
+ * @returns The Uint32Array.
616
+ */
617
+ readUint32Array: (offset?: number, length?: number, check?: boolean) => Uint32Array;
618
+ /**
619
+ * **[LITTLE ENDIAN]** Reads a certain amount of bytes from the buffer and converts it into a big integer.
620
+ * @param offset The offset to start reading from (optional, defaults to 0).
621
+ * @param length The length to read (optional, defaults to the buffer length - offset).
622
+ * @returns The big integer.
623
+ */
624
+ readBigIntLE: (offset?: number, length?: number, check?: boolean) => bigint;
625
+ /**
626
+ * **[BIG ENDIAN]** Reads a certain amount of bytes from the buffer and converts it into a big integer.
627
+ * @param offset The offset to start reading from (optional, defaults to 0).
628
+ * @param length The length to read (optional, defaults to the buffer length - offset).
629
+ * @returns The big integer.
630
+ */
631
+ readBigIntBE: (offset?: number, length?: number, check?: boolean) => bigint;
632
+ /**
633
+ * Reads a certain amount of bytes from the buffer and converts it into a big integer.
634
+ * @param offset The offset to start reading from (optional, defaults to 0).
635
+ * @param length The length to read (optional, defaults to the buffer length - offset).
636
+ * @param endianness Whether to read the value in Little Endian (optional, defaults to `false`).
637
+ * @returns The big integer.
638
+ */
639
+ readBigInt: (offset?: number, length?: number, endianness?: Endianness, check?: boolean) => bigint;
640
+ /**
641
+ * ====================
642
+ * CONVERSION METHODS
643
+ * ====================
644
+ */
645
+ /**
646
+ * Converts the buffer into an hexadecimal string (always uppercase).
647
+ * @param prefix Whether to prefix the hexadecimal string with `0x` (optional, defaults to `false`).
648
+ * @param endianness The endianness to use (optional, defaults to the platform's endianness).
649
+ * @returns The hexadecimal string.
650
+ */
651
+ toHexString: (prefix?: boolean, endianness?: Endianness) => string;
652
+ /**
653
+ * Converts the buffer into an UTF-8 string.
654
+ * @returns The UTF-8 string.
655
+ */
656
+ toUtf8String: () => string;
657
+ /**
658
+ * Converts the buffer into a string representation (hex string is always uppercase).
659
+ * @param encoding The encoding to use (optional, defaults to "hex").
660
+ * @param hexPrefix Whether to prefix the hexadecimal string with `0x` (optional, defaults to `false`).
661
+ * @returns The string representation of the buffer.
662
+ */
663
+ toString: (encoding?: "utf8" | "hex", hexPrefix?: boolean) => string;
664
+ /**
665
+ * Converts the buffer into a bit array.
666
+ * @param msbFirst Whether to read the bits from the most significant bit (optional, defaults to `true`).
667
+ * @returns The bit array.
668
+ */
669
+ toBits: (msbFirst?: boolean) => Bit[];
670
+ /**
671
+ * Converts the buffer into a Uint8Array.
672
+ * @returns The Uint8Array.
673
+ */
674
+ toUint8Array: () => Uint8Array;
675
+ /**
676
+ * Converts the buffer into a Uint16Array.
677
+ * @returns The Uint16Array.
678
+ */
679
+ toUint16Array: () => Uint16Array;
680
+ /**
681
+ * Converts the buffer into a Uint32Array.
682
+ * @returns The Uint32Array.
683
+ */
684
+ toUint32Array: () => Uint32Array;
685
+ /**
686
+ * Converts the buffer into a big integer.
687
+ * @param endianness The endianness to use (optional, defaults to the platform's endianness).
688
+ * @returns The big integer.
689
+ */
690
+ toBigInt: (endianness?: Endianness) => bigint;
691
+ /**
692
+ * ===============
693
+ * CHECK METHODS
694
+ * ===============
695
+ */
696
+ /**
697
+ * Checks if the current buffer is equal to the specified buffer.
698
+ * @param buffer The buffer to compare to.
699
+ * @returns Whether the buffers are equal.
700
+ */
701
+ equals: (buffer: CyBuffer) => boolean;
702
+ /**
703
+ * Check if the buffer is empty.
704
+ * @returns Whether the buffer is empty.
705
+ */
706
+ isEmpty: () => boolean;
707
+ /**
708
+ * Check if the buffer is full.
709
+ * @returns Whether the buffer is full.
710
+ */
711
+ isFull: () => boolean;
712
+ /**
713
+ * ====================
714
+ * RANDOMNESS METHODS
715
+ * ====================
716
+ */
717
+ /**
718
+ * Randomly fills the buffer with bytes.
719
+ *
720
+ * **WARNING:** This method is not safe for cryptographic contexts, use the `safeRandomFill` method
721
+ * when security is a concern.
722
+ * @param offset The offset to start filling at (optional, defaults to 0).
723
+ * @param length The length to fill (optional, defaults to the buffer length - offset).
724
+ */
725
+ randomFill: (offset?: number, length?: number) => void;
726
+ /**
727
+ * Randomly fills the buffer with bytes.
728
+ *
729
+ * **WARNING:** This method is safe for cryptographic contexts, but is far slower than the `randomFill` method,
730
+ * use this method when security is a concern. Uses the Node.js `crypto.randomFillSync` method.
731
+ * @param offset The offset to start filling at (optional, defaults to 0).
732
+ * @param length The length to fill (optional, defaults to the buffer length - offset).
733
+ */
734
+ safeRandomFill: (offset?: number, length?: number) => Uint8Array<ArrayBufferLike>;
735
+ /**
736
+ * =================
737
+ * UTILITY METHODS
738
+ * =================
739
+ */
740
+ /**
741
+ * Copies the buffer into a new buffer.
742
+ * @param offset The offset to start copying at (optional, defaults to 0).
743
+ * @param length The length to copy (optional, defaults to the buffer length).
744
+ * @returns A new buffer containing the copied data.
745
+ */
746
+ copy: (offset?: number, length?: number) => CyBuffer;
747
+ /**
748
+ * Returns a new buffer that references the same memory as the original buffer,
749
+ * A lot more efficient than copying it (~ 4 times faster than the `copy` method).
750
+ * @param offset The start offset (optional, defaults to 0).
751
+ * @param end The end offset (optional, defaults to the buffer length).
752
+ * @returns The subarray pointing to the same memory.
753
+ */
754
+ subarray: (offset?: number, length?: number) => CyBuffer;
755
+ /**
756
+ * Swaps the order of the buffer.
757
+ * @param offset The offset to start swapping at (optional, defaults to 0).
758
+ * @param length The length to swap (optional, defaults to the buffer length).
759
+ * @param wordLength The length per word (optional, defaults to 4).
760
+ * @returns The current buffer instance.
761
+ */
762
+ swap: (offset?: number, length?: number, wordLength?: number) => this;
763
+ /**
764
+ * Reverses a part of the buffer.
765
+ * @param offset The offset to start reversing at (optional, defaults to 0).
766
+ * @param length The length to reverse (optional, defaults to the buffer length).
767
+ * @returns The current buffer instance.
768
+ */
769
+ partialReverse: (offset?: number, length?: number) => this;
770
+ /**
771
+ * Reverses the entire buffer.
772
+ * @returns The current buffer instance.
773
+ */
774
+ reverse: () => this;
775
+ /**
776
+ * Rotates the buffer to the left.
777
+ * @returns The current buffer instance.
778
+ */
779
+ rotateLeft: () => this;
780
+ /**
781
+ * Rotates the buffer to the right.
782
+ * @returns The current buffer instance.
783
+ */
784
+ rotateRight: () => this;
785
+ /**
786
+ * Shifts the buffer to the left, filling the empty space with zeros.
787
+ * @param offset The offset to start shifting at (optional, defaults to 0).
788
+ * @param length The length to shift (optional, defaults to the buffer length).
789
+ * @param shift The number of bytes to shift (optional, defaults to 1).
790
+ * @returns The current buffer instance.
791
+ */
792
+ shiftLeft: (offset?: number, length?: number, shift?: number) => this;
793
+ /**
794
+ * Shifts the buffer to the right, filling the empty spaces with zeros.
795
+ * @param offset The offset to start shifting at (optional, defaults to 0).
796
+ * @param length The length to shift (optional, defaults to the buffer length).
797
+ * @param shift The number of bytes to shift (optional, defaults to 1).
798
+ * @returns The current buffer instance.
799
+ */
800
+ shiftRight: (offset?: number, length?: number, shift?: number) => this;
801
+ /**
802
+ * Fills the buffer with a specified value.
803
+ * @param value The value to fill the buffer with.
804
+ * @param offset The offset to start filling at (optional, defaults to 0).
805
+ * @param length The length to fill (optional, defaults to the buffer length).
806
+ * @returns The current buffer instance.
807
+ */
808
+ fill: (value: number, offset?: number, length?: number) => this;
809
+ /**
810
+ * Clears the buffer by filling it with zeros.
811
+ * @param offset The offset to start clearing at (optional, defaults to 0).
812
+ * @param length The length to clear (optional, defaults to the buffer length).
813
+ * @returns The current buffer instance.
814
+ */
815
+ clear: (offset?: number, length?: number) => this;
816
+ }
817
+
818
+ /**
819
+ * The type definition for an error object.
820
+ */
821
+ type ErrorObj = {
822
+ status: number
823
+ name: string
824
+ message: string
825
+ data: unknown
826
+ }
827
+
828
+ /**
829
+ * Formats an `ErrorObj` into a standard error sent back by an API endpoint.
830
+ */
831
+ declare function formatErrorResponse(error: ErrorObj, customMessage?: string, additionalData?: unknown): {
832
+ success: boolean;
833
+ message: string;
834
+ error: ErrorObj;
835
+ };
836
+ /**
837
+ * Formats an `ErrorObj` and stringifies it for it to be supported by the `Error` class.
838
+ * @param error The `ErrorObj` object to format.
839
+ * @param message Replaces the standard error message with a custom one (optional).
840
+ * @param additionalData Additional data to include in the error (optional).
841
+ * @returns The formatted error string.
842
+ */
843
+ declare function stringifyError(error: ErrorObj, message?: string, additionalData?: unknown): string;
844
+ /**
845
+ * Contains all the standard available errors for the application, it serves as a base
846
+ * to extend with your custom errors.
847
+ *
848
+ * The recommended way is to create an `AppErrors` object that extends this one, preferably
849
+ * at a place similar to `lib/utils/errors.ts`:
850
+ * ```typescript
851
+ * import { BaseErrors } from "@cybearl/cypack"
852
+ *
853
+ * export const AppErrors = {
854
+ * ...BaseErrors,
855
+ * // Add your custom errors here
856
+ * }
857
+ * ```
858
+ */
859
+ declare const BaseErrors: {
860
+ readonly BAD_REQUEST: {
861
+ readonly status: 400;
862
+ readonly name: "BadRequest";
863
+ readonly message: "Bad request.";
864
+ readonly data: null;
865
+ };
866
+ readonly UNAUTHORIZED: {
867
+ readonly status: 401;
868
+ readonly name: "Unauthorized";
869
+ readonly message: "Unauthorized.";
870
+ readonly data: null;
871
+ };
872
+ readonly PAYMENT_REQUIRED: {
873
+ readonly status: 402;
874
+ readonly name: "PaymentRequired";
875
+ readonly message: "Payment required.";
876
+ readonly data: null;
877
+ };
878
+ readonly FORBIDDEN: {
879
+ readonly status: 403;
880
+ readonly name: "Forbidden";
881
+ readonly message: "Forbidden.";
882
+ readonly data: null;
883
+ };
884
+ readonly NOT_FOUND: {
885
+ readonly status: 404;
886
+ readonly name: "NotFound";
887
+ readonly message: "Not found.";
888
+ readonly data: null;
889
+ };
890
+ readonly METHOD_NOT_ALLOWED: {
891
+ readonly status: 405;
892
+ readonly name: "MethodNotAllowed";
893
+ readonly message: "Method not allowed.";
894
+ readonly data: null;
895
+ };
896
+ readonly REQUEST_TIMEOUT: {
897
+ readonly status: 408;
898
+ readonly name: "RequestTimeout";
899
+ readonly message: "Request timed out.";
900
+ readonly data: null;
901
+ };
902
+ readonly CONFLICT: {
903
+ readonly status: 409;
904
+ readonly name: "Conflict";
905
+ readonly message: "Conflict.";
906
+ readonly data: null;
907
+ };
908
+ readonly INTERNAL_SERVER_ERROR: {
909
+ readonly status: 500;
910
+ readonly name: "InternalServerError";
911
+ readonly message: "Internal server error.";
912
+ readonly data: null;
913
+ };
914
+ readonly BACKEND_FUNCTION_RUNNING_ON_CLIENT: {
915
+ readonly status: 500;
916
+ readonly name: "BackendFunctionRunningOnClient";
917
+ readonly message: "A function reserved for the backend is running on the client.";
918
+ readonly data: null;
919
+ };
920
+ readonly NOT_IMPLEMENTED: {
921
+ readonly status: 501;
922
+ readonly name: "NotImplemented";
923
+ readonly message: "Not implemented.";
924
+ readonly data: null;
925
+ };
926
+ readonly BANDWIDTH_LIMIT_EXCEEDED: {
927
+ readonly status: 509;
928
+ readonly name: "BandwidthLimitExceeded";
929
+ readonly message: "Bandwidth limit exceeded.";
930
+ readonly data: null;
931
+ };
932
+ };
933
+
934
+ /**
935
+ * Get the name of the host on which the application is running.
936
+ * @returns The name of the host.
937
+ */
938
+ declare function getHostname(): string | undefined;
939
+
940
+ /**
941
+ * The type of the parameters object.
942
+ */
943
+ type Parameters = {
944
+ level: pino.LevelWithSilentOrString;
945
+ showLevel: boolean;
946
+ showTimestamp: boolean;
947
+ foreignObjectStartAtNewLine: boolean;
948
+ foreignObjectPadding: number | "after-timestamp" | "after-level";
949
+ foreignObjectIndent: number;
950
+ };
951
+ /**
952
+ * A custom logger instance compatible with both front and back-end, allowing to log messages
953
+ * with different levels and colors.
954
+ *
955
+ * The available levels are:
956
+ * - `fatal`
957
+ * - `error`
958
+ * - `warn`
959
+ * - `info`
960
+ * - `debug`
961
+ * - `trace`
962
+ *
963
+ * The available parameters are:
964
+ * - `setLevel`: Set the logger level (defaults to `"trace"`).
965
+ * - `setShowLevel`: Set the logger level display (defaults to `true`).
966
+ * - `setShowTimestamp`: Set the logger timestamp display (defaults to `true`).
967
+ * - `setForeignObjectStartAtNewLine`: Set the logger foreign object new line display (defaults to `false`).
968
+ * - `setForeignObjectPadding`: Set the padding for foreign objects (defaults to `0`).
969
+ * - `setForeignObjectIndent`: Set the indent for foreign objects (defaults to `4`).
970
+ * - `setParameters`: Set all the parameters at once.
971
+ * - `resetParameters`: Reset all the parameters to their default values.
972
+ */
973
+ declare const logger: pino.Logger & {
974
+ /**
975
+ * Set the logger level, available levels are:
976
+ * - `fatal`
977
+ * - `error`
978
+ * - `warn`
979
+ * - `info`
980
+ * - `debug`
981
+ * - `trace`
982
+ *
983
+ * The logging level is a **minimum** level. For instance if `logger.level` is `"info"` then all
984
+ * `"fatal"`, `"error"`, `"warn"` and `"info"` logs will be enabled.
985
+ * @param level The new logger level.
986
+ */
987
+ setLevel: (level: Parameters["level"]) => void;
988
+ /**
989
+ * Set the logger level display.
990
+ * @param showLevel Whether to show the level or not.
991
+ */
992
+ setShowLevel: (showLevel: Parameters["showLevel"]) => void;
993
+ /**
994
+ * Set the logger timestamp display.
995
+ * @param showTimestamp Whether to show the timestamp or not.
996
+ */
997
+ setShowTimestamp: (showTimestamp: Parameters["showTimestamp"]) => void;
998
+ /**
999
+ * Set the logger foreign object new line display (wether to start the foreign object on a new line or not).
1000
+ * @param foreignObjectStartAtNewLine Whether to start the foreign object on a new line or not.
1001
+ */
1002
+ setForeignObjectStartAtNewLine: (foreignObjectStartAtNewLine: Parameters["foreignObjectStartAtNewLine"]) => void;
1003
+ /**
1004
+ * Set the padding for foreign objects, it also accepts `"after-timestamp"` and
1005
+ * `"after-level"` to automatically calculate the padding to match the beginning of the
1006
+ * specified element.
1007
+ * @param padding The padding for foreign objects.
1008
+ */
1009
+ setForeignObjectPadding: (padding: Parameters["foreignObjectPadding"]) => void;
1010
+ /**
1011
+ * Set the indent for foreign objects.
1012
+ * @param indent The indent for foreign objects.
1013
+ */
1014
+ setForeignObjectIndent: (indent: Parameters["foreignObjectIndent"]) => void;
1015
+ /**
1016
+ * Set all the parameters at once.
1017
+ * @param parameters The new parameters.
1018
+ */
1019
+ setParameters: (parameters: Parameters) => void;
1020
+ /**
1021
+ * Reset all the parameters to their default values.
1022
+ */
1023
+ resetParameters: () => void;
1024
+ };
1025
+
1026
+ /**
1027
+ * A session with a user extended with optional roles.
1028
+ */
1029
+ type ExtendedSession = Session & {
1030
+ user: Session["user"] & {
1031
+ roles: string[] | undefined;
1032
+ };
1033
+ };
1034
+ /**
1035
+ * The type for the authentication options.
1036
+ */
1037
+ type AuthOptions = {
1038
+ requireAuth?: boolean;
1039
+ hasRole?: string;
1040
+ hasSomeRoles?: string[];
1041
+ hasAllRoles?: string[];
1042
+ };
1043
+ /**
1044
+ * The type for the overall wrapper options.
1045
+ */
1046
+ type WrapperOptions = {
1047
+ authFunction?: (req: NextApiRequest, res: NextApiResponse) => Promise<ExtendedSession | null>;
1048
+ roles?: string[];
1049
+ } & AuthOptions;
1050
+ /**
1051
+ * The type for a Next API wrapped method.
1052
+ */
1053
+ type NextApiMethodInput = {
1054
+ req: NextApiRequest;
1055
+ res: NextApiResponse;
1056
+ session: ExtendedSession | null;
1057
+ wrapper: NextApiWrapper;
1058
+ };
1059
+ /**
1060
+ * The type for a Next API wrapped method.
1061
+ */
1062
+ type NextApiMethod = ({ req, res, session, wrapper }: NextApiMethodInput) => Promise<void> | void;
1063
+ /**
1064
+ * The type for a Next API wrapped method, extended with specific auth options.
1065
+ */
1066
+ type NextApiMethodWithAuthOptions = {
1067
+ method: NextApiMethod;
1068
+ authOptions?: AuthOptions;
1069
+ };
1070
+ /**
1071
+ * An object containing all methods for the API route.
1072
+ */
1073
+ type NextApiMethods = {
1074
+ read?: NextApiMethod | NextApiMethodWithAuthOptions;
1075
+ write?: NextApiMethod | NextApiMethodWithAuthOptions;
1076
+ update?: NextApiMethod | NextApiMethodWithAuthOptions;
1077
+ remove?: NextApiMethod | NextApiMethodWithAuthOptions;
1078
+ };
1079
+ /**
1080
+ * A class that wraps the Next.js API routes.
1081
+ */
1082
+ declare class NextApiWrapper {
1083
+ private _req;
1084
+ private _res;
1085
+ private _read;
1086
+ private _write;
1087
+ private _update;
1088
+ private _remove;
1089
+ private _options;
1090
+ /**
1091
+ * The constructor for the NextApiWrapper class.
1092
+ * @param req The `NextApiRequest` object.
1093
+ * @param res The `NextApiResponse` object.
1094
+ * @param methods The methods to be used for the API route:
1095
+ * - `read`: The *GET* method.
1096
+ * - `write`: The *POST* method.
1097
+ * - `update`: The *PATCH* method.
1098
+ * - `remove`: The *DELETE* method.
1099
+ * @param options The options for the wrapper:
1100
+ * - `authFunction`: The function to be used for authentication.
1101
+ * - `roles`: The roles to be used for the wrapper.
1102
+ * - `requireAuth`: Whether to require authentication (defaults to `false`).
1103
+ * - `hasRole`: The user needs to have the role.
1104
+ * - `hasSomeRoles`: The user needs to have at least one of the roles.
1105
+ * - `hasAllRoles`: The user needs to have all of the roles.
1106
+ */
1107
+ constructor(req: NextApiRequest, res: NextApiResponse, methods?: NextApiMethods, options?: WrapperOptions);
1108
+ /**
1109
+ * Set request and response objects.
1110
+ * @param req The new `NextApiRequest` object.
1111
+ * @param res The new `NextApiResponse` object.
1112
+ */
1113
+ setRequestResponse(req: NextApiRequest, res: NextApiResponse): void;
1114
+ /**
1115
+ * Set methods for the API route.
1116
+ * @param methods The new methods to be used for the API route:
1117
+ * - `read`: The *GET* method.
1118
+ * - `write`: The *POST* method.
1119
+ * - `update`: The *PATCH* method.
1120
+ * - `remove`: The *DELETE* method.
1121
+ */
1122
+ setMethods(methods: NextApiMethods): void;
1123
+ /**
1124
+ * Set options for the API route.
1125
+ * @param options The new options for the wrapper:
1126
+ * - `requireAuth`: Whether to require authentication (defaults to `false`).
1127
+ * - `hasRole`: The user needs to have the role.
1128
+ * - `hasSomeRoles`: The user needs to have at least one of the roles.
1129
+ * - `hasAllRoles`: The user needs to have all of the roles.
1130
+ */
1131
+ setOptions(options: Partial<AuthOptions>): void;
1132
+ /**
1133
+ * A private method to check data validity.
1134
+ * @param data The data to be checked.
1135
+ * @returns Whether the data is valid.
1136
+ */
1137
+ private _checkDataValidity;
1138
+ /**
1139
+ * Returns a properly formatted success response.
1140
+ * @param status Status code to be sent in the response.
1141
+ * @param data Data to be sent in the response (optional, defaults to `null`).
1142
+ */
1143
+ successResponse(status: number, data?: unknown): void;
1144
+ /**
1145
+ * Returns a properly formatted error response, based on error constants.
1146
+ * @param error Error code constant to be sent in the response.
1147
+ * @param data Additional data to be sent in the response (optional).
1148
+ * @param message Error message to be sent in the response (optional, defaults to the internal error message).
1149
+ */
1150
+ errorResponse(error: ErrorObj, data?: unknown, message?: string): void;
1151
+ /**
1152
+ * Verify if the user has a specific role.
1153
+ * @param user The user object.
1154
+ * @param role The role to be checked.
1155
+ * @returns Whether the user has the role.
1156
+ */
1157
+ hasRole(user: ExtendedSession["user"], role: string): boolean | undefined;
1158
+ /**
1159
+ * Verify if the user has at least one of the roles.
1160
+ * @param user The user object.
1161
+ * @param roles The roles to be checked.
1162
+ * @returns Whether the user has at least one of the roles.
1163
+ */
1164
+ hasSomeRoles(user: ExtendedSession["user"], roles: string[]): boolean;
1165
+ /**
1166
+ * Verify if the user has all of the roles.
1167
+ * @param user The user object.
1168
+ * @param roles The roles to be checked.
1169
+ * @returns Whether the user has all of the roles.
1170
+ */
1171
+ hasAllRoles(user: ExtendedSession["user"], roles: string[]): boolean;
1172
+ /**
1173
+ * Check the authentication and roles of the user based on the auth options.
1174
+ * @param session The session object.
1175
+ * @param authOptions The authentication options.
1176
+ * @returns Whether the user has the required authentication and roles.
1177
+ */
1178
+ checkAuthOptions(session: ExtendedSession | null, authOptions: AuthOptions): boolean;
1179
+ /**
1180
+ * Check if a method is a direct method or a method with auth options and execute it.
1181
+ * @param method The method to be checked.
1182
+ * @param methodInput The method input object.
1183
+ * @returns Whether the method was executed successfully.
1184
+ */
1185
+ private _executeMethod;
1186
+ /**
1187
+ * Run and route the request to the appropriate method.
1188
+ * @returns The response from the method.
1189
+ */
1190
+ run(): Promise<boolean | void>;
1191
+ }
1192
+
1193
+ export { BaseErrors, Bench, type BenchmarkResult, type BenchmarkResults, type Bit, CyBuffer, type Endianness, NextApiWrapper, type StringEncoding, formatErrorResponse, generateCGASStatus, getHostname, logger, stringifyError };