hono-openapi 0.5.0-rc.1 → 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 +33 -0
- package/dist/index.d.cts +20 -4
- package/dist/index.d.ts +20 -4
- package/dist/index.js +33 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -407,8 +407,41 @@ function describeRoute(spec) {
|
|
|
407
407
|
}
|
|
408
408
|
});
|
|
409
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
|
+
}
|
|
410
442
|
|
|
411
443
|
exports.ALLOWED_METHODS = ALLOWED_METHODS;
|
|
444
|
+
exports.describeResponse = describeResponse;
|
|
412
445
|
exports.describeRoute = describeRoute;
|
|
413
446
|
exports.generateSpecs = generateSpecs;
|
|
414
447
|
exports.openAPIRouteHandler = openAPIRouteHandler;
|
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.
|
|
@@ -360,7 +376,7 @@ type RegisterSchemaPathOptions = {
|
|
|
360
376
|
* @param options Options for generating OpenAPI specs
|
|
361
377
|
* @returns Middleware handler for OpenAPI specs
|
|
362
378
|
*/
|
|
363
|
-
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?: GenerateSpecOptions): MiddlewareHandler$1<E, P, I>;
|
|
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>;
|
|
364
380
|
/**
|
|
365
381
|
* Generate OpenAPI specs for the given Hono instance
|
|
366
382
|
* @param hono Instance of Hono
|
|
@@ -417,4 +433,4 @@ declare function generateSpecs<E extends Env = BlankEnv, P extends string = stri
|
|
|
417
433
|
jsonSchemaDialect?: string;
|
|
418
434
|
}>;
|
|
419
435
|
|
|
420
|
-
export { ALLOWED_METHODS, type AllowedMethods, type DescribeRouteOptions, type GenerateSpecOptions, type HandlerUniqueProperty, type PromiseOr, type RegisterSchemaPathOptions, type ResolverReturnType, type SanitizedGenerateSpecOptions, describeRoute, generateSpecs, openAPIRouteHandler, 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.
|
|
@@ -360,7 +376,7 @@ type RegisterSchemaPathOptions = {
|
|
|
360
376
|
* @param options Options for generating OpenAPI specs
|
|
361
377
|
* @returns Middleware handler for OpenAPI specs
|
|
362
378
|
*/
|
|
363
|
-
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?: GenerateSpecOptions): MiddlewareHandler$1<E, P, I>;
|
|
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>;
|
|
364
380
|
/**
|
|
365
381
|
* Generate OpenAPI specs for the given Hono instance
|
|
366
382
|
* @param hono Instance of Hono
|
|
@@ -417,4 +433,4 @@ declare function generateSpecs<E extends Env = BlankEnv, P extends string = stri
|
|
|
417
433
|
jsonSchemaDialect?: string;
|
|
418
434
|
}>;
|
|
419
435
|
|
|
420
|
-
export { ALLOWED_METHODS, type AllowedMethods, type DescribeRouteOptions, type GenerateSpecOptions, type HandlerUniqueProperty, type PromiseOr, type RegisterSchemaPathOptions, type ResolverReturnType, type SanitizedGenerateSpecOptions, describeRoute, generateSpecs, openAPIRouteHandler, 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
|
@@ -405,5 +405,37 @@ function describeRoute(spec) {
|
|
|
405
405
|
}
|
|
406
406
|
});
|
|
407
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
|
+
}
|
|
408
440
|
|
|
409
|
-
export { ALLOWED_METHODS, describeRoute, generateSpecs, openAPIRouteHandler, registerSchemaPath, removeExcludedPaths, resolver, uniqueSymbol, validator };
|
|
441
|
+
export { ALLOWED_METHODS, describeResponse, describeRoute, generateSpecs, openAPIRouteHandler, registerSchemaPath, removeExcludedPaths, resolver, uniqueSymbol, validator };
|