@okay-e9g/hono-config 0.0.36 → 0.0.37

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/package.json CHANGED
@@ -48,5 +48,5 @@
48
48
  "prepublishOnly": "bun run typecheck && bun test",
49
49
  "typecheck": "tsc --noEmit"
50
50
  },
51
- "version": "0.0.36"
51
+ "version": "0.0.37"
52
52
  }
@@ -2,7 +2,7 @@
2
2
  * SPDX-License-Identifier: MIT
3
3
  */
4
4
 
5
- import { OpenAPIHono } from '@hono/zod-openapi';
5
+ import { OpenAPIHono, type OpenAPIHonoOptions } from '@hono/zod-openapi';
6
6
  import type { Factory } from 'hono/factory';
7
7
  import type { HonoOptions } from 'hono/hono-base';
8
8
  import type { Env } from 'hono/types';
@@ -13,6 +13,26 @@ import { ensureResponseType } from '../middlewares/ensure-rt';
13
13
  import { superjson } from '../middlewares/superjson';
14
14
  import { type CreateHonoFactoryOpts, createHonoFactory } from './factory';
15
15
 
16
+ /**
17
+ * Options used to customize the shared OpenAPI Hono application factory.
18
+ *
19
+ * @typeParam T - Hono environment type for bindings and variables.
20
+ */
21
+ export interface CreateOpenAPIHonoFactoryOpts<T extends Env> extends CreateHonoFactoryOpts<T> {
22
+ /**
23
+ * Default validation hook used by every app created by this factory.
24
+ *
25
+ * Receives the validation result and the app's typed Hono context on both
26
+ * success and failure. Supports synchronous and asynchronous hooks. Return a
27
+ * response to end the request, or throw an error to invoke the app's error
28
+ * handler. A hook supplied to `app.openapi` takes precedence for that route.
29
+ *
30
+ * When omitted or `undefined`, the default hook throws validation errors and
31
+ * lets successful validation continue. A custom hook replaces this behavior.
32
+ */
33
+ validationDefaultHook: OpenAPIHonoOptions<T>['defaultHook'];
34
+ }
35
+
16
36
  /**
17
37
  * Hono factory whose apps expose OpenAPI route helpers.
18
38
  *
@@ -20,7 +40,11 @@ import { type CreateHonoFactoryOpts, createHonoFactory } from './factory';
20
40
  */
21
41
  export type OpenAPIHonoFactory<T extends Env> = Omit<Factory<T>, 'createApp'> & {
22
42
  /**
23
- * Creates an `OpenAPIHono` app configured with the shared defaults.
43
+ * Creates an `OpenAPIHono` app with the factory's middleware, handlers, and
44
+ * validation default hook. Routing is always non-strict.
45
+ *
46
+ * @param opts - Hono application options.
47
+ * @returns A new app with OpenAPI route helpers.
24
48
  */
25
49
  createApp: (opts?: HonoOptions<T>) => OpenAPIHono<T>;
26
50
  };
@@ -28,34 +52,39 @@ export type OpenAPIHonoFactory<T extends Env> = Omit<Factory<T>, 'createApp'> &
28
52
  /**
29
53
  * Creates a Hono factory that produces `OpenAPIHono` apps with shared defaults.
30
54
  *
31
- * The generated apps use non-strict routing, throw validation failures from the
32
- * OpenAPI default hook, install the shared not-found and error handlers, enable
33
- * SuperJSON response serialization, apply application middleware when present,
34
- * and finish with the response-type guard.
55
+ * The generated apps use non-strict routing, install the configured not-found
56
+ * and error handlers, enable SuperJSON response serialization, apply application
57
+ * middleware when present, and finish with the response-type guard.
58
+ *
59
+ * By default, OpenAPI validation failures are thrown to the app's error handler.
60
+ * Set `validationDefaultHook` to replace this behavior for all apps created by
61
+ * the factory. Individual routes can override it with a hook passed to
62
+ * `app.openapi`.
35
63
  *
36
64
  * Include exactly one response-type middleware, such as `apiResponseType()` or
37
65
  * `rpcResponseType()`, in `appMiddlewares` so the shared handlers can build the
38
66
  * correct response envelope.
39
67
  *
40
68
  * @typeParam T - Hono environment type for bindings and variables.
41
- * @param opts - Optional overrides for middleware and default handlers.
69
+ * @param opts - Optional overrides for middleware, handlers, and the validation default hook.
42
70
  * @returns A Hono factory whose `createApp` method returns an `OpenAPIHono`.
43
71
  */
44
72
  export const createOpenAPIHonoFactory = <T extends Env>({
45
73
  appMiddlewares = [],
46
74
  notFoundHandler = notFound,
47
75
  onErrorHandler = onError,
48
- }: Partial<CreateHonoFactoryOpts<T>> = {}): OpenAPIHonoFactory<T> => {
76
+ validationDefaultHook = (result) => {
77
+ if (!result.success) {
78
+ throw result.error;
79
+ }
80
+ },
81
+ }: Partial<CreateOpenAPIHonoFactoryOpts<T>> = {}): OpenAPIHonoFactory<T> => {
49
82
  return {
50
83
  ...createHonoFactory<T>(),
51
84
  createApp: (opts) => {
52
85
  const app = new OpenAPIHono<T>({
53
86
  ...(opts as HonoOptions<Env>),
54
- defaultHook: (result) => {
55
- if (!result.success) {
56
- throw result.error;
57
- }
58
- },
87
+ defaultHook: validationDefaultHook,
59
88
  strict: false,
60
89
  });
61
90