@kalutskii/foundation 0.5.1 → 0.5.2
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 -28
- package/dist/index.js +8 -2
- package/package.json +2 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ErrorHandler, MiddlewareHandler, Context, TypedResponse } from 'hono';
|
|
2
|
-
import z
|
|
2
|
+
import z from 'zod';
|
|
3
3
|
import { Locale } from 'date-fns';
|
|
4
4
|
import { SymmetricAlgorithm } from 'hono/utils/jwt/jwa';
|
|
5
5
|
|
|
@@ -51,25 +51,6 @@ type FetchResult<T> = {
|
|
|
51
51
|
type SerializeDates<T> = T extends Date ? string : T extends (infer U)[] ? SerializeDates<U>[] : T extends readonly (infer U)[] ? readonly SerializeDates<U>[] : T extends object ? {
|
|
52
52
|
[K in keyof T]: SerializeDates<T[K]>;
|
|
53
53
|
} : T;
|
|
54
|
-
type QueryPrimitive = string | number | boolean | bigint | Date | null | undefined;
|
|
55
|
-
/**
|
|
56
|
-
* Type utility that recursively transforms all fields to string, as well as handling arrays and objects.
|
|
57
|
-
* This is useful for serializing complex data structures into query parameters, which must be strings.
|
|
58
|
-
* Example usage: `AsQuery<{ id: number; name: string; tags: string[] }>` = `{ id: string; name: string; tags: string[] }`
|
|
59
|
-
*/
|
|
60
|
-
type AsQuery<T> = T extends QueryPrimitive ? string : T extends readonly (infer Item)[] ? AsQuery<Item>[] : T extends object ? {
|
|
61
|
-
[Key in keyof T]: AsQuery<T[Key]>;
|
|
62
|
-
} : string;
|
|
63
|
-
/**
|
|
64
|
-
* Transforms a string to a number (when passing query parameters).
|
|
65
|
-
* Example usage: `asQueryNumber(z.number())('123') = 123`
|
|
66
|
-
*/
|
|
67
|
-
declare const asQueryNumber: <T extends ZodNumber>(schema: T) => z.ZodPreprocess<T>;
|
|
68
|
-
/**
|
|
69
|
-
* Transforms various string representations of boolean values into actual booleans.
|
|
70
|
-
* See POSITIVE_VALUES and NEGATIVE_VALUES arrays below for supported inputs.
|
|
71
|
-
*/
|
|
72
|
-
declare const asQueryBoolean: <T extends z.ZodTypeAny>(schema: T) => z.ZodPreprocess<T>;
|
|
73
54
|
|
|
74
55
|
/**
|
|
75
56
|
* Wraps c.json with a typed success payload / (or void data) & possible APIError response.
|
|
@@ -122,15 +103,15 @@ declare function fetchAndThrow<TResult extends APIContractResult<unknown>>(fetch
|
|
|
122
103
|
* });
|
|
123
104
|
* ```
|
|
124
105
|
*/
|
|
125
|
-
declare const paginationSchema: z
|
|
126
|
-
offset: z
|
|
127
|
-
limit: z
|
|
128
|
-
}, z
|
|
106
|
+
declare const paginationSchema: z.ZodObject<{
|
|
107
|
+
offset: z.ZodOptional<z.ZodNumber>;
|
|
108
|
+
limit: z.ZodOptional<z.ZodNumber>;
|
|
109
|
+
}, z.core.$strip>;
|
|
129
110
|
/**
|
|
130
111
|
* A TypeScript type representing the pagination options validated by the `paginationSchema`.
|
|
131
112
|
* Read documentation for `paginationSchema` for more details on the structure and usage of this type.
|
|
132
113
|
*/
|
|
133
|
-
type PaginationOptions = z
|
|
114
|
+
type PaginationOptions = z.infer<typeof paginationSchema>;
|
|
134
115
|
|
|
135
116
|
/**
|
|
136
117
|
* A utility function that extracts pagination options from the provided input.
|
|
@@ -267,6 +248,45 @@ type XOR<T, U> = Simplify<T & {
|
|
|
267
248
|
*/
|
|
268
249
|
type AtLeastOne<T, Keys extends keyof T = keyof T> = Keys extends keyof T ? Simplify<Required<Pick<T, Keys>> & Partial<Omit<T, Keys>>> : never;
|
|
269
250
|
|
|
251
|
+
/**
|
|
252
|
+
* A Zod refinement function that ensures at least one of the specified keys in a Zod object schema is defined.
|
|
253
|
+
* This is useful for validating input where at least one of several optional fields must be provided.
|
|
254
|
+
*
|
|
255
|
+
* ```ts
|
|
256
|
+
* const mySchema = atLeastOneDefined(
|
|
257
|
+
* z.object({
|
|
258
|
+
* name: z.string().optional(),
|
|
259
|
+
* email: z.string().email().optional(),
|
|
260
|
+
* phone: z.string().optional(),
|
|
261
|
+
* })
|
|
262
|
+
* );
|
|
263
|
+
*
|
|
264
|
+
* // Valid: { name: "Alice" }, { email: "alice@example.com" }, { phone: "123-456-7890" }
|
|
265
|
+
* // Invalid: {}, { name: undefined, email: undefined, phone: undefined }
|
|
266
|
+
* ```
|
|
267
|
+
*/
|
|
268
|
+
declare const atLeastOneDefined: <T extends z.ZodRawShape>(schema: z.ZodObject<T>) => z.ZodObject<T, z.core.$strip>;
|
|
269
|
+
/**
|
|
270
|
+
* Transforms a string to a number (when passing query parameters).
|
|
271
|
+
* Example usage: `asQueryNumber(z.number())('123') = 123`
|
|
272
|
+
*/
|
|
273
|
+
declare const asQueryNumber: <T extends z.ZodNumber>(schema: T) => z.ZodPreprocess<T>;
|
|
274
|
+
/**
|
|
275
|
+
* Transforms various string representations of boolean values into actual booleans.
|
|
276
|
+
* See POSITIVE_VALUES and NEGATIVE_VALUES arrays below for supported inputs.
|
|
277
|
+
*/
|
|
278
|
+
declare const asQueryBoolean: <T extends z.ZodTypeAny>(schema: T) => z.ZodPreprocess<T>;
|
|
279
|
+
|
|
280
|
+
type QueryPrimitive = string | number | boolean | bigint | Date | null | undefined;
|
|
281
|
+
/**
|
|
282
|
+
* Type utility that recursively transforms all fields to string, as well as handling arrays and objects.
|
|
283
|
+
* This is useful for serializing complex data structures into query parameters, which must be strings.
|
|
284
|
+
* Example usage: `AsQuery<{ id: number; name: string; tags: string[] }>` = `{ id: string; name: string; tags: string[] }`
|
|
285
|
+
*/
|
|
286
|
+
type AsQuery<T> = T extends QueryPrimitive ? string : T extends readonly (infer Item)[] ? AsQuery<Item>[] : T extends object ? {
|
|
287
|
+
[Key in keyof T]: AsQuery<T[Key]>;
|
|
288
|
+
} : string;
|
|
289
|
+
|
|
270
290
|
/** Options for configuring the JWT service. */
|
|
271
291
|
type JWTServiceOptions = {
|
|
272
292
|
/** The algorithm to use for signing and verifying JWTs. Defaults to 'HS256'. */
|
|
@@ -280,9 +300,9 @@ type JWTSignOptions = {
|
|
|
280
300
|
expiresInSeconds?: number;
|
|
281
301
|
};
|
|
282
302
|
/** Utility types for inferring payload types from Zod schemas. */
|
|
283
|
-
type Payload<T> = T extends z
|
|
303
|
+
type Payload<T> = T extends z.ZodType ? z.infer<T> : T;
|
|
284
304
|
/** Utility type to extract the payload schema type from a Zod schema. */
|
|
285
|
-
type PayloadSchema<T> = T extends z
|
|
305
|
+
type PayloadSchema<T> = T extends z.ZodType ? T : never;
|
|
286
306
|
|
|
287
307
|
/**
|
|
288
308
|
* A service class for handling JWT operations with optional Zod schema validation for the payload.
|
|
@@ -315,4 +335,4 @@ declare class ZodJWTService<TPayloadOrSchema> {
|
|
|
315
335
|
verifyOrThrow(token: string, secret: string): Promise<Payload<TPayloadOrSchema>>;
|
|
316
336
|
}
|
|
317
337
|
|
|
318
|
-
export { type APIContractData, type APIContractError, type APIContractResult, type APIError, type APISuccess, type AsQuery, type AtLeastOne, EXCEPTION_STATUS_CODES, type ExceptionStatusCode, type FetchResult, type JWTServiceOptions, type JWTSignOptions, type MeasuredExecution, type PaginationOptions, type Payload, type PayloadSchema, SUCCESS_STATUS_CODES, type SerializeDates, type Simplify, type SuccessStatusCode, type XOR, ZodJWTService, asQueryBoolean, asQueryNumber, failure, fetchAndThrow, fetchSafely, formatTime, generateRandomString, getColoredHTTPStatus, getFormattedDate, getFormattedTime, getPagination, getUTCOffset, getZonedTime, honoLoggingHandler, log, measureExecutionTime, onHandlerError, paginationSchema, respond, safeExecute, success };
|
|
338
|
+
export { type APIContractData, type APIContractError, type APIContractResult, type APIError, type APISuccess, type AsQuery, type AtLeastOne, EXCEPTION_STATUS_CODES, type ExceptionStatusCode, type FetchResult, type JWTServiceOptions, type JWTSignOptions, type MeasuredExecution, type PaginationOptions, type Payload, type PayloadSchema, SUCCESS_STATUS_CODES, type SerializeDates, type Simplify, type SuccessStatusCode, type XOR, ZodJWTService, asQueryBoolean, asQueryNumber, atLeastOneDefined, failure, fetchAndThrow, fetchSafely, formatTime, generateRandomString, getColoredHTTPStatus, getFormattedDate, getFormattedTime, getPagination, getUTCOffset, getZonedTime, honoLoggingHandler, log, measureExecutionTime, onHandlerError, paginationSchema, respond, safeExecute, success };
|
package/dist/index.js
CHANGED
|
@@ -166,8 +166,13 @@ async function measureExecutionTime(execution) {
|
|
|
166
166
|
return { result, executionTime: Math.round(executionTime) };
|
|
167
167
|
}
|
|
168
168
|
|
|
169
|
-
// src/
|
|
170
|
-
import
|
|
169
|
+
// src/validation/validation.refiners.ts
|
|
170
|
+
import z2 from "zod";
|
|
171
|
+
var AT_LEAST_ONE_DEFINED_ERROR = (keys) => `At least one of the specified keys must be defined: ${keys.join(", ")}`;
|
|
172
|
+
var atLeastOneDefined = (schema) => {
|
|
173
|
+
const keys = Object.keys(schema.shape);
|
|
174
|
+
return schema.refine((value) => keys.some((key) => value[key] !== void 0 && value[key] !== null), { message: AT_LEAST_ONE_DEFINED_ERROR(keys) });
|
|
175
|
+
};
|
|
171
176
|
var asQueryNumber = (schema) => z2.preprocess((v) => typeof v === "string" ? Number(v) : v, schema);
|
|
172
177
|
var asQueryBoolean = (schema) => {
|
|
173
178
|
const POSITIVE_VALUES = ["1", "true", "yes", "y", "on"];
|
|
@@ -240,6 +245,7 @@ export {
|
|
|
240
245
|
ZodJWTService,
|
|
241
246
|
asQueryBoolean,
|
|
242
247
|
asQueryNumber,
|
|
248
|
+
atLeastOneDefined,
|
|
243
249
|
failure,
|
|
244
250
|
fetchAndThrow,
|
|
245
251
|
fetchSafely,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kalutskii/foundation",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.2",
|
|
4
4
|
"description": "Typescript collection of most common utilities, schemas and functions among private projects.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"repository": {
|
|
@@ -40,6 +40,7 @@
|
|
|
40
40
|
"esbuild-plugin-tsconfig-paths": "^1.0.2",
|
|
41
41
|
"eslint": "^10.2.1",
|
|
42
42
|
"eslint-config-prettier": "^10.1.8",
|
|
43
|
+
"jiti": "^2.7.0",
|
|
43
44
|
"prettier": "^3.8.1",
|
|
44
45
|
"tsup": "^8.5.1",
|
|
45
46
|
"typescript-eslint": "^8.58.2"
|