hono-openapi 1.0.3 → 1.0.5
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 +18 -8
- package/dist/index.d.cts +9 -3
- package/dist/index.d.ts +9 -3
- package/dist/index.js +20 -11
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -32,7 +32,7 @@ const toOpenAPIPath = (path) => path.split("/").map((x) => {
|
|
|
32
32
|
}).join("/");
|
|
33
33
|
const capitalize = (word) => word.charAt(0).toUpperCase() + word.slice(1);
|
|
34
34
|
const generateOperationId = (route) => {
|
|
35
|
-
let operationId = route.method;
|
|
35
|
+
let operationId = route.method.toLowerCase();
|
|
36
36
|
if (route.path === "/") return `${operationId}Index`;
|
|
37
37
|
for (const segment of route.path.split("/")) {
|
|
38
38
|
if (segment.charCodeAt(0) === 123) {
|
|
@@ -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(
|
|
@@ -319,10 +320,7 @@ async function getSpec(middlewareHandler, defaultOptions) {
|
|
|
319
320
|
const result2 = await raw.schema.toOpenAPISchema();
|
|
320
321
|
raw.schema = result2.schema;
|
|
321
322
|
if (result2.components) {
|
|
322
|
-
components =
|
|
323
|
-
...components,
|
|
324
|
-
...result2.components
|
|
325
|
-
};
|
|
323
|
+
components = mergeComponentsObjects(components, result2.components);
|
|
326
324
|
}
|
|
327
325
|
}
|
|
328
326
|
}
|
|
@@ -381,9 +379,12 @@ function generateParameters(target, schema) {
|
|
|
381
379
|
in: target === "param" ? "path" : target,
|
|
382
380
|
name: key,
|
|
383
381
|
// @ts-expect-error
|
|
384
|
-
schema: value
|
|
385
|
-
required: schema.required?.includes(key)
|
|
382
|
+
schema: value
|
|
386
383
|
};
|
|
384
|
+
const isRequired = schema.required?.includes(key);
|
|
385
|
+
if (isRequired) {
|
|
386
|
+
def.required = true;
|
|
387
|
+
}
|
|
387
388
|
if (def.schema && "description" in def.schema && def.schema.description) {
|
|
388
389
|
def.description = def.schema.description;
|
|
389
390
|
def.schema.description = void 0;
|
|
@@ -395,7 +396,7 @@ function generateParameters(target, schema) {
|
|
|
395
396
|
function mergeComponentsObjects(...components) {
|
|
396
397
|
return components.reduce(
|
|
397
398
|
(prev, component, index) => {
|
|
398
|
-
if (
|
|
399
|
+
if (component == null || index === 0) return prev;
|
|
399
400
|
if (prev.schemas && Object.keys(prev.schemas).length > 0 || component.schemas && Object.keys(component.schemas).length > 0) {
|
|
400
401
|
prev.schemas = {
|
|
401
402
|
...prev.schemas,
|
|
@@ -414,6 +415,14 @@ function mergeComponentsObjects(...components) {
|
|
|
414
415
|
);
|
|
415
416
|
}
|
|
416
417
|
|
|
418
|
+
function loadVendor(vendor, fn) {
|
|
419
|
+
if (fn.toJSONSchema) {
|
|
420
|
+
standardJson.loadVendor(vendor, fn.toJSONSchema);
|
|
421
|
+
}
|
|
422
|
+
if (fn.toOpenAPISchema) {
|
|
423
|
+
standardOpenapi.loadVendor(vendor, fn.toOpenAPISchema);
|
|
424
|
+
}
|
|
425
|
+
}
|
|
417
426
|
function resolver(schema, options) {
|
|
418
427
|
return {
|
|
419
428
|
vendor: schema["~standard"].vendor,
|
|
@@ -480,6 +489,7 @@ exports.ALLOWED_METHODS = ALLOWED_METHODS;
|
|
|
480
489
|
exports.describeResponse = describeResponse;
|
|
481
490
|
exports.describeRoute = describeRoute;
|
|
482
491
|
exports.generateSpecs = generateSpecs;
|
|
492
|
+
exports.loadVendor = loadVendor;
|
|
483
493
|
exports.openAPIRouteHandler = openAPIRouteHandler;
|
|
484
494
|
exports.registerSchemaPath = registerSchemaPath;
|
|
485
495
|
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<
|
|
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<
|
|
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 = [
|
|
@@ -30,7 +30,7 @@ const toOpenAPIPath = (path) => path.split("/").map((x) => {
|
|
|
30
30
|
}).join("/");
|
|
31
31
|
const capitalize = (word) => word.charAt(0).toUpperCase() + word.slice(1);
|
|
32
32
|
const generateOperationId = (route) => {
|
|
33
|
-
let operationId = route.method;
|
|
33
|
+
let operationId = route.method.toLowerCase();
|
|
34
34
|
if (route.path === "/") return `${operationId}Index`;
|
|
35
35
|
for (const segment of route.path.split("/")) {
|
|
36
36
|
if (segment.charCodeAt(0) === 123) {
|
|
@@ -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(
|
|
@@ -317,10 +318,7 @@ async function getSpec(middlewareHandler, defaultOptions) {
|
|
|
317
318
|
const result2 = await raw.schema.toOpenAPISchema();
|
|
318
319
|
raw.schema = result2.schema;
|
|
319
320
|
if (result2.components) {
|
|
320
|
-
components =
|
|
321
|
-
...components,
|
|
322
|
-
...result2.components
|
|
323
|
-
};
|
|
321
|
+
components = mergeComponentsObjects(components, result2.components);
|
|
324
322
|
}
|
|
325
323
|
}
|
|
326
324
|
}
|
|
@@ -379,9 +377,12 @@ function generateParameters(target, schema) {
|
|
|
379
377
|
in: target === "param" ? "path" : target,
|
|
380
378
|
name: key,
|
|
381
379
|
// @ts-expect-error
|
|
382
|
-
schema: value
|
|
383
|
-
required: schema.required?.includes(key)
|
|
380
|
+
schema: value
|
|
384
381
|
};
|
|
382
|
+
const isRequired = schema.required?.includes(key);
|
|
383
|
+
if (isRequired) {
|
|
384
|
+
def.required = true;
|
|
385
|
+
}
|
|
385
386
|
if (def.schema && "description" in def.schema && def.schema.description) {
|
|
386
387
|
def.description = def.schema.description;
|
|
387
388
|
def.schema.description = void 0;
|
|
@@ -393,7 +394,7 @@ function generateParameters(target, schema) {
|
|
|
393
394
|
function mergeComponentsObjects(...components) {
|
|
394
395
|
return components.reduce(
|
|
395
396
|
(prev, component, index) => {
|
|
396
|
-
if (
|
|
397
|
+
if (component == null || index === 0) return prev;
|
|
397
398
|
if (prev.schemas && Object.keys(prev.schemas).length > 0 || component.schemas && Object.keys(component.schemas).length > 0) {
|
|
398
399
|
prev.schemas = {
|
|
399
400
|
...prev.schemas,
|
|
@@ -412,6 +413,14 @@ function mergeComponentsObjects(...components) {
|
|
|
412
413
|
);
|
|
413
414
|
}
|
|
414
415
|
|
|
416
|
+
function loadVendor(vendor, fn) {
|
|
417
|
+
if (fn.toJSONSchema) {
|
|
418
|
+
loadVendor$1(vendor, fn.toJSONSchema);
|
|
419
|
+
}
|
|
420
|
+
if (fn.toOpenAPISchema) {
|
|
421
|
+
loadVendor$2(vendor, fn.toOpenAPISchema);
|
|
422
|
+
}
|
|
423
|
+
}
|
|
415
424
|
function resolver(schema, options) {
|
|
416
425
|
return {
|
|
417
426
|
vendor: schema["~standard"].vendor,
|
|
@@ -474,4 +483,4 @@ function describeResponse(handler, responses, options) {
|
|
|
474
483
|
});
|
|
475
484
|
}
|
|
476
485
|
|
|
477
|
-
export { ALLOWED_METHODS, describeResponse, describeRoute, generateSpecs, openAPIRouteHandler, registerSchemaPath, removeExcludedPaths, resolver, uniqueSymbol, validator };
|
|
486
|
+
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
|
+
"version": "1.0.5",
|
|
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.
|
|
47
|
-
"@standard-community/standard-openapi": "^0.2.
|
|
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"
|