@mrxsys/mrx-core 2.6.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 (104) hide show
  1. package/CHANGELOG.md +84 -19
  2. package/dist/chunk-4mt568fz.js +26 -0
  3. package/dist/chunk-7g8k2epn.js +104 -0
  4. package/dist/chunk-kv9hms2z.js +28 -0
  5. package/dist/{chunk-7m70tmz2.js → chunk-m18th1g5.js} +1 -1
  6. package/dist/chunk-ncc0m208.js +8 -0
  7. package/dist/chunk-r1kcf1q6.js +62 -0
  8. package/dist/chunk-sepwfqdh.js +6 -0
  9. package/dist/chunk-snqdnkk7.js +10 -0
  10. package/dist/{chunk-9wk2pajt.js → chunk-syhskygx.js} +9 -8
  11. package/dist/{chunk-64d6w5kt.js → chunk-y78xrx17.js} +2 -2
  12. package/dist/chunk-yd82hdxv.js +6 -0
  13. package/dist/chunk-z1skzn1j.js +8 -0
  14. package/dist/modules/data/data.d.ts +1 -1
  15. package/dist/modules/database/index.js +2 -2
  16. package/dist/modules/elysia/cache/cache.d.ts +306 -0
  17. package/dist/modules/elysia/cache/index.d.ts +1 -0
  18. package/dist/modules/elysia/cache/index.js +68 -0
  19. package/dist/modules/elysia/cache/types/cacheItem.d.ts +7 -0
  20. package/dist/modules/elysia/cache/types/cacheOptions.d.ts +21 -0
  21. package/dist/modules/elysia/cache/types/index.d.ts +1 -0
  22. package/dist/modules/elysia/cache/utils/generateCacheKey.d.ts +5 -0
  23. package/dist/modules/elysia/cache/utils/index.d.ts +1 -0
  24. package/dist/modules/elysia/cache/utils/index.js +7 -0
  25. package/dist/modules/elysia/crud/crud.d.ts +1 -1
  26. package/dist/modules/elysia/crud/index.js +13 -10
  27. package/dist/modules/elysia/crud/types/crudOptions.d.ts +2 -0
  28. package/dist/modules/elysia/dbResolver/dbResolver.d.ts +1 -1
  29. package/dist/modules/elysia/dbResolver/index.js +3 -3
  30. package/dist/modules/elysia/error/index.js +11 -2
  31. package/dist/modules/elysia/rateLimit/enums/index.d.ts +1 -0
  32. package/dist/modules/elysia/rateLimit/enums/index.js +7 -0
  33. package/dist/modules/elysia/rateLimit/enums/rateLimitErrorKeys.d.ts +3 -0
  34. package/dist/modules/elysia/rateLimit/index.d.ts +1 -0
  35. package/dist/modules/elysia/rateLimit/index.js +59 -0
  36. package/dist/modules/elysia/{ratelimit/ratelimit.d.ts → rateLimit/rateLimit.d.ts} +32 -15
  37. package/dist/modules/elysia/{ratelimit → rateLimit}/types/rateLimitOptions.d.ts +7 -16
  38. package/dist/modules/jwt/enums/index.d.ts +2 -0
  39. package/dist/modules/jwt/enums/index.js +11 -0
  40. package/dist/modules/jwt/enums/jwtErrorKeys.d.ts +5 -0
  41. package/dist/modules/jwt/enums/parseHumanTimeToSecondsErrorKeys.d.ts +3 -0
  42. package/dist/modules/jwt/index.d.ts +1 -0
  43. package/dist/modules/jwt/index.js +58 -0
  44. package/dist/modules/jwt/jwt.d.ts +3 -0
  45. package/dist/modules/jwt/utils/index.d.ts +1 -0
  46. package/dist/modules/jwt/utils/index.js +9 -0
  47. package/dist/modules/jwt/utils/parseHumanTimeToSeconds.d.ts +17 -0
  48. package/dist/modules/kvStore/enums/index.d.ts +1 -0
  49. package/dist/modules/kvStore/enums/index.js +7 -0
  50. package/dist/modules/kvStore/enums/kvStoreErrorKeys.d.ts +5 -0
  51. package/dist/modules/kvStore/ioredis/index.d.ts +1 -0
  52. package/dist/modules/kvStore/ioredis/index.js +102 -0
  53. package/dist/modules/kvStore/ioredis/ioredisStore.d.ts +107 -0
  54. package/dist/modules/kvStore/memory/index.d.ts +1 -0
  55. package/dist/modules/kvStore/memory/index.js +9 -0
  56. package/dist/modules/kvStore/memory/memoryStore.d.ts +119 -0
  57. package/dist/modules/kvStore/memory/types/memoryStoreEntry.d.ts +11 -0
  58. package/dist/modules/kvStore/types/index.d.ts +1 -0
  59. package/dist/modules/kvStore/types/index.js +1 -0
  60. package/dist/modules/kvStore/types/kvStore.d.ts +81 -0
  61. package/dist/modules/repository/index.js +1 -1
  62. package/dist/modules/repository/types/queryOptions.d.ts +5 -1
  63. package/dist/modules/totp/enums/index.d.ts +1 -0
  64. package/dist/modules/totp/enums/index.js +7 -0
  65. package/dist/modules/totp/enums/totpErrorKeys.d.ts +7 -0
  66. package/dist/modules/totp/hotp.d.ts +11 -0
  67. package/dist/modules/totp/index.d.ts +3 -0
  68. package/dist/modules/totp/index.js +110 -0
  69. package/dist/modules/totp/otpAuthUri.d.ts +21 -0
  70. package/dist/modules/totp/totp.d.ts +23 -0
  71. package/dist/modules/totp/types/index.d.ts +3 -0
  72. package/dist/modules/totp/types/index.js +1 -0
  73. package/dist/modules/totp/types/otpAuthUri.d.ts +35 -0
  74. package/dist/modules/totp/types/totpOptions.d.ts +23 -0
  75. package/dist/modules/totp/types/verifyOptions.d.ts +18 -0
  76. package/dist/modules/totp/utils/base32.d.ts +19 -0
  77. package/dist/modules/totp/utils/createCounterBuffer.d.ts +8 -0
  78. package/dist/modules/totp/utils/dynamicTruncation.d.ts +9 -0
  79. package/dist/modules/totp/utils/generateHmac.d.ts +9 -0
  80. package/dist/modules/totp/utils/generateSecretBytes.d.ts +10 -0
  81. package/dist/modules/totp/utils/index.d.ts +6 -0
  82. package/dist/modules/totp/utils/index.js +81 -0
  83. package/dist/modules/totp/utils/timeRemaining.d.ts +9 -0
  84. package/dist/utils/types/index.d.ts +1 -0
  85. package/dist/utils/types/renameKey.d.ts +10 -0
  86. package/package.json +86 -75
  87. package/dist/chunk-cqw9xq4y.js +0 -7
  88. package/dist/chunk-twaga0fp.js +0 -6
  89. package/dist/modules/elysia/jwt/enums/index.d.ts +0 -1
  90. package/dist/modules/elysia/jwt/enums/index.js +0 -7
  91. package/dist/modules/elysia/jwt/enums/jwtErrorKeys.d.ts +0 -4
  92. package/dist/modules/elysia/jwt/index.d.ts +0 -1
  93. package/dist/modules/elysia/jwt/index.js +0 -77
  94. package/dist/modules/elysia/jwt/jwt.d.ts +0 -119
  95. package/dist/modules/elysia/jwt/types/index.d.ts +0 -1
  96. package/dist/modules/elysia/jwt/types/jwtOptions.d.ts +0 -98
  97. package/dist/modules/elysia/ratelimit/enums/index.d.ts +0 -1
  98. package/dist/modules/elysia/ratelimit/enums/index.js +0 -7
  99. package/dist/modules/elysia/ratelimit/enums/ratelimitErrorKeys.d.ts +0 -3
  100. package/dist/modules/elysia/ratelimit/index.d.ts +0 -1
  101. package/dist/modules/elysia/ratelimit/index.js +0 -52
  102. /package/dist/modules/elysia/{jwt → cache}/types/index.js +0 -0
  103. /package/dist/modules/elysia/{ratelimit → rateLimit}/types/index.d.ts +0 -0
  104. /package/dist/modules/elysia/{ratelimit → rateLimit}/types/index.js +0 -0
