@kalutskii/foundation 0.6.13 → 0.6.17
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 +33 -21
- package/dist/index.js +12 -3
- package/package.json +1 -1
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.ZodCoercedNumber<unknown
|
|
284
|
-
limit: z.ZodOptional<z.ZodCoercedNumber<unknown
|
|
283
|
+
offset: z.ZodOptional<z.ZodDefault<z.ZodCoercedNumber<unknown>>>;
|
|
284
|
+
limit: z.ZodOptional<z.ZodDefault<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.ZodCoercedNumber<unknown
|
|
295
|
-
limit: z.ZodOptional<z.ZodCoercedNumber<unknown
|
|
294
|
+
offset: z.ZodOptional<z.ZodDefault<z.ZodCoercedNumber<unknown>>>;
|
|
295
|
+
limit: z.ZodOptional<z.ZodDefault<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
|
@@ -218,13 +218,21 @@ var refinePagination = (options) => ({
|
|
|
218
218
|
import z from "zod";
|
|
219
219
|
var zodPaginationSchema = z.object({
|
|
220
220
|
/** Integer representing the starting point for pagination. */
|
|
221
|
-
offset: z.coerce.number().int().nonnegative().optional(),
|
|
221
|
+
offset: z.coerce.number().int().nonnegative().default(0).optional(),
|
|
222
222
|
/** Integer representing the maximum number of items to return. */
|
|
223
|
-
limit: z.coerce.number().int().positive().optional()
|
|
223
|
+
limit: z.coerce.number().int().positive().default(10).optional()
|
|
224
224
|
});
|
|
225
225
|
var zodPaginationShape = zodPaginationSchema.shape;
|
|
226
226
|
|
|
227
227
|
// src/zod-validation/zod-validation.refiners.ts
|
|
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
|
|
228
236
|
import z2 from "zod";
|
|
229
237
|
|
|
230
238
|
// src/zod-validation/zod-validation.utilities.ts
|
|
@@ -232,7 +240,7 @@ var isPlainObject = (value) => {
|
|
|
232
240
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
233
241
|
};
|
|
234
242
|
|
|
235
|
-
// src/zod-validation/zod-validation.
|
|
243
|
+
// src/zod-validation/zod-validation.parsing.ts
|
|
236
244
|
var parseQueryValue = (value) => {
|
|
237
245
|
if (Array.isArray(value))
|
|
238
246
|
return value.map(parseQueryValue);
|
|
@@ -279,6 +287,7 @@ export {
|
|
|
279
287
|
safeExecute,
|
|
280
288
|
sqlWhere,
|
|
281
289
|
success,
|
|
290
|
+
zodAtLeastOne,
|
|
282
291
|
zodPaginationSchema,
|
|
283
292
|
zodPaginationShape
|
|
284
293
|
};
|