@kalutskii/foundation 0.5.2 → 0.6.3

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
@@ -42,15 +42,15 @@ type FetchResult<T> = {
42
42
  data: null;
43
43
  };
44
44
 
45
+ type QueryPrimitive = string | number | boolean | bigint | Date | null | undefined;
45
46
  /**
46
- * Type utility that recursively transforms all Date fields to string, as well as handling arrays and objects.
47
- * This is necessary for proper typing when working with RPC, as JSON does not support the Date type directly.
48
- * Example usage: `SerializeDates<{ createdAt: Date; nested: { updatedAt: Date }; tags: Date[] }>` \
49
- * = `{ createdAt: string; nested: { updatedAt: string }; tags: string[] }`
47
+ * Type utility that recursively transforms all fields to string, as well as handling arrays and objects.
48
+ * This is useful for serializing complex data structures into query parameters, which must be strings.
49
+ * Example usage: `AsQuery<{ id: number; name: string; tags: string[] }>` = `{ id: string; name: string; tags: string[] }`
50
50
  */
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
- [K in keyof T]: SerializeDates<T[K]>;
53
- } : T;
51
+ type AsQuery<T> = T extends QueryPrimitive ? string : T extends readonly (infer Item)[] ? AsQuery<Item>[] : T extends object ? {
52
+ [Key in keyof T]: AsQuery<T[Key]>;
53
+ } : string;
54
54
 
55
55
  /**
56
56
  * Wraps c.json with a typed success payload / (or void data) & possible APIError response.
@@ -59,7 +59,7 @@ type SerializeDates<T> = T extends Date ? string : T extends (infer U)[] ? Seria
59
59
  declare function respond<T extends object = Record<string, never>, S extends SuccessStatusCode = SuccessStatusCode>(c: Context, options: {
60
60
  status: S;
61
61
  data?: T;
62
- }): Response & TypedResponse<APISuccess<SerializeDates<T>> | APIError, S, 'json'>;
62
+ }): Response & TypedResponse<APISuccess<AsQuery<T>> | APIError, S, 'json'>;
63
63
 
64
64
  /**
65
65
  * Factory for creating successful API responses with serializable data.
@@ -120,11 +120,11 @@ type PaginationOptions = z.infer<typeof paginationSchema>;
120
120
  *
121
121
  * ```ts
122
122
  * await db.query.someTable.findMany({
123
- * ...getPagination(options),
123
+ * ...refinePagination(options),
124
124
  * });
125
125
  * ```
126
126
  */
127
- declare const getPagination: (options?: PaginationOptions) => {
127
+ declare const refinePagination: (options?: PaginationOptions) => {
128
128
  offset?: number | undefined;
129
129
  limit?: number | undefined;
130
130
  };
@@ -218,6 +218,18 @@ declare const log: {
218
218
  type Simplify<T> = {
219
219
  [K in keyof T]: T[K];
220
220
  } & {};
221
+
222
+ /**
223
+ * Transforms a string to a number (when passing query parameters).
224
+ * Example usage: `asQueryNumber(z.number())('123') = 123`
225
+ */
226
+ declare const asQueryNumber: <T extends z.ZodNumber | z.ZodInt>(schema: T) => z.ZodPreprocess<T>;
227
+ /**
228
+ * Transforms various string representations of boolean values into actual booleans.
229
+ * See POSITIVE_VALUES and NEGATIVE_VALUES arrays below for supported inputs.
230
+ */
231
+ declare const asQueryBoolean: <T extends z.ZodTypeAny>(schema: T) => z.ZodPreprocess<T>;
232
+
221
233
  /**
222
234
  * A utility type that represents a value that can be of type T or null.
223
235
  * Commonly used for optional fields in data models or function return types.
@@ -248,45 +260,6 @@ type XOR<T, U> = Simplify<T & {
248
260
  */
249
261
  type AtLeastOne<T, Keys extends keyof T = keyof T> = Keys extends keyof T ? Simplify<Required<Pick<T, Keys>> & Partial<Omit<T, Keys>>> : never;
250
262
 
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
-
290
263
  /** Options for configuring the JWT service. */
291
264
  type JWTServiceOptions = {
292
265
  /** The algorithm to use for signing and verifying JWTs. Defaults to 'HS256'. */
@@ -335,4 +308,4 @@ declare class ZodJWTService<TPayloadOrSchema> {
335
308
  verifyOrThrow(token: string, secret: string): Promise<Payload<TPayloadOrSchema>>;
336
309
  }
337
310
 
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 };
311
+ 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 Simplify, type SuccessStatusCode, type XOR, ZodJWTService, asQueryBoolean, asQueryNumber, failure, fetchAndThrow, fetchSafely, formatTime, generateRandomString, getColoredHTTPStatus, getFormattedDate, getFormattedTime, getUTCOffset, getZonedTime, honoLoggingHandler, log, measureExecutionTime, onHandlerError, paginationSchema, refinePagination, respond, safeExecute, success };
package/dist/index.js CHANGED
@@ -137,8 +137,8 @@ var paginationSchema = z.object({
137
137
  limit: z.number().int().positive().optional()
138
138
  });
139
139
 
140
- // src/pagination/pagination.utilities.ts
141
- var getPagination = (options) => {
140
+ // src/pagination/pagination.refiners.ts
141
+ var refinePagination = (options) => {
142
142
  const pagination = {};
143
143
  if (options?.offset !== void 0) {
144
144
  pagination.offset = options.offset;
@@ -168,11 +168,6 @@ async function measureExecutionTime(execution) {
168
168
 
169
169
  // src/validation/validation.refiners.ts
170
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
- };
176
171
  var asQueryNumber = (schema) => z2.preprocess((v) => typeof v === "string" ? Number(v) : v, schema);
177
172
  var asQueryBoolean = (schema) => {
178
173
  const POSITIVE_VALUES = ["1", "true", "yes", "y", "on"];
@@ -245,7 +240,6 @@ export {
245
240
  ZodJWTService,
246
241
  asQueryBoolean,
247
242
  asQueryNumber,
248
- atLeastOneDefined,
249
243
  failure,
250
244
  fetchAndThrow,
251
245
  fetchSafely,
@@ -254,7 +248,6 @@ export {
254
248
  getColoredHTTPStatus,
255
249
  getFormattedDate,
256
250
  getFormattedTime,
257
- getPagination,
258
251
  getUTCOffset,
259
252
  getZonedTime,
260
253
  honoLoggingHandler,
@@ -262,6 +255,7 @@ export {
262
255
  measureExecutionTime,
263
256
  onHandlerError,
264
257
  paginationSchema,
258
+ refinePagination,
265
259
  respond,
266
260
  safeExecute,
267
261
  success
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kalutskii/foundation",
3
- "version": "0.5.2",
3
+ "version": "0.6.3",
4
4
  "description": "Typescript collection of most common utilities, schemas and functions among private projects.",
5
5
  "type": "module",
6
6
  "repository": {