hono-openapi 0.4.7 → 0.5.0-rc.0

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.
@@ -0,0 +1,413 @@
1
+ import * as openapi_types from 'openapi-types';
2
+ import { OpenAPIV3_1 } from 'openapi-types';
3
+ import { MiddlewareHandler, ValidationTargets, Env, Input, Context, Hono } from 'hono';
4
+ import { ValidationTargets as ValidationTargets$1, RouterRoute, BlankEnv, Input as Input$1, BlankInput, Schema, BlankSchema } from 'hono/types';
5
+ import { Hook } from '@hono/standard-validator';
6
+
7
+ // ==================================================================================================
8
+ // JSON Schema Draft 07
9
+ // ==================================================================================================
10
+ // https://tools.ietf.org/html/draft-handrews-json-schema-validation-01
11
+ // --------------------------------------------------------------------------------------------------
12
+
13
+ /**
14
+ * Primitive type
15
+ * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.1.1
16
+ */
17
+ type JSONSchema7TypeName =
18
+ | "string" //
19
+ | "number"
20
+ | "integer"
21
+ | "boolean"
22
+ | "object"
23
+ | "array"
24
+ | "null";
25
+
26
+ /**
27
+ * Primitive type
28
+ * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.1.1
29
+ */
30
+ type JSONSchema7Type =
31
+ | string //
32
+ | number
33
+ | boolean
34
+ | JSONSchema7Object
35
+ | JSONSchema7Array
36
+ | null;
37
+
38
+ // Workaround for infinite type recursion
39
+ interface JSONSchema7Object {
40
+ [key: string]: JSONSchema7Type;
41
+ }
42
+
43
+ // Workaround for infinite type recursion
44
+ // https://github.com/Microsoft/TypeScript/issues/3496#issuecomment-128553540
45
+ interface JSONSchema7Array extends Array<JSONSchema7Type> {}
46
+
47
+ /**
48
+ * Meta schema
49
+ *
50
+ * Recommended values:
51
+ * - 'http://json-schema.org/schema#'
52
+ * - 'http://json-schema.org/hyper-schema#'
53
+ * - 'http://json-schema.org/draft-07/schema#'
54
+ * - 'http://json-schema.org/draft-07/hyper-schema#'
55
+ *
56
+ * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-5
57
+ */
58
+ type JSONSchema7Version = string;
59
+
60
+ /**
61
+ * JSON Schema v7
62
+ * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01
63
+ */
64
+ type JSONSchema7Definition = JSONSchema7 | boolean;
65
+ interface JSONSchema7 {
66
+ $id?: string | undefined;
67
+ $ref?: string | undefined;
68
+ $schema?: JSONSchema7Version | undefined;
69
+ $comment?: string | undefined;
70
+
71
+ /**
72
+ * @see https://datatracker.ietf.org/doc/html/draft-bhutton-json-schema-00#section-8.2.4
73
+ * @see https://datatracker.ietf.org/doc/html/draft-bhutton-json-schema-validation-00#appendix-A
74
+ */
75
+ $defs?: {
76
+ [key: string]: JSONSchema7Definition;
77
+ } | undefined;
78
+
79
+ /**
80
+ * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.1
81
+ */
82
+ type?: JSONSchema7TypeName | JSONSchema7TypeName[] | undefined;
83
+ enum?: JSONSchema7Type[] | undefined;
84
+ const?: JSONSchema7Type | undefined;
85
+
86
+ /**
87
+ * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.2
88
+ */
89
+ multipleOf?: number | undefined;
90
+ maximum?: number | undefined;
91
+ exclusiveMaximum?: number | undefined;
92
+ minimum?: number | undefined;
93
+ exclusiveMinimum?: number | undefined;
94
+
95
+ /**
96
+ * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.3
97
+ */
98
+ maxLength?: number | undefined;
99
+ minLength?: number | undefined;
100
+ pattern?: string | undefined;
101
+
102
+ /**
103
+ * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.4
104
+ */
105
+ items?: JSONSchema7Definition | JSONSchema7Definition[] | undefined;
106
+ additionalItems?: JSONSchema7Definition | undefined;
107
+ maxItems?: number | undefined;
108
+ minItems?: number | undefined;
109
+ uniqueItems?: boolean | undefined;
110
+ contains?: JSONSchema7Definition | undefined;
111
+
112
+ /**
113
+ * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.5
114
+ */
115
+ maxProperties?: number | undefined;
116
+ minProperties?: number | undefined;
117
+ required?: string[] | undefined;
118
+ properties?: {
119
+ [key: string]: JSONSchema7Definition;
120
+ } | undefined;
121
+ patternProperties?: {
122
+ [key: string]: JSONSchema7Definition;
123
+ } | undefined;
124
+ additionalProperties?: JSONSchema7Definition | undefined;
125
+ dependencies?: {
126
+ [key: string]: JSONSchema7Definition | string[];
127
+ } | undefined;
128
+ propertyNames?: JSONSchema7Definition | undefined;
129
+
130
+ /**
131
+ * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.6
132
+ */
133
+ if?: JSONSchema7Definition | undefined;
134
+ then?: JSONSchema7Definition | undefined;
135
+ else?: JSONSchema7Definition | undefined;
136
+
137
+ /**
138
+ * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.7
139
+ */
140
+ allOf?: JSONSchema7Definition[] | undefined;
141
+ anyOf?: JSONSchema7Definition[] | undefined;
142
+ oneOf?: JSONSchema7Definition[] | undefined;
143
+ not?: JSONSchema7Definition | undefined;
144
+
145
+ /**
146
+ * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-7
147
+ */
148
+ format?: string | undefined;
149
+
150
+ /**
151
+ * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-8
152
+ */
153
+ contentMediaType?: string | undefined;
154
+ contentEncoding?: string | undefined;
155
+
156
+ /**
157
+ * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-9
158
+ */
159
+ definitions?: {
160
+ [key: string]: JSONSchema7Definition;
161
+ } | undefined;
162
+
163
+ /**
164
+ * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-10
165
+ */
166
+ title?: string | undefined;
167
+ description?: string | undefined;
168
+ default?: JSONSchema7Type | undefined;
169
+ readOnly?: boolean | undefined;
170
+ writeOnly?: boolean | undefined;
171
+ examples?: JSONSchema7Type | undefined;
172
+ }
173
+
174
+ /** The Standard Schema interface. */
175
+ interface StandardSchemaV1<Input = unknown, Output = Input> {
176
+ /** The Standard Schema properties. */
177
+ readonly "~standard": StandardSchemaV1.Props<Input, Output>;
178
+ }
179
+ declare namespace StandardSchemaV1 {
180
+ /** The Standard Schema properties interface. */
181
+ export interface Props<Input = unknown, Output = Input> {
182
+ /** The version number of the standard. */
183
+ readonly version: 1;
184
+ /** The vendor name of the schema library. */
185
+ readonly vendor: string;
186
+ /** Validates unknown input values. */
187
+ readonly validate: (value: unknown) => Result<Output> | Promise<Result<Output>>;
188
+ /** Inferred types associated with the schema. */
189
+ readonly types?: Types<Input, Output> | undefined;
190
+ }
191
+ /** The result interface of the validate function. */
192
+ export type Result<Output> = SuccessResult<Output> | FailureResult;
193
+ /** The result interface if validation succeeds. */
194
+ export interface SuccessResult<Output> {
195
+ /** The typed output value. */
196
+ readonly value: Output;
197
+ /** The non-existent issues. */
198
+ readonly issues?: undefined;
199
+ }
200
+ /** The result interface if validation fails. */
201
+ export interface FailureResult {
202
+ /** The issues of failed validation. */
203
+ readonly issues: ReadonlyArray<Issue>;
204
+ }
205
+ /** The issue interface of the failure output. */
206
+ export interface Issue {
207
+ /** The error message of the issue. */
208
+ readonly message: string;
209
+ /** The path of the issue, if any. */
210
+ readonly path?: ReadonlyArray<PropertyKey | PathSegment> | undefined;
211
+ }
212
+ /** The path segment interface of the issue. */
213
+ export interface PathSegment {
214
+ /** The key representing a path segment. */
215
+ readonly key: PropertyKey;
216
+ }
217
+ /** The Standard Schema types interface. */
218
+ export interface Types<Input = unknown, Output = Input> {
219
+ /** The input type of the schema. */
220
+ readonly input: Input;
221
+ /** The output type of the schema. */
222
+ readonly output: Output;
223
+ }
224
+ /** Infers the input type of a Standard Schema. */
225
+ export type InferInput<Schema extends StandardSchemaV1> = NonNullable<Schema["~standard"]["types"]>["input"];
226
+ /** Infers the output type of a Standard Schema. */
227
+ export type InferOutput<Schema extends StandardSchemaV1> = NonNullable<Schema["~standard"]["types"]>["output"];
228
+ export { };
229
+ }
230
+
231
+ /**
232
+ * Generate a resolver for a validation schema
233
+ * @param schema Validation schema
234
+ * @returns Resolver result
235
+ */
236
+ declare function resolver<Schema extends StandardSchemaV1>(schema: Schema): {
237
+ vendor: string;
238
+ validate: (value: unknown) => StandardSchemaV1.Result<unknown> | Promise<StandardSchemaV1.Result<unknown>>;
239
+ toJSONSchema: (options?: Record<string, unknown>) => Promise<JSONSchema7>;
240
+ toOpenAPISchema: (options?: Record<string, unknown>) => Promise<{
241
+ schema: OpenAPIV3_1.SchemaObject;
242
+ components: OpenAPIV3_1.ComponentsObject | undefined;
243
+ }>;
244
+ };
245
+ type HasUndefined<T> = undefined extends T ? true : false;
246
+ /**
247
+ * Create a validator middleware
248
+ * @param target Target for validation
249
+ * @param schema Validation schema
250
+ * @param hook Hook for validation
251
+ * @returns Middleware handler
252
+ */
253
+ declare function validator<Schema extends StandardSchemaV1, Target extends keyof ValidationTargets, E extends Env, P extends string, In = StandardSchemaV1.InferInput<Schema>, Out = StandardSchemaV1.InferOutput<Schema>, I extends Input = {
254
+ in: HasUndefined<In> extends true ? {
255
+ [K in Target]?: In extends ValidationTargets[K] ? In : {
256
+ [K2 in keyof In]?: ValidationTargets[K][K2];
257
+ };
258
+ } : {
259
+ [K in Target]: In extends ValidationTargets[K] ? In : {
260
+ [K2 in keyof In]: ValidationTargets[K][K2];
261
+ };
262
+ };
263
+ out: {
264
+ [K in Target]: Out;
265
+ };
266
+ }, V extends I = I>(target: Target, schema: Schema, hook?: Hook<StandardSchemaV1.InferOutput<Schema>, E, P, Target>, options?: Record<string, unknown>): MiddlewareHandler<E, P, V>;
267
+ /**
268
+ * Describe a route with OpenAPI specs.
269
+ * @param spec Options for describing a route
270
+ * @returns Middleware handler
271
+ */
272
+ declare function describeRoute(spec: DescribeRouteOptions): MiddlewareHandler;
273
+
274
+ /**
275
+ * 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.
276
+ */
277
+ declare const uniqueSymbol: unique symbol;
278
+ declare const ALLOWED_METHODS: readonly ["GET", "PUT", "POST", "DELETE", "OPTIONS", "HEAD", "PATCH", "TRACE"];
279
+ type AllowedMethods = (typeof ALLOWED_METHODS)[number];
280
+ declare function registerSchemaPath({ route, specs, paths, }: RegisterSchemaPathOptions): void;
281
+ declare function removeExcludedPaths(paths: OpenAPIV3_1.PathsObject, ctx: {
282
+ options: SanitizedGenerateSpecOptions;
283
+ }): OpenAPIV3_1.PathsObject<{}, {}>;
284
+
285
+ type PromiseOr<T> = T | Promise<T>;
286
+ type ResolverReturnType = ReturnType<typeof resolver>;
287
+ type HandlerUniqueProperty = (ResolverReturnType & {
288
+ target: keyof ValidationTargets$1;
289
+ options?: Record<string, unknown>;
290
+ }) | {
291
+ spec: DescribeRouteOptions;
292
+ };
293
+ type GenerateSpecOptions = {
294
+ /**
295
+ * Customize OpenAPI config, refers to Swagger 2.0 config
296
+ *
297
+ * @see https://swagger.io/specification/v2/
298
+ */
299
+ documentation: Omit<Partial<OpenAPIV3_1.Document>, "x-express-openapi-additional-middleware" | "x-express-openapi-validation-strict">;
300
+ /**
301
+ * Include paths which don't have the handlers.
302
+ * This is useful when you want to document the
303
+ * API without implementing it or index all the paths.
304
+ */
305
+ includeEmptyPaths: boolean;
306
+ /**
307
+ * Determine if Swagger should exclude static files.
308
+ *
309
+ * @default true
310
+ */
311
+ excludeStaticFile: boolean;
312
+ /**
313
+ * Paths to exclude from OpenAPI endpoint
314
+ *
315
+ * @default []
316
+ */
317
+ exclude: string | RegExp | Array<string | RegExp>;
318
+ /**
319
+ * Exclude methods from the specs
320
+ */
321
+ excludeMethods: AllowedMethods[];
322
+ /**
323
+ * Exclude tags from OpenAPI
324
+ */
325
+ excludeTags: string[];
326
+ /**
327
+ * Default options for `describeRoute` method
328
+ */
329
+ defaultOptions: Partial<Record<AllowedMethods | "ALL", DescribeRouteOptions>>;
330
+ };
331
+ type HaveDefaultValues = "documentation" | "excludeStaticFile" | "exclude" | "excludeMethods" | "excludeTags";
332
+ type SanitizedGenerateSpecOptions = Pick<GenerateSpecOptions, HaveDefaultValues> & Omit<Partial<GenerateSpecOptions>, HaveDefaultValues>;
333
+ type DescribeRouteOptions = Omit<OpenAPIV3_1.OperationObject, "responses" | "parameters"> & {
334
+ /**
335
+ * Pass `true` to hide route from OpenAPI/swagger document
336
+ */
337
+ hide?: boolean | ((c: Context) => boolean);
338
+ /**
339
+ * Responses of the request
340
+ */
341
+ responses?: {
342
+ [key: string]: (OpenAPIV3_1.ResponseObject & {
343
+ content?: {
344
+ [key: string]: Omit<OpenAPIV3_1.MediaTypeObject, "schema"> & {
345
+ schema?: OpenAPIV3_1.ReferenceObject | OpenAPIV3_1.SchemaObject | ResolverReturnType;
346
+ };
347
+ };
348
+ }) | OpenAPIV3_1.ReferenceObject;
349
+ };
350
+ };
351
+ type RegisterSchemaPathOptions = {
352
+ route: RouterRoute;
353
+ specs?: DescribeRouteOptions | Pick<OpenAPIV3_1.OperationObject, "parameters" | "requestBody">;
354
+ paths: Partial<OpenAPIV3_1.PathsObject>;
355
+ };
356
+
357
+ /**
358
+ * Generate OpenAPI specs for the given Hono instance
359
+ * @param hono Instance of Hono
360
+ * @param options Options for generating OpenAPI specs
361
+ * @param config Configuration for OpenAPI route handler
362
+ * @param Context Route context for hiding routes
363
+ * @returns OpenAPI specs
364
+ */
365
+ declare function generateSpecs<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>, c?: Context<E, P, I>): Promise<{
366
+ tags: openapi_types.OpenAPIV3.TagObject[];
367
+ info: {
368
+ description: string;
369
+ title: string;
370
+ termsOfService?: string;
371
+ contact?: openapi_types.OpenAPIV3.ContactObject;
372
+ version: string;
373
+ summary?: string;
374
+ license?: OpenAPIV3_1.LicenseObject;
375
+ };
376
+ paths: {
377
+ [x: string]: Omit<openapi_types.OpenAPIV3.PathItemObject<{}>, "parameters" | "servers"> & {
378
+ servers?: OpenAPIV3_1.ServerObject[];
379
+ parameters?: (OpenAPIV3_1.ReferenceObject | OpenAPIV3_1.ParameterObject)[];
380
+ } & {
381
+ get?: OpenAPIV3_1.OperationObject<{}>;
382
+ put?: OpenAPIV3_1.OperationObject<{}>;
383
+ post?: OpenAPIV3_1.OperationObject<{}>;
384
+ delete?: OpenAPIV3_1.OperationObject<{}>;
385
+ options?: OpenAPIV3_1.OperationObject<{}>;
386
+ head?: OpenAPIV3_1.OperationObject<{}>;
387
+ patch?: OpenAPIV3_1.OperationObject<{}>;
388
+ trace?: OpenAPIV3_1.OperationObject<{}>;
389
+ };
390
+ };
391
+ components: {
392
+ schemas: {
393
+ [x: string]: OpenAPIV3_1.SchemaObject;
394
+ };
395
+ responses?: Record<string, OpenAPIV3_1.ReferenceObject | OpenAPIV3_1.ResponseObject>;
396
+ parameters?: Record<string, OpenAPIV3_1.ReferenceObject | OpenAPIV3_1.ParameterObject>;
397
+ examples?: Record<string, OpenAPIV3_1.ReferenceObject | OpenAPIV3_1.ExampleObject>;
398
+ requestBodies?: Record<string, OpenAPIV3_1.ReferenceObject | OpenAPIV3_1.RequestBodyObject>;
399
+ headers?: Record<string, OpenAPIV3_1.ReferenceObject | OpenAPIV3_1.HeaderObject>;
400
+ securitySchemes?: Record<string, OpenAPIV3_1.ReferenceObject | OpenAPIV3_1.SecuritySchemeObject>;
401
+ links?: Record<string, OpenAPIV3_1.ReferenceObject | OpenAPIV3_1.LinkObject>;
402
+ callbacks?: Record<string, OpenAPIV3_1.ReferenceObject | OpenAPIV3_1.CallbackObject>;
403
+ pathItems?: Record<string, OpenAPIV3_1.ReferenceObject | OpenAPIV3_1.PathItemObject>;
404
+ };
405
+ openapi: string;
406
+ externalDocs?: openapi_types.OpenAPIV3.ExternalDocumentationObject;
407
+ security?: openapi_types.OpenAPIV3.SecurityRequirementObject[];
408
+ servers?: OpenAPIV3_1.ServerObject[];
409
+ webhooks?: Record<string, OpenAPIV3_1.PathItemObject | OpenAPIV3_1.ReferenceObject>;
410
+ jsonSchemaDialect?: string;
411
+ }>;
412
+
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 };