@kalutskii/foundation 0.2.2 → 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 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 >= (min || 0) && status <= (max || 600));
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).padEnd(18);
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.message + error.stack : JSON.stringify(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
  }
@@ -145,7 +145,7 @@ async function safeExecute(fn, onError) {
145
145
  }
146
146
 
147
147
  // src/hono/hono.validation.ts
148
- var FALLBACK_ERROR_MESSAGE = "Failed to read payload";
148
+ var FALLBACK_ERROR_MESSAGE = "Failed to decode request payload, please ensure it is correctly formatted.";
149
149
  var requestReaders = {
150
150
  json: async (c) => safeExecute(() => c.req.json(), () => {
151
151
  throw new HTTPException2(400, { message: FALLBACK_ERROR_MESSAGE });
@@ -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.parse(payload);
218
+ return this.payloadSchema.parseAsync(payload);
210
219
  }
211
220
  };
212
221
  export {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kalutskii/foundation",
3
- "version": "0.2.2",
3
+ "version": "0.3.0",
4
4
  "description": "Typescript collection of most common utilities, schemas and functions among private projects.",
5
5
  "type": "module",
6
6
  "repository": {