@kalutskii/foundation 0.6.12 → 0.6.16

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,8 +1,8 @@
1
- import { SQL } from 'drizzle-orm';
1
+ import { SQL, Simplify as Simplify$1 } from 'drizzle-orm';
2
2
  import { ErrorHandler, MiddlewareHandler, Context, TypedResponse } from 'hono';
3
3
  import { Locale } from 'date-fns';
4
4
  import { SymmetricAlgorithm } from 'hono/utils/jwt/jwa';
5
- import z from 'zod';
5
+ import z, { ZodObject, ZodRawShape, z as z$1 } from 'zod';
6
6
 
7
7
  /**
8
8
  * Builds a Drizzle SQL `where` condition from a plain object.
@@ -66,6 +66,20 @@ type QueryPrimitive = string | number | boolean | bigint | Date;
66
66
  type AsQuery<T> = T extends null | undefined ? T : T extends QueryPrimitive ? string : T extends readonly (infer Item)[] ? AsQuery<Item>[] : T extends object ? {
67
67
  [Key in keyof T]: AsQuery<T[Key]>;
68
68
  } : string;
69
+ /**
70
+ * Utility type that ensures at least one property from the specified
71
+ * keys of a given type T is required, while the rest remain optional.
72
+ *
73
+ * This is useful for scenarios where you want to enforce that at least one
74
+ * of several optional (nullable) properties must be provided in an object.
75
+ *
76
+ * ```ts
77
+ * type Example = AtLeastOne<{ a?: string; b?: number; c?: boolean }>;
78
+ * // Valid: { a: "hello" }, { b: 42 }, { c: true }, { a: "hello", b: 42 }
79
+ * // Invalid: {}, { a: undefined, b: undefined, c: undefined }
80
+ * ```
81
+ */
82
+ type AtLeastOne<T, Keys extends keyof T = keyof T> = Keys extends keyof T ? Simplify$1<Required<Pick<T, Keys>> & Partial<Omit<T, Keys>>> : never;
69
83
 
70
84
  /**
71
85
  * Wraps c.json with a typed success payload / (or void data) & possible APIError response.
@@ -212,20 +226,6 @@ declare const log: {
212
226
  type Simplify<T> = {
213
227
  [K in keyof T]: T[K];
214
228
  } & {};
215
- /**
216
- * Utility type that ensures at least one property from the specified
217
- * keys of a given type T is required, while the rest remain optional.
218
- *
219
- * This is useful for scenarios where you want to enforce that at least one
220
- * of several optional (nullable) properties must be provided in an object.
221
- *
222
- * ```ts
223
- * type Example = AtLeastOne<{ a?: string; b?: number; c?: boolean }>;
224
- * // Valid: { a: "hello" }, { b: 42 }, { c: true }, { a: "hello", b: 42 }
225
- * // Invalid: {}, { a: undefined, b: undefined, c: undefined }
226
- * ```
227
- */
228
- type AtLeastOne<T, Keys extends keyof T = keyof T> = Keys extends keyof T ? Simplify<Required<Pick<T, Keys>> & Partial<Omit<T, Keys>>> : never;
229
229
 
230
230
  /** Options for configuring the JWT service. */
