@kalutskii/foundation 0.1.6 → 0.1.9
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/README.md +3 -2
- package/dist/index.d.ts +9 -3
- package/dist/index.js +18 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# @kalutskii/foundation
|
|
2
2
|
|
|
3
|
-
Collection of utilities, types, and helpers for building typed API contracts and responses in TypeScript projects.
|
|
3
|
+
Collection of utilities, types, and helpers for building typed API contracts and responses in TypeScript projects.
|
|
4
|
+
Designed for use in Bun/Node.js and Cloudflare Workers environments.
|
|
4
5
|
|
|
5
6
|
## What this package provides
|
|
6
7
|
|
|
@@ -62,7 +63,7 @@ declare function getUser(): Promise<APIContractResult<User>>;
|
|
|
62
63
|
|
|
63
64
|
const safe = await fetchSafely(getUser);
|
|
64
65
|
if (safe.error) {
|
|
65
|
-
console.error(safe.error
|
|
66
|
+
console.error(safe.error);
|
|
66
67
|
} else {
|
|
67
68
|
console.log(safe.data.name);
|
|
68
69
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ErrorHandler, Context, TypedResponse } from 'hono';
|
|
1
|
+
import { ErrorHandler, MiddlewareHandler, Context, TypedResponse } from 'hono';
|
|
2
2
|
import { z, ZodNumber } from 'zod';
|
|
3
3
|
import { Locale } from 'date-fns';
|
|
4
4
|
|
|
@@ -25,7 +25,7 @@ type FetchResult<T> = {
|
|
|
25
25
|
error: null;
|
|
26
26
|
data: T;
|
|
27
27
|
} | {
|
|
28
|
-
error:
|
|
28
|
+
error: string;
|
|
29
29
|
data: null;
|
|
30
30
|
};
|
|
31
31
|
|
|
@@ -51,6 +51,12 @@ declare function fetchAndThrow<TResult extends APIContractResult<unknown>>(fetch
|
|
|
51
51
|
*/
|
|
52
52
|
declare const onHandlerError: ErrorHandler;
|
|
53
53
|
|
|
54
|
+
/**
|
|
55
|
+
* Request logging middleware for Hono, providing detailed logs for each incoming request.
|
|
56
|
+
* Example log output: [12:12:12 (+4 UTC)] hono | POST 200 123ms /api/v1/users (search=term)
|
|
57
|
+
*/
|
|
58
|
+
declare const honoLoggingHandler: MiddlewareHandler;
|
|
59
|
+
|
|
54
60
|
/**
|
|
55
61
|
* Type utility that recursively transforms all Date fields to string, as well as handling arrays and objects.
|
|
56
62
|
* This is necessary for proper typing when working with RPC, as JSON does not support the Date type directly.
|
|
@@ -144,4 +150,4 @@ declare const log: {
|
|
|
144
150
|
error: (message: string, service?: string, stack?: string) => void;
|
|
145
151
|
};
|
|
146
152
|
|
|
147
|
-
export { type APIContractData, type APIContractError, type APIContractResult, type APIError, type APISuccess, EXCEPTION_STATUS_CODES, type ExceptionStatusCode, type FetchResult, SUCCESS_STATUS_CODES, type SerializeDates, type SuccessStatusCode, asQueryBoolean, asQueryNumber, failure, fetchAndThrow, fetchSafely, formatTime, generateRandomString, getColoredHTTPStatus, getFormattedDate, getFormattedTime, getUTCOffset, getZonedTime, log, onHandlerError, respond, safeExecute, success };
|
|
153
|
+
export { type APIContractData, type APIContractError, type APIContractResult, type APIError, type APISuccess, EXCEPTION_STATUS_CODES, type ExceptionStatusCode, type FetchResult, SUCCESS_STATUS_CODES, type SerializeDates, type SuccessStatusCode, asQueryBoolean, asQueryNumber, failure, fetchAndThrow, fetchSafely, formatTime, generateRandomString, getColoredHTTPStatus, getFormattedDate, getFormattedTime, getUTCOffset, getZonedTime, honoLoggingHandler, log, onHandlerError, respond, safeExecute, success };
|
package/dist/index.js
CHANGED
|
@@ -14,7 +14,7 @@ function failure({ status, error }) {
|
|
|
14
14
|
async function fetchSafely(fetcher) {
|
|
15
15
|
const response = await fetcher();
|
|
16
16
|
if (response.kind === "error")
|
|
17
|
-
return { error:
|
|
17
|
+
return { error: response.error, data: null };
|
|
18
18
|
return { error: null, data: response.data };
|
|
19
19
|
}
|
|
20
20
|
async function fetchAndThrow(fetcher) {
|
|
@@ -109,6 +109,22 @@ var onHandlerError = (error, c) => {
|
|
|
109
109
|
return c.json(response, response.status);
|
|
110
110
|
};
|
|
111
111
|
|
|
112
|
+
// src/hono/hono.logging.ts
|
|
113
|
+
import { blue as blue2, bold, dim as dim2, white as white2 } from "kleur/colors";
|
|
114
|
+
var honoLoggingHandler = async (c, next) => {
|
|
115
|
+
const requestUrl = new URL(c.req.url);
|
|
116
|
+
const searchParams = requestUrl.searchParams.toString();
|
|
117
|
+
const startTime = performance.now();
|
|
118
|
+
await next();
|
|
119
|
+
const duration = Math.round(performance.now() - startTime);
|
|
120
|
+
const coloredMethod = bold(blue2(c.req.method.padEnd(4)));
|
|
121
|
+
const coloredStatus = getColoredHTTPStatus(c.res.status)(String(c.res.status).padEnd(4));
|
|
122
|
+
const coloredTime = dim2(`${duration}ms`.padStart(6));
|
|
123
|
+
const coloredPath = white2(c.req.path.padEnd(44));
|
|
124
|
+
const coloredSearchParams = searchParams ? dim2(` (${searchParams})`) : "";
|
|
125
|
+
log.info(`${coloredMethod} ${coloredStatus} ${coloredTime} ${coloredPath}${coloredSearchParams}`, "hono");
|
|
126
|
+
};
|
|
127
|
+
|
|
112
128
|
// src/hono/hono.respond.ts
|
|
113
129
|
function respond(c, options) {
|
|
114
130
|
return c.json(success({ status: options.status, data: options.data ?? {} }), options.status);
|
|
@@ -159,6 +175,7 @@ export {
|
|
|
159
175
|
getFormattedTime,
|
|
160
176
|
getUTCOffset,
|
|
161
177
|
getZonedTime,
|
|
178
|
+
honoLoggingHandler,
|
|
162
179
|
log,
|
|
163
180
|
onHandlerError,
|
|
164
181
|
respond,
|