@kalutskii/foundation 0.2.3 → 0.3.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/dist/index.d.ts +5 -0
- package/dist/index.js +14 -5
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -188,6 +188,11 @@ declare class ZodJWTService<TSchema extends z$1.ZodObject<z$1.ZodRawShape>> {
|
|
|
188
188
|
* Example usage: `const accessToken = await JWTServiceInstance.sign({ userId: 123 }, 'my-secret', { expiresInSeconds: 300 });`
|
|
189
189
|
*/
|
|
190
190
|
sign(payload: z$1.infer<TSchema>, secret: string, options?: JWTSignOptions): Promise<string>;
|
|
191
|
+
/**
|
|
192
|
+
* Decodes a JWT token, and validates the payload against the Zod schema.
|
|
193
|
+
* Note: This method does NOT verify the token's signature or check for expiration.
|
|
194
|
+
*/
|
|
195
|
+
decode(token: string): Promise<z$1.infer<TSchema> | null>;
|
|
191
196
|
/**
|
|
192
197
|
* Verifies and decodes a JWT token using the provided secret and configured algorithm,
|
|
193
198
|
* then validates the payload against the Zod schema, also throws on expired tokens.
|
package/dist/index.js
CHANGED
|
@@ -75,13 +75,13 @@ function getColoredHTTPStatus(status) {
|
|
|
75
75
|
{ range: [400, 499], colorFn: yellow },
|
|
76
76
|
{ range: [500, 599], colorFn: red }
|
|
77
77
|
];
|
|
78
|
-
const statusColor = colorMap.find(({ range: [min, max] }) => status >=
|
|
78
|
+
const statusColor = colorMap.find(({ range: [min, max] }) => status >= min && status <= max);
|
|
79
79
|
return statusColor ? statusColor.colorFn : (text) => text;
|
|
80
80
|
}
|
|
81
81
|
function writeLog(message, level, service = "log", stack) {
|
|
82
82
|
const logColorMap = { info: blue, warn: yellow, error: red };
|
|
83
83
|
const timestamp = dim(getFormattedTime());
|
|
84
|
-
const serviceName = logColorMap[level](service
|
|
84
|
+
const serviceName = logColorMap[level](service.padEnd(12));
|
|
85
85
|
const formattedMessage = white(message);
|
|
86
86
|
console.log(`[${timestamp}] ${serviceName} | ${formattedMessage}`);
|
|
87
87
|
if (stack)
|
|
@@ -96,7 +96,7 @@ var log = {
|
|
|
96
96
|
// src/hono/hono.execution.ts
|
|
97
97
|
function proceedUnhandledError(error) {
|
|
98
98
|
const errorId = generateRandomString(6);
|
|
99
|
-
const errorMessage = error instanceof Error ? error.
|
|
99
|
+
const errorMessage = error instanceof Error ? error.stack ?? error.message : JSON.stringify(error);
|
|
100
100
|
log.error(`Unhandled error: ${red2(errorMessage)}`, errorId);
|
|
101
101
|
return failure({ status: 500, error: `Internal server error | ${errorId}` });
|
|
102
102
|
}
|
|
@@ -182,7 +182,7 @@ var asQueryBoolean = (schema) => {
|
|
|
182
182
|
};
|
|
183
183
|
|
|
184
184
|
// src/zod-jwt/zod-jwt.schemas.ts
|
|
185
|
-
import { sign as signJWT, verify as verifyJWT } from "hono/jwt";
|
|
185
|
+
import { decode as decodeJWT, sign as signJWT, verify as verifyJWT } from "hono/jwt";
|
|
186
186
|
var ZodJWTService = class {
|
|
187
187
|
payloadSchema;
|
|
188
188
|
algorithm;
|
|
@@ -200,13 +200,22 @@ var ZodJWTService = class {
|
|
|
200
200
|
const exp = Math.floor(Date.now() / 1e3) + (options?.expiresInSeconds ?? this.defaultExpirationSeconds);
|
|
201
201
|
return signJWT({ ...payload, exp }, secret, this.algorithm);
|
|
202
202
|
}
|
|
203
|
+
/**
|
|
204
|
+
* Decodes a JWT token, and validates the payload against the Zod schema.
|
|
205
|
+
* Note: This method does NOT verify the token's signature or check for expiration.
|
|
206
|
+
*/
|
|
207
|
+
async decode(token) {
|
|
208
|
+
const { payload } = decodeJWT(token);
|
|
209
|
+
const { success: success2, data } = await this.payloadSchema.safeParseAsync(payload);
|
|
210
|
+
return success2 ? data : null;
|
|
211
|
+
}
|
|
203
212
|
/**
|
|
204
213
|
* Verifies and decodes a JWT token using the provided secret and configured algorithm,
|
|
205
214
|
* then validates the payload against the Zod schema, also throws on expired tokens.
|
|
206
215
|
*/
|
|
207
216
|
async verifyOrThrow(token, secret) {
|
|
208
217
|
const payload = await verifyJWT(token, secret, this.algorithm);
|
|
209
|
-
return this.payloadSchema.
|
|
218
|
+
return this.payloadSchema.parseAsync(payload);
|
|
210
219
|
}
|
|
211
220
|
};
|
|
212
221
|
export {
|