@mrxsys/mrx-core 2.7.0 → 2.8.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.
Files changed (77) hide show
  1. package/CHANGELOG.md +67 -48
  2. package/dist/chunk-7g8k2epn.js +104 -0
  3. package/dist/chunk-kv9hms2z.js +28 -0
  4. package/dist/{chunk-qb6x364m.js → chunk-m18th1g5.js} +1 -1
  5. package/dist/chunk-ncc0m208.js +8 -0
  6. package/dist/chunk-r1kcf1q6.js +62 -0
  7. package/dist/chunk-sepwfqdh.js +6 -0
  8. package/dist/{chunk-z6q192p8.js → chunk-syhskygx.js} +4 -4
  9. package/dist/{chunk-tm71j126.js → chunk-y78xrx17.js} +2 -2
  10. package/dist/chunk-z1skzn1j.js +8 -0
  11. package/dist/modules/data/data.d.ts +1 -1
  12. package/dist/modules/database/index.js +2 -2
  13. package/dist/modules/elysia/cache/cache.d.ts +306 -0
  14. package/dist/modules/elysia/cache/index.d.ts +1 -0
  15. package/dist/modules/elysia/cache/index.js +68 -0
  16. package/dist/modules/elysia/cache/types/cacheItem.d.ts +7 -0
  17. package/dist/modules/elysia/cache/types/cacheOptions.d.ts +21 -0
  18. package/dist/modules/elysia/cache/types/index.d.ts +1 -0
  19. package/dist/modules/elysia/cache/utils/generateCacheKey.d.ts +5 -0
  20. package/dist/modules/elysia/cache/utils/index.d.ts +1 -0
  21. package/dist/modules/elysia/cache/utils/index.js +7 -0
  22. package/dist/modules/elysia/crud/crud.d.ts +1 -1
  23. package/dist/modules/elysia/crud/index.js +13 -10
  24. package/dist/modules/elysia/crud/types/crudOptions.d.ts +2 -0
  25. package/dist/modules/elysia/dbResolver/dbResolver.d.ts +1 -1
  26. package/dist/modules/elysia/dbResolver/index.js +3 -3
  27. package/dist/modules/elysia/error/index.js +11 -2
  28. package/dist/modules/elysia/rateLimit/index.js +15 -95
  29. package/dist/modules/elysia/rateLimit/rateLimit.d.ts +28 -11
  30. package/dist/modules/elysia/rateLimit/types/rateLimitOptions.d.ts +3 -15
  31. package/dist/modules/jwt/enums/index.d.ts +2 -0
  32. package/dist/modules/jwt/enums/index.js +11 -0
  33. package/dist/modules/jwt/enums/jwtErrorKeys.d.ts +5 -0
  34. package/dist/modules/jwt/enums/parseHumanTimeToSecondsErrorKeys.d.ts +3 -0
  35. package/dist/modules/jwt/index.d.ts +1 -0
  36. package/dist/modules/jwt/index.js +58 -0
  37. package/dist/modules/jwt/jwt.d.ts +3 -0
  38. package/dist/modules/jwt/utils/index.d.ts +1 -0
  39. package/dist/modules/jwt/utils/index.js +9 -0
  40. package/dist/modules/jwt/utils/parseHumanTimeToSeconds.d.ts +17 -0
  41. package/dist/modules/kvStore/enums/index.d.ts +1 -0
  42. package/dist/modules/kvStore/enums/index.js +7 -0
  43. package/dist/modules/kvStore/enums/kvStoreErrorKeys.d.ts +5 -0
  44. package/dist/modules/kvStore/ioredis/index.d.ts +1 -0
  45. package/dist/modules/kvStore/ioredis/index.js +102 -0
  46. package/dist/modules/kvStore/ioredis/ioredisStore.d.ts +107 -0
  47. package/dist/modules/kvStore/memory/index.d.ts +1 -0
  48. package/dist/modules/kvStore/memory/index.js +9 -0
  49. package/dist/modules/kvStore/memory/memoryStore.d.ts +119 -0
  50. package/dist/modules/{elysia/rateLimit → kvStore/memory}/types/memoryStoreEntry.d.ts +2 -2
  51. package/dist/modules/kvStore/types/index.d.ts +1 -0
  52. package/dist/modules/kvStore/types/index.js +1 -0
  53. package/dist/modules/kvStore/types/kvStore.d.ts +81 -0
  54. package/dist/modules/repository/index.js +1 -1
  55. package/dist/modules/repository/types/queryOptions.d.ts +5 -1
  56. package/dist/modules/totp/hotp.d.ts +11 -0
  57. package/dist/modules/totp/index.d.ts +3 -1
  58. package/dist/modules/totp/index.js +27 -30
  59. package/dist/modules/totp/otpAuthUri.d.ts +21 -0
  60. package/dist/modules/totp/totp.d.ts +0 -40
  61. package/dist/modules/totp/utils/base32.d.ts +1 -1
  62. package/dist/modules/totp/utils/index.d.ts +3 -2
  63. package/dist/modules/totp/utils/index.js +6 -0
  64. package/dist/modules/totp/utils/timeRemaining.d.ts +9 -0
  65. package/package.json +79 -72
  66. package/dist/chunk-cqw9xq4y.js +0 -7
  67. package/dist/modules/elysia/jwt/enums/index.d.ts +0 -1
  68. package/dist/modules/elysia/jwt/enums/index.js +0 -7
  69. package/dist/modules/elysia/jwt/enums/jwtErrorKeys.d.ts +0 -4
  70. package/dist/modules/elysia/jwt/index.d.ts +0 -1
  71. package/dist/modules/elysia/jwt/index.js +0 -77
  72. package/dist/modules/elysia/jwt/jwt.d.ts +0 -119
  73. package/dist/modules/elysia/jwt/types/index.d.ts +0 -1
  74. package/dist/modules/elysia/jwt/types/jwtOptions.d.ts +0 -98
  75. package/dist/modules/elysia/rateLimit/stores/memoryStore.d.ts +0 -47
  76. package/dist/modules/elysia/rateLimit/types/rateLimitStore.d.ts +0 -21
  77. /package/dist/modules/elysia/{jwt → cache}/types/index.js +0 -0
