@kalutskii/foundation 0.4.4 → 0.5.0

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
@@ -80,6 +80,15 @@ declare const honoLoggingHandler: MiddlewareHandler;
80
80
  type SerializeDates<T> = T extends Date ? string : T extends (infer U)[] ? SerializeDates<U>[] : T extends readonly (infer U)[] ? readonly SerializeDates<U>[] : T extends object ? {
81
81
  [K in keyof T]: SerializeDates<T[K]>;
82
82
  } : T;
83
+ type QueryPrimitive = string | number | boolean | bigint | Date | null | undefined;
84
+ /**
85
+ * Type utility that recursively transforms all fields to string, as well as handling arrays and objects.
86
+ * This is useful for serializing complex data structures into query parameters, which must be strings.
87
+ * Example usage: `AsQuery<{ id: number; name: string; tags: string[] }>` = `{ id: string; name: string; tags: string[] }`
88
+ */
89
+ type AsQuery<T> = T extends QueryPrimitive ? string : T extends readonly (infer Item)[] ? AsQuery<Item>[] : T extends object ? {
90
+ [Key in keyof T]: AsQuery<T[Key]>;
91
+ } : string;
83
92
  /**
84
93
  * Transforms a string to a number (when passing query parameters).
85
94
  * Example usage: `asQueryNumber(z.number())('123') = 123`
@@ -182,21 +191,42 @@ declare const log: {
182
191
  error: (message: string, service?: string, stack?: string) => void;
183
192
  };
184
193
 
194
+ /**
195
+ * Utility type that simplifies a given type T by flattening its structure.
196
+ * This is particularly useful for improving the readability of complex types.
197
+ */
198
+ type Simplify<T> = {
199
+ [K in keyof T]: T[K];
200
+ } & {};
185
201
  /**
186
202
  * A utility type that represents a value that can be of type T or null.
187
203
  * Commonly used for optional fields in data models or function return types.
188
204
  *
189
- * ```
205
+ * ```ts
190
206
  * export type ObjectSelect = XOR<
191
207
  * Pick<Object, 'id'>,
192
208
  * Pick<Object, 'anotherUniqueField'>
193
209
  * >;
194
210
  */
195
- type XOR<T, U> = (T & {
211
+ type XOR<T, U> = Simplify<T & {
196
212
  [K in keyof U]?: never;
197
- }) | (U & {
213
+ }> | Simplify<U & {
198
214
  [K in keyof T]?: never;
199
- });
215
+ }>;
216
+ /**
217
+ * Utility type that ensures at least one property from the specified
218
+ * keys of a given type T is required, while the rest remain optional.
219
+ *
220
+ * This is useful for scenarios where you want to enforce that at least one
221
+ * of several optional (nullable) properties must be provided in an object.
222
+ *
223
+ * ```ts
224
+ * type Example = AtLeastOne<{ a?: string; b?: number; c?: boolean }>;
225
+ * // Valid: { a: "hello" }, { b: 42 }, { c: true }, { a: "hello", b: 42 }
226
+ * // Invalid: {}, { a: undefined, b: undefined, c: undefined }
227
+ * ```
228
+ */
229
+ type AtLeastOne<T, Keys extends keyof T = keyof T> = Keys extends keyof T ? Simplify<Required<Pick<T, Keys>> & Partial<Omit<T, Keys>>> : never;
200
230
 
201
231
  /** Options for configuring the JWT service. */
202
232
  type JWTServiceOptions = {
@@ -232,15 +262,18 @@ declare class ZodJWTService<TPayloadOrSchema> {
232
262
  /**
233
263
  * Decodes a JWT token, and if a Zod schema is provided, validates the payload against it.
234
264
  * Returns the decoded payload if valid, or null if invalid or if the token cannot be decoded.
265
+ *
235
266
  * Example usage: `const payload = await JWTServiceInstance.decode(token);`
236
267
  */
237
268
  decode(token: string): Promise<Payload<TPayloadOrSchema> | null>;
238
269
  /**
239
270
  * Verifies and decodes a JWT token using the provided secret and configured algorithm,
240
- * then validates the payload against the Zod schema if one is provided. Throws an error if verification fails or if the payload is invalid.
271
+ * then validates the payload against the Zod schema if one is provided.
272
+ *
273
+ * Throws an error if verification fails or if the payload is invalid according to the schema.
241
274
  * Example usage: `const payload = await JWTServiceInstance.verifyOrThrow(token, 'my-secret');`
242
275
  */
243
276
  verifyOrThrow(token: string, secret: string): Promise<Payload<TPayloadOrSchema>>;
244
277
  }
245
278
 
246
- export { type APIContractData, type APIContractError, type APIContractResult, type APIError, type APISuccess, EXCEPTION_STATUS_CODES, type ExceptionStatusCode, type FetchResult, type JWTServiceOptions, type JWTSignOptions, type MeasuredExecution, type Payload, type PayloadSchema, SUCCESS_STATUS_CODES, type SerializeDates, type SuccessStatusCode, type XOR, ZodJWTService, asQueryBoolean, asQueryNumber, failure, fetchAndThrow, fetchSafely, formatTime, generateRandomString, getColoredHTTPStatus, getFormattedDate, getFormattedTime, getUTCOffset, getZonedTime, honoLoggingHandler, log, measureExecutionTime, onHandlerError, respond, safeExecute, success };
279
+ 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 SerializeDates, type Simplify, type SuccessStatusCode, type XOR, ZodJWTService, asQueryBoolean, asQueryNumber, failure, fetchAndThrow, fetchSafely, formatTime, generateRandomString, getColoredHTTPStatus, getFormattedDate, getFormattedTime, getUTCOffset, getZonedTime, honoLoggingHandler, log, measureExecutionTime, onHandlerError, respond, safeExecute, success };
package/dist/index.js CHANGED
@@ -189,6 +189,7 @@ var ZodJWTService = class {
189
189
  /**
190
190
  * Decodes a JWT token, and if a Zod schema is provided, validates the payload against it.
191
191
  * Returns the decoded payload if valid, or null if invalid or if the token cannot be decoded.
192
+ *
192
193
  * Example usage: `const payload = await JWTServiceInstance.decode(token);`
193
194
  */
194
195
  async decode(token) {
@@ -201,7 +202,9 @@ var ZodJWTService = class {
201
202
  }
202
203
  /**
203
204
  * Verifies and decodes a JWT token using the provided secret and configured algorithm,
204
- * then validates the payload against the Zod schema if one is provided. Throws an error if verification fails or if the payload is invalid.
205
+ * then validates the payload against the Zod schema if one is provided.
206
+ *
207
+ * Throws an error if verification fails or if the payload is invalid according to the schema.
205
208
  * Example usage: `const payload = await JWTServiceInstance.verifyOrThrow(token, 'my-secret');`
206
209
  */
207
210
  async verifyOrThrow(token, secret) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kalutskii/foundation",
3
- "version": "0.4.4",
3
+ "version": "0.5.0",
4
4
  "description": "Typescript collection of most common utilities, schemas and functions among private projects.",
5
5
  "type": "module",
6
6
  "repository": {