@kalutskii/foundation 0.6.10 → 0.6.12
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 +12 -82
- package/dist/index.js +10 -44
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -58,12 +58,12 @@ type FetchResult<T> = {
|
|
|
58
58
|
data: null;
|
|
59
59
|
};
|
|
60
60
|
|
|
61
|
-
type QueryPrimitive = string | number | boolean | bigint | Date
|
|
61
|
+
type QueryPrimitive = string | number | boolean | bigint | Date;
|
|
62
62
|
/**
|
|
63
63
|
* Recursively transforms all fields of T to `string`, matching how query parameters are serialized.
|
|
64
64
|
* Handles nested objects, arrays, and primitive values (including null and undefined as optional).
|
|
65
65
|
*/
|
|
66
|
-
type AsQuery<T> = T extends QueryPrimitive ? string : T extends readonly (infer Item)[] ? AsQuery<Item>[] : T extends object ? {
|
|
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
69
|
|
|
@@ -315,93 +315,23 @@ type ZodPaginationOptions = z.infer<typeof zodPaginationSchema>;
|
|
|
315
315
|
*/
|
|
316
316
|
declare const refinePagination: (options?: ZodPaginationOptions) => ZodPaginationOptions;
|
|
317
317
|
|
|
318
|
-
type ZodShape = Record<string, z.ZodTypeAny>;
|
|
319
|
-
type AnyZodObject = z.ZodObject<ZodShape>;
|
|
320
|
-
type UnionToIntersection<T> = (T extends unknown ? (value: T) => void : never) extends (value: infer I) => void ? I : never;
|
|
321
|
-
type UnwrapOptional<T extends z.ZodTypeAny> = T extends z.ZodOptional<infer Inner> ? Inner : T;
|
|
322
|
-
type IsOptional<T extends z.ZodTypeAny> = T extends z.ZodOptional<z.ZodTypeAny> ? true : false;
|
|
323
|
-
type Optionalize<TSchema extends z.ZodTypeAny, TShouldOptionalize extends boolean> = TShouldOptionalize extends true ? z.ZodOptional<TSchema> : TSchema;
|
|
324
|
-
type FlattenObjectField<TSchema extends z.ZodTypeAny> = UnwrapOptional<TSchema> extends z.ZodObject<infer InnerShape extends ZodShape> ? {
|
|
325
|
-
[K in keyof InnerShape]: Optionalize<InnerShape[K], IsOptional<TSchema>>;
|
|
326
|
-
} : never;
|
|
327
|
-
type FlattenNestedObjectFields<TShape extends ZodShape> = {
|
|
328
|
-
[K in keyof TShape]: FlattenObjectField<TShape[K]>;
|
|
329
|
-
}[keyof TShape];
|
|
330
|
-
/**
|
|
331
|
-
* The resulting shape after all nested `z.ZodObject` fields have been lifted to the top level.
|
|
332
|
-
* Primitive fields are kept as-is; nested object fields are replaced with their inner keys.
|
|
333
|
-
* If the parent field was optional, all promoted inner fields become optional as well.
|
|
334
|
-
*/
|
|
335
|
-
type FlattenZodShape<TShape extends ZodShape> = {
|
|
336
|
-
[K in keyof TShape as UnwrapOptional<TShape[K]> extends AnyZodObject ? never : K]: TShape[K];
|
|
337
|
-
} & UnionToIntersection<FlattenNestedObjectFields<TShape>>;
|
|
338
|
-
|
|
339
|
-
/**
|
|
340
|
-
* Recursively lifts all nested `z.ZodObject` fields to the top level of the shape.
|
|
341
|
-
* Primitive fields are preserved as-is. If the parent field was optional, all promoted
|
|
342
|
-
* inner keys become optional to avoid requiring fields that were previously nested under an optional object.
|
|
343
|
-
*
|
|
344
|
-
* ```ts
|
|
345
|
-
* const flat = flattenZodShape({
|
|
346
|
-
* name: z.string(),
|
|
347
|
-
* address: z.object({ city: z.string(), zip: z.string() }).optional(),
|
|
348
|
-
* });
|
|
349
|
-
* // { name: z.string(), city: z.string().optional(), zip: z.string().optional() }
|
|
350
|
-
* ```
|
|
351
|
-
*/
|
|
352
|
-
declare const flattenZodShape: <TShape extends ZodShape>(shape: TShape) => FlattenZodShape<TShape>;
|
|
353
|
-
/**
|
|
354
|
-
* Builds a strict flat query schema from a nested `z.ZodObject`.
|
|
355
|
-
* All nested object fields are lifted to the top level while preserving optionality,
|
|
356
|
-
* making the result suitable for validating flat query parameter objects.
|
|
357
|
-
*
|
|
358
|
-
* ```ts
|
|
359
|
-
* const schema = asQuerySchema(
|
|
360
|
-
* z.object({
|
|
361
|
-
* search: z.string().optional(),
|
|
362
|
-
* sort: z.object({ field: z.string(), order: z.enum(['asc', 'desc']) }).optional(),
|
|
363
|
-
* })
|
|
364
|
-
* );
|
|
365
|
-
*
|
|
366
|
-
* schema.parse({ search: 'foo', field: 'name', order: 'asc' });
|
|
367
|
-
* // { search: 'foo', field: 'name', order: 'asc' }
|
|
368
|
-
* ```
|
|
369
|
-
*/
|
|
370
|
-
declare const asQuerySchema: <TShape extends ZodShape>(schema: z.ZodObject<TShape>) => z.ZodObject<FlattenZodShape<TShape> extends infer T ? { -readonly [P in keyof T]: T[P]; } : never, z.core.$strict>;
|
|
371
|
-
|
|
372
|
-
/**
|
|
373
|
-
* Checks if the provided schema is an instance of `z.ZodObject`.
|
|
374
|
-
* @returns `true` if the schema is a `z.ZodObject`, `false` otherwise.
|
|
375
|
-
*/
|
|
376
|
-
declare const isZodObject: (schema: z.ZodTypeAny) => schema is AnyZodObject;
|
|
377
|
-
/**
|
|
378
|
-
* Checks if the provided schema is an instance of `z.ZodOptional`.
|
|
379
|
-
* @returns `true` if the schema is a `z.ZodOptional`, `false` otherwise.
|
|
380
|
-
*/
|
|
381
|
-
declare const isZodOptional: (schema: z.ZodTypeAny) => schema is z.ZodOptional<z.ZodTypeAny>;
|
|
382
|
-
/**
|
|
383
|
-
* Unwraps a `z.ZodOptional` schema to retrieve its inner schema.
|
|
384
|
-
* If the provided schema is not optional, it is returned unchanged.
|
|
385
|
-
*/
|
|
386
|
-
declare const unwrapOptional: (schema: z.ZodTypeAny) => z.ZodTypeAny;
|
|
387
|
-
|
|
388
318
|
declare const parseQueryValue: (value: unknown) => unknown;
|
|
389
319
|
/**
|
|
390
|
-
*
|
|
391
|
-
*
|
|
320
|
+
* Preprocesses query-like values before Zod validation.
|
|
321
|
+
*
|
|
322
|
+
* Query parameters are string-based by nature, even when they semantically represent
|
|
323
|
+
* numbers or booleans. This helper converts only clear primitive values, allowing
|
|
324
|
+
* regular schemas like `z.number()` and `z.boolean()` to validate query input directly.
|
|
392
325
|
*
|
|
393
326
|
* ```ts
|
|
394
|
-
* const schema = z.object({
|
|
327
|
+
* const schema = asQuery(z.object({
|
|
395
328
|
* page: z.number().int().positive(),
|
|
396
329
|
* isActive: z.boolean(),
|
|
397
|
-
* });
|
|
330
|
+
* }));
|
|
398
331
|
*
|
|
399
|
-
*
|
|
400
|
-
*
|
|
332
|
+
* schema.parse({ page: '2', isActive: 'true' });
|
|
333
|
+
* // { page: 2, isActive: true }
|
|
401
334
|
* ```
|
|
402
|
-
*
|
|
403
|
-
* @param schema - The Zod schema to validate the query parameters against.
|
|
404
|
-
* @returns A new Zod schema that preprocesses query parameter values before validation.
|
|
405
335
|
*/
|
|
406
336
|
declare const asQuery: <T extends z.ZodTypeAny>(schema: T) => z.ZodPreprocess<T>;
|
|
407
337
|
|
|
@@ -411,4 +341,4 @@ declare const asQuery: <T extends z.ZodTypeAny>(schema: T) => z.ZodPreprocess<T>
|
|
|
411
341
|
*/
|
|
412
342
|
declare const isPlainObject: (value: unknown) => value is Record<string, unknown>;
|
|
413
343
|
|
|
414
|
-
export { type APIContractData, type APIContractError, type APIContractResult, type APIError, type APISuccess, type
|
|
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 };
|
package/dist/index.js
CHANGED
|
@@ -219,9 +219,19 @@ import z2 from "zod";
|
|
|
219
219
|
|
|
220
220
|
// src/zod-validation/zod-validation.refiners.ts
|
|
221
221
|
import z from "zod";
|
|
222
|
+
|
|
223
|
+
// src/zod-validation/zod-validation.utilities.ts
|
|
224
|
+
var isPlainObject = (value) => {
|
|
225
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
226
|
+
};
|
|
227
|
+
|
|
228
|
+
// src/zod-validation/zod-validation.refiners.ts
|
|
222
229
|
var parseQueryValue = (value) => {
|
|
223
230
|
if (Array.isArray(value))
|
|
224
231
|
return value.map(parseQueryValue);
|
|
232
|
+
if (isPlainObject(value)) {
|
|
233
|
+
return Object.fromEntries(Object.entries(value).map(([key, value2]) => [key, parseQueryValue(value2)]));
|
|
234
|
+
}
|
|
225
235
|
if (typeof value !== "string")
|
|
226
236
|
return value;
|
|
227
237
|
const normalized = value.trim().toLowerCase();
|
|
@@ -245,55 +255,14 @@ var zodPaginationSchema = z2.object({
|
|
|
245
255
|
limit: asQuery(z2.number().int().positive()).optional()
|
|
246
256
|
});
|
|
247
257
|
var zodPaginationShape = zodPaginationSchema.shape;
|
|
248
|
-
|
|
249
|
-
// src/zod-flatten/zod-flatten.schemas.ts
|
|
250
|
-
import z4 from "zod";
|
|
251
|
-
|
|
252
|
-
// src/zod-flatten/zod-flatten.utilities.ts
|
|
253
|
-
import z3 from "zod";
|
|
254
|
-
var isZodObject = (schema) => {
|
|
255
|
-
return schema instanceof z3.ZodObject;
|
|
256
|
-
};
|
|
257
|
-
var isZodOptional = (schema) => {
|
|
258
|
-
return schema instanceof z3.ZodOptional;
|
|
259
|
-
};
|
|
260
|
-
var unwrapOptional = (schema) => {
|
|
261
|
-
return isZodOptional(schema) ? schema.unwrap() : schema;
|
|
262
|
-
};
|
|
263
|
-
|
|
264
|
-
// src/zod-flatten/zod-flatten.schemas.ts
|
|
265
|
-
var flattenZodShape = (shape) => {
|
|
266
|
-
const flat = {};
|
|
267
|
-
for (const [key, schema] of Object.entries(shape)) {
|
|
268
|
-
const unwrappedSchema = unwrapOptional(schema);
|
|
269
|
-
if (isZodObject(unwrappedSchema) === false) {
|
|
270
|
-
flat[key] = schema;
|
|
271
|
-
continue;
|
|
272
|
-
}
|
|
273
|
-
for (const [innerKey, innerSchema] of Object.entries(unwrappedSchema.shape)) {
|
|
274
|
-
flat[innerKey] = isZodOptional(schema) ? innerSchema.optional() : innerSchema;
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
return flat;
|
|
278
|
-
};
|
|
279
|
-
var asQuerySchema = (schema) => {
|
|
280
|
-
return z4.object(flattenZodShape(schema.shape)).strict();
|
|
281
|
-
};
|
|
282
|
-
|
|
283
|
-
// src/zod-validation/zod-validation.utilities.ts
|
|
284
|
-
var isPlainObject = (value) => {
|
|
285
|
-
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
286
|
-
};
|
|
287
258
|
export {
|
|
288
259
|
EXCEPTION_STATUS_CODES,
|
|
289
260
|
SUCCESS_STATUS_CODES,
|
|
290
261
|
ZodJWTService,
|
|
291
262
|
asQuery,
|
|
292
|
-
asQuerySchema,
|
|
293
263
|
failure,
|
|
294
264
|
fetchAndThrow,
|
|
295
265
|
fetchSafely,
|
|
296
|
-
flattenZodShape,
|
|
297
266
|
formatTime,
|
|
298
267
|
generateRandomString,
|
|
299
268
|
getColoredHTTPStatus,
|
|
@@ -303,8 +272,6 @@ export {
|
|
|
303
272
|
getZonedTime,
|
|
304
273
|
honoLoggingHandler,
|
|
305
274
|
isPlainObject,
|
|
306
|
-
isZodObject,
|
|
307
|
-
isZodOptional,
|
|
308
275
|
log,
|
|
309
276
|
measureExecutionTime,
|
|
310
277
|
onHandlerError,
|
|
@@ -314,7 +281,6 @@ export {
|
|
|
314
281
|
safeExecute,
|
|
315
282
|
sqlWhere,
|
|
316
283
|
success,
|
|
317
|
-
unwrapOptional,
|
|
318
284
|
zodPaginationSchema,
|
|
319
285
|
zodPaginationShape
|
|
320
286
|
};
|