package/package.json CHANGED
@@ -1,15 +1,33 @@
1
1
  {
2
2
  "name": "@mrxsys/mrx-core",
3
- "version": "2.6.0",
3
+ "version": "2.8.0",
4
4
  "author": "Ruby",
5
- "description": " Core provides a set of tools to help you build a microservice",
6
- "type": "module",
7
- "license": "MIT",
8
- "keywords": [
9
- "mrxsys",
10
- "mrxsys-core",
11
- "core"
12
- ],
5
+ "devDependencies": {
6
+ "@eslint/js": "^9.34.0",
7
+ "@sinclair/typebox": "0.34.40",
8
+ "@stylistic/eslint-plugin": "^5.2.3",
9
+ "@types/bun": "^1.2.21",
10
+ "@types/nodemailer": "^7.0.1",
11
+ "elysia": "^1.3.20",
12
+ "eslint": "^9.34.0",
13
+ "globals": "^16.3.0",
14
+ "ioredis": "^5.7.0",
15
+ "jose": "^6.0.13",
16
+ "knex": "^3.1.0",
17
+ "mssql": "^11.0.1",
18
+ "nodemailer": "^7.0.5",
19
+ "typescript-eslint": "^8.41.0"
20
+ },
21
+ "peerDependencies": {
22
+ "@sinclair/typebox": "0.34.40",
23
+ "elysia": "^1.3.20",
24
+ "ioredis": "^5.7.0",
25
+ "jose": "^6.0.13",
26
+ "knex": "^3.1.0",
27
+ "mssql": "^11.0.1",
28
+ "nodemailer": "^7.0.5",
29
+ "typescript": "^5.9.2"
30
+ },
13
31
  "exports": {
14
32
  "./errors": "./dist/errors/index.js",
15
33
  "./errors/enums": "./dist/errors/enums/index.js",
@@ -22,6 +40,9 @@
22
40
  "./modules/database/enums": "./dist/modules/database/enums/index.js",
23
41
  "./modules/database/events": "./dist/modules/database/events/index.js",
24
42
  "./modules/database/types": "./dist/modules/database/types/index.js",
43
+ "./modules/elysia/cache": "./dist/modules/elysia/cache/index.js",
44
+ "./modules/elysia/cache/types": "./dist/modules/elysia/cache/types/index.js",
45
+ "./modules/elysia/cache/utils": "./dist/modules/elysia/cache/utils/index.js",
25
46
  "./modules/elysia/crud": "./dist/modules/elysia/crud/index.js",
26
47
  "./modules/elysia/crud/types": "./dist/modules/elysia/crud/types/index.js",
27
48
  "./modules/elysia/crudSchema": "./dist/modules/elysia/crudSchema/index.js",
@@ -31,13 +52,17 @@
31
52
  "./modules/elysia/dbResolver/enums": "./dist/modules/elysia/dbResolver/enums/index.js",
32
53
  "./modules/elysia/dbResolver/types": "./dist/modules/elysia/dbResolver/types/index.js",
33
54
  "./modules/elysia/error": "./dist/modules/elysia/error/index.js",
34
- "./modules/elysia/jwt": "./dist/modules/elysia/jwt/index.js",
35
- "./modules/elysia/jwt/enums": "./dist/modules/elysia/jwt/enums/index.js",
36
- "./modules/elysia/jwt/types": "./dist/modules/elysia/jwt/types/index.js",
37
55
  "./modules/elysia/microservice": "./dist/modules/elysia/microservice/index.js",
38
- "./modules/elysia/ratelimit": "./dist/modules/elysia/ratelimit/index.js",
39
- "./modules/elysia/ratelimit/enums": "./dist/modules/elysia/ratelimit/enums/index.js",
40
- "./modules/elysia/ratelimit/types": "./dist/modules/elysia/ratelimit/types/index.js",
56
+ "./modules/elysia/rateLimit": "./dist/modules/elysia/rateLimit/index.js",
57
+ "./modules/elysia/rateLimit/enums": "./dist/modules/elysia/rateLimit/enums/index.js",
58
+ "./modules/elysia/rateLimit/types": "./dist/modules/elysia/rateLimit/types/index.js",
59
+ "./modules/jwt": "./dist/modules/jwt/index.js",
60
+ "./modules/jwt/enums": "./dist/modules/jwt/enums/index.js",
61
+ "./modules/jwt/utils": "./dist/modules/jwt/utils/index.js",
62
+ "./modules/kvStore/enums": "./dist/modules/kvStore/enums/index.js",
63
+ "./modules/kvStore/ioredis": "./dist/modules/kvStore/ioredis/index.js",
64
+ "./modules/kvStore/memory": "./dist/modules/kvStore/memory/index.js",
65
+ "./modules/kvStore/types": "./dist/modules/kvStore/types/index.js",
41
66
  "./modules/logger": "./dist/modules/logger/index.js",
42
67
  "./modules/logger/enums": "./dist/modules/logger/enums/index.js",
43
68
  "./modules/logger/events": "./dist/modules/logger/events/index.js",
@@ -50,71 +75,16 @@
50
75
  "./modules/repository/types": "./dist/modules/repository/types/index.js",
51
76
  "./modules/singletonManager": "./dist/modules/singletonManager/index.js",
52
77
  "./modules/singletonManager/enums": "./dist/modules/singletonManager/enums/index.js",
78
+ "./modules/totp": "./dist/modules/totp/index.js",
79
+ "./modules/totp/enums": "./dist/modules/totp/enums/index.js",
80
+ "./modules/totp/types": "./dist/modules/totp/types/index.js",
81
+ "./modules/totp/utils": "./dist/modules/totp/utils/index.js",
53
82
  "./modules/typedEventEmitter": "./dist/modules/typedEventEmitter/index.js",
54
83
  "./modules/typedEventEmitter/types": "./dist/modules/typedEventEmitter/types/index.js",
55
84
  "./utils": "./dist/utils/index.js",
56
85
  "./utils/enums": "./dist/utils/enums/index.js",
57
86
  "./utils/types": "./dist/utils/types/index.js"
58
87
  },
59
- "scripts": {
60
- "build": "bun builder.ts",
61
- "docs": "bunx typedoc --tsconfig tsconfig.build.json",
62
- "fix-lint": "eslint --fix ./source",
63
- "lint": "eslint ./source",
64
- "start": "bun build/index.js",
65
- "test:integration": "bun test --timeout 5800 $(find test/integration -name '*.spec.ts')",
66
- "test:unit": "bun test --timeout 5800 --coverage $(find test/unit -name '*.spec.ts')",
67
- "test": "bun test --coverage --timeout 5500"
68
- },
69
- "devDependencies": {
70
- "@eslint/js": "^9.32.0",
71
- "@sinclair/typebox": "0.34.38",
72
- "@stylistic/eslint-plugin": "^5.2.2",
73
- "@types/bun": "^1.2.19",
74
- "@types/nodemailer": "^6.4.17",
75
- "elysia": "^1.3.8",
76
- "eslint": "^9.32.0",
77
- "globals": "^16.3.0",
78
- "ioredis": "^5.7.0",
79
- "jose": "^6.0.12",
80
- "knex": "^3.1.0",
81
- "mssql": "^11.0.1",
82
- "nodemailer": "^7.0.5",
83
- "typescript-eslint": "^8.38.0"
84
- },
85
- "peerDependencies": {
86
- "@sinclair/typebox": "0.34.38",
87
- "elysia": "^1.3.8",
88
- "ioredis": "^5.7.0",
89
- "jose": "^6.0.12",
90
- "knex": "^3.1.0",
91
- "mssql": "^11.0.1",
92
- "nodemailer": "^7.0.5",
93
- "typescript": "^5.9.2"
94
- },
95
- "peerDependenciesMeta": {
96
- "@sinclair/typebox": {
97
- "optional": true
98
- },
99
- "elysia": {
100
- "optional": true
101
- },
102
- "ioredis": {
103
- "optional": true
104
- },
105
- "jose": {
106
- "optional": true
107
- },
108
- "knex": {
109
- "optional": true
110
- },
111
- "mssql": {
112
- "optional": true
113
- },
114
- "nodemailer": {
115
- "optional": true
116
- }
117
- },
118
88
  "changelog": {
119
89
  "types": {
120
90
  "feat": {
@@ -166,5 +136,46 @@
166
136
  "tagMessage": "v{{newVersion}}",
167
137
  "tagBody": "v{{newVersion}}"
168
138
  }
169
- }
139
+ },
140
+ "description": " Core provides a set of tools to help you build a microservice",
141
+ "keywords": [
142
+ "mrxsys",
143
+ "mrxsys-core",
144
+ "core"
145
+ ],
146
+ "license": "MIT",
147
+ "peerDependenciesMeta": {
148
+ "@sinclair/typebox": {
149
+ "optional": true
150
+ },
151
+ "elysia": {
152
+ "optional": true
153
+ },
154
+ "ioredis": {
155
+ "optional": true
156
+ },
157
+ "jose": {
158
+ "optional": true
159
+ },
160
+ "knex": {
161
+ "optional": true
162
+ },
163
+ "mssql": {
164
+ "optional": true
165
+ },
166
+ "nodemailer": {
167
+ "optional": true
168
+ }
169
+ },
170
+ "scripts": {
171
+ "build": "bun builder.ts",
172
+ "docs": "bunx typedoc --tsconfig tsconfig.build.json",
173
+ "fix-lint": "eslint --fix ./source",
174
+ "lint": "eslint ./source",
175
+ "start": "bun build/index.js",
176
+ "test:integration": "bun test --timeout 5800 $(find test/integration -name '*.spec.ts')",
177
+ "test:unit": "bun test --timeout 5800 --coverage $(find test/unit -name '*.spec.ts')",
178
+ "test": "bun test --coverage --timeout 5500"
179
+ },
180
+ "type": "module"
170
181
  }