@@ -1,4 +0,0 @@
1
- export declare const JWT_ERROR_KEYS: {
2
- readonly JWT_SIGN_ERROR: "elysia.jwt.error.sign_error";
3
- readonly JWT_SECRET_NOT_FOUND: "elysia.jwt.error.secret_not_found";
4
- };
@@ -1 +0,0 @@
1
- export { jwt } from './jwt';
@@ -1,77 +0,0 @@
1
- // @bun
2
- import {
3
- JWT_ERROR_KEYS
4
- } from "../../../chunk-cqw9xq4y.js";
5
- import {
6
- HttpError
7
- } from "../../../chunk-683sda6e.js";
8
- import"../../../chunk-9nw6qekv.js";
9
- import"../../../chunk-vknq69e0.js";
10
-
11
- // source/modules/elysia/jwt/jwt.ts
12
- import { Elysia } from "elysia";
13
- import {
14
- SignJWT,
15
- jwtVerify
16
- } from "jose";
17
- var jwt = (options) => {
18
- if (!options.secret)
19
- throw new HttpError({
20
- message: JWT_ERROR_KEYS.JWT_SECRET_NOT_FOUND,
21
- httpStatusCode: "INTERNAL_SERVER_ERROR"
22
- });
23
- const encodedKey = new TextEncoder().encode(options.secret);
24
- const contextKeyName = options.jwtKeyName ?? "jwt";
25
- const defaultAlgorithm = "HS256";
26
- const defaultExpiration = "15m";
27
- return new Elysia({
28
- name: "jwt",
29
- seed: options
30
- }).decorate(contextKeyName, {
31
- async sign(additionalPayload, expiration = options.exp ?? defaultExpiration) {
32
- const jwtId = crypto.randomUUID();
33
- const finalPayload = {
34
- iss: "elysia-jwt-plugin",
35
- aud: "elysia-application",
36
- jti: jwtId,
37
- iat: Math.floor(Date.now() / 1000),
38
- ...options.payload,
39
- ...additionalPayload
40
- };
41
- try {
42
- const headerParams = {
43
- alg: defaultAlgorithm,
44
- typ: "JWT",
45
- ...options.header ? Object.fromEntries(Object.entries(options.header).filter(([key]) => key !== "b64")) : {}
46
- };
47
- const jwt2 = new SignJWT(finalPayload).setProtectedHeader(headerParams).setIssuedAt().setExpirationTime(expiration);
48
- if (finalPayload.iss)
49
- jwt2.setIssuer(finalPayload.iss);
50
- if (finalPayload.aud)
51
- jwt2.setAudience(Array.isArray(finalPayload.aud) ? finalPayload.aud : [finalPayload.aud]);
52
- return await jwt2.sign(encodedKey);
53
- } catch (error) {
54
- throw new HttpError({
55
- message: JWT_ERROR_KEYS.JWT_SIGN_ERROR,
56
- httpStatusCode: "INTERNAL_SERVER_ERROR",
57
- cause: error
58
- });
59
- }
60
- },
61
- async verify(token) {
62
- if (!token || token.trim() === "")
63
- return false;
64
- try {
65
- const result = await jwtVerify(token, encodedKey, {
66
- algorithms: [defaultAlgorithm]
67
- });
68
- return result;
69
- } catch {
70
- return false;
71
- }
72
- }
73
- }).as("scoped");
74
- };
75
- export {
76
- jwt
77
- };
@@ -1,119 +0,0 @@
1
- import { Elysia } from 'elysia';
2
- import { type JWTPayload, type JWTVerifyResult } from 'jose';
3
- import type { JWTOptions } from './types/jwtOptions';
4
- /**
5
- * The `elysiaJwtPlugin` provides secure JSON Web Token (JWT) authentication capabilities for Elysia applications.
6
- *
7
- * This plugin leverages the industry-standard `jose` library to ensure robust JWT handling with modern cryptographic standards.
8
- *
9
- * @template TJWTKeyName - The name to use for JWT functionality in the Elysia context (defaults to 'jwt')
10
- * @param options - Configuration options for the JWT plugin
11
- *
12
- * @returns An Elysia plugin that adds JWT functionality to the application context
13
- *
14
- * @throws ({@link HttpError}) - When the secret key is missing or invalid
15
- *
16
- * @example
17
- * Basic usage
18
- * ```ts
19
- * const app = new Elysia()
20
- * .use(jwt({
21
- * secret: process.env.JWT_SECRET!,
22
- * exp: '1h'
23
- * }))
24
- * .post('/login', ({ jwt }) => {
25
- * return jwt.sign({ userId: 123, role: 'user' });
26
- * })
27
- * .get('/protected', async ({ jwt, headers }) => {
28
- * const token = headers.authorization?.replace('Bearer ', '');
29
- * const payload = await jwt.verify(token);
30
- * if (!payload) throw new Error('Invalid token');
31
- * return { user: payload };
32
- * });
33
- * ```
34
- * @example
35
- * Custom context key name
36
- * ```ts
37
- * const app2 = new Elysia()
38
- * .use(jwt({
39
- * jwtKeyName: 'auth',
40
- * secret: 'my-secret'
41
- * }))
42
- * .post('/login', ({ auth }) => auth.sign({ id: 1 }));
43
- * ```
44
- */
45
- export declare const jwt: <const TJWTKeyName extends string = "jwt">(options: JWTOptions<TJWTKeyName>) => Elysia<"", {
46
- decorator: { [name in TJWTKeyName]: {
47
- /**
48
- * Signs a JWT with the provided payload and optional expiration time.
49
- *
50
- * @param additionalPayload - Custom payload data to include in the JWT
51
- * @param expiration - Token expiration time (overrides default if provided)
52
- *
53
- * @returns A Promise that resolves to the signed JWT string
54
- *
55
- * @throws ({@link HttpError}) - When JWT signing fails due to invalid payload or configuration
56
- *
57
- * @example
58
- * Sign with user data
59
- * ```ts
60
- * const token = await jwt.sign({ userId: 123, role: 'admin' });
61
- * ```
62
- * @example
63
- * Sign with custom expiration
64
- * ```ts
65
- * const token = await jwt.sign({ userId: 123 }, '2h');
66
- * ````
67
- * @example
68
- * Sign with Date expiration
69
- * ```ts
70
- * const token = await jwt.sign({ userId: 123 }, new Date('2024-12-31'));
71
- * ```
72
- */
73
- sign(additionalPayload?: JWTPayload, expiration?: number | string | Date): Promise<string>;
74
- /**
75
- * Verifies and decodes a JWT token.
76
- *
77
- * @param token - The JWT string to verify and decode
78
- *
79
- * @returns A Promise that resolves to the decoded payload or false if invalid
80
- *
81
- * @example
82
- * Verify token from Authorization header
83
- * ```ts
84
- * const token = headers.authorization?.replace('Bearer ', '');
85
- * const payload = await jwt.verify(token);
86
- *
87
- * if (payload) {
88
- * console.log('User ID:', payload.userId);
89
- * console.log('Token expires:', new Date(payload.exp! * 1000));
90
- * } else {
91
- * console.log('Invalid or expired token');
92
- * }
93
- * ```
94
- */
95
- verify(token?: string): Promise<JWTVerifyResult | false>;
96
- }; } extends infer T ? { [K in keyof T]: T[K]; } : never;
97
- store: {};
98
- derive: {};
99
- resolve: {};
100
- }, {
101
- typebox: {};
102
- error: {};
103
- }, {
104
- schema: {};
105
- standaloneSchema: {};
106
- macro: {};
107
- macroFn: {};
108
- parser: {};
109
- }, {}, {
110
- derive: {};
111
- resolve: {};
112
- schema: import("elysia").MergeSchema<{}, {}, "">;
113
- standaloneSchema: import("elysia/types").PrettifySchema<{}>;
114
- }, {
115
- derive: {};
116
- resolve: {};
117
- schema: {};
118
- standaloneSchema: {};
119
- }>;
@@ -1 +0,0 @@
1
- export type { JWTOptions } from './jwtOptions';
@@ -1,98 +0,0 @@
1
- import type { JWSHeaderParameters, JWTPayload } from 'jose';
2
- /**
3
- * Configuration options for the JWT plugin.
4
- *
5
- * @template TJWTKeyName - The name to be used for accessing the JWT functionality in the Elysia context
6
- *
7
- * @example
8
- * ```ts
9
- * const options: JWTOption = {
10
- * name: 'auth',
11
- * secret: process.env.JWT_SECRET || 'your-secret-key',
12
- * exp: '1d',
13
- * header: {
14
- * alg: 'HS256',
15
- * typ: 'JWT'
16
- * },
17
- * payload: {
18
- * iss: 'my-api',
19
- * aud: 'my-client'
20
- * }
21
- * };
22
- * ```
23
- */
24
- export interface JWTOptions<TJWTKeyName extends string = 'jwt'> {
25
- /**
26
- * JWT name to add in context with decorate.
27
- *
28
- * This allows you to customize how you access the JWT functionality in your
29
- * route handlers. For example, if set to 'auth', you would use `auth.sign()`
30
- * instead of the default `jwt.sign()`.
31
- *
32
- * @defaultValue 'jwt'
33
- */
34
- readonly jwtKeyName?: TJWTKeyName;
35
- /**
36
- * Secret key used to sign and verify JWTs.
37
- *
38
- * @example
39
- * Using a string secret
40
- * ```ts
41
- * secret: 'your-very-secret-key'
42
- *```
43
- * @example
44
- * Using an environment variable (recommended for production)
45
- * ```ts
46
- * secret: process.env.JWT_SECRET
47
- * ```
48
- */
49
- readonly secret: string;
50
- /**
51
- * JWT expiration setting. Applies as the default expiration for tokens.
52
- *
53
- * Controls how long tokens are valid before they expire. This setting provides
54
- * a good balance between security (limiting the window of opportunity for token misuse)
55
- * and user experience (not requiring frequent re-authentication).
56
- *
57
- * @defaultValue '15m' (15 minutes)
58
- *
59
- * @example
60
- * Set tokens to expire after 1 hour
61
- * ```ts
62
- * exp: '1h'
63
- * ```
64
- * @example
65
- * Set tokens to expire at a specific date
66
- * ```ts
67
- * exp: new Date('2023-12-31')
68
- * ```
69
- * @example
70
- * Set tokens to expire after 3600 seconds (1 hour)
71
- * ```ts
72
- * exp: 3600
73
- * ```
74
- */
75
- readonly exp?: number | string | Date;
76
- /**
77
- * Default JWT header parameters to include in every JWT.
78
- *
79
- * This allows you to specify additional metadata about the token.
80
- */
81
- readonly header?: JWSHeaderParameters;
82
- /**
83
- * Default payload values to include in every JWT.
84
- *
85
- * These values are merged with any additional payload provided during signing,
86
- * allowing you to set standard claims that should be included in all tokens
87
- * generated by your application.
88
- *
89
- * @example
90
- * ```ts
91
- * payload: {
92
- * iss: 'my-api', // Issuer
93
- * aud: 'my-frontend', // Audience
94
- * }
95
- * ```
96
- */
97
- readonly payload?: JWTPayload;
98
- }
@@ -1,47 +0,0 @@
1
- import type { RateLimitStore } from '../../../../modules/elysia/rateLimit/types/rateLimitStore';
2
- export declare class MemoryStore implements RateLimitStore {
3
- /**
4
- * Internal storage map.
5
- */
6
- private readonly _store;
7
- /**
8
- * Cleanup interval (5 minutes by default).
9
- */
10
- private readonly _cleanupInterval;
11
- /**
12
- * Timer for cleanup operations.
13
- */
14
- private _cleanupTimer;
15
- /**
16
- * Creates instance and starts cleanup process.
17
- */
18
- constructor(cleanupIntervalMs?: number);
19
- /**
20
- * Get current count for a key.
21
- */
22
- get(key: string): string | null;
23
- /**
24
- * Set key with expiration time.
25
- */
26
- setex(key: string, seconds: number, value: string): void;
27
- /**
28
- * Increment key value and return new count.
29
- */
30
- incr(key: string): number;
31
- /**
32
- * Get time to live for key in seconds.
33
- */
34
- ttl(key: string): number;
35
- /**
36
- * Start cleanup process.
37
- */
38
- private _startCleanup;
39
- /**
40
- * Clean up expired entries.
41
- */
42
- private _cleanup;
43
- /**
44
- * Stop cleanup and clear data.
45
- */
46
- destroy(): void;
47
- }
@@ -1,21 +0,0 @@
1
- /**
2
- * Common interface for rate limit storage backends (Memory, Redis, etc.).
3
- */
4
- export interface RateLimitStore {
5
- /**
6
- * Get value for a key.
7
- */
8
- get(key: string): string | null | Promise<string | null>;
9
- /**
10
- * Set key with expiration.
11
- */
12
- setex(key: string, seconds: number, value: string): void | Promise<void>;
13
- /**
14
- * Increment key value.
15
- */
16
- incr(key: string): number | Promise<number>;
17
- /**
18
- * Get time to live for key.
19
- */
20
- ttl(key: string): number | Promise<number>;
21
- }