231
231
  type JWTServiceOptions = {
@@ -280,8 +280,8 @@ declare class ZodJWTService<TPayloadOrSchema> {
280
280
  }
281
281
 
282
282
  declare const zodPaginationSchema: z.ZodObject<{
283
- offset: z.ZodOptional<z.ZodPreprocess<z.ZodNumber>>;
284
- limit: z.ZodOptional<z.ZodPreprocess<z.ZodNumber>>;
283
+ offset: z.ZodOptional<z.ZodCoercedNumber<unknown>>;
284
+ limit: z.ZodOptional<z.ZodCoercedNumber<unknown>>;
285
285
  }, z.core.$strip>;
286
286
  /**
287
287
  * Reusable pagination fields for flat query schemas, allowing for
@@ -291,8 +291,8 @@ declare const zodPaginationSchema: z.ZodObject<{
291
291
  * or use `zodPaginationSchema.extend(...)` when building directly from the schema.
292
292
  */
293
293
  declare const zodPaginationShape: {
294
- offset: z.ZodOptional<z.ZodPreprocess<z.ZodNumber>>;
295
- limit: z.ZodOptional<z.ZodPreprocess<z.ZodNumber>>;
294
+ offset: z.ZodOptional<z.ZodCoercedNumber<unknown>>;
295
+ limit: z.ZodOptional<z.ZodCoercedNumber<unknown>>;
296
296
  };
297
297
  /**
298
298
  * TypeScript type representing the pagination options validated by the `zodPaginationSchema`.
@@ -315,6 +315,18 @@ type ZodPaginationOptions = z.infer<typeof zodPaginationSchema>;
315
315
  */
316
316
  declare const refinePagination: (options?: ZodPaginationOptions) => ZodPaginationOptions;
317
317
 
318
+ /**
319
+ * Wraps a partial Zod object schema with:
320
+ * 1. A runtime check ensuring at least one field is non-undefined.
321
+ * 2. A `.transform()` that narrows the output type to `AtLeastOne<T>`,
322
+ * making it directly assignable to domain `*Select` and `*Update` types without casting.
323
+ *
324
+ * ```ts
325
+ * zQuery(zodAtLeastOne(userSelectSchema)) // result: UserSelect ✓
326
+ * ```
327
+ */
328
+ declare const zodAtLeastOne: <T extends ZodObject<ZodRawShape>>(schema: T) => z$1.ZodPipe<T, z$1.ZodTransform<Awaited<AtLeastOne<z$1.core.output<T>>>, z$1.core.output<T>>>;
329
+
318
330
  declare const parseQueryValue: (value: unknown) => unknown;
319
331
  /**
320
332
  * Preprocesses query-like values before Zod validation.
@@ -341,4 +353,4 @@ declare const asQuery: <T extends z.ZodTypeAny>(schema: T) => z.ZodPreprocess<T>
341
353
  */
342
354
  declare const isPlainObject: (value: unknown) => value is Record<string, unknown>;
343
355
 
344
- 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 Payload, type PayloadSchema, SUCCESS_STATUS_CODES, type Simplify, type SuccessStatusCode, ZodJWTService, type ZodPaginationOptions, asQuery, failure, fetchAndThrow, fetchSafely, formatTime, generateRandomString, getColoredHTTPStatus, getFormattedDate, getFormattedTime, getUTCOffset, getZonedTime, honoLoggingHandler, isPlainObject, log, measureExecutionTime, onHandlerError, parseQueryValue, refinePagination, respond, safeExecute, sqlWhere, success, zodPaginationSchema, zodPaginationShape };
356
+ 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 Payload, type PayloadSchema, SUCCESS_STATUS_CODES, type Simplify, type SuccessStatusCode, ZodJWTService, type ZodPaginationOptions, asQuery, failure, fetchAndThrow, fetchSafely, formatTime, generateRandomString, getColoredHTTPStatus, getFormattedDate, getFormattedTime, getUTCOffset, getZonedTime, honoLoggingHandler, isPlainObject, log, measureExecutionTime, onHandlerError, parseQueryValue, refinePagination, respond, safeExecute, sqlWhere, success, zodAtLeastOne, zodPaginationSchema, zodPaginationShape };
package/dist/index.js CHANGED
@@ -215,17 +215,32 @@ var refinePagination = (options) => ({
215
215
  });
216
216
 
217
217
  // src/zod-pagination/zod-pagination.schemas.ts
218
- import z2 from "zod";
218
+ import z from "zod";
219
+ var zodPaginationSchema = z.object({
220
+ /** Integer representing the starting point for pagination. */
221
+ offset: z.coerce.number().int().nonnegative().optional(),
222
+ /** Integer representing the maximum number of items to return. */
223
+ limit: z.coerce.number().int().positive().optional()
224
+ });
225
+ var zodPaginationShape = zodPaginationSchema.shape;
219
226
 
220
227
  // src/zod-validation/zod-validation.refiners.ts
221
- import z from "zod";
228
+ var DEFAULT_ERROR_MESSAGE = "Invalid input. At least one field must be provided.";
229
+ var zodAtLeastOne = (schema) => schema.superRefine((val, ctx) => {
230
+ const hasValue = Object.values(val).some((v) => v !== void 0);
231
+ if (!hasValue)
232
+ ctx.addIssue({ code: "custom", message: DEFAULT_ERROR_MESSAGE });
233
+ }).transform((val) => val);
234
+
235
+ // src/zod-validation/zod-validation.parsing.ts
236
+ import z2 from "zod";
222
237
 
223
238
  // src/zod-validation/zod-validation.utilities.ts
224
239
  var isPlainObject = (value) => {
225
240
  return typeof value === "object" && value !== null && !Array.isArray(value);
226
241
  };
227
242
 
228
- // src/zod-validation/zod-validation.refiners.ts
243
+ // src/zod-validation/zod-validation.parsing.ts
229
244
  var parseQueryValue = (value) => {
230
245
  if (Array.isArray(value))
231
246
  return value.map(parseQueryValue);
@@ -245,16 +260,7 @@ var parseQueryValue = (value) => {
245
260
  return Number(normalized);
246
261
  return value;
247
262
  };
248
- var asQuery = (schema) => z.preprocess(parseQueryValue, schema);
249
-
250
- // src/zod-pagination/zod-pagination.schemas.ts
251
- var zodPaginationSchema = z2.object({
252
- /** Integer representing the starting point for pagination. */
253
- offset: asQuery(z2.number().int().nonnegative()).optional(),
254
- /** Integer representing the maximum number of items to return. */
255
- limit: asQuery(z2.number().int().positive()).optional()
256
- });
257
- var zodPaginationShape = zodPaginationSchema.shape;
263
+ var asQuery = (schema) => z2.preprocess(parseQueryValue, schema);
258
264
  export {
259
265
  EXCEPTION_STATUS_CODES,
260
266
  SUCCESS_STATUS_CODES,
@@ -281,6 +287,7 @@ export {
281
287
  safeExecute,
282
288
  sqlWhere,
283
289
  success,
290
+ zodAtLeastOne,
284
291
  zodPaginationSchema,
285
292
  zodPaginationShape
286
293
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kalutskii/foundation",
3
- "version": "0.6.12",
3
+ "version": "0.6.16",
4
4
  "description": "Typescript collection of most common utilities, schemas and functions among private projects.",
5
5
  "type": "module",
6
6
  "repository": {