@kalutskii/foundation 0.1.11 → 0.2.1
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 +16 -2
- package/dist/index.js +24 -0
- package/package.json +1 -1
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,25 @@ async function safeExecute(fn, onError) {
|
|
|
141
144
|
}
|
|
142
145
|
}
|
|
143
146
|
|
|
147
|
+
// src/hono/hono.validation.ts
|
|
148
|
+
var FALLBACK_ERROR_MESSAGE = "Failed to read payload";
|
|
149
|
+
var requestReaders = {
|
|
150
|
+
json: async (c) => c.req.json(),
|
|
151
|
+
query: (c) => c.req.query(),
|
|
152
|
+
param: (c) => c.req.param()
|
|
153
|
+
};
|
|
154
|
+
async function requirePayload(c, target, schema) {
|
|
155
|
+
return safeExecute(async () => {
|
|
156
|
+
const rawPayload = await requestReaders[target](c);
|
|
157
|
+
const parsedPayload = await schema.safeParseAsync(rawPayload);
|
|
158
|
+
if (parsedPayload.success)
|
|
159
|
+
return parsedPayload.data;
|
|
160
|
+
throw new HTTPException2(400, { message: parsedPayload.error.message });
|
|
161
|
+
}, () => {
|
|
162
|
+
throw new HTTPException2(400, { message: FALLBACK_ERROR_MESSAGE });
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
|
|
144
166
|
// src/utilities/serialization.utilities.ts
|
|
145
167
|
import { z } from "zod";
|
|
146
168
|
var asQueryNumber = (schema) => z.preprocess((v) => typeof v === "string" ? Number(v) : v, schema);
|
|
@@ -208,6 +230,8 @@ export {
|
|
|
208
230
|
honoLoggingHandler,
|
|
209
231
|
log,
|
|
210
232
|
onHandlerError,
|
|
233
|
+
requestReaders,
|
|
234
|
+
requirePayload,
|
|
211
235
|
respond,
|
|
212
236
|
safeExecute,
|
|
213
237
|
success
|