@kalutskii/foundation 0.1.11 → 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 CHANGED
@@ -1,4 +1,4 @@
1
- import { ErrorHandler, MiddlewareHandler, Context, TypedResponse } from 'hono';
1
+ import { ErrorHandler, MiddlewareHandler, Context, TypedResponse, ValidationTargets } from 'hono';
2
2
  import z$1, { z, ZodNumber } from 'zod';
3
3
  import { Locale } from 'date-fns';
4
4
  import { SymmetricAlgorithm } from 'hono/utils/jwt/jwa';
@@ -87,6 +87,20 @@ declare function respond<T extends object = Record<string, never>, S extends Suc
87
87
  data?: T;
88
88
  }): Response & TypedResponse<APISuccess<SerializeDates<T>> | APIError, S, 'json'>;
89
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
+
90
104
  /**
91
105
  * Returns the current time in the specified timezone.
92
106
  * Example: getZonedTime({ tz: 'America/New_York' }) = new Date('2026-03-15T12:00:00Z')
@@ -181,4 +195,4 @@ declare class ZodJWTService<TSchema extends z$1.ZodObject<z$1.ZodRawShape>> {
181
195
  verifyOrThrow(token: string, secret: string): Promise<z$1.infer<TSchema>>;
182
196
  }
183
197
 
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 };
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);
@@ -208,6 +227,8 @@ export {
208
227
  honoLoggingHandler,
209
228
  log,
210
229
  onHandlerError,
230
+ requestReaders,
231
+ requirePayload,
211
232
  respond,
212
233
  safeExecute,
213
234
  success
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kalutskii/foundation",
3
- "version": "0.1.11",
3
+ "version": "0.2.0",
4
4
  "description": "Typescript collection of most common utilities, schemas and functions among private projects.",
5
5
  "type": "module",
6
6
  "repository": {