@kalutskii/foundation 0.1.10 → 0.2.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 +48 -3
- package/dist/index.js +51 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { ErrorHandler, MiddlewareHandler, Context, TypedResponse } from 'hono';
|
|
2
|
-
import { z, ZodNumber } from 'zod';
|
|
1
|
+
import { ErrorHandler, MiddlewareHandler, Context, TypedResponse, ValidationTargets } from 'hono';
|
|
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];
|
|
@@ -86,6 +87,20 @@ declare function respond<T extends object = Record<string, never>, S extends Suc
|
|
|
86
87
|
data?: T;
|
|
87
88
|
}): Response & TypedResponse<APISuccess<SerializeDates<T>> | APIError, S, 'json'>;
|
|
88
89
|
|
|
90
|
+
/** The subset of Hono validation targets supported for payload extraction. */
|
|
91
|
+
type PayloadTarget = Extract<keyof ValidationTargets, 'json' | 'query' | 'param'>;
|
|
92
|
+
/** Raw payload readers for each supported target, keyed by {@link PayloadTarget}. */
|
|
93
|
+
declare const requestReaders: {
|
|
94
|
+
json: (c: Context) => Promise<unknown>;
|
|
95
|
+
query: (c: Context) => unknown;
|
|
96
|
+
param: (c: Context) => unknown;
|
|
97
|
+
};
|
|
98
|
+
/**
|
|
99
|
+
* Reads the raw payload from the request and validates it against the provided Zod schema.
|
|
100
|
+
* Throws an `HTTPException(400)` when validation fails or when the payload cannot be read.
|
|
101
|
+
*/
|
|
102
|
+
declare function requirePayload<TSchema extends z.ZodTypeAny>(c: Context, target: PayloadTarget, schema: TSchema): Promise<z.infer<TSchema>>;
|
|
103
|
+
|
|
89
104
|
/**
|
|
90
105
|
* Returns the current time in the specified timezone.
|
|
91
106
|
* Example: getZonedTime({ tz: 'America/New_York' }) = new Date('2026-03-15T12:00:00Z')
|
|
@@ -150,4 +165,34 @@ declare const log: {
|
|
|
150
165
|
error: (message: string, service?: string, stack?: string) => void;
|
|
151
166
|
};
|
|
152
167
|
|
|
153
|
-
|
|
168
|
+
/** Options for configuring the JWT service. */
|
|
169
|
+
type JWTServiceOptions = {
|
|
170
|
+
/** The algorithm to use for signing and verifying JWTs. Defaults to 'HS256'. */
|
|
171
|
+
algorithm?: SymmetricAlgorithm;
|
|
172
|
+
/** The default number of seconds until a signed JWT expires. Defaults to 900 seconds (15 minutes). */
|
|
173
|
+
defaultExpirationSeconds?: number;
|
|
174
|
+
};
|
|
175
|
+
/** Options for signing a JWT. */
|
|
176
|
+
type JWTSignOptions = {
|
|
177
|
+
/** The number of seconds until the JWT expires. */
|
|
178
|
+
expiresInSeconds?: number;
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
declare class ZodJWTService<TSchema extends z$1.ZodObject<z$1.ZodRawShape>> {
|
|
182
|
+
readonly payloadSchema: TSchema;
|
|
183
|
+
protected readonly algorithm: SymmetricAlgorithm;
|
|
184
|
+
protected readonly defaultExpirationSeconds: number;
|
|
185
|
+
constructor(payloadSchema: TSchema, options?: JWTServiceOptions);
|
|
186
|
+
/**
|
|
187
|
+
* Signs a JWT token with the provided payload and secret, using the configured algorithm and expiration settings.
|
|
188
|
+
* Example usage: `const accessToken = await JWTServiceInstance.sign({ userId: 123 }, 'my-secret', { expiresInSeconds: 300 });`
|
|
189
|
+
*/
|
|
190
|
+
sign(payload: z$1.infer<TSchema>, secret: string, options?: JWTSignOptions): Promise<string>;
|
|
191
|
+
/**
|
|
192
|
+
* Verifies and decodes a JWT token using the provided secret and configured algorithm,
|
|
193
|
+
* then validates the payload against the Zod schema, also throws on expired tokens.
|
|
194
|
+
*/
|
|
195
|
+
verifyOrThrow(token: string, secret: string): Promise<z$1.infer<TSchema>>;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
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, requestReaders, requirePayload, respond, safeExecute, success };
|
package/dist/index.js
CHANGED
|
@@ -130,6 +130,9 @@ function respond(c, options) {
|
|
|
130
130
|
return c.json(success({ status: options.status, data: options.data ?? {} }), options.status);
|
|
131
131
|
}
|
|
132
132
|
|
|
133
|
+
// src/hono/hono.validation.ts
|
|
134
|
+
import { HTTPException as HTTPException2 } from "hono/http-exception";
|
|
135
|
+
|
|
133
136
|
// src/utilities/execution.utilities.ts
|
|
134
137
|
async function safeExecute(fn, onError) {
|
|
135
138
|
try {
|
|
@@ -141,6 +144,22 @@ async function safeExecute(fn, onError) {
|
|
|
141
144
|
}
|
|
142
145
|
}
|
|
143
146
|
|
|
147
|
+
// src/hono/hono.validation.ts
|
|
148
|
+
var requestReaders = {
|
|
149
|
+
json: async (c) => c.req.json(),
|
|
150
|
+
query: (c) => c.req.query(),
|
|
151
|
+
param: (c) => c.req.param()
|
|
152
|
+
};
|
|
153
|
+
async function requirePayload(c, target, schema) {
|
|
154
|
+
return safeExecute(async () => {
|
|
155
|
+
const rawPayload = await requestReaders[target](c);
|
|
156
|
+
const parsedPayload = await schema.safeParseAsync(rawPayload);
|
|
157
|
+
if (parsedPayload.success)
|
|
158
|
+
return parsedPayload.data;
|
|
159
|
+
throw new HTTPException2(400, { message: parsedPayload.error.message });
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
|
|
144
163
|
// src/utilities/serialization.utilities.ts
|
|
145
164
|
import { z } from "zod";
|
|
146
165
|
var asQueryNumber = (schema) => z.preprocess((v) => typeof v === "string" ? Number(v) : v, schema);
|
|
@@ -160,9 +179,39 @@ var asQueryBoolean = (schema) => {
|
|
|
160
179
|
return value;
|
|
161
180
|
}, schema);
|
|
162
181
|
};
|
|
182
|
+
|
|
183
|
+
// src/zod-jwt/zod-jwt.schemas.ts
|
|
184
|
+
import { sign as signJWT, verify as verifyJWT } from "hono/jwt";
|
|
185
|
+
var ZodJWTService = class {
|
|
186
|
+
payloadSchema;
|
|
187
|
+
algorithm;
|
|
188
|
+
defaultExpirationSeconds;
|
|
189
|
+
constructor(payloadSchema, options) {
|
|
190
|
+
this.payloadSchema = payloadSchema;
|
|
191
|
+
this.algorithm = options?.algorithm ?? "HS256";
|
|
192
|
+
this.defaultExpirationSeconds = options?.defaultExpirationSeconds ?? 60 * 15;
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Signs a JWT token with the provided payload and secret, using the configured algorithm and expiration settings.
|
|
196
|
+
* Example usage: `const accessToken = await JWTServiceInstance.sign({ userId: 123 }, 'my-secret', { expiresInSeconds: 300 });`
|
|
197
|
+
*/
|
|
198
|
+
async sign(payload, secret, options) {
|
|
199
|
+
const exp = Math.floor(Date.now() / 1e3) + (options?.expiresInSeconds ?? this.defaultExpirationSeconds);
|
|
200
|
+
return signJWT({ ...payload, exp }, secret, this.algorithm);
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Verifies and decodes a JWT token using the provided secret and configured algorithm,
|
|
204
|
+
* then validates the payload against the Zod schema, also throws on expired tokens.
|
|
205
|
+
*/
|
|
206
|
+
async verifyOrThrow(token, secret) {
|
|
207
|
+
const payload = await verifyJWT(token, secret, this.algorithm);
|
|
208
|
+
return this.payloadSchema.parse(payload);
|
|
209
|
+
}
|
|
210
|
+
};
|
|
163
211
|
export {
|
|
164
212
|
EXCEPTION_STATUS_CODES,
|
|
165
213
|
SUCCESS_STATUS_CODES,
|
|
214
|
+
ZodJWTService,
|
|
166
215
|
asQueryBoolean,
|
|
167
216
|
asQueryNumber,
|
|
168
217
|
failure,
|
|
@@ -178,6 +227,8 @@ export {
|
|
|
178
227
|
honoLoggingHandler,
|
|
179
228
|
log,
|
|
180
229
|
onHandlerError,
|
|
230
|
+
requestReaders,
|
|
231
|
+
requirePayload,
|
|
181
232
|
respond,
|
|
182
233
|
safeExecute,
|
|
183
234
|
success
|