@znight/openapi 0.0.0-stage → 0.1.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.
Files changed (54) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +572 -2
  3. package/dist/build-BTUC96mY.d.mts +174 -0
  4. package/dist/build-DXunOzlI.mjs +733 -0
  5. package/dist/build-DXunOzlI.mjs.map +1 -0
  6. package/dist/compiler.d.mts +16 -0
  7. package/dist/compiler.mjs +2639 -0
  8. package/dist/compiler.mjs.map +1 -0
  9. package/dist/emit-child.d.mts +1 -0
  10. package/dist/emit-child.mjs +124 -0
  11. package/dist/emit-child.mjs.map +1 -0
  12. package/dist/emit.d.mts +2 -0
  13. package/dist/emit.mjs +252 -0
  14. package/dist/emit.mjs.map +1 -0
  15. package/dist/index-CqQ6IRnN.d.mts +60 -0
  16. package/dist/index.d.mts +260 -0
  17. package/dist/index.mjs +777 -0
  18. package/dist/index.mjs.map +1 -0
  19. package/dist/openapi-CQk5dHqc.mjs +39 -0
  20. package/dist/openapi-CQk5dHqc.mjs.map +1 -0
  21. package/dist/shared-BZxtyaB0.mjs +147 -0
  22. package/dist/shared-BZxtyaB0.mjs.map +1 -0
  23. package/dist/testing.d.mts +39 -0
  24. package/dist/testing.mjs +155 -0
  25. package/dist/testing.mjs.map +1 -0
  26. package/dist/types-VzBexMKj.d.mts +213 -0
  27. package/dist/vite.d.mts +24 -0
  28. package/dist/vite.mjs +64 -0
  29. package/dist/vite.mjs.map +1 -0
  30. package/package.json +115 -4
  31. package/src/build.ts +898 -0
  32. package/src/compiler/analyze.ts +81 -0
  33. package/src/compiler/document.ts +612 -0
  34. package/src/compiler/generate.ts +79 -0
  35. package/src/compiler/index.ts +47 -0
  36. package/src/compiler/model.ts +141 -0
  37. package/src/compiler/operations.ts +1475 -0
  38. package/src/compiler/schemas.ts +706 -0
  39. package/src/compiler/validate.ts +102 -0
  40. package/src/compiler/values.ts +81 -0
  41. package/src/converters.ts +137 -0
  42. package/src/decorators.ts +187 -0
  43. package/src/emit/child.ts +144 -0
  44. package/src/emit/index.ts +377 -0
  45. package/src/emit/protocol.ts +35 -0
  46. package/src/index.ts +94 -0
  47. package/src/metadata.ts +112 -0
  48. package/src/openapi.ts +55 -0
  49. package/src/plugin.ts +810 -0
  50. package/src/shared.ts +154 -0
  51. package/src/testing.ts +224 -0
  52. package/src/types.ts +240 -0
  53. package/src/ui.ts +215 -0
  54. package/src/vite.ts +126 -0