@@ -1,7 +0,0 @@
1
- // source/modules/elysia/jwt/enums/jwtErrorKeys.ts
2
- var JWT_ERROR_KEYS = {
3
- JWT_SIGN_ERROR: "elysia.jwt.error.sign_error",
4
- JWT_SECRET_NOT_FOUND: "elysia.jwt.error.secret_not_found"
5
- };
6
-
7
- export { JWT_ERROR_KEYS };
@@ -1,6 +0,0 @@
1
- // source/modules/elysia/ratelimit/enums/ratelimitErrorKeys.ts
2
- var RATELIMIT_ERROR_KEYS = {
3
- RATELIMIT_EXCEEDED: "elysia.ratelimit.error.exceeded"
4
- };
5
-
6
- export { RATELIMIT_ERROR_KEYS };
@@ -1 +0,0 @@
1
- export { JWT_ERROR_KEYS } from './jwtErrorKeys';
@@ -1,7 +0,0 @@
1
- // @bun
2
- import {
3
- JWT_ERROR_KEYS
4
- } from "../../../../chunk-cqw9xq4y.js";
5
- export {
6
- JWT_ERROR_KEYS
7
- };
@@ -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 +0,0 @@
1
- export { RATELIMIT_ERROR_KEYS } from './ratelimitErrorKeys';
@@ -1,7 +0,0 @@
1
- // @bun
2
- import {
3
- RATELIMIT_ERROR_KEYS
4
- } from "../../../../chunk-twaga0fp.js";
5
- export {
6
- RATELIMIT_ERROR_KEYS
7
- };
@@ -1,3 +0,0 @@
1
- export declare const RATELIMIT_ERROR_KEYS: {
2
- readonly RATELIMIT_EXCEEDED: "elysia.ratelimit.error.exceeded";
3
- };
@@ -1 +0,0 @@
1
- export { rateLimit } from './ratelimit';
@@ -1,52 +0,0 @@
1
- // @bun
2
- import {
3
- RATELIMIT_ERROR_KEYS
4
- } from "../../../chunk-twaga0fp.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/ratelimit/ratelimit.ts
12
- import { Elysia } from "elysia";
13
- var rateLimit = ({ redis, limit, window }) => new Elysia({
14
- name: "rateLimit",
15
- seed: {
16
- redis,
17
- limit,
18
- window
19
- }
20
- }).onRequest(async ({ set, request, server }) => {
21
- const ip = request.headers.get("x-forwarded-for") || request.headers.get("x-real-ip") || server?.requestIP(request)?.address || "127.0.0.1";
22
- const key = `ratelimit:${ip}`;
23
- const current = await redis.get(key);
24
- const count = current ? parseInt(current) : 0;
25
- if (count === 0)
26
- await redis.setex(key, window, "1");
27
- else
28
- await redis.incr(key);
29
- const newCount = await redis.get(key);
30
- const currentCount = newCount ? parseInt(newCount) : 0;
31
- if (currentCount > limit) {
32
- set.status = 429;
33
- throw new HttpError({
34
- message: RATELIMIT_ERROR_KEYS.RATELIMIT_EXCEEDED,
35
- httpStatusCode: "TOO_MANY_REQUESTS",
36
- cause: {
37
- limit,
38
- window,
39
- remaining: 0,
40
- reset: await redis.ttl(key)
41
- }
42
- });
43
- }
44
- set.headers = {
45
- "X-RateLimit-Limit": limit.toString(),
46
- "X-RateLimit-Remaining": Math.max(0, limit - currentCount).toString(),
47
- "X-RateLimit-Reset": (await redis.ttl(key)).toString()
48
- };
49
- }).as("scoped");
50
- export {
51
- rateLimit
52
- };