hono-openapi 0.5.0-rc.0 → 0.5.0-rc.2
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 +42 -0
- package/dist/index.d.cts +26 -3
- package/dist/index.d.ts +26 -3
- package/dist/index.js +41 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -184,6 +184,14 @@ const DEFAULT_OPTIONS = {
|
|
|
184
184
|
excludeMethods: ["OPTIONS"],
|
|
185
185
|
excludeTags: []
|
|
186
186
|
};
|
|
187
|
+
function openAPIRouteHandler(hono, options) {
|
|
188
|
+
let specs;
|
|
189
|
+
return async (c) => {
|
|
190
|
+
if (specs) return c.json(specs);
|
|
191
|
+
specs = await generateSpecs(hono, options, c);
|
|
192
|
+
return c.json(specs);
|
|
193
|
+
};
|
|
194
|
+
}
|
|
187
195
|
async function generateSpecs(hono, options = DEFAULT_OPTIONS, c) {
|
|
188
196
|
const ctx = {
|
|
189
197
|
components: {},
|
|
@@ -399,10 +407,44 @@ function describeRoute(spec) {
|
|
|
399
407
|
}
|
|
400
408
|
});
|
|
401
409
|
}
|
|
410
|
+
function describeResponse(handler, responses) {
|
|
411
|
+
const _responses = Object.entries(responses).reduce(
|
|
412
|
+
(acc, [statusCode, response]) => {
|
|
413
|
+
if (response.content) {
|
|
414
|
+
const content = Object.entries(response.content).reduce(
|
|
415
|
+
(contentAcc, [mediaType, media]) => {
|
|
416
|
+
if (media.vSchema) {
|
|
417
|
+
contentAcc[mediaType] = {
|
|
418
|
+
...media,
|
|
419
|
+
schema: resolver(media.vSchema)
|
|
420
|
+
};
|
|
421
|
+
} else {
|
|
422
|
+
contentAcc[mediaType] = media;
|
|
423
|
+
}
|
|
424
|
+
return contentAcc;
|
|
425
|
+
},
|
|
426
|
+
{}
|
|
427
|
+
);
|
|
428
|
+
acc[statusCode] = { ...response, content };
|
|
429
|
+
} else {
|
|
430
|
+
acc[statusCode] = response;
|
|
431
|
+
}
|
|
432
|
+
return acc;
|
|
433
|
+
},
|
|
434
|
+
{}
|
|
435
|
+
);
|
|
436
|
+
return Object.assign(handler, {
|
|
437
|
+
[uniqueSymbol]: {
|
|
438
|
+
spec: { responses: _responses }
|
|
439
|
+
}
|
|
440
|
+
});
|
|
441
|
+
}
|
|
402
442
|
|
|
403
443
|
exports.ALLOWED_METHODS = ALLOWED_METHODS;
|
|
444
|
+
exports.describeResponse = describeResponse;
|
|
404
445
|
exports.describeRoute = describeRoute;
|
|
405
446
|
exports.generateSpecs = generateSpecs;
|
|
447
|
+
exports.openAPIRouteHandler = openAPIRouteHandler;
|
|
406
448
|
exports.registerSchemaPath = registerSchemaPath;
|
|
407
449
|
exports.removeExcludedPaths = removeExcludedPaths;
|
|
408
450
|
exports.resolver = resolver;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import * as openapi_types from 'openapi-types';
|
|
2
2
|
import { OpenAPIV3_1 } from 'openapi-types';
|
|
3
|
-
import {
|
|
4
|
-
import { ValidationTargets as ValidationTargets$1, RouterRoute, BlankEnv, Input as Input$1,
|
|
3
|
+
import { Env, Input, Context, Next, MiddlewareHandler, ValidationTargets, Hono } from 'hono';
|
|
4
|
+
import { BlankInput, TypedResponse, ValidationTargets as ValidationTargets$1, RouterRoute, BlankEnv, Input as Input$1, Schema, BlankSchema, MiddlewareHandler as MiddlewareHandler$1 } from 'hono/types';
|
|
5
5
|
import { Hook } from '@hono/standard-validator';
|
|
6
|
+
import { StatusCode } from 'hono/utils/http-status';
|
|
6
7
|
|
|
7
8
|
// ==================================================================================================
|
|
8
9
|
// JSON Schema Draft 07
|
|
@@ -270,6 +271,21 @@ declare function validator<Schema extends StandardSchemaV1, Target extends keyof
|
|
|
270
271
|
* @returns Middleware handler
|
|
271
272
|
*/
|
|
272
273
|
declare function describeRoute(spec: DescribeRouteOptions): MiddlewareHandler;
|
|
274
|
+
type ResponseObject<T extends Partial<Record<StatusCode, StandardSchemaV1>>> = {
|
|
275
|
+
[K in keyof T]: OpenAPIV3_1.ReferenceObject | (OpenAPIV3_1.ResponseObject & {
|
|
276
|
+
content?: {
|
|
277
|
+
[media: string]: OpenAPIV3_1.MediaTypeObject & {
|
|
278
|
+
vSchema?: T[K];
|
|
279
|
+
};
|
|
280
|
+
};
|
|
281
|
+
});
|
|
282
|
+
};
|
|
283
|
+
type Num<T> = T extends `${infer N extends number}` ? N : T;
|
|
284
|
+
type HandlerResponse<T extends Partial<Record<StatusCode, StandardSchemaV1>> = Partial<Record<StatusCode, StandardSchemaV1>>> = {
|
|
285
|
+
[K in keyof T]: T[K] extends StandardSchemaV1 ? PromiseOr<TypedResponse<StandardSchemaV1.InferOutput<T[K]>, Num<K> extends StatusCode ? Num<K> : never>> : never;
|
|
286
|
+
}[keyof T];
|
|
287
|
+
type Handler<E extends Env = any, P extends string = any, I extends Input = BlankInput, T extends Partial<Record<StatusCode, StandardSchemaV1>> = Partial<Record<StatusCode, StandardSchemaV1>>> = (c: Context<E, P, I>, next: Next) => HandlerResponse<T>;
|
|
288
|
+
declare function describeResponse<E extends Env = any, P extends string = any, I extends Input = BlankInput, T extends Partial<Record<StatusCode, StandardSchemaV1>> = Partial<Record<StatusCode, StandardSchemaV1>>>(handler: Handler<E, P, I, T>, responses: ResponseObject<T>): Handler<E, P, I, T>;
|
|
273
289
|
|
|
274
290
|
/**
|
|
275
291
|
* The unique symbol for the middlewares, which makes it easier to identify them. Not meant to be used directly, unless you're creating a custom middleware.
|
|
@@ -354,6 +370,13 @@ type RegisterSchemaPathOptions = {
|
|
|
354
370
|
paths: Partial<OpenAPIV3_1.PathsObject>;
|
|
355
371
|
};
|
|
356
372
|
|
|
373
|
+
/**
|
|
374
|
+
* Route handler for OpenAPI specs
|
|
375
|
+
* @param hono Instance of Hono
|
|
376
|
+
* @param options Options for generating OpenAPI specs
|
|
377
|
+
* @returns Middleware handler for OpenAPI specs
|
|
378
|
+
*/
|
|
379
|
+
declare function openAPIRouteHandler<E extends Env = BlankEnv, P extends string = string, I extends Input$1 = BlankInput, S extends Schema = BlankSchema>(hono: Hono<E, S, P>, options?: Partial<GenerateSpecOptions>): MiddlewareHandler$1<E, P, I>;
|
|
357
380
|
/**
|
|
358
381
|
* Generate OpenAPI specs for the given Hono instance
|
|
359
382
|
* @param hono Instance of Hono
|
|
@@ -410,4 +433,4 @@ declare function generateSpecs<E extends Env = BlankEnv, P extends string = stri
|
|
|
410
433
|
jsonSchemaDialect?: string;
|
|
411
434
|
}>;
|
|
412
435
|
|
|
413
|
-
export { ALLOWED_METHODS, type AllowedMethods, type DescribeRouteOptions, type GenerateSpecOptions, type HandlerUniqueProperty, type PromiseOr, type RegisterSchemaPathOptions, type ResolverReturnType, type SanitizedGenerateSpecOptions, describeRoute, generateSpecs, registerSchemaPath, removeExcludedPaths, resolver, uniqueSymbol, validator };
|
|
436
|
+
export { ALLOWED_METHODS, type AllowedMethods, type DescribeRouteOptions, type GenerateSpecOptions, type HandlerUniqueProperty, type PromiseOr, type RegisterSchemaPathOptions, type ResolverReturnType, type SanitizedGenerateSpecOptions, describeResponse, describeRoute, generateSpecs, openAPIRouteHandler, registerSchemaPath, removeExcludedPaths, resolver, uniqueSymbol, validator };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import * as openapi_types from 'openapi-types';
|
|
2
2
|
import { OpenAPIV3_1 } from 'openapi-types';
|
|
3
|
-
import {
|
|
4
|
-
import { ValidationTargets as ValidationTargets$1, RouterRoute, BlankEnv, Input as Input$1,
|
|
3
|
+
import { Env, Input, Context, Next, MiddlewareHandler, ValidationTargets, Hono } from 'hono';
|
|
4
|
+
import { BlankInput, TypedResponse, ValidationTargets as ValidationTargets$1, RouterRoute, BlankEnv, Input as Input$1, Schema, BlankSchema, MiddlewareHandler as MiddlewareHandler$1 } from 'hono/types';
|
|
5
5
|
import { Hook } from '@hono/standard-validator';
|
|
6
|
+
import { StatusCode } from 'hono/utils/http-status';
|
|
6
7
|
|
|
7
8
|
// ==================================================================================================
|
|
8
9
|
// JSON Schema Draft 07
|
|
@@ -270,6 +271,21 @@ declare function validator<Schema extends StandardSchemaV1, Target extends keyof
|
|
|
270
271
|
* @returns Middleware handler
|
|
271
272
|
*/
|
|
272
273
|
declare function describeRoute(spec: DescribeRouteOptions): MiddlewareHandler;
|
|
274
|
+
type ResponseObject<T extends Partial<Record<StatusCode, StandardSchemaV1>>> = {
|
|
275
|
+
[K in keyof T]: OpenAPIV3_1.ReferenceObject | (OpenAPIV3_1.ResponseObject & {
|
|
276
|
+
content?: {
|
|
277
|
+
[media: string]: OpenAPIV3_1.MediaTypeObject & {
|
|
278
|
+
vSchema?: T[K];
|
|
279
|
+
};
|
|
280
|
+
};
|
|
281
|
+
});
|
|
282
|
+
};
|
|
283
|
+
type Num<T> = T extends `${infer N extends number}` ? N : T;
|
|
284
|
+
type HandlerResponse<T extends Partial<Record<StatusCode, StandardSchemaV1>> = Partial<Record<StatusCode, StandardSchemaV1>>> = {
|
|
285
|
+
[K in keyof T]: T[K] extends StandardSchemaV1 ? PromiseOr<TypedResponse<StandardSchemaV1.InferOutput<T[K]>, Num<K> extends StatusCode ? Num<K> : never>> : never;
|
|
286
|
+
}[keyof T];
|
|
287
|
+
type Handler<E extends Env = any, P extends string = any, I extends Input = BlankInput, T extends Partial<Record<StatusCode, StandardSchemaV1>> = Partial<Record<StatusCode, StandardSchemaV1>>> = (c: Context<E, P, I>, next: Next) => HandlerResponse<T>;
|
|
288
|
+
declare function describeResponse<E extends Env = any, P extends string = any, I extends Input = BlankInput, T extends Partial<Record<StatusCode, StandardSchemaV1>> = Partial<Record<StatusCode, StandardSchemaV1>>>(handler: Handler<E, P, I, T>, responses: ResponseObject<T>): Handler<E, P, I, T>;
|
|
273
289
|
|
|
274
290
|
/**
|
|
275
291
|
* The unique symbol for the middlewares, which makes it easier to identify them. Not meant to be used directly, unless you're creating a custom middleware.
|
|
@@ -354,6 +370,13 @@ type RegisterSchemaPathOptions = {
|
|
|
354
370
|
paths: Partial<OpenAPIV3_1.PathsObject>;
|
|
355
371
|
};
|
|
356
372
|
|
|
373
|
+
/**
|
|
374
|
+
* Route handler for OpenAPI specs
|
|
375
|
+
* @param hono Instance of Hono
|
|
376
|
+
* @param options Options for generating OpenAPI specs
|
|
377
|
+
* @returns Middleware handler for OpenAPI specs
|
|
378
|
+
*/
|
|
379
|
+
declare function openAPIRouteHandler<E extends Env = BlankEnv, P extends string = string, I extends Input$1 = BlankInput, S extends Schema = BlankSchema>(hono: Hono<E, S, P>, options?: Partial<GenerateSpecOptions>): MiddlewareHandler$1<E, P, I>;
|
|
357
380
|
/**
|
|
358
381
|
* Generate OpenAPI specs for the given Hono instance
|
|
359
382
|
* @param hono Instance of Hono
|
|
@@ -410,4 +433,4 @@ declare function generateSpecs<E extends Env = BlankEnv, P extends string = stri
|
|
|
410
433
|
jsonSchemaDialect?: string;
|
|
411
434
|
}>;
|
|
412
435
|
|
|
413
|
-
export { ALLOWED_METHODS, type AllowedMethods, type DescribeRouteOptions, type GenerateSpecOptions, type HandlerUniqueProperty, type PromiseOr, type RegisterSchemaPathOptions, type ResolverReturnType, type SanitizedGenerateSpecOptions, describeRoute, generateSpecs, registerSchemaPath, removeExcludedPaths, resolver, uniqueSymbol, validator };
|
|
436
|
+
export { ALLOWED_METHODS, type AllowedMethods, type DescribeRouteOptions, type GenerateSpecOptions, type HandlerUniqueProperty, type PromiseOr, type RegisterSchemaPathOptions, type ResolverReturnType, type SanitizedGenerateSpecOptions, describeResponse, describeRoute, generateSpecs, openAPIRouteHandler, registerSchemaPath, removeExcludedPaths, resolver, uniqueSymbol, validator };
|
package/dist/index.js
CHANGED
|
@@ -182,6 +182,14 @@ const DEFAULT_OPTIONS = {
|
|
|
182
182
|
excludeMethods: ["OPTIONS"],
|
|
183
183
|
excludeTags: []
|
|
184
184
|
};
|
|
185
|
+
function openAPIRouteHandler(hono, options) {
|
|
186
|
+
let specs;
|
|
187
|
+
return async (c) => {
|
|
188
|
+
if (specs) return c.json(specs);
|
|
189
|
+
specs = await generateSpecs(hono, options, c);
|
|
190
|
+
return c.json(specs);
|
|
191
|
+
};
|
|
192
|
+
}
|
|
185
193
|
async function generateSpecs(hono, options = DEFAULT_OPTIONS, c) {
|
|
186
194
|
const ctx = {
|
|
187
195
|
components: {},
|
|
@@ -397,5 +405,37 @@ function describeRoute(spec) {
|
|
|
397
405
|
}
|
|
398
406
|
});
|
|
399
407
|
}
|
|
408
|
+
function describeResponse(handler, responses) {
|
|
409
|
+
const _responses = Object.entries(responses).reduce(
|
|
410
|
+
(acc, [statusCode, response]) => {
|
|
411
|
+
if (response.content) {
|
|
412
|
+
const content = Object.entries(response.content).reduce(
|
|
413
|
+
(contentAcc, [mediaType, media]) => {
|
|
414
|
+
if (media.vSchema) {
|
|
415
|
+
contentAcc[mediaType] = {
|
|
416
|
+
...media,
|
|
417
|
+
schema: resolver(media.vSchema)
|
|
418
|
+
};
|
|
419
|
+
} else {
|
|
420
|
+
contentAcc[mediaType] = media;
|
|
421
|
+
}
|
|
422
|
+
return contentAcc;
|
|
423
|
+
},
|
|
424
|
+
{}
|
|
425
|
+
);
|
|
426
|
+
acc[statusCode] = { ...response, content };
|
|
427
|
+
} else {
|
|
428
|
+
acc[statusCode] = response;
|
|
429
|
+
}
|
|
430
|
+
return acc;
|
|
431
|
+
},
|
|
432
|
+
{}
|
|
433
|
+
);
|
|
434
|
+
return Object.assign(handler, {
|
|
435
|
+
[uniqueSymbol]: {
|
|
436
|
+
spec: { responses: _responses }
|
|
437
|
+
}
|
|
438
|
+
});
|
|
439
|
+
}
|
|
400
440
|
|
|
401
|
-
export { ALLOWED_METHODS, describeRoute, generateSpecs, registerSchemaPath, removeExcludedPaths, resolver, uniqueSymbol, validator };
|
|
441
|
+
export { ALLOWED_METHODS, describeResponse, describeRoute, generateSpecs, openAPIRouteHandler, registerSchemaPath, removeExcludedPaths, resolver, uniqueSymbol, validator };
|