@kalutskii/foundation 0.5.0 → 0.5.1

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
@@ -3,6 +3,18 @@ import z$1, { z, ZodNumber } from 'zod';
3
3
  import { Locale } from 'date-fns';
4
4
  import { SymmetricAlgorithm } from 'hono/utils/jwt/jwa';
5
5
 
6
+ /**
7
+ * Global error handler for Hono framework. Catches all exceptions thrown in route handlers and middlewares.
8
+ * Distinguishes between expected HTTPExceptions (mapped to their status codes) and unexpected errors (500).
9
+ */
10
+ declare const onHandlerError: ErrorHandler;
11
+
12
+ /**
13
+ * Request logging middleware for Hono, providing deeply detailed logs for each incoming request.
14
+ * Example log output: [12:12:12 (+4 UTC)] hono | POST 200 123ms /api/v1/users (search=term)
15
+ */
16
+ declare const honoLoggingHandler: MiddlewareHandler;
17
+
6
18
  declare const SUCCESS_STATUS_CODES: readonly [200, 201, 202, 307];
7
19
  declare const EXCEPTION_STATUS_CODES: readonly [400, 401, 403, 404, 405, 409, 500];
8
20
 
@@ -30,47 +42,6 @@ type FetchResult<T> = {
30
42
  data: null;
31
43
  };
32
44
 
