@kizunajs/openapi 2.0.0-beta.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,306 @@
1
+ import * as _$kizunajs from "kizunajs";
2
+ import { ApiDefinition, ClientTarget } from "kizunajs";
3
+ import { z } from "zod";
4
+ import { RoutePath, WithSlug } from "kizunajs/plugin";
5
+ import { RouteDefinition } from "kizunajs/generator";
6
+
7
+ //#region src/docs-html.d.ts
8
+ /**
9
+ * The API reference UI rendered by the docs endpoint.
10
+ */
11
+ type DocsProvider = 'scalar' | 'swagger';
12
+ interface DocsHtmlOptions {
13
+ /**
14
+ * URL the UI fetches the OpenAPI document from. Omit it and pass
15
+ * {@link DocsHtmlOptions.specContent} instead.
16
+ */
17
+ specUrl?: string;
18
+ /**
19
+ * The document itself, embedded in the page rather than fetched.
20
+ */
21
+ specContent?: unknown;
22
+ /**
23
+ * Which API reference UI to render.
24
+ *
25
+ * @default 'scalar'
26
+ */
27
+ provider?: DocsProvider;
28
+ /**
29
+ * The page's `<title>`.
30
+ *
31
+ * @default 'API Reference'
32
+ */
33
+ pageTitle?: string;
34
+ /**
35
+ * Where to load the UI's assets from. Point this at a self-hosted copy for
36
+ * air-gapped deployments or a strict CSP.
37
+ *
38
+ * For `'scalar'` this is the script URL. For `'swagger'` it is the
39
+ * directory holding `swagger-ui.css` and `swagger-ui-bundle.js`.
40
+ *
41
+ * @default 'https://cdn.jsdelivr.net/npm/@scalar/api-reference@1' | 'https://cdn.jsdelivr.net/npm/swagger-ui-dist@5'
42
+ */
43
+ cdnUrl?: string;
44
+ /**
45
+ * Merged into `Scalar.createApiReference` or `SwaggerUIBundle`. See the
46
+ * provider's own documentation for the available keys.
47
+ */
48
+ configuration?: Record<string, unknown>;
49
+ }
50
+ /**
51
+ * Render a self-contained page that loads an API reference UI over your OpenAPI
52
+ * document. `openApiPlugin` calls this, so reach for it directly only when
53
+ * kizuna has no adapter for your framework.
54
+ *
55
+ * @example
56
+ * ```ts
57
+ * const html = renderDocsHtml({
58
+ * specUrl: '/openapi.json',
59
+ * pageTitle: 'My API',
60
+ * });
61
+ * ```
62
+ */
63
+ declare const renderDocsHtml: (options: DocsHtmlOptions) => string;
64
+ //#endregion
65
+ //#region src/types.d.ts
66
+ /**
67
+ * The OpenAPI Specification version declared in the document's `openapi` field.
68
+ */
69
+ type OpenApiVersion = '3.1.0';
70
+ interface OpenApiInfo {
71
+ title: string;
72
+ version: string;
73
+ description?: string;
74
+ }
75
+ interface OpenApiServer {
76
+ url: string;
77
+ description?: string;
78
+ }
79
+ interface OpenApiParameter {
80
+ name: string;
81
+ in: 'path' | 'query' | 'header' | 'cookie';
82
+ required?: boolean;
83
+ description?: string;
84
+ schema: Record<string, unknown>;
85
+ }
86
+ interface OpenApiResponseObject {
87
+ description: string;
88
+ headers?: Record<string, {
89
+ description?: string;
90
+ required?: boolean;
91
+ schema: Record<string, unknown>;
92
+ }>;
93
+ content?: Record<string, {
94
+ schema: Record<string, unknown>;
95
+ }>;
96
+ }
97
+ interface OpenApiExternalDocs {
98
+ url: string;
99
+ description?: string;
100
+ }
101
+ interface OpenApiTag {
102
+ name: string;
103
+ description?: string;
104
+ externalDocs?: OpenApiExternalDocs;
105
+ }
106
+ interface OpenApiOperation {
107
+ operationId?: string;
108
+ summary?: string;
109
+ description?: string;
110
+ deprecated?: boolean;
111
+ tags?: string[];
112
+ security?: Array<Record<string, string[]>>;
113
+ /**
114
+ * The `custom` schemes guarding this operation. They emit no `security`
115
+ * requirement, so this marks the operation as protected, not public.
116
+ */
117
+ 'x-kizuna-guarded'?: string[];
118
+ /**
119
+ * The roles the route accepts, from the route's `auth.roles`.
120
+ */
121
+ 'x-kizuna-roles'?: string[];
122
+ /**
123
+ * The permissions the caller has to hold, from the route's `auth.requires`.
124
+ */
125
+ 'x-kizuna-requires'?: Record<string, string[]>;
126
+ externalDocs?: OpenApiExternalDocs;
127
+ parameters?: OpenApiParameter[];
128
+ requestBody?: {
129
+ required?: boolean;
130
+ content: Record<string, {
131
+ schema: Record<string, unknown>;
132
+ }>;
133
+ };
134
+ responses: Record<string, OpenApiResponseObject>;
135
+ }
136
+ interface OpenApiDocument {
137
+ openapi: string;
138
+ info: OpenApiInfo;
139
+ servers?: OpenApiServer[];
140
+ paths: Record<string, Record<string, OpenApiOperation>>;
141
+ tags?: OpenApiTag[];
142
+ externalDocs?: OpenApiExternalDocs;
143
+ components?: {
144
+ securitySchemes?: Record<string, unknown>;
145
+ schemas?: Record<string, unknown>;
146
+ };
147
+ }
148
+ interface GenerateOpenApiOptions {
149
+ /**
150
+ * The OpenAPI Specification version to declare in the document's `openapi` field.
151
+ *
152
+ * Defaults to `'3.1.0'`.
153
+ */
154
+ openApiVersion?: OpenApiVersion;
155
+ info: OpenApiInfo;
156
+ servers?: OpenApiServer[];
157
+ externalDocs?: OpenApiExternalDocs;
158
+ setOperationId?: boolean | 'concatenated-path';
159
+ operationMapper?: (operation: OpenApiOperation, route: RouteDefinition, operationId: string) => OpenApiOperation;
160
+ /**
161
+ * Document the derived `head` operation on every `GET` path without a declared one.
162
+ *
163
+ * @default false
164
+ */
165
+ derivedHead?: boolean;
166
+ }
167
+ interface OpenApiRenderer {
168
+ (format: 'json'): OpenApiDocument;
169
+ (format: 'yaml'): string;
170
+ }
171
+ //#endregion
172
+ //#region src/plugin.d.ts
173
+ declare const OPENAPI_PLUGIN_SLUG = "openApi";
174
+ type JsonDocumentPath = `${RoutePath}.json`;
175
+ type YamlDocumentPath = `${RoutePath}.yaml`;
176
+ interface OpenApiPluginProps<Slug extends string = typeof OPENAPI_PLUGIN_SLUG> extends GenerateOpenApiOptions {
177
+ /**
178
+ * What handlers reach this plugin under. Give a second document its own.
179
+ *
180
+ * @default 'openApi'
181
+ */
182
+ slug?: Slug;
183
+ /**
184
+ * Where the reference UI is served.
185
+ */
186
+ docsPath?: RoutePath;
187
+ /**
188
+ * Where the document is served. The UI embeds it, so this is only for
189
+ * publishing the file.
190
+ */
191
+ jsonPath?: JsonDocumentPath;
192
+ /**
193
+ * Where the document is served as YAML.
194
+ */
195
+ yamlPath?: YamlDocumentPath;
196
+ /**
197
+ * Which API reference UI to render.
198
+ *
199
+ * @default 'scalar'
200
+ */
201
+ provider?: DocsProvider;
202
+ /**
203
+ * Where to load the UI's assets from, for a self-hosted copy. The script
204
+ * URL for `'scalar'`; the directory holding `swagger-ui.css` and
205
+ * `swagger-ui-bundle.js` for `'swagger'`.
206
+ */
207
+ cdnUrl?: string;
208
+ /**
209
+ * The page's `<title>`.
210
+ *
211
+ * @default the document's `info.title`
212
+ */
213
+ pageTitle?: string;
214
+ /**
215
+ * Merged into `Scalar.createApiReference` or `SwaggerUIBundle`.
216
+ */
217
+ configuration?: Record<string, unknown>;
218
+ }
219
+ /**
220
+ * Serve an API reference UI for the OpenAPI document, and the document itself
221
+ * if you publish it.
222
+ *
223
+ * The routes are public. Gate them with your framework's own middleware if that
224
+ * is not what you want.
225
+ *
226
+ * @example
227
+ * ```ts
228
+ * export default defineConfig({
229
+ * routes,
230
+ * plugins: [
231
+ * openApiPlugin({
232
+ * info: {
233
+ * title: 'My API',
234
+ * version: '1.0.0',
235
+ * },
236
+ * }),
237
+ * ],
238
+ * });
239
+ * ```
240
+ */
241
+ declare const declare: (slug: string, props: OpenApiPluginProps<string>) => _$kizunajs.PluginDeclaration<{
242
+ readonly yaml?: {
243
+ readonly method: "GET";
244
+ readonly path: `/${string}.yaml`;
245
+ readonly summary: "OpenAPI document, as YAML";
246
+ readonly responses: {
247
+ readonly 200: z.ZodString;
248
+ };
249
+ } | undefined;
250
+ readonly json?: {
251
+ readonly method: "GET";
252
+ readonly path: `/${string}.json`;
253
+ readonly summary: "OpenAPI document";
254
+ readonly responses: {
255
+ readonly 200: z.ZodUnknown;
256
+ };
257
+ } | undefined;
258
+ readonly page?: {
259
+ readonly method: "GET";
260
+ readonly path: `/${string}`;
261
+ readonly summary: "API reference";
262
+ readonly responses: {
263
+ readonly 200: z.ZodString;
264
+ };
265
+ } | undefined;
266
+ }, OpenApiPluginProps<string>, undefined, string, unknown>;
267
+ declare function openApiPlugin<const Slug extends string = typeof OPENAPI_PLUGIN_SLUG>(props: OpenApiPluginProps<Slug>): WithSlug<ReturnType<typeof declare>, Slug>;
268
+ //#endregion
269
+ //#region src/generator.d.ts
270
+ /**
271
+ * Render from options held directly, for the plugin's own routes.
272
+ */
273
+ declare function renderOpenApi(api: ApiDefinition, options: GenerateOpenApiOptions): OpenApiRenderer;
274
+ /**
275
+ * Generate an OpenAPI 3.1.0 document from an api. Returns a renderer: call
276
+ * it with `'json'` or `'yaml'`. Options come from the `openApiPlugin` installed
277
+ * on the api, so the document matches the one the server serves.
278
+ *
279
+ * Pass `overrides` for what only a build step knows, such as the public
280
+ * `servers` list.
281
+ */
282
+ declare function generateOpenApi(api: ApiDefinition, overrides?: Partial<GenerateOpenApiOptions>): OpenApiRenderer;
283
+ //#endregion
284
+ //#region src/document-target.d.ts
285
+ /**
286
+ * Where the generated OpenAPI document is written. The format follows the
287
+ * output extension: `.json` renders JSON, anything else renders YAML.
288
+ */
289
+ interface OpenApiDocumentOptions extends Partial<GenerateOpenApiOptions> {
290
+ /**
291
+ * Path the generated document is written to.
292
+ */
293
+ output: string;
294
+ }
295
+ /**
296
+ * An OpenAPI document target for `kizuna.config.ts`.
297
+ *
298
+ * @example
299
+ * openApiDocument({ output: './openapi.yaml' });
300
+ */
301
+ declare const openApiDocument: ({
302
+ output,
303
+ ...overrides
304
+ }: OpenApiDocumentOptions) => ClientTarget;
305
+ //#endregion
306
+ export { type DocsHtmlOptions, type DocsProvider, type GenerateOpenApiOptions, type JsonDocumentPath, type OpenApiDocument, type OpenApiDocumentOptions, type OpenApiInfo, type OpenApiOperation, type OpenApiParameter, type OpenApiPluginProps, type OpenApiRenderer, type OpenApiResponseObject, type OpenApiServer, type OpenApiTag, type OpenApiVersion, type YamlDocumentPath, generateOpenApi, openApiDocument, openApiPlugin, renderDocsHtml, renderOpenApi };
@@ -0,0 +1,306 @@
1
+ import { z } from "zod";
2
+ import { RoutePath, WithSlug } from "kizunajs/plugin";
3
+ import { RouteDefinition } from "kizunajs/generator";
4
+ import * as _$kizunajs from "kizunajs";
5
+ import { ApiDefinition, ClientTarget } from "kizunajs";
6
+
7
+ //#region src/docs-html.d.ts
8
+ /**
9
+ * The API reference UI rendered by the docs endpoint.
10
+ */
11
+ type DocsProvider = 'scalar' | 'swagger';
12
+ interface DocsHtmlOptions {
13
+ /**
14
+ * URL the UI fetches the OpenAPI document from. Omit it and pass
15
+ * {@link DocsHtmlOptions.specContent} instead.
16
+ */
17
+ specUrl?: string;
18
+ /**
19
+ * The document itself, embedded in the page rather than fetched.
20
+ */
21
+ specContent?: unknown;
22
+ /**
23
+ * Which API reference UI to render.
24
+ *
25
+ * @default 'scalar'
26
+ */
27
+ provider?: DocsProvider;
28
+ /**
29
+ * The page's `<title>`.
30
+ *
31
+ * @default 'API Reference'
32
+ */
33
+ pageTitle?: string;
34
+ /**
35
+ * Where to load the UI's assets from. Point this at a self-hosted copy for
36
+ * air-gapped deployments or a strict CSP.
37
+ *
38
+ * For `'scalar'` this is the script URL. For `'swagger'` it is the
39
+ * directory holding `swagger-ui.css` and `swagger-ui-bundle.js`.
40
+ *
41
+ * @default 'https://cdn.jsdelivr.net/npm/@scalar/api-reference@1' | 'https://cdn.jsdelivr.net/npm/swagger-ui-dist@5'
42
+ */
43
+ cdnUrl?: string;
44
+ /**
45
+ * Merged into `Scalar.createApiReference` or `SwaggerUIBundle`. See the
46
+ * provider's own documentation for the available keys.
47
+ */
48
+ configuration?: Record<string, unknown>;
49
+ }
50
+ /**
51
+ * Render a self-contained page that loads an API reference UI over your OpenAPI
52
+ * document. `openApiPlugin` calls this, so reach for it directly only when
53
+ * kizuna has no adapter for your framework.
54
+ *
55
+ * @example
56
+ * ```ts
57
+ * const html = renderDocsHtml({
58
+ * specUrl: '/openapi.json',
59
+ * pageTitle: 'My API',
60
+ * });
61
+ * ```
62
+ */
63
+ declare const renderDocsHtml: (options: DocsHtmlOptions) => string;
64
+ //#endregion
65
+ //#region src/types.d.ts
66
+ /**
67
+ * The OpenAPI Specification version declared in the document's `openapi` field.
68
+ */
69
+ type OpenApiVersion = '3.1.0';
70
+ interface OpenApiInfo {
71
+ title: string;
72
+ version: string;
73
+ description?: string;
74
+ }
75
+ interface OpenApiServer {
76
+ url: string;
77
+ description?: string;
78
+ }
79
+ interface OpenApiParameter {
80
+ name: string;
81
+ in: 'path' | 'query' | 'header' | 'cookie';
82
+ required?: boolean;
83
+ description?: string;
84
+ schema: Record<string, unknown>;
85
+ }
86
+ interface OpenApiResponseObject {
87
+ description: string;
88
+ headers?: Record<string, {
89
+ description?: string;
90
+ required?: boolean;
91
+ schema: Record<string, unknown>;
92
+ }>;
93
+ content?: Record<string, {
94
+ schema: Record<string, unknown>;
95
+ }>;
96
+ }
97
+ interface OpenApiExternalDocs {
98
+ url: string;
99
+ description?: string;
100
+ }
101
+ interface OpenApiTag {
102
+ name: string;
103
+ description?: string;
104
+ externalDocs?: OpenApiExternalDocs;
105
+ }
106
+ interface OpenApiOperation {
107
+ operationId?: string;
108
+ summary?: string;
109
+ description?: string;
110
+ deprecated?: boolean;
111
+ tags?: string[];
112
+ security?: Array<Record<string, string[]>>;
113
+ /**
114
+ * The `custom` schemes guarding this operation. They emit no `security`
115
+ * requirement, so this marks the operation as protected, not public.
116
+ */
117
+ 'x-kizuna-guarded'?: string[];
118
+ /**
119
+ * The roles the route accepts, from the route's `auth.roles`.
120
+ */
121
+ 'x-kizuna-roles'?: string[];
122
+ /**
123
+ * The permissions the caller has to hold, from the route's `auth.requires`.
124
+ */
125
+ 'x-kizuna-requires'?: Record<string, string[]>;
126
+ externalDocs?: OpenApiExternalDocs;
127
+ parameters?: OpenApiParameter[];
128
+ requestBody?: {
129
+ required?: boolean;
130
+ content: Record<string, {
131
+ schema: Record<string, unknown>;
132
+ }>;
133
+ };
134
+ responses: Record<string, OpenApiResponseObject>;
135
+ }
136
+ interface OpenApiDocument {
137
+ openapi: string;
138
+ info: OpenApiInfo;
139
+ servers?: OpenApiServer[];
140
+ paths: Record<string, Record<string, OpenApiOperation>>;
141
+ tags?: OpenApiTag[];
142
+ externalDocs?: OpenApiExternalDocs;
143
+ components?: {
144
+ securitySchemes?: Record<string, unknown>;
145
+ schemas?: Record<string, unknown>;
146
+ };
147
+ }
148
+ interface GenerateOpenApiOptions {
149
+ /**
150
+ * The OpenAPI Specification version to declare in the document's `openapi` field.
151
+ *
152
+ * Defaults to `'3.1.0'`.
153
+ */
154
+ openApiVersion?: OpenApiVersion;
155
+ info: OpenApiInfo;
156
+ servers?: OpenApiServer[];
157
+ externalDocs?: OpenApiExternalDocs;
158
+ setOperationId?: boolean | 'concatenated-path';
159
+ operationMapper?: (operation: OpenApiOperation, route: RouteDefinition, operationId: string) => OpenApiOperation;
160
+ /**
161
+ * Document the derived `head` operation on every `GET` path without a declared one.
162
+ *
163
+ * @default false
164
+ */
165
+ derivedHead?: boolean;
166
+ }
167
+ interface OpenApiRenderer {
168
+ (format: 'json'): OpenApiDocument;
169
+ (format: 'yaml'): string;
170
+ }
171
+ //#endregion
172
+ //#region src/plugin.d.ts
173
+ declare const OPENAPI_PLUGIN_SLUG = "openApi";
174
+ type JsonDocumentPath = `${RoutePath}.json`;
175
+ type YamlDocumentPath = `${RoutePath}.yaml`;
176
+ interface OpenApiPluginProps<Slug extends string = typeof OPENAPI_PLUGIN_SLUG> extends GenerateOpenApiOptions {
177
+ /**
178
+ * What handlers reach this plugin under. Give a second document its own.
179
+ *
180
+ * @default 'openApi'
181
+ */
182
+ slug?: Slug;
183
+ /**
184
+ * Where the reference UI is served.
185
+ */
186
+ docsPath?: RoutePath;
187
+ /**
188
+ * Where the document is served. The UI embeds it, so this is only for
189
+ * publishing the file.
190
+ */
191
+ jsonPath?: JsonDocumentPath;
192
+ /**
193
+ * Where the document is served as YAML.
194
+ */
195
+ yamlPath?: YamlDocumentPath;
196
+ /**
197
+ * Which API reference UI to render.
198
+ *
199
+ * @default 'scalar'
200
+ */
201
+ provider?: DocsProvider;
202
+ /**
203
+ * Where to load the UI's assets from, for a self-hosted copy. The script
204
+ * URL for `'scalar'`; the directory holding `swagger-ui.css` and
205
+ * `swagger-ui-bundle.js` for `'swagger'`.
206
+ */
207
+ cdnUrl?: string;
208
+ /**
209
+ * The page's `<title>`.
210
+ *
211
+ * @default the document's `info.title`
212
+ */
213
+ pageTitle?: string;
214
+ /**
215
+ * Merged into `Scalar.createApiReference` or `SwaggerUIBundle`.
216
+ */
217
+ configuration?: Record<string, unknown>;
218
+ }
219
+ /**
220
+ * Serve an API reference UI for the OpenAPI document, and the document itself
221
+ * if you publish it.
222
+ *
223
+ * The routes are public. Gate them with your framework's own middleware if that
224
+ * is not what you want.
225
+ *
226
+ * @example
227
+ * ```ts
228
+ * export default defineConfig({
229
+ * routes,
230
+ * plugins: [
231
+ * openApiPlugin({
232
+ * info: {
233
+ * title: 'My API',
234
+ * version: '1.0.0',
235
+ * },
236
+ * }),
237
+ * ],
238
+ * });
239
+ * ```
240
+ */
241
+ declare const declare: (slug: string, props: OpenApiPluginProps<string>) => _$kizunajs.PluginDeclaration<{
242
+ readonly yaml?: {
243
+ readonly method: "GET";
244
+ readonly path: `/${string}.yaml`;
245
+ readonly summary: "OpenAPI document, as YAML";
246
+ readonly responses: {
247
+ readonly 200: z.ZodString;
248
+ };
249
+ } | undefined;
250
+ readonly json?: {
251
+ readonly method: "GET";
252
+ readonly path: `/${string}.json`;
253
+ readonly summary: "OpenAPI document";
254
+ readonly responses: {
255
+ readonly 200: z.ZodUnknown;
256
+ };
257
+ } | undefined;
258
+ readonly page?: {
259
+ readonly method: "GET";
260
+ readonly path: `/${string}`;
261
+ readonly summary: "API reference";
262
+ readonly responses: {
263
+ readonly 200: z.ZodString;
264
+ };
265
+ } | undefined;
266
+ }, OpenApiPluginProps<string>, undefined, string, unknown>;
267
+ declare function openApiPlugin<const Slug extends string = typeof OPENAPI_PLUGIN_SLUG>(props: OpenApiPluginProps<Slug>): WithSlug<ReturnType<typeof declare>, Slug>;
268
+ //#endregion
269
+ //#region src/generator.d.ts
270
+ /**
271
+ * Render from options held directly, for the plugin's own routes.
272
+ */
273
+ declare function renderOpenApi(api: ApiDefinition, options: GenerateOpenApiOptions): OpenApiRenderer;
274
+ /**
275
+ * Generate an OpenAPI 3.1.0 document from an api. Returns a renderer: call
276
+ * it with `'json'` or `'yaml'`. Options come from the `openApiPlugin` installed
277
+ * on the api, so the document matches the one the server serves.
278
+ *
279
+ * Pass `overrides` for what only a build step knows, such as the public
280
+ * `servers` list.
281
+ */
282
+ declare function generateOpenApi(api: ApiDefinition, overrides?: Partial<GenerateOpenApiOptions>): OpenApiRenderer;
283
+ //#endregion
284
+ //#region src/document-target.d.ts
285
+ /**
286
+ * Where the generated OpenAPI document is written. The format follows the
287
+ * output extension: `.json` renders JSON, anything else renders YAML.
288
+ */
289
+ interface OpenApiDocumentOptions extends Partial<GenerateOpenApiOptions> {
290
+ /**
291
+ * Path the generated document is written to.
292
+ */
293
+ output: string;
294
+ }
295
+ /**
296
+ * An OpenAPI document target for `kizuna.config.ts`.
297
+ *
298
+ * @example
299
+ * openApiDocument({ output: './openapi.yaml' });
300
+ */
301
+ declare const openApiDocument: ({
302
+ output,
303
+ ...overrides
304
+ }: OpenApiDocumentOptions) => ClientTarget;
305
+ //#endregion
306
+ export { type DocsHtmlOptions, type DocsProvider, type GenerateOpenApiOptions, type JsonDocumentPath, type OpenApiDocument, type OpenApiDocumentOptions, type OpenApiInfo, type OpenApiOperation, type OpenApiParameter, type OpenApiPluginProps, type OpenApiRenderer, type OpenApiResponseObject, type OpenApiServer, type OpenApiTag, type OpenApiVersion, type YamlDocumentPath, generateOpenApi, openApiDocument, openApiPlugin, renderDocsHtml, renderOpenApi };