hono-utils 0.2.0 → 0.3.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/README.md +10 -1
- package/dist/index.cjs +53 -7
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +21 -4
- package/dist/index.d.ts +21 -4
- package/dist/index.js +52 -6
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import * as hono from 'hono';
|
|
2
|
-
import { Env, Context, ErrorHandler, NotFoundHandler } from 'hono';
|
|
2
|
+
import { Env, Context, ErrorHandler, NotFoundHandler, Hono } from 'hono';
|
|
3
3
|
import { Logger, Details } from 'hierarchical-area-logger';
|
|
4
4
|
import * as hono_utils_http_status from 'hono/utils/http-status';
|
|
5
|
-
import { ContentfulStatusCode, SuccessStatusCode, ClientErrorStatusCode } from 'hono/utils/http-status';
|
|
5
|
+
import { ContentfulStatusCode, SuccessStatusCode, ClientErrorStatusCode, StatusCode } from 'hono/utils/http-status';
|
|
6
6
|
import z$1, { z, ZodObject } from 'zod';
|
|
7
7
|
import { Message, MessageBatch, Queue } from '@cloudflare/workers-types';
|
|
8
8
|
import * as hono_utils_types from 'hono/utils/types';
|
|
9
|
+
import { hc, ClientResponse } from 'hono/client';
|
|
9
10
|
|
|
10
11
|
type HonoLoggerVariables = {
|
|
11
12
|
logger: Logger;
|
|
@@ -354,8 +355,24 @@ declare namespace sha {
|
|
|
354
355
|
export { sha_generateSalt as generateSalt, sha_hash as hash };
|
|
355
356
|
}
|
|
356
357
|
|
|
357
|
-
declare const onError: ErrorHandler;
|
|
358
|
+
declare const onError: <Bindings extends Record<string, unknown>, Variables extends Record<string, unknown>>(parseError?: (err: Error, env: Bindings, get: <K extends keyof Variables>(key: K) => Variables[K]) => Promise<string>) => ErrorHandler;
|
|
358
359
|
|
|
359
360
|
declare const onNotFound: NotFoundHandler;
|
|
360
361
|
|
|
361
|
-
|
|
362
|
+
type ErrorBody = Record<string, unknown>;
|
|
363
|
+
interface TypedClientCallbacks {
|
|
364
|
+
onStart?: () => void;
|
|
365
|
+
onSuccess?: (parsedData: unknown, headers: Headers) => void;
|
|
366
|
+
onError?: (parsedData: unknown, headers: Headers) => void;
|
|
367
|
+
onEnd?: () => void;
|
|
368
|
+
errorHandler?: (status: StatusCode | number, body?: ErrorBody) => never;
|
|
369
|
+
}
|
|
370
|
+
interface CreateTypedClientOptions {
|
|
371
|
+
url: string;
|
|
372
|
+
headers?: Record<string, string> | (() => Record<string, string> | Promise<Record<string, string>>);
|
|
373
|
+
fetch?: typeof fetch;
|
|
374
|
+
callbacks?: TypedClientCallbacks;
|
|
375
|
+
}
|
|
376
|
+
declare const createTypedClient: <TApp extends Hono<any, any, any>>(options: CreateTypedClientOptions) => <TSuccessData>(fn: (c: ReturnType<typeof hc<TApp>>) => Promise<ClientResponse<TSuccessData>>) => Promise<TSuccessData>;
|
|
377
|
+
|
|
378
|
+
export { type ContextFn, type CreateTypedClientOptions, type HonoI18nVariables, type HonoIsBotVariables, type HonoLanguageVariables, type HonoLoggerVariables, type HonoResponseVariables, type MessageHandlers, pbkdf2 as PBKDF2, QueueHandler, sha as SHA, type WrappableMiddleware, createTypedClient, i18n, isBot, jsonValidator, logger, onError, onNotFound, queue, response, withLogger };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import * as hono from 'hono';
|
|
2
|
-
import { Env, Context, ErrorHandler, NotFoundHandler } from 'hono';
|
|
2
|
+
import { Env, Context, ErrorHandler, NotFoundHandler, Hono } from 'hono';
|
|
3
3
|
import { Logger, Details } from 'hierarchical-area-logger';
|
|
4
4
|
import * as hono_utils_http_status from 'hono/utils/http-status';
|
|
5
|
-
import { ContentfulStatusCode, SuccessStatusCode, ClientErrorStatusCode } from 'hono/utils/http-status';
|
|
5
|
+
import { ContentfulStatusCode, SuccessStatusCode, ClientErrorStatusCode, StatusCode } from 'hono/utils/http-status';
|
|
6
6
|
import z$1, { z, ZodObject } from 'zod';
|
|
7
7
|
import { Message, MessageBatch, Queue } from '@cloudflare/workers-types';
|
|
8
8
|
import * as hono_utils_types from 'hono/utils/types';
|
|
9
|
+
import { hc, ClientResponse } from 'hono/client';
|
|
9
10
|
|
|
10
11
|
type HonoLoggerVariables = {
|
|
11
12
|
logger: Logger;
|
|
@@ -354,8 +355,24 @@ declare namespace sha {
|
|
|
354
355
|
export { sha_generateSalt as generateSalt, sha_hash as hash };
|
|
355
356
|
}
|
|
356
357
|
|
|
357
|
-
declare const onError: ErrorHandler;
|
|
358
|
+
declare const onError: <Bindings extends Record<string, unknown>, Variables extends Record<string, unknown>>(parseError?: (err: Error, env: Bindings, get: <K extends keyof Variables>(key: K) => Variables[K]) => Promise<string>) => ErrorHandler;
|
|
358
359
|
|
|
359
360
|
declare const onNotFound: NotFoundHandler;
|
|
360
361
|
|
|
361
|
-
|
|
362
|
+
type ErrorBody = Record<string, unknown>;
|
|
363
|
+
interface TypedClientCallbacks {
|
|
364
|
+
onStart?: () => void;
|
|
365
|
+
onSuccess?: (parsedData: unknown, headers: Headers) => void;
|
|
366
|
+
onError?: (parsedData: unknown, headers: Headers) => void;
|
|
367
|
+
onEnd?: () => void;
|
|
368
|
+
errorHandler?: (status: StatusCode | number, body?: ErrorBody) => never;
|
|
369
|
+
}
|
|
370
|
+
interface CreateTypedClientOptions {
|
|
371
|
+
url: string;
|
|
372
|
+
headers?: Record<string, string> | (() => Record<string, string> | Promise<Record<string, string>>);
|
|
373
|
+
fetch?: typeof fetch;
|
|
374
|
+
callbacks?: TypedClientCallbacks;
|
|
375
|
+
}
|
|
376
|
+
declare const createTypedClient: <TApp extends Hono<any, any, any>>(options: CreateTypedClientOptions) => <TSuccessData>(fn: (c: ReturnType<typeof hc<TApp>>) => Promise<ClientResponse<TSuccessData>>) => Promise<TSuccessData>;
|
|
377
|
+
|
|
378
|
+
export { type ContextFn, type CreateTypedClientOptions, type HonoI18nVariables, type HonoIsBotVariables, type HonoLanguageVariables, type HonoLoggerVariables, type HonoResponseVariables, type MessageHandlers, pbkdf2 as PBKDF2, QueueHandler, sha as SHA, type WrappableMiddleware, createTypedClient, i18n, isBot, jsonValidator, logger, onError, onNotFound, queue, response, withLogger };
|
package/dist/index.js
CHANGED
|
@@ -14120,14 +14120,18 @@ var defaultMessageMap = {
|
|
|
14120
14120
|
};
|
|
14121
14121
|
|
|
14122
14122
|
// src/router/onError.ts
|
|
14123
|
-
var onError = (err, { json: json2,
|
|
14123
|
+
var onError = (parseError) => async (err, { json: json2, env, get }) => {
|
|
14124
14124
|
try {
|
|
14125
|
-
const { error: error48 } = logger2.getArea(`error`);
|
|
14126
|
-
error48(err.message, err);
|
|
14127
14125
|
const status = "status" in err ? err.status : 500;
|
|
14128
|
-
return json2(
|
|
14129
|
-
|
|
14130
|
-
|
|
14126
|
+
return json2(
|
|
14127
|
+
{
|
|
14128
|
+
message: !parseError ? err.message : await parseError(err, env, get)
|
|
14129
|
+
},
|
|
14130
|
+
status
|
|
14131
|
+
);
|
|
14132
|
+
} catch (error48) {
|
|
14133
|
+
console.error("Failed on error handler:", err);
|
|
14134
|
+
console.error("Failed to parse error:", error48);
|
|
14131
14135
|
return json2({ message: defaultMessageMap.internalError }, 500);
|
|
14132
14136
|
}
|
|
14133
14137
|
};
|
|
@@ -14141,10 +14145,52 @@ var onNotFound = async (c) => {
|
|
|
14141
14145
|
404
|
|
14142
14146
|
);
|
|
14143
14147
|
};
|
|
14148
|
+
|
|
14149
|
+
// src/client/createTypedClient.ts
|
|
14150
|
+
import { hc } from "hono/client";
|
|
14151
|
+
import { parseResponse, DetailedError } from "hono/client";
|
|
14152
|
+
import { HTTPException as HTTPException4 } from "hono/http-exception";
|
|
14153
|
+
var createTypedClient = (options) => {
|
|
14154
|
+
const client = hc(options.url, {
|
|
14155
|
+
headers: options.headers,
|
|
14156
|
+
fetch: options.fetch
|
|
14157
|
+
});
|
|
14158
|
+
const rpcClient = async (fn) => {
|
|
14159
|
+
options.callbacks?.onStart?.();
|
|
14160
|
+
let responseHeaders = new Headers();
|
|
14161
|
+
try {
|
|
14162
|
+
const response2 = await fn(client);
|
|
14163
|
+
responseHeaders = response2.headers;
|
|
14164
|
+
const data = await parseResponse(response2);
|
|
14165
|
+
options.callbacks?.onSuccess?.(data, responseHeaders);
|
|
14166
|
+
return data;
|
|
14167
|
+
} catch (err) {
|
|
14168
|
+
const errorBody = { message: err.message };
|
|
14169
|
+
let status = 500;
|
|
14170
|
+
if (err instanceof DetailedError) {
|
|
14171
|
+
const { detail, statusCode } = err;
|
|
14172
|
+
status = statusCode ?? 500;
|
|
14173
|
+
if (!detail) {
|
|
14174
|
+
options.callbacks?.errorHandler?.(500, {
|
|
14175
|
+
message: "Fetch malformed"
|
|
14176
|
+
});
|
|
14177
|
+
throw new HTTPException4(500, { message: "Fetch malformed" });
|
|
14178
|
+
}
|
|
14179
|
+
}
|
|
14180
|
+
options.callbacks?.onError?.(errorBody, responseHeaders);
|
|
14181
|
+
options.callbacks?.errorHandler?.(status, errorBody);
|
|
14182
|
+
throw new HTTPException4(status, errorBody);
|
|
14183
|
+
} finally {
|
|
14184
|
+
options.callbacks?.onEnd?.();
|
|
14185
|
+
}
|
|
14186
|
+
};
|
|
14187
|
+
return rpcClient;
|
|
14188
|
+
};
|
|
14144
14189
|
export {
|
|
14145
14190
|
pbkdf2_exports as PBKDF2,
|
|
14146
14191
|
QueueHandler,
|
|
14147
14192
|
sha_exports as SHA,
|
|
14193
|
+
createTypedClient,
|
|
14148
14194
|
i18n,
|
|
14149
14195
|
isBot,
|
|
14150
14196
|
jsonValidator,
|