33
- /**
34
- * Factory for creating successful API responses with serializable data.
35
- * Example usage: `success({ status: 200, data: { message: 'Operation successful' } })`
36
- */
37
- declare function success<T = unknown>({ status, data }: {
38
- status: SuccessStatusCode;
39
- data: T;
40
- }): APISuccess<T>;
41
- /**
42
- * Factory for creating API error responses, including the error message.
43
- * Example usage: `failure({ status: 404, error: 'User not found' })`
44
- */
45
- declare function failure({ status, error }: {
46
- status: ExceptionStatusCode;
47
- error: string;
48
- }): APIError;
49
-
50
- /**
51
- * Resolves an APIContractResult fetcher into a FetchResult discriminated union.
52
- * Does not throw errors, instead returning them in the error property of the FetchResult.
53
- * Example usage: const result = await fetchSafely(() => api.fetchUser(userId));
54
- */
55
- declare function fetchSafely<TResult extends APIContractResult<unknown>>(fetcher: () => Promise<TResult>): Promise<FetchResult<APIContractData<TResult>>>;
56
- /**
57
- * Resolves an APIContractResult fetcher, throwing an error if the result is an APIError.
58
- * Example usage: const data = await fetchAndThrow(() => api.fetchUser(userId));
59
- */
60
- declare function fetchAndThrow<TResult extends APIContractResult<unknown>>(fetcher: () => Promise<TResult>): Promise<APIContractData<TResult>>;
61
-
62
- /**
63
- * Global error handler for Hono framework. Catches all exceptions thrown in route handlers and middlewares.
64
- * Distinguishes between expected HTTPExceptions (mapped to their status codes) and unexpected errors (500).
65
- */
66
- declare const onHandlerError: ErrorHandler;
67
-
68
- /**
69
- * Request logging middleware for Hono, providing deeply detailed logs for each incoming request.
70
- * Example log output: [12:12:12 (+4 UTC)] hono | POST 200 123ms /api/v1/users (search=term)
71
- */
72
- declare const honoLoggingHandler: MiddlewareHandler;
73
-
74
45
  /**
75
46
  * Type utility that recursively transforms all Date fields to string, as well as handling arrays and objects.
76
47
  * This is necessary for proper typing when working with RPC, as JSON does not support the Date type directly.
@@ -109,6 +80,74 @@ declare function respond<T extends object = Record<string, never>, S extends Suc
109
80
  data?: T;
110
81
  }): Response & TypedResponse<APISuccess<SerializeDates<T>> | APIError, S, 'json'>;
111
82
 
83
+ /**
84
+ * Factory for creating successful API responses with serializable data.
85
+ * Example usage: `success({ status: 200, data: { message: 'Operation successful' } })`
86
+ */
87
+ declare function success<T = unknown>({ status, data }: {
88
+ status: SuccessStatusCode;
89
+ data: T;
90
+ }): APISuccess<T>;
91
+ /**
92
+ * Factory for creating API error responses, including the error message.
93
+ * Example usage: `failure({ status: 404, error: 'User not found' })`
94
+ */
95
+ declare function failure({ status, error }: {
96
+ status: ExceptionStatusCode;
97
+ error: string;
98
+ }): APIError;
99
+
100
+ /**
101
+ * Resolves an APIContractResult fetcher into a FetchResult discriminated union.
102
+ * Does not throw errors, instead returning them in the error property of the FetchResult.
103
+ * Example usage: const result = await fetchSafely(() => api.fetchUser(userId));
104
+ */
105
+ declare function fetchSafely<TResult extends APIContractResult<unknown>>(fetcher: () => Promise<TResult>): Promise<FetchResult<APIContractData<TResult>>>;
106
+ /**
107
+ * Resolves an APIContractResult fetcher, throwing an error if the result is an APIError.
108
+ * Example usage: const data = await fetchAndThrow(() => api.fetchUser(userId));
109
+ */
110
+ declare function fetchAndThrow<TResult extends APIContractResult<unknown>>(fetcher: () => Promise<TResult>): Promise<APIContractData<TResult>>;
111
+
112
+ /**
113
+ * A Zod schema for validating pagination options, specifically the `offset` and `limit` parameters.
114
+ * - `offset`: An optional non-negative integer representing the starting point for pagination.
115
+ * - `limit`: An optional positive integer representing the maximum number of items to return.
116
+ *
117
+ * This schema can be used to inject in other Zod schemas for validating pagination options:
118
+ *
119
+ * ```ts
120
+ * const mySchema = paginationSchema.extend({
121
+ * // other fields here
122
+ * });
123
+ * ```
124
+ */
125
+ declare const paginationSchema: z$1.ZodObject<{
126
+ offset: z$1.ZodOptional<z$1.ZodNumber>;
127
+ limit: z$1.ZodOptional<z$1.ZodNumber>;
128
+ }, z$1.core.$strip>;
129
+ /**
130
+ * A TypeScript type representing the pagination options validated by the `paginationSchema`.
131
+ * Read documentation for `paginationSchema` for more details on the structure and usage of this type.
132
+ */
133
+ type PaginationOptions = z$1.infer<typeof paginationSchema>;
134
+
135
+ /**
136
+ * A utility function that extracts pagination options from the provided input.
137
+ * It returns an object containing the `offset` and `limit` properties if they are defined.
138
+ * If the input is undefined or does not contain these properties, it returns an empty object.
139
+ *
140
+ * ```ts
141
+ * await db.query.someTable.findMany({
142
+ * ...getPagination(options),
143
+ * });
144
+ * ```
145
+ */
146
+ declare const getPagination: (options?: PaginationOptions) => {
147
+ offset?: number | undefined;
148
+ limit?: number | undefined;
149
+ };
150
+
112
151
  /**
113
152
  * Returns the current time in the specified timezone.
114
153
  * Example usage: `getZonedTime({ tz: 'America/New_York' }) = new Date('2026-03-15T12:00:00Z')`
@@ -276,4 +315,4 @@ declare class ZodJWTService<TPayloadOrSchema> {
276
315
  verifyOrThrow(token: string, secret: string): Promise<Payload<TPayloadOrSchema>>;
277
316
  }
278
317
 
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 };
318
+ 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, failure, fetchAndThrow, fetchSafely, formatTime, generateRandomString, getColoredHTTPStatus, getFormattedDate, getFormattedTime, getPagination, getUTCOffset, getZonedTime, honoLoggingHandler, log, measureExecutionTime, onHandlerError, paginationSchema, respond, safeExecute, success };
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
- // src/http/http.constants.ts
2
- var SUCCESS_STATUS_CODES = [200, 201, 202, 307];
3
- var EXCEPTION_STATUS_CODES = [400, 401, 403, 404, 405, 409, 500];
1
+ // src/hono/hono.execution.ts
2
+ import { HTTPException } from "hono/http-exception";
3
+ import { red as red2 } from "kleur/colors";
4
4
 
5
5
  // src/http/http.factory.ts
6
6
  function success({ status, data }) {
@@ -10,24 +10,6 @@ function failure({ status, error }) {
10
10
  return { kind: "error", status, error };
11
11
  }
12
12
 
13
- // src/http/http.resolvers.ts
14
- async function fetchSafely(fetcher) {
15
- const response = await fetcher();
16
- if (response.kind === "error")
17
- return { error: response.error, data: null };
18
- return { error: null, data: response.data };
19
- }
20
- async function fetchAndThrow(fetcher) {
21
- const response = await fetcher();
22
- if (response.kind === "error")
23
- throw new Error(response.error);
24
- return response.data;
25
- }
26
-
27
- // src/hono/hono.execution.ts
28
- import { HTTPException } from "hono/http-exception";
29
- import { red as red2 } from "kleur/colors";
30
-
31
13
  // src/utilities/generation.utilities.ts
32
14
  var DEFAULT_CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
33
15
  function generateRandomString(length = 10) {
@@ -130,6 +112,43 @@ function respond(c, options) {
130
112
  return c.json(success({ status: options.status, data: options.data ?? {} }), options.status);
131
113
  }
132
114
 
115
+ // src/http/http.constants.ts
116
+ var SUCCESS_STATUS_CODES = [200, 201, 202, 307];
117
+ var EXCEPTION_STATUS_CODES = [400, 401, 403, 404, 405, 409, 500];
118
+
119
+ // src/http/http.resolvers.ts
120
+ async function fetchSafely(fetcher) {
121
+ const response = await fetcher();
122
+ if (response.kind === "error")
123
+ return { error: response.error, data: null };
124
+ return { error: null, data: response.data };
125
+ }
126
+ async function fetchAndThrow(fetcher) {
127
+ const response = await fetcher();
128
+ if (response.kind === "error")
129
+ throw new Error(response.error);
130
+ return response.data;
131
+ }
132
+
133
+ // src/pagination/pagination.schemas.ts
134
+ import z from "zod";
135
+ var paginationSchema = z.object({
136
+ offset: z.number().int().nonnegative().optional(),
137
+ limit: z.number().int().positive().optional()
138
+ });
139
+
140
+ // src/pagination/pagination.utilities.ts
141
+ var getPagination = (options) => {
142
+ const pagination = {};
143
+ if (options?.offset !== void 0) {
144
+ pagination.offset = options.offset;
145
+ }
146
+ if (options?.limit !== void 0) {
147
+ pagination.limit = options.limit;
148
+ }
149
+ return pagination;
150
+ };
151
+
133
152
  // src/utilities/execution.utilities.ts
134
153
  async function safeExecute(fn, onError) {
135
154
  try {
@@ -148,12 +167,12 @@ async function measureExecutionTime(execution) {
148
167
  }
149
168
 
150
169
  // src/utilities/serialization.utilities.ts
151
- import { z } from "zod";
152
- var asQueryNumber = (schema) => z.preprocess((v) => typeof v === "string" ? Number(v) : v, schema);
170
+ import { z as z2 } from "zod";
171
+ var asQueryNumber = (schema) => z2.preprocess((v) => typeof v === "string" ? Number(v) : v, schema);
153
172
  var asQueryBoolean = (schema) => {
154
173
  const POSITIVE_VALUES = ["1", "true", "yes", "y", "on"];
155
174
  const NEGATIVE_VALUES = ["0", "false", "no", "n", "off"];
156
- return z.preprocess((value) => {
175
+ return z2.preprocess((value) => {
157
176
  if (typeof value === "boolean")
158
177
  return value;
159
178
  if (typeof value === "string") {
@@ -229,12 +248,14 @@ export {
229
248
  getColoredHTTPStatus,
230
249
  getFormattedDate,
231
250
  getFormattedTime,
251
+ getPagination,
232
252
  getUTCOffset,
233
253
  getZonedTime,
234
254
  honoLoggingHandler,
235
255
  log,
236
256
  measureExecutionTime,
237
257
  onHandlerError,
258
+ paginationSchema,
238
259
  respond,
239
260
  safeExecute,
240
261
  success
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kalutskii/foundation",
3
- "version": "0.5.0",
3
+ "version": "0.5.1",
4
4
  "description": "Typescript collection of most common utilities, schemas and functions among private projects.",
5
5
  "type": "module",
6
6
  "repository": {