@kalutskii/foundation 0.1.9 → 0.1.11

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
@@ -1,6 +1,7 @@
1
1
  import { ErrorHandler, MiddlewareHandler, Context, TypedResponse } from 'hono';
2
- import { z, ZodNumber } from 'zod';
2
+ import z$1, { z, ZodNumber } from 'zod';
3
3
  import { Locale } from 'date-fns';
4
+ import { SymmetricAlgorithm } from 'hono/utils/jwt/jwa';
4
5
 
5
6
  declare const SUCCESS_STATUS_CODES: readonly [200, 201, 202, 307];
6
7
  declare const EXCEPTION_STATUS_CODES: readonly [400, 401, 403, 404, 405, 409, 500];
@@ -150,4 +151,34 @@ declare const log: {
150
151
  error: (message: string, service?: string, stack?: string) => void;
151
152
  };
152
153
 
153
- export { type APIContractData, type APIContractError, type APIContractResult, type APIError, type APISuccess, EXCEPTION_STATUS_CODES, type ExceptionStatusCode, type FetchResult, SUCCESS_STATUS_CODES, type SerializeDates, type SuccessStatusCode, asQueryBoolean, asQueryNumber, failure, fetchAndThrow, fetchSafely, formatTime, generateRandomString, getColoredHTTPStatus, getFormattedDate, getFormattedTime, getUTCOffset, getZonedTime, honoLoggingHandler, log, onHandlerError, respond, safeExecute, success };
154
+ /** Options for configuring the JWT service. */
155
+ type JWTServiceOptions = {
156
+ /** The algorithm to use for signing and verifying JWTs. Defaults to 'HS256'. */
157
+ algorithm?: SymmetricAlgorithm;
158
+ /** The default number of seconds until a signed JWT expires. Defaults to 900 seconds (15 minutes). */
159
+ defaultExpirationSeconds?: number;
160
+ };
161
+ /** Options for signing a JWT. */
162
+ type JWTSignOptions = {
163
+ /** The number of seconds until the JWT expires. */
164
+ expiresInSeconds?: number;
165
+ };
166
+
167
+ declare class ZodJWTService<TSchema extends z$1.ZodObject<z$1.ZodRawShape>> {
168
+ readonly payloadSchema: TSchema;
169
+ protected readonly algorithm: SymmetricAlgorithm;
170
+ protected readonly defaultExpirationSeconds: number;
171
+ constructor(payloadSchema: TSchema, options?: JWTServiceOptions);
172
+ /**
173
+ * Signs a JWT token with the provided payload and secret, using the configured algorithm and expiration settings.
174
+ * Example usage: `const accessToken = await JWTServiceInstance.sign({ userId: 123 }, 'my-secret', { expiresInSeconds: 300 });`
175
+ */
176
+ sign(payload: z$1.infer<TSchema>, secret: string, options?: JWTSignOptions): Promise<string>;
177
+ /**
178
+ * Verifies and decodes a JWT token using the provided secret and configured algorithm,
179
+ * then validates the payload against the Zod schema, also throws on expired tokens.
180
+ */
181
+ verifyOrThrow(token: string, secret: string): Promise<z$1.infer<TSchema>>;
182
+ }
183
+
184
+ export { type APIContractData, type APIContractError, type APIContractResult, type APIError, type APISuccess, EXCEPTION_STATUS_CODES, type ExceptionStatusCode, type FetchResult, type JWTServiceOptions, type JWTSignOptions, SUCCESS_STATUS_CODES, type SerializeDates, type SuccessStatusCode, ZodJWTService, asQueryBoolean, asQueryNumber, failure, fetchAndThrow, fetchSafely, formatTime, generateRandomString, getColoredHTTPStatus, getFormattedDate, getFormattedTime, getUTCOffset, getZonedTime, honoLoggingHandler, log, onHandlerError, respond, safeExecute, success };
package/dist/index.js CHANGED
@@ -160,9 +160,39 @@ var asQueryBoolean = (schema) => {
160
160
  return value;
161
161
  }, schema);
162
162
  };
163
+
164
+ // src/zod-jwt/zod-jwt.schemas.ts
165
+ import { sign as signJWT, verify as verifyJWT } from "hono/jwt";
166
+ var ZodJWTService = class {
167
+ payloadSchema;
168
+ algorithm;
169
+ defaultExpirationSeconds;
170
+ constructor(payloadSchema, options) {
171
+ this.payloadSchema = payloadSchema;
172
+ this.algorithm = options?.algorithm ?? "HS256";
173
+ this.defaultExpirationSeconds = options?.defaultExpirationSeconds ?? 60 * 15;
174
+ }
175
+ /**
176
+ * Signs a JWT token with the provided payload and secret, using the configured algorithm and expiration settings.
177
+ * Example usage: `const accessToken = await JWTServiceInstance.sign({ userId: 123 }, 'my-secret', { expiresInSeconds: 300 });`
178
+ */
179
+ async sign(payload, secret, options) {
180
+ const exp = Math.floor(Date.now() / 1e3) + (options?.expiresInSeconds ?? this.defaultExpirationSeconds);
181
+ return signJWT({ ...payload, exp }, secret, this.algorithm);
182
+ }
183
+ /**
184
+ * Verifies and decodes a JWT token using the provided secret and configured algorithm,
185
+ * then validates the payload against the Zod schema, also throws on expired tokens.
186
+ */
187
+ async verifyOrThrow(token, secret) {
188
+ const payload = await verifyJWT(token, secret, this.algorithm);
189
+ return this.payloadSchema.parse(payload);
190
+ }
191
+ };
163
192
  export {
164
193
  EXCEPTION_STATUS_CODES,
165
194
  SUCCESS_STATUS_CODES,
195
+ ZodJWTService,
166
196
  asQueryBoolean,
167
197
  asQueryNumber,
168
198
  failure,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kalutskii/foundation",
3
- "version": "0.1.9",
3
+ "version": "0.1.11",
4
4
  "description": "Typescript collection of most common utilities, schemas and functions among private projects.",
5
5
  "type": "module",
6
6
  "repository": {