hono-openapi 1.0.4 → 1.0.6

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.cjs CHANGED
@@ -141,6 +141,7 @@ function removeExcludedPaths(paths, ctx) {
141
141
  if (isPathExcluded && !(key.includes("*") && !key.includes("{")) && isStaticFileExcluded && value != null) {
142
142
  for (const method of Object.keys(value)) {
143
143
  const schema = value[method];
144
+ if (schema == null) continue;
144
145
  if (key.includes("{")) {
145
146
  if (!schema.parameters) schema.parameters = [];
146
147
  const pathParameters = key.split("/").filter(
@@ -251,6 +252,7 @@ async function generateSpecs(hono, options = DEFAULT_OPTIONS, c) {
251
252
  }
252
253
  async function generatePaths(hono, ctx) {
253
254
  const paths = {};
255
+ const promises = [];
254
256
  for (const route of hono.routes) {
255
257
  const middlewareHandler = route.handler[uniqueSymbol];
256
258
  if (!middlewareHandler) {
@@ -272,17 +274,20 @@ async function generatePaths(hono, ctx) {
272
274
  }
273
275
  }
274
276
  const defaultOptionsForThisMethod = ctx.options.defaultOptions?.[routeMethod];
275
- const { schema: routeSpecs, components = {} } = await getSpec(
276
- middlewareHandler,
277
- defaultOptionsForThisMethod
277
+ promises.push(
278
+ getSpec(middlewareHandler, defaultOptionsForThisMethod).then(
279
+ ({ schema: routeSpecs, components = {} }) => {
280
+ ctx.components = mergeComponentsObjects(ctx.components, components);
281
+ registerSchemaPath({
282
+ route,
283
+ specs: routeSpecs,
284
+ paths
285
+ });
286
+ }
287
+ )
278
288
  );
279
- ctx.components = mergeComponentsObjects(ctx.components, components);
280
- registerSchemaPath({
281
- route,
282
- specs: routeSpecs,
283
- paths
284
- });
285
289
  }
290
+ await Promise.all(promises);
286
291
  return paths;
287
292
  }
288
293
  function getHiddenValue(options) {
@@ -319,7 +324,10 @@ async function getSpec(middlewareHandler, defaultOptions) {
319
324
  const result2 = await raw.schema.toOpenAPISchema();
320
325
  raw.schema = result2.schema;
321
326
  if (result2.components) {
322
- components = mergeComponentsObjects(components, result2.components);
327
+ components = mergeComponentsObjects(
328
+ components,
329
+ result2.components
330
+ );
323
331
  }
324
332
  }
325
333
  }
@@ -378,9 +386,12 @@ function generateParameters(target, schema) {
378
386
  in: target === "param" ? "path" : target,
379
387
  name: key,
380
388
  // @ts-expect-error
381
- schema: value,
382
- required: schema.required?.includes(key)
389
+ schema: value
383
390
  };
391
+ const isRequired = schema.required?.includes(key);
392
+ if (isRequired) {
393
+ def.required = true;
394
+ }
384
395
  if (def.schema && "description" in def.schema && def.schema.description) {
385
396
  def.description = def.schema.description;
386
397
  def.schema.description = void 0;
@@ -392,7 +403,7 @@ function generateParameters(target, schema) {
392
403
  function mergeComponentsObjects(...components) {
393
404
  return components.reduce(
394
405
  (prev, component, index) => {
395
- if (!component || index === 0) return prev;
406
+ if (component == null || index === 0) return prev;
396
407
  if (prev.schemas && Object.keys(prev.schemas).length > 0 || component.schemas && Object.keys(component.schemas).length > 0) {
397
408
  prev.schemas = {
398
409
  ...prev.schemas,
@@ -411,6 +422,14 @@ function mergeComponentsObjects(...components) {
411
422
  );
412
423
  }
413
424
 
425
+ function loadVendor(vendor, fn) {
426
+ if (fn.toJSONSchema) {
427
+ standardJson.loadVendor(vendor, fn.toJSONSchema);
428
+ }
429
+ if (fn.toOpenAPISchema) {
430
+ standardOpenapi.loadVendor(vendor, fn.toOpenAPISchema);
431
+ }
432
+ }
414
433
  function resolver(schema, options) {
415
434
  return {
416
435
  vendor: schema["~standard"].vendor,
@@ -477,6 +496,7 @@ exports.ALLOWED_METHODS = ALLOWED_METHODS;
477
496
  exports.describeResponse = describeResponse;
478
497
  exports.describeRoute = describeRoute;
479
498
  exports.generateSpecs = generateSpecs;
499
+ exports.loadVendor = loadVendor;
480
500
  exports.openAPIRouteHandler = openAPIRouteHandler;
481
501
  exports.registerSchemaPath = registerSchemaPath;
482
502
  exports.removeExcludedPaths = removeExcludedPaths;
package/dist/index.d.cts CHANGED
@@ -2,9 +2,11 @@ import * as openapi_types from 'openapi-types';
2
2
  import { OpenAPIV3_1 } from 'openapi-types';
3
3
  import { Env, Input, Context, Next, MiddlewareHandler, ValidationTargets, Hono } from 'hono';
4
4
  import { TypedResponse, RouterRoute, ValidationTargets as ValidationTargets$1, BlankEnv, Input as Input$1, BlankInput, Schema, BlankSchema, MiddlewareHandler as MiddlewareHandler$1 } from 'hono/types';
5
- import * as json_schema from 'json-schema';
6
5
  import { Hook } from '@hono/standard-validator';
6
+ import { loadVendor as loadVendor$1 } from '@standard-community/standard-json';
7
+ import { loadVendor as loadVendor$2 } from '@standard-community/standard-openapi';
7
8
  import { StatusCode } from 'hono/utils/http-status';
9
+ import { JSONSchema7 } from 'json-schema';
8
10
 
9
11
  /** The Standard Schema interface. */
10
12
  interface StandardSchemaV1<Input = unknown, Output = Input> {
@@ -63,6 +65,10 @@ declare namespace StandardSchemaV1 {
63
65
  export { };
64
66
  }
65
67
 
68
+ declare function loadVendor(vendor: string, fn: {
69
+ toJSONSchema?: Parameters<typeof loadVendor$1>[1];
70
+ toOpenAPISchema?: Parameters<typeof loadVendor$2>[1];
71
+ }): void;
66
72
  /**
67
73
  * Generate a resolver for a validation schema
68
74
  * @param schema Validation schema
@@ -71,7 +77,7 @@ declare namespace StandardSchemaV1 {
71
77
  declare function resolver<Schema extends StandardSchemaV1>(schema: Schema, options?: Record<string, unknown>): {
72
78
  vendor: string;
73
79
  validate: (value: unknown) => StandardSchemaV1.Result<unknown> | Promise<StandardSchemaV1.Result<unknown>>;
74
- toJSONSchema: () => Promise<json_schema.JSONSchema7>;
80
+ toJSONSchema: () => JSONSchema7 | Promise<JSONSchema7>;
75
81
  toOpenAPISchema: () => Promise<{
76
82
  schema: OpenAPIV3_1.SchemaObject;
77
83
  components: OpenAPIV3_1.ComponentsObject | undefined;
@@ -272,4 +278,4 @@ declare function generateSpecs<E extends Env = BlankEnv, P extends string = stri
272
278
  jsonSchemaDialect?: string;
273
279
  }>;
274
280
 
275
- export { ALLOWED_METHODS, type AllowedMethods, type DescribeRouteOptions, type GenerateSpecOptions, type HandlerUniqueProperty, type PromiseOr, type RegisterSchemaPathOptions, type ResolverReturnType, type SpecContext, describeResponse, describeRoute, generateSpecs, openAPIRouteHandler, registerSchemaPath, removeExcludedPaths, resolver, uniqueSymbol, validator };
281
+ export { ALLOWED_METHODS, type AllowedMethods, type DescribeRouteOptions, type GenerateSpecOptions, type HandlerUniqueProperty, type PromiseOr, type RegisterSchemaPathOptions, type ResolverReturnType, type SpecContext, describeResponse, describeRoute, generateSpecs, loadVendor, openAPIRouteHandler, registerSchemaPath, removeExcludedPaths, resolver, uniqueSymbol, validator };
package/dist/index.d.ts CHANGED
@@ -2,9 +2,11 @@ import * as openapi_types from 'openapi-types';
2
2
  import { OpenAPIV3_1 } from 'openapi-types';
3
3
  import { Env, Input, Context, Next, MiddlewareHandler, ValidationTargets, Hono } from 'hono';
4
4
  import { TypedResponse, RouterRoute, ValidationTargets as ValidationTargets$1, BlankEnv, Input as Input$1, BlankInput, Schema, BlankSchema, MiddlewareHandler as MiddlewareHandler$1 } from 'hono/types';
5
- import * as json_schema from 'json-schema';
6
5
  import { Hook } from '@hono/standard-validator';
6
+ import { loadVendor as loadVendor$1 } from '@standard-community/standard-json';
7
+ import { loadVendor as loadVendor$2 } from '@standard-community/standard-openapi';
7
8
  import { StatusCode } from 'hono/utils/http-status';
9
+ import { JSONSchema7 } from 'json-schema';
8
10
 
9
11
  /** The Standard Schema interface. */
10
12
  interface StandardSchemaV1<Input = unknown, Output = Input> {
@@ -63,6 +65,10 @@ declare namespace StandardSchemaV1 {
63
65
  export { };
64
66
  }
65
67
 
68
+ declare function loadVendor(vendor: string, fn: {
69
+ toJSONSchema?: Parameters<typeof loadVendor$1>[1];
70
+ toOpenAPISchema?: Parameters<typeof loadVendor$2>[1];
71
+ }): void;
66
72
  /**
67
73
  * Generate a resolver for a validation schema
68
74
  * @param schema Validation schema
@@ -71,7 +77,7 @@ declare namespace StandardSchemaV1 {
71
77
  declare function resolver<Schema extends StandardSchemaV1>(schema: Schema, options?: Record<string, unknown>): {
72
78
  vendor: string;
73
79
  validate: (value: unknown) => StandardSchemaV1.Result<unknown> | Promise<StandardSchemaV1.Result<unknown>>;
74
- toJSONSchema: () => Promise<json_schema.JSONSchema7>;
80
+ toJSONSchema: () => JSONSchema7 | Promise<JSONSchema7>;
75
81
  toOpenAPISchema: () => Promise<{
76
82
  schema: OpenAPIV3_1.SchemaObject;
77
83
  components: OpenAPIV3_1.ComponentsObject | undefined;
@@ -272,4 +278,4 @@ declare function generateSpecs<E extends Env = BlankEnv, P extends string = stri
272
278
  jsonSchemaDialect?: string;
273
279
  }>;
274
280
 
275
- export { ALLOWED_METHODS, type AllowedMethods, type DescribeRouteOptions, type GenerateSpecOptions, type HandlerUniqueProperty, type PromiseOr, type RegisterSchemaPathOptions, type ResolverReturnType, type SpecContext, describeResponse, describeRoute, generateSpecs, openAPIRouteHandler, registerSchemaPath, removeExcludedPaths, resolver, uniqueSymbol, validator };
281
+ export { ALLOWED_METHODS, type AllowedMethods, type DescribeRouteOptions, type GenerateSpecOptions, type HandlerUniqueProperty, type PromiseOr, type RegisterSchemaPathOptions, type ResolverReturnType, type SpecContext, describeResponse, describeRoute, generateSpecs, loadVendor, openAPIRouteHandler, registerSchemaPath, removeExcludedPaths, resolver, uniqueSymbol, validator };
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { sValidator } from '@hono/standard-validator';
2
- import { toJsonSchema } from '@standard-community/standard-json';
3
- import { toOpenAPISchema } from '@standard-community/standard-openapi';
2
+ import { loadVendor as loadVendor$1, toJsonSchema } from '@standard-community/standard-json';
3
+ import { loadVendor as loadVendor$2, toOpenAPISchema } from '@standard-community/standard-openapi';
4
4
 
5
5
  const uniqueSymbol = Symbol("openapi");
6
6
  const ALLOWED_METHODS = [
@@ -139,6 +139,7 @@ function removeExcludedPaths(paths, ctx) {
139
139
  if (isPathExcluded && !(key.includes("*") && !key.includes("{")) && isStaticFileExcluded && value != null) {
140
140
  for (const method of Object.keys(value)) {
141
141
  const schema = value[method];
142
+ if (schema == null) continue;
142
143
  if (key.includes("{")) {
143
144
  if (!schema.parameters) schema.parameters = [];
144
145
  const pathParameters = key.split("/").filter(
@@ -249,6 +250,7 @@ async function generateSpecs(hono, options = DEFAULT_OPTIONS, c) {
249
250
  }
250
251
  async function generatePaths(hono, ctx) {
251
252
  const paths = {};
253
+ const promises = [];
252
254
  for (const route of hono.routes) {
253
255
  const middlewareHandler = route.handler[uniqueSymbol];
254
256
  if (!middlewareHandler) {
@@ -270,17 +272,20 @@ async function generatePaths(hono, ctx) {
270
272
  }
271
273
  }
272
274
  const defaultOptionsForThisMethod = ctx.options.defaultOptions?.[routeMethod];
273
- const { schema: routeSpecs, components = {} } = await getSpec(
274
- middlewareHandler,
275
- defaultOptionsForThisMethod
275
+ promises.push(
276
+ getSpec(middlewareHandler, defaultOptionsForThisMethod).then(
277
+ ({ schema: routeSpecs, components = {} }) => {
278
+ ctx.components = mergeComponentsObjects(ctx.components, components);
279
+ registerSchemaPath({
280
+ route,
281
+ specs: routeSpecs,
282
+ paths
283
+ });
284
+ }
285
+ )
276
286
  );
277
- ctx.components = mergeComponentsObjects(ctx.components, components);
278
- registerSchemaPath({
279
- route,
280
- specs: routeSpecs,
281
- paths
282
- });
283
287
  }
288
+ await Promise.all(promises);
284
289
  return paths;
285
290
  }
286
291
  function getHiddenValue(options) {
@@ -317,7 +322,10 @@ async function getSpec(middlewareHandler, defaultOptions) {
317
322
  const result2 = await raw.schema.toOpenAPISchema();
318
323
  raw.schema = result2.schema;
319
324
  if (result2.components) {
320
- components = mergeComponentsObjects(components, result2.components);
325
+ components = mergeComponentsObjects(
326
+ components,
327
+ result2.components
328
+ );
321
329
  }
322
330
  }
323
331
  }
@@ -376,9 +384,12 @@ function generateParameters(target, schema) {
376
384
  in: target === "param" ? "path" : target,
377
385
  name: key,
378
386
  // @ts-expect-error
379
- schema: value,
380
- required: schema.required?.includes(key)
387
+ schema: value
381
388
  };
389
+ const isRequired = schema.required?.includes(key);
390
+ if (isRequired) {
391
+ def.required = true;
392
+ }
382
393
  if (def.schema && "description" in def.schema && def.schema.description) {
383
394
  def.description = def.schema.description;
384
395
  def.schema.description = void 0;
@@ -390,7 +401,7 @@ function generateParameters(target, schema) {
390
401
  function mergeComponentsObjects(...components) {
391
402
  return components.reduce(
392
403
  (prev, component, index) => {
393
- if (!component || index === 0) return prev;
404
+ if (component == null || index === 0) return prev;
394
405
  if (prev.schemas && Object.keys(prev.schemas).length > 0 || component.schemas && Object.keys(component.schemas).length > 0) {
395
406
  prev.schemas = {
396
407
  ...prev.schemas,
@@ -409,6 +420,14 @@ function mergeComponentsObjects(...components) {
409
420
  );
410
421
  }
411
422
 
423
+ function loadVendor(vendor, fn) {
424
+ if (fn.toJSONSchema) {
425
+ loadVendor$1(vendor, fn.toJSONSchema);
426
+ }
427
+ if (fn.toOpenAPISchema) {
428
+ loadVendor$2(vendor, fn.toOpenAPISchema);
429
+ }
430
+ }
412
431
  function resolver(schema, options) {
413
432
  return {
414
433
  vendor: schema["~standard"].vendor,
@@ -471,4 +490,4 @@ function describeResponse(handler, responses, options) {
471
490
  });
472
491
  }
473
492
 
474
- export { ALLOWED_METHODS, describeResponse, describeRoute, generateSpecs, openAPIRouteHandler, registerSchemaPath, removeExcludedPaths, resolver, uniqueSymbol, validator };
493
+ export { ALLOWED_METHODS, describeResponse, describeRoute, generateSpecs, loadVendor, openAPIRouteHandler, registerSchemaPath, removeExcludedPaths, resolver, uniqueSymbol, validator };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "hono-openapi",
3
3
  "description": "OpenAPI schema generator for Hono",
4
- "version": "1.0.4",
4
+ "version": "1.0.6",
5
5
  "type": "module",
6
6
  "main": "dist/index.cjs",
7
7
  "module": "dist/index.js",
@@ -43,8 +43,8 @@
43
43
  },
44
44
  "peerDependencies": {
45
45
  "@hono/standard-validator": "^0.1.2",
46
- "@standard-community/standard-json": "^0.3.0-rc.4",
47
- "@standard-community/standard-openapi": "^0.2.0-rc.1",
46
+ "@standard-community/standard-json": "^0.3.1",
47
+ "@standard-community/standard-openapi": "^0.2.3",
48
48
  "@types/json-schema": "^7.0.15",
49
49
  "hono": "^4.8.3",
50
50
  "openapi-types": "^12.1.3"
@@ -63,6 +63,9 @@
63
63
  "@valibot/to-json-schema": "^1.3.0",
64
64
  "arktype": "^2.1.22",
65
65
  "effect": "^3.17.13",
66
+ "husky": "^9.1.7",
67
+ "is-ci": "^4.1.0",
68
+ "nano-staged": "^0.8.0",
66
69
  "pkgroll": "^2.13.1",
67
70
  "typescript": "^5.8.3",
68
71
  "valibot": "^1.1.0",