@kalutskii/foundation 0.5.2 → 0.6.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 +19 -34
- package/dist/index.js +10 -4
- package/package.json +1 -1
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
|
|
47
|
-
* This is
|
|
48
|
-
* Example usage: `
|
|
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
|
|
52
|
-
[
|
|
53
|
-
} :
|
|
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<
|
|
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
|
-
* ...
|
|
123
|
+
* ...refinePagination(options),
|
|
124
124
|
* });
|
|
125
125
|
* ```
|
|
126
126
|
*/
|
|
127
|
-
declare const
|
|
127
|
+
declare const refinePagination: (options?: PaginationOptions) => {
|
|
128
128
|
offset?: number | undefined;
|
|
129
129
|
limit?: number | undefined;
|
|
130
130
|
};
|
|
@@ -218,6 +218,7 @@ declare const log: {
|
|
|
218
218
|
type Simplify<T> = {
|
|
219
219
|
[K in keyof T]: T[K];
|
|
220
220
|
} & {};
|
|
221
|
+
|
|
221
222
|
/**
|
|
222
223
|
* A utility type that represents a value that can be of type T or null.
|
|
223
224
|
* Commonly used for optional fields in data models or function return types.
|
|
@@ -249,23 +250,17 @@ type XOR<T, U> = Simplify<T & {
|
|
|
249
250
|
type AtLeastOne<T, Keys extends keyof T = keyof T> = Keys extends keyof T ? Simplify<Required<Pick<T, Keys>> & Partial<Omit<T, Keys>>> : never;
|
|
250
251
|
|
|
251
252
|
/**
|
|
252
|
-
*
|
|
253
|
-
* This is useful for validating
|
|
253
|
+
* Refines a Zod object schema to ensure that at least one of its keys is defined.
|
|
254
|
+
* This is useful for validating objects where at least one property must be present.
|
|
254
255
|
*
|
|
255
256
|
* ```ts
|
|
256
|
-
* const mySchema =
|
|
257
|
-
* z.
|
|
258
|
-
*
|
|
259
|
-
*
|
|
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 }
|
|
257
|
+
* const mySchema = z.object({
|
|
258
|
+
* a: z.string()
|
|
259
|
+
* b: z.number()
|
|
260
|
+
* }).refine(atLeastOneDefined);
|
|
266
261
|
* ```
|
|
267
262
|
*/
|
|
268
|
-
declare const atLeastOneDefined: <T extends z.ZodRawShape>(schema: z.ZodObject<T>) => z.ZodObject<T
|
|
263
|
+
declare const atLeastOneDefined: <T extends z.ZodRawShape>(schema: z.ZodObject<T>) => z.ZodType<AtLeastOne<z.infer<z.ZodObject<T>>>>;
|
|
269
264
|
/**
|
|
270
265
|
* Transforms a string to a number (when passing query parameters).
|
|
271
266
|
* Example usage: `asQueryNumber(z.number())('123') = 123`
|
|
@@ -277,16 +272,6 @@ declare const asQueryNumber: <T extends z.ZodNumber>(schema: T) => z.ZodPreproce
|
|
|
277
272
|
*/
|
|
278
273
|
declare const asQueryBoolean: <T extends z.ZodTypeAny>(schema: T) => z.ZodPreprocess<T>;
|
|
279
274
|
|
|
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
275
|
/** Options for configuring the JWT service. */
|
|
291
276
|
type JWTServiceOptions = {
|
|
292
277
|
/** The algorithm to use for signing and verifying JWTs. Defaults to 'HS256'. */
|
|
@@ -335,4 +320,4 @@ declare class ZodJWTService<TPayloadOrSchema> {
|
|
|
335
320
|
verifyOrThrow(token: string, secret: string): Promise<Payload<TPayloadOrSchema>>;
|
|
336
321
|
}
|
|
337
322
|
|
|
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
|
|
323
|
+
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, atLeastOneDefined, 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.
|
|
141
|
-
var
|
|
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,10 +168,16 @@ async function measureExecutionTime(execution) {
|
|
|
168
168
|
|
|
169
169
|
// src/validation/validation.refiners.ts
|
|
170
170
|
import z2 from "zod";
|
|
171
|
+
|
|
172
|
+
// src/validation/validation.messages.ts
|
|
171
173
|
var AT_LEAST_ONE_DEFINED_ERROR = (keys) => `At least one of the specified keys must be defined: ${keys.join(", ")}`;
|
|
174
|
+
|
|
175
|
+
// src/validation/validation.refiners.ts
|
|
172
176
|
var atLeastOneDefined = (schema) => {
|
|
173
177
|
const keys = Object.keys(schema.shape);
|
|
174
|
-
return schema.refine((value) => keys.some((key) => value[key]
|
|
178
|
+
return schema.partial().refine((value) => keys.some((key) => value[key] != null), {
|
|
179
|
+
message: AT_LEAST_ONE_DEFINED_ERROR(keys)
|
|
180
|
+
});
|
|
175
181
|
};
|
|
176
182
|
var asQueryNumber = (schema) => z2.preprocess((v) => typeof v === "string" ? Number(v) : v, schema);
|
|
177
183
|
var asQueryBoolean = (schema) => {
|
|
@@ -254,7 +260,6 @@ export {
|
|
|
254
260
|
getColoredHTTPStatus,
|
|
255
261
|
getFormattedDate,
|
|
256
262
|
getFormattedTime,
|
|
257
|
-
getPagination,
|
|
258
263
|
getUTCOffset,
|
|
259
264
|
getZonedTime,
|
|
260
265
|
honoLoggingHandler,
|
|
@@ -262,6 +267,7 @@ export {
|
|
|
262
267
|
measureExecutionTime,
|
|
263
268
|
onHandlerError,
|
|
264
269
|
paginationSchema,
|
|
270
|
+
refinePagination,
|
|
265
271
|
respond,
|
|
266
272
|
safeExecute,
|
|
267
273
|
success
|