@@ -0,0 +1,174 @@
1
+ import { A as SecuritySchemeObject, P as TagObject, _ as OpenApiObject, a as ExternalDocumentationObject, c as InfoObject, g as OpenApiMethod, j as ServerObject, k as SecurityRequirementObject, l as JsonSchema, u as JsonValue, y as OperationObjectOf } from "./types-VzBexMKj.mjs";
2
+ import { ErrorCode, Token, ZnightMetadata } from "@znight/core";
3
+ import { StandardSchemaV1 } from "@znight/http";
4
+ /** What a converter receives next to the schema. */
5
+ interface SchemaConverterContext {
6
+ /** `input` for requests (what clients send), `output` for responses (what the server sends). */
7
+ readonly io: "input" | "output";
8
+ readonly target: "draft-2020-12";
9
+ /** Retry after a failure with `unrepresentable: 'warn'`: approximate instead of throwing. */
10
+ readonly lenient: boolean;
11
+ }
12
+ /** Converts Standard Schemas of one library to JSON Schema 2020-12. */
13
+ interface SchemaConverter {
14
+ readonly name: string;
15
+ /** `undefined`: not supported, the next converter is tried. Throwing reports ZNIGHT037. */
16
+ convert(schema: StandardSchemaV1, context: SchemaConverterContext): JsonSchema | undefined | Promise<JsonSchema | undefined>;
17
+ }
18
+ /**
19
+ * Standard JSON Schema (`schema['~standard'].jsonSchema[io]({ target: 'draft-2020-12' })`): Zod ≥ 4.2,
20
+ * ArkType ≥ 2.1.28 and every library implementing the interface.
21
+ */
22
+ declare const standardJsonSchemaConverter: SchemaConverter;
23
+ /** Valibot schemas (no `~standard.jsonSchema`) through `toStandardJsonSchema()` of `@valibot/to-json-schema`. */
24
+ declare const valibotConverter: SchemaConverter;
25
+ /** Built-in converters, tried after the user's. */
26
+ declare const BUILT_IN_CONVERTERS: readonly SchemaConverter[];
27
+ /** Format version of {@link OpenApiMetadata}. */
28
+ declare const OPENAPI_METADATA_VERSION = 1;
29
+ /** A Standard Schema converted at assembly: `{ $ref }` to its component once replaced. */
30
+ interface SchemaPlaceholder {
31
+ readonly $znight: "schema";
32
+ /** Slot name ({@link OpenApiSchemaSlot.name}). */
33
+ readonly name: string;
34
+ readonly io: "input" | "output";
35
+ }
36
+ /**
37
+ * In `parameters` arrays: one parameter per property of the (input) object schema of a slot
38
+ * (`@Query(SearchQuery)`), `required` from its `required` list.
39
+ */
40
+ interface ParametersPlaceholder {
41
+ readonly $znight: "parameters";
42
+ readonly in: "path" | "query" | "header" | "cookie";
43
+ /** Slot name. */
44
+ readonly name: string;
45
+ readonly io: "input";
46
+ }
47
+ /** JSON Schema, or a placeholder of a Standard Schema. */
48
+ type SchemaTemplate = JsonSchema | SchemaPlaceholder;
49
+ /** An Operation Object whose schemas are {@link SchemaTemplate}s and whose parameters may be {@link ParametersPlaceholder}s. */
50
+ type OperationTemplate = OperationObjectOf<SchemaTemplate, ParametersPlaceholder>;
51
+ /** Root fields of the document known at build time. */
52
+ interface OpenApiDocumentTemplate {
53
+ readonly openapi: string;
54
+ readonly info: InfoObject;
55
+ readonly servers?: readonly ServerObject[];
56
+ readonly tags?: readonly TagObject[];
57
+ readonly externalDocs?: ExternalDocumentationObject;
58
+ readonly security?: readonly SecurityRequirementObject[];
59
+ readonly [extension: `x-${string}`]: JsonValue | undefined;
60
+ }
61
+ /** A documented operation. */
62
+ interface OpenApiOperationDefinition {
63
+ /** Controller token. */
64
+ readonly controller: Token;
65
+ readonly handler: string;
66
+ /** The compiled route method (`'ALL'` gives one definition per documented method). */
67
+ readonly route: string;
68
+ readonly method: OpenApiMethod;
69
+ /** OpenAPI path template, without the HTTP prefix (`/books/{id}`). */
70
+ readonly path: string;
71
+ readonly operation: OperationTemplate;
72
+ }
73
+ /** A Standard Schema converted when the document is assembled. */
74
+ interface OpenApiSchemaSlot {
75
+ /** Component name (export name of the binding: `CreateBook`). */
76
+ readonly name: string;
77
+ /** The imported schema value. */
78
+ readonly schema: StandardSchemaV1;
79
+ /** The JSON Schemas needed: `input` for requests, `output` for responses. */
80
+ readonly io: readonly ("input" | "output")[];
81
+ /** Where the schema is declared (relative path), for messages. */
82
+ readonly source?: {
83
+ readonly file: string;
84
+ readonly line: number;
85
+ readonly column: number;
86
+ };
87
+ }
88
+ /** `metadata.extensions.openapi`. */
89
+ interface OpenApiMetadata {
90
+ readonly version: 1;
91
+ /** SHA-256 (base64url) of the section, schema values replaced by their names (stale-file detection). */
92
+ readonly fingerprint: string;
93
+ readonly document: OpenApiDocumentTemplate;
94
+ /** Sorted by path, then by method (get, put, post, delete, options, head, patch). */
95
+ readonly operations: readonly OpenApiOperationDefinition[];
96
+ /** JSON Schemas computed by the compiler from TypeScript declarations, by component name (sorted). */
97
+ readonly components: Readonly<Record<string, SchemaTemplate>>;
98
+ readonly securitySchemes: Readonly<Record<string, SecuritySchemeObject>>;
99
+ /** Standard Schemas converted at assembly, sorted by name. */
100
+ readonly schemas: readonly OpenApiSchemaSlot[];
101
+ /** Standard error components; absent with `errors: false`. */
102
+ readonly errors?: {
103
+ readonly contentType: string;
104
+ readonly schema?: string;
105
+ };
106
+ /** Descriptions of the default tags (JSDoc of the controllers), by tag name; absent when empty. */
107
+ readonly tagDescriptions?: Readonly<Record<string, string>>;
108
+ }
109
+ declare module "@znight/core" {
110
+ interface ZnightExtensionsMetadata {
111
+ readonly openapi?: OpenApiMetadata;
112
+ }
113
+ }
114
+ /** A static view of an operation (no schema conversion), used by `filter` and `OpenApi.operations`. */
115
+ interface OpenApiOperationView {
116
+ readonly method: OpenApiMethod;
117
+ /** Path template without the HTTP prefix (`/books/{id}`). */
118
+ readonly path: string;
119
+ readonly operationId: string;
120
+ readonly tags: readonly string[];
121
+ /** Controller display name. */
122
+ readonly controller: string;
123
+ readonly handler: string;
124
+ /** Effective route metadata (`createRouteDecorator` values, the nearest decision of each group). */
125
+ readonly meta: Readonly<Record<string, unknown>>;
126
+ }
127
+ /** Options of {@link buildOpenApiDocument}. */
128
+ interface BuildOpenApiOptions {
129
+ /** The HTTP prefix: `servers` defaults to `[{ url: prefix }]` when nothing declares servers. */
130
+ readonly prefix?: string;
131
+ /** Merged over the static `info`. */
132
+ readonly info?: Partial<InfoObject>;
133
+ /** Replaces the declared `servers`. */
134
+ readonly servers?: readonly ServerObject[];
135
+ /** Hides operations; unreferenced components are pruned. */
136
+ readonly filter?: (operation: OpenApiOperationView) => boolean;
137
+ /** Last-resort escape hatch, applied before freezing. */
138
+ readonly transform?: (document: OpenApiObject) => OpenApiObject;
139
+ /** Extra converters, tried before the built-in ones. */
140
+ readonly converters?: readonly SchemaConverter[];
141
+ /** `'warn'`: a schema that cannot be converted becomes `{}` with a ZNIGHT037 warning. Default `'error'`. */
142
+ readonly unrepresentable?: "error" | "warn";
143
+ }
144
+ /** A warning of the assembly. */
145
+ interface OpenApiWarning {
146
+ readonly code: ErrorCode;
147
+ readonly message: string;
148
+ readonly hints: readonly string[];
149
+ }
150
+ /** Result of {@link buildOpenApiDocument}. */
151
+ interface OpenApiBuildResult {
152
+ /** The document, deep-frozen. */
153
+ readonly document: OpenApiObject;
154
+ /** `JSON.stringify(document, null, 2)` and a final newline. */
155
+ readonly json: string;
156
+ readonly warnings: readonly OpenApiWarning[];
157
+ readonly stats: {
158
+ readonly operations: number;
159
+ readonly schemas: number;
160
+ readonly durationMs: number;
161
+ };
162
+ }
163
+ /**
164
+ * Assembles the OpenAPI 3.1 document of an application from its generated metadata: converts the
165
+ * Standard Schemas (user converters, Standard JSON Schema, Valibot), hoists their `$defs`, names
166
+ * the components, fills the placeholders of the compiler, applies `filter`, prunes the unused
167
+ * components, applies `transform`, deep-freezes and serialises once.
168
+ *
169
+ * @throws ZnightError ZNIGHT030 (no OpenAPI section), ZNIGHT018 (another format version), ZNIGHT034
170
+ * (component name conflict), ZNIGHT037 (schema conversion, unless `unrepresentable: 'warn'`).
171
+ */
172
+ declare function buildOpenApiDocument(metadata: ZnightMetadata, options?: BuildOpenApiOptions): Promise<OpenApiBuildResult>;
173
+ export { SchemaConverterContext as _, buildOpenApiDocument as a, OpenApiMetadata as c, OperationTemplate as d, ParametersPlaceholder as f, SchemaConverter as g, BUILT_IN_CONVERTERS as h, OpenApiWarning as i, OpenApiOperationDefinition as l, SchemaTemplate as m, OpenApiBuildResult as n, OPENAPI_METADATA_VERSION as o, SchemaPlaceholder as p, OpenApiOperationView as r, OpenApiDocumentTemplate as s, BuildOpenApiOptions as t, OpenApiSchemaSlot as u, standardJsonSchemaConverter as v, valibotConverter as y };
174
+ //# sourceMappingURL=build-BTUC96mY.d.mts.map