@riktajs/swagger 0.1.2 → 0.2.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.
- package/dist/constants.d.ts +0 -45
- package/dist/constants.js +0 -50
- package/dist/decorators/api-body.decorator.d.ts +0 -29
- package/dist/decorators/api-body.decorator.js +0 -29
- package/dist/decorators/api-exclude.decorator.d.ts +0 -62
- package/dist/decorators/api-exclude.decorator.js +0 -64
- package/dist/decorators/api-header.decorator.d.ts +0 -21
- package/dist/decorators/api-header.decorator.js +0 -21
- package/dist/decorators/api-operation.decorator.d.ts +0 -28
- package/dist/decorators/api-operation.decorator.js +0 -28
- package/dist/decorators/api-param.decorator.d.ts +0 -24
- package/dist/decorators/api-param.decorator.js +0 -24
- package/dist/decorators/api-property.decorator.d.ts +0 -58
- package/dist/decorators/api-property.decorator.js +0 -56
- package/dist/decorators/api-query.decorator.d.ts +0 -26
- package/dist/decorators/api-query.decorator.js +0 -26
- package/dist/decorators/api-response.decorator.d.ts +0 -55
- package/dist/decorators/api-response.decorator.js +0 -55
- package/dist/decorators/api-security.decorator.d.ts +0 -80
- package/dist/decorators/api-security.decorator.js +0 -84
- package/dist/decorators/api-tags.decorator.d.ts +0 -28
- package/dist/decorators/api-tags.decorator.js +0 -30
- package/dist/decorators/index.d.ts +0 -6
- package/dist/decorators/index.js +0 -14
- package/dist/index.d.ts +0 -55
- package/dist/index.js +0 -62
- package/dist/openapi/generator.d.ts +0 -49
- package/dist/openapi/generator.js +2 -86
- package/dist/openapi/index.d.ts +0 -6
- package/dist/openapi/index.js +0 -9
- package/dist/openapi/zod-to-openapi.d.ts +2 -49
- package/dist/openapi/zod-to-openapi.js +18 -75
- package/dist/plugin/index.d.ts +0 -6
- package/dist/plugin/index.js +0 -6
- package/dist/plugin/swagger.plugin.d.ts +0 -119
- package/dist/plugin/swagger.plugin.js +1 -110
- package/dist/types.d.ts +0 -173
- package/dist/types.js +0 -1
- package/package.json +62 -61
- package/dist/constants.d.ts.map +0 -1
- package/dist/constants.js.map +0 -1
- package/dist/decorators/api-body.decorator.d.ts.map +0 -1
- package/dist/decorators/api-body.decorator.js.map +0 -1
- package/dist/decorators/api-exclude.decorator.d.ts.map +0 -1
- package/dist/decorators/api-exclude.decorator.js.map +0 -1
- package/dist/decorators/api-header.decorator.d.ts.map +0 -1
- package/dist/decorators/api-header.decorator.js.map +0 -1
- package/dist/decorators/api-operation.decorator.d.ts.map +0 -1
- package/dist/decorators/api-operation.decorator.js.map +0 -1
- package/dist/decorators/api-param.decorator.d.ts.map +0 -1
- package/dist/decorators/api-param.decorator.js.map +0 -1
- package/dist/decorators/api-property.decorator.d.ts.map +0 -1
- package/dist/decorators/api-property.decorator.js.map +0 -1
- package/dist/decorators/api-query.decorator.d.ts.map +0 -1
- package/dist/decorators/api-query.decorator.js.map +0 -1
- package/dist/decorators/api-response.decorator.d.ts.map +0 -1
- package/dist/decorators/api-response.decorator.js.map +0 -1
- package/dist/decorators/api-security.decorator.d.ts.map +0 -1
- package/dist/decorators/api-security.decorator.js.map +0 -1
- package/dist/decorators/api-tags.decorator.d.ts.map +0 -1
- package/dist/decorators/api-tags.decorator.js.map +0 -1
- package/dist/decorators/index.d.ts.map +0 -1
- package/dist/decorators/index.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/openapi/generator.d.ts.map +0 -1
- package/dist/openapi/generator.js.map +0 -1
- package/dist/openapi/index.d.ts.map +0 -1
- package/dist/openapi/index.js.map +0 -1
- package/dist/openapi/zod-to-openapi.d.ts.map +0 -1
- package/dist/openapi/zod-to-openapi.js.map +0 -1
- package/dist/plugin/index.d.ts.map +0 -1
- package/dist/plugin/index.js.map +0 -1
- package/dist/plugin/swagger.plugin.d.ts.map +0 -1
- package/dist/plugin/swagger.plugin.js.map +0 -1
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js.map +0 -1
|
@@ -1,30 +1,17 @@
|
|
|
1
|
-
import {
|
|
2
|
-
/**
|
|
3
|
-
* Type guard to check if a value is a Zod schema
|
|
4
|
-
* Uses duck typing to detect Zod schemas without requiring the full library
|
|
5
|
-
*/
|
|
1
|
+
import { z } from 'zod';
|
|
6
2
|
export function isZodSchema(value) {
|
|
7
3
|
return (value !== null &&
|
|
8
4
|
typeof value === 'object' &&
|
|
9
|
-
'_def' in value &&
|
|
5
|
+
('_zod' in value || '_def' in value) &&
|
|
10
6
|
'safeParse' in value &&
|
|
11
7
|
typeof value.safeParse === 'function');
|
|
12
8
|
}
|
|
13
|
-
/**
|
|
14
|
-
* Convert JSON Schema 7 to OpenAPI 3.0 compatible schema
|
|
15
|
-
*
|
|
16
|
-
* Main differences:
|
|
17
|
-
* - OpenAPI uses `nullable: true` instead of `type: ['string', 'null']`
|
|
18
|
-
* - OpenAPI 3.0 doesn't support `$schema`, `$id`, etc.
|
|
19
|
-
* - Some JSON Schema features need to be simplified
|
|
20
|
-
*/
|
|
21
9
|
function jsonSchemaToOpenApi(jsonSchema) {
|
|
22
10
|
if (typeof jsonSchema === 'boolean') {
|
|
23
11
|
return jsonSchema ? {} : { not: {} };
|
|
24
12
|
}
|
|
25
13
|
const result = {};
|
|
26
14
|
const schema = jsonSchema;
|
|
27
|
-
// Handle type with null (convert to nullable)
|
|
28
15
|
if (Array.isArray(schema.type)) {
|
|
29
16
|
const types = schema.type;
|
|
30
17
|
const nullIndex = types.indexOf('null');
|
|
@@ -35,7 +22,6 @@ function jsonSchemaToOpenApi(jsonSchema) {
|
|
|
35
22
|
result.type = nonNullTypes[0];
|
|
36
23
|
}
|
|
37
24
|
else if (nonNullTypes.length > 1) {
|
|
38
|
-
// Multiple non-null types - use oneOf
|
|
39
25
|
result.oneOf = nonNullTypes.map(t => ({ type: t }));
|
|
40
26
|
}
|
|
41
27
|
}
|
|
@@ -46,7 +32,6 @@ function jsonSchemaToOpenApi(jsonSchema) {
|
|
|
46
32
|
else if (schema.type) {
|
|
47
33
|
result.type = schema.type;
|
|
48
34
|
}
|
|
49
|
-
// Copy simple properties
|
|
50
35
|
const simpleProps = [
|
|
51
36
|
'format', 'description', 'default', 'example', 'enum',
|
|
52
37
|
'minimum', 'maximum', 'exclusiveMinimum', 'exclusiveMaximum',
|
|
@@ -60,24 +45,20 @@ function jsonSchemaToOpenApi(jsonSchema) {
|
|
|
60
45
|
result[prop] = schema[prop];
|
|
61
46
|
}
|
|
62
47
|
}
|
|
63
|
-
// Handle items (for arrays)
|
|
64
48
|
if (schema.items) {
|
|
65
49
|
if (Array.isArray(schema.items)) {
|
|
66
|
-
// Tuple - OpenAPI 3.0 doesn't support tuple validation well
|
|
67
50
|
result.items = { oneOf: schema.items.map(jsonSchemaToOpenApi) };
|
|
68
51
|
}
|
|
69
52
|
else {
|
|
70
53
|
result.items = jsonSchemaToOpenApi(schema.items);
|
|
71
54
|
}
|
|
72
55
|
}
|
|
73
|
-
// Handle properties (for objects)
|
|
74
56
|
if (schema.properties) {
|
|
75
57
|
result.properties = {};
|
|
76
58
|
for (const [key, value] of Object.entries(schema.properties)) {
|
|
77
59
|
result.properties[key] = jsonSchemaToOpenApi(value);
|
|
78
60
|
}
|
|
79
61
|
}
|
|
80
|
-
// Handle additionalProperties
|
|
81
62
|
if (schema.additionalProperties !== undefined) {
|
|
82
63
|
if (typeof schema.additionalProperties === 'boolean') {
|
|
83
64
|
result.additionalProperties = schema.additionalProperties;
|
|
@@ -86,7 +67,6 @@ function jsonSchemaToOpenApi(jsonSchema) {
|
|
|
86
67
|
result.additionalProperties = jsonSchemaToOpenApi(schema.additionalProperties);
|
|
87
68
|
}
|
|
88
69
|
}
|
|
89
|
-
// Handle allOf, oneOf, anyOf
|
|
90
70
|
if (schema.allOf) {
|
|
91
71
|
result.allOf = schema.allOf.map(jsonSchemaToOpenApi);
|
|
92
72
|
}
|
|
@@ -96,69 +76,34 @@ function jsonSchemaToOpenApi(jsonSchema) {
|
|
|
96
76
|
if (schema.anyOf) {
|
|
97
77
|
result.anyOf = schema.anyOf.map(jsonSchemaToOpenApi);
|
|
98
78
|
}
|
|
99
|
-
// Handle not
|
|
100
79
|
if (schema.not) {
|
|
101
80
|
result.not = jsonSchemaToOpenApi(schema.not);
|
|
102
81
|
}
|
|
103
|
-
// Handle const (convert to enum with single value)
|
|
104
82
|
if (schema.const !== undefined) {
|
|
105
83
|
result.enum = [schema.const];
|
|
106
84
|
}
|
|
107
85
|
return result;
|
|
108
86
|
}
|
|
109
|
-
/**
|
|
110
|
-
* Convert a Zod schema to an OpenAPI 3.0 schema object
|
|
111
|
-
*
|
|
112
|
-
* Uses `zod-to-json-schema` for the heavy lifting, then converts
|
|
113
|
-
* JSON Schema 7 to OpenAPI 3.0 compatible format.
|
|
114
|
-
*
|
|
115
|
-
* Supports all Zod types including:
|
|
116
|
-
* - Primitives: string, number, boolean, bigint
|
|
117
|
-
* - Complex: object, array, tuple, record
|
|
118
|
-
* - Modifiers: optional, nullable, default
|
|
119
|
-
* - Validators: min, max, email, uuid, url, etc.
|
|
120
|
-
* - Enums: enum, nativeEnum
|
|
121
|
-
* - Unions and intersections
|
|
122
|
-
* - Literals
|
|
123
|
-
* - Effects (transform, refine)
|
|
124
|
-
*
|
|
125
|
-
* @param schema - The Zod schema to convert
|
|
126
|
-
* @returns OpenAPI schema object
|
|
127
|
-
*
|
|
128
|
-
* @example
|
|
129
|
-
* ```typescript
|
|
130
|
-
* const UserSchema = z.object({
|
|
131
|
-
* id: z.string().uuid(),
|
|
132
|
-
* email: z.string().email(),
|
|
133
|
-
* age: z.number().int().min(0).optional(),
|
|
134
|
-
* });
|
|
135
|
-
*
|
|
136
|
-
* const openApiSchema = zodToOpenApi(UserSchema);
|
|
137
|
-
* // {
|
|
138
|
-
* // type: 'object',
|
|
139
|
-
* // properties: {
|
|
140
|
-
* // id: { type: 'string', format: 'uuid' },
|
|
141
|
-
* // email: { type: 'string', format: 'email' },
|
|
142
|
-
* // age: { type: 'integer', minimum: 0 }
|
|
143
|
-
* // },
|
|
144
|
-
* // required: ['id', 'email']
|
|
145
|
-
* // }
|
|
146
|
-
* ```
|
|
147
|
-
*/
|
|
148
87
|
export function zodToOpenApi(schema) {
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
88
|
+
const jsonSchema = z.toJSONSchema(schema, {
|
|
89
|
+
target: 'openapi-3.0',
|
|
90
|
+
unrepresentable: 'any',
|
|
91
|
+
override: (ctx) => {
|
|
92
|
+
const def = ctx.zodSchema._zod?.def;
|
|
93
|
+
if (!def)
|
|
94
|
+
return;
|
|
95
|
+
if (def.type === 'date') {
|
|
96
|
+
ctx.jsonSchema.type = 'string';
|
|
97
|
+
ctx.jsonSchema.format = 'date-time';
|
|
98
|
+
}
|
|
99
|
+
if (def.type === 'bigint') {
|
|
100
|
+
ctx.jsonSchema.type = 'integer';
|
|
101
|
+
ctx.jsonSchema.format = 'int64';
|
|
102
|
+
}
|
|
103
|
+
},
|
|
153
104
|
});
|
|
154
|
-
// Convert JSON Schema to OpenAPI format
|
|
155
|
-
// The result from zodToJsonSchema may include $schema and definitions
|
|
156
|
-
// which are not part of JsonSchema7Type but are compatible
|
|
157
105
|
return jsonSchemaToOpenApi(jsonSchema);
|
|
158
106
|
}
|
|
159
|
-
/**
|
|
160
|
-
* Convert a Zod schema to OpenAPI schema, or pass through if already an OpenAPI schema
|
|
161
|
-
*/
|
|
162
107
|
export function toOpenApiSchema(schemaOrOpenApi) {
|
|
163
108
|
if (!schemaOrOpenApi) {
|
|
164
109
|
return undefined;
|
|
@@ -166,7 +111,5 @@ export function toOpenApiSchema(schemaOrOpenApi) {
|
|
|
166
111
|
if (isZodSchema(schemaOrOpenApi)) {
|
|
167
112
|
return zodToOpenApi(schemaOrOpenApi);
|
|
168
113
|
}
|
|
169
|
-
// Already an OpenAPI schema object
|
|
170
114
|
return schemaOrOpenApi;
|
|
171
115
|
}
|
|
172
|
-
//# sourceMappingURL=zod-to-openapi.js.map
|
package/dist/plugin/index.d.ts
CHANGED
package/dist/plugin/index.js
CHANGED
|
@@ -2,59 +2,20 @@ import type { FastifyInstance, FastifyPluginAsync } from 'fastify';
|
|
|
2
2
|
import { type FastifySwaggerUiOptions } from '@fastify/swagger-ui';
|
|
3
3
|
import type { SwaggerConfig, OpenApiDocument, OpenApiInfoObject } from '../types.js';
|
|
4
4
|
type Constructor<T = unknown> = new (...args: any[]) => T;
|
|
5
|
-
/**
|
|
6
|
-
* Swagger plugin options
|
|
7
|
-
*/
|
|
8
5
|
export interface SwaggerPluginOptions {
|
|
9
|
-
/**
|
|
10
|
-
* Swagger/OpenAPI configuration
|
|
11
|
-
*/
|
|
12
6
|
config?: SwaggerConfig;
|
|
13
|
-
/**
|
|
14
|
-
* API Info (title, version, description, etc.)
|
|
15
|
-
* Can also be provided via config.info
|
|
16
|
-
*/
|
|
17
7
|
info?: OpenApiInfoObject;
|
|
18
|
-
/**
|
|
19
|
-
* Controllers to document (optional - will auto-discover from registry if not provided)
|
|
20
|
-
*/
|
|
21
8
|
controllers?: Constructor[];
|
|
22
|
-
/**
|
|
23
|
-
* Path to serve the OpenAPI JSON specification
|
|
24
|
-
* @default '/docs/json'
|
|
25
|
-
*/
|
|
26
9
|
jsonPath?: string;
|
|
27
|
-
/**
|
|
28
|
-
* Path to serve the Swagger UI
|
|
29
|
-
* @default '/docs'
|
|
30
|
-
*/
|
|
31
10
|
uiPath?: string;
|
|
32
|
-
/**
|
|
33
|
-
* Whether to expose the Swagger UI
|
|
34
|
-
* @default true
|
|
35
|
-
*/
|
|
36
11
|
exposeUI?: boolean;
|
|
37
|
-
/**
|
|
38
|
-
* Whether to expose the OpenAPI JSON specification
|
|
39
|
-
* @default true
|
|
40
|
-
*/
|
|
41
12
|
exposeSpec?: boolean;
|
|
42
|
-
/**
|
|
43
|
-
* Logo configuration for Swagger UI
|
|
44
|
-
*/
|
|
45
13
|
logo?: {
|
|
46
14
|
url: string;
|
|
47
15
|
altText?: string;
|
|
48
16
|
backgroundColor?: string;
|
|
49
17
|
};
|
|
50
|
-
/**
|
|
51
|
-
* Swagger UI theme
|
|
52
|
-
* @default 'default'
|
|
53
|
-
*/
|
|
54
18
|
theme?: 'default' | 'dark';
|
|
55
|
-
/**
|
|
56
|
-
* Transform the OpenAPI specification before serving
|
|
57
|
-
*/
|
|
58
19
|
transform?: (spec: OpenApiDocument) => OpenApiDocument | Promise<OpenApiDocument>;
|
|
59
20
|
}
|
|
60
21
|
declare module 'fastify' {
|
|
@@ -62,87 +23,8 @@ declare module 'fastify' {
|
|
|
62
23
|
openApiSpec?: OpenApiDocument;
|
|
63
24
|
}
|
|
64
25
|
}
|
|
65
|
-
/**
|
|
66
|
-
* Swagger plugin for Fastify/Rikta
|
|
67
|
-
*
|
|
68
|
-
* Automatically generates OpenAPI documentation from Rikta controllers
|
|
69
|
-
* and serves Swagger UI for interactive API exploration.
|
|
70
|
-
*
|
|
71
|
-
* @example
|
|
72
|
-
* ```typescript
|
|
73
|
-
* import { RiktaFactory } from '@riktajs/core';
|
|
74
|
-
* import { swaggerPlugin } from '@riktajs/swagger';
|
|
75
|
-
*
|
|
76
|
-
* const app = await RiktaFactory.create();
|
|
77
|
-
*
|
|
78
|
-
* await app.register(swaggerPlugin, {
|
|
79
|
-
* info: {
|
|
80
|
-
* title: 'My API',
|
|
81
|
-
* version: '1.0.0',
|
|
82
|
-
* description: 'API documentation for My App',
|
|
83
|
-
* },
|
|
84
|
-
* config: {
|
|
85
|
-
* servers: [
|
|
86
|
-
* { url: 'http://localhost:3000', description: 'Development' },
|
|
87
|
-
* ],
|
|
88
|
-
* },
|
|
89
|
-
* });
|
|
90
|
-
*
|
|
91
|
-
* // Swagger UI available at /docs
|
|
92
|
-
* // OpenAPI JSON at /docs/json
|
|
93
|
-
* ```
|
|
94
|
-
*/
|
|
95
26
|
export declare const swaggerPlugin: FastifyPluginAsync<SwaggerPluginOptions>;
|
|
96
|
-
/**
|
|
97
|
-
* Register swagger plugin with a Rikta application
|
|
98
|
-
*
|
|
99
|
-
* Helper function that provides a simpler API for registering the plugin.
|
|
100
|
-
*
|
|
101
|
-
* @param app - Fastify instance
|
|
102
|
-
* @param options - Swagger plugin options
|
|
103
|
-
*
|
|
104
|
-
* @example
|
|
105
|
-
* ```typescript
|
|
106
|
-
* import { RiktaFactory } from '@riktajs/core';
|
|
107
|
-
* import { registerSwagger } from '@riktajs/swagger';
|
|
108
|
-
*
|
|
109
|
-
* const app = await RiktaFactory.create();
|
|
110
|
-
*
|
|
111
|
-
* await registerSwagger(app, {
|
|
112
|
-
* info: {
|
|
113
|
-
* title: 'My API',
|
|
114
|
-
* version: '1.0.0',
|
|
115
|
-
* },
|
|
116
|
-
* });
|
|
117
|
-
* ```
|
|
118
|
-
*/
|
|
119
27
|
export declare function registerSwagger(app: FastifyInstance, options?: SwaggerPluginOptions): Promise<void>;
|
|
120
|
-
/**
|
|
121
|
-
* Create a standalone Swagger configuration for manual use
|
|
122
|
-
*
|
|
123
|
-
* Use this when you need more control over the Swagger setup,
|
|
124
|
-
* or when integrating with an existing Fastify application.
|
|
125
|
-
*
|
|
126
|
-
* @param options - Swagger plugin options
|
|
127
|
-
* @returns Object containing swagger and swaggerUI options and specification
|
|
128
|
-
*
|
|
129
|
-
* @example
|
|
130
|
-
* ```typescript
|
|
131
|
-
* import fastify from 'fastify';
|
|
132
|
-
* import fastifySwagger from '@fastify/swagger';
|
|
133
|
-
* import fastifySwaggerUI from '@fastify/swagger-ui';
|
|
134
|
-
* import { createSwaggerConfig } from '@riktajs/swagger';
|
|
135
|
-
*
|
|
136
|
-
* const app = fastify();
|
|
137
|
-
* const { swaggerOptions, swaggerUIOptions, specification } = createSwaggerConfig({
|
|
138
|
-
* info: { title: 'My API', version: '1.0.0' },
|
|
139
|
-
* controllers: [UserController, ProductController],
|
|
140
|
-
* });
|
|
141
|
-
*
|
|
142
|
-
* await app.register(fastifySwagger, swaggerOptions);
|
|
143
|
-
* await app.register(fastifySwaggerUI, swaggerUIOptions);
|
|
144
|
-
* ```
|
|
145
|
-
*/
|
|
146
28
|
export declare function createSwaggerConfig(options?: SwaggerPluginOptions): {
|
|
147
29
|
swaggerOptions: {
|
|
148
30
|
mode: 'static';
|
|
@@ -154,4 +36,3 @@ export declare function createSwaggerConfig(options?: SwaggerPluginOptions): {
|
|
|
154
36
|
specification: OpenApiDocument;
|
|
155
37
|
};
|
|
156
38
|
export {};
|
|
157
|
-
//# sourceMappingURL=swagger.plugin.d.ts.map
|
|
@@ -3,60 +3,42 @@ import fastifySwagger from '@fastify/swagger';
|
|
|
3
3
|
import fastifySwaggerUI from '@fastify/swagger-ui';
|
|
4
4
|
import { OpenApiGenerator } from '../openapi/generator.js';
|
|
5
5
|
import { registry } from '@riktajs/core';
|
|
6
|
-
/**
|
|
7
|
-
* Default plugin options
|
|
8
|
-
*/
|
|
9
6
|
const DEFAULT_OPTIONS = {
|
|
10
7
|
jsonPath: '/docs/json',
|
|
11
8
|
uiPath: '/docs',
|
|
12
9
|
exposeUI: true,
|
|
13
10
|
exposeSpec: true,
|
|
14
11
|
};
|
|
15
|
-
/**
|
|
16
|
-
* Swagger plugin implementation
|
|
17
|
-
*
|
|
18
|
-
* Registers @fastify/swagger and @fastify/swagger-ui with automatic
|
|
19
|
-
* OpenAPI specification generation from Rikta controllers.
|
|
20
|
-
*/
|
|
21
12
|
async function swaggerPluginImpl(fastify, options) {
|
|
22
13
|
const mergedOptions = { ...DEFAULT_OPTIONS, ...options };
|
|
23
14
|
const { jsonPath, uiPath, exposeUI, exposeSpec, controllers, config, info, transform } = mergedOptions;
|
|
24
|
-
// Build info object from options or config
|
|
25
15
|
const apiInfo = info || config?.info || {
|
|
26
16
|
title: 'API Documentation',
|
|
27
17
|
version: '1.0.0',
|
|
28
18
|
};
|
|
29
|
-
// Create OpenAPI generator
|
|
30
19
|
const generator = new OpenApiGenerator({
|
|
31
20
|
...config,
|
|
32
21
|
info: apiInfo,
|
|
33
22
|
});
|
|
34
|
-
// Get controllers to document
|
|
35
23
|
const controllersToDocument = controllers || registry.getControllers();
|
|
36
|
-
// Add controllers to generator
|
|
37
24
|
for (const controller of controllersToDocument) {
|
|
38
25
|
generator.addController(controller);
|
|
39
26
|
}
|
|
40
|
-
// Add security schemes from config
|
|
41
27
|
if (config?.securitySchemes) {
|
|
42
28
|
for (const [name, scheme] of Object.entries(config.securitySchemes)) {
|
|
43
29
|
generator.addSecurityScheme(name, scheme);
|
|
44
30
|
}
|
|
45
31
|
}
|
|
46
|
-
// Generate OpenAPI specification
|
|
47
32
|
let spec = generator.generate();
|
|
48
|
-
// Apply transform if provided
|
|
49
33
|
if (transform) {
|
|
50
34
|
spec = await transform(spec);
|
|
51
35
|
}
|
|
52
|
-
// Register @fastify/swagger with static specification
|
|
53
36
|
await fastify.register(fastifySwagger, {
|
|
54
37
|
mode: 'static',
|
|
55
38
|
specification: {
|
|
56
|
-
document: spec,
|
|
39
|
+
document: spec,
|
|
57
40
|
},
|
|
58
41
|
});
|
|
59
|
-
// Register @fastify/swagger-ui if enabled
|
|
60
42
|
if (exposeUI) {
|
|
61
43
|
const uiOptions = {
|
|
62
44
|
routePrefix: uiPath,
|
|
@@ -73,7 +55,6 @@ async function swaggerPluginImpl(fastify, options) {
|
|
|
73
55
|
staticCSP: true,
|
|
74
56
|
transformStaticCSP: (header) => header,
|
|
75
57
|
};
|
|
76
|
-
// Add logo if provided
|
|
77
58
|
if (mergedOptions.logo) {
|
|
78
59
|
uiOptions.logo = {
|
|
79
60
|
type: 'image/png',
|
|
@@ -83,11 +64,6 @@ async function swaggerPluginImpl(fastify, options) {
|
|
|
83
64
|
}
|
|
84
65
|
await fastify.register(fastifySwaggerUI, uiOptions);
|
|
85
66
|
}
|
|
86
|
-
// Add JSON spec route if enabled
|
|
87
|
-
// @fastify/swagger-ui registers routes at {uiPath}/json when UI is enabled
|
|
88
|
-
// So we only add custom route if:
|
|
89
|
-
// 1. UI is disabled (no swagger-ui default route exists)
|
|
90
|
-
// 2. jsonPath differs from swagger-ui default path
|
|
91
67
|
const defaultSwaggerUIJsonPath = `${uiPath}/json`;
|
|
92
68
|
const shouldRegisterCustomJsonRoute = exposeSpec && (!exposeUI ||
|
|
93
69
|
(jsonPath !== defaultSwaggerUIJsonPath && jsonPath !== '/docs/json'));
|
|
@@ -100,115 +76,31 @@ async function swaggerPluginImpl(fastify, options) {
|
|
|
100
76
|
return spec;
|
|
101
77
|
});
|
|
102
78
|
}
|
|
103
|
-
// Store spec on fastify instance for later access
|
|
104
79
|
fastify.decorate('openApiSpec', spec);
|
|
105
80
|
}
|
|
106
|
-
/**
|
|
107
|
-
* Swagger plugin for Fastify/Rikta
|
|
108
|
-
*
|
|
109
|
-
* Automatically generates OpenAPI documentation from Rikta controllers
|
|
110
|
-
* and serves Swagger UI for interactive API exploration.
|
|
111
|
-
*
|
|
112
|
-
* @example
|
|
113
|
-
* ```typescript
|
|
114
|
-
* import { RiktaFactory } from '@riktajs/core';
|
|
115
|
-
* import { swaggerPlugin } from '@riktajs/swagger';
|
|
116
|
-
*
|
|
117
|
-
* const app = await RiktaFactory.create();
|
|
118
|
-
*
|
|
119
|
-
* await app.register(swaggerPlugin, {
|
|
120
|
-
* info: {
|
|
121
|
-
* title: 'My API',
|
|
122
|
-
* version: '1.0.0',
|
|
123
|
-
* description: 'API documentation for My App',
|
|
124
|
-
* },
|
|
125
|
-
* config: {
|
|
126
|
-
* servers: [
|
|
127
|
-
* { url: 'http://localhost:3000', description: 'Development' },
|
|
128
|
-
* ],
|
|
129
|
-
* },
|
|
130
|
-
* });
|
|
131
|
-
*
|
|
132
|
-
* // Swagger UI available at /docs
|
|
133
|
-
* // OpenAPI JSON at /docs/json
|
|
134
|
-
* ```
|
|
135
|
-
*/
|
|
136
81
|
export const swaggerPlugin = fp(swaggerPluginImpl, {
|
|
137
82
|
fastify: '>=4.0.0',
|
|
138
83
|
name: '@riktajs/swagger',
|
|
139
84
|
dependencies: [],
|
|
140
85
|
});
|
|
141
|
-
/**
|
|
142
|
-
* Register swagger plugin with a Rikta application
|
|
143
|
-
*
|
|
144
|
-
* Helper function that provides a simpler API for registering the plugin.
|
|
145
|
-
*
|
|
146
|
-
* @param app - Fastify instance
|
|
147
|
-
* @param options - Swagger plugin options
|
|
148
|
-
*
|
|
149
|
-
* @example
|
|
150
|
-
* ```typescript
|
|
151
|
-
* import { RiktaFactory } from '@riktajs/core';
|
|
152
|
-
* import { registerSwagger } from '@riktajs/swagger';
|
|
153
|
-
*
|
|
154
|
-
* const app = await RiktaFactory.create();
|
|
155
|
-
*
|
|
156
|
-
* await registerSwagger(app, {
|
|
157
|
-
* info: {
|
|
158
|
-
* title: 'My API',
|
|
159
|
-
* version: '1.0.0',
|
|
160
|
-
* },
|
|
161
|
-
* });
|
|
162
|
-
* ```
|
|
163
|
-
*/
|
|
164
86
|
export async function registerSwagger(app, options = {}) {
|
|
165
87
|
await app.register(swaggerPlugin, options);
|
|
166
88
|
}
|
|
167
|
-
/**
|
|
168
|
-
* Create a standalone Swagger configuration for manual use
|
|
169
|
-
*
|
|
170
|
-
* Use this when you need more control over the Swagger setup,
|
|
171
|
-
* or when integrating with an existing Fastify application.
|
|
172
|
-
*
|
|
173
|
-
* @param options - Swagger plugin options
|
|
174
|
-
* @returns Object containing swagger and swaggerUI options and specification
|
|
175
|
-
*
|
|
176
|
-
* @example
|
|
177
|
-
* ```typescript
|
|
178
|
-
* import fastify from 'fastify';
|
|
179
|
-
* import fastifySwagger from '@fastify/swagger';
|
|
180
|
-
* import fastifySwaggerUI from '@fastify/swagger-ui';
|
|
181
|
-
* import { createSwaggerConfig } from '@riktajs/swagger';
|
|
182
|
-
*
|
|
183
|
-
* const app = fastify();
|
|
184
|
-
* const { swaggerOptions, swaggerUIOptions, specification } = createSwaggerConfig({
|
|
185
|
-
* info: { title: 'My API', version: '1.0.0' },
|
|
186
|
-
* controllers: [UserController, ProductController],
|
|
187
|
-
* });
|
|
188
|
-
*
|
|
189
|
-
* await app.register(fastifySwagger, swaggerOptions);
|
|
190
|
-
* await app.register(fastifySwaggerUI, swaggerUIOptions);
|
|
191
|
-
* ```
|
|
192
|
-
*/
|
|
193
89
|
export function createSwaggerConfig(options = {}) {
|
|
194
90
|
const mergedOptions = { ...DEFAULT_OPTIONS, ...options };
|
|
195
91
|
const { uiPath, controllers, config, info } = mergedOptions;
|
|
196
|
-
// Build info object
|
|
197
92
|
const apiInfo = info || config?.info || {
|
|
198
93
|
title: 'API Documentation',
|
|
199
94
|
version: '1.0.0',
|
|
200
95
|
};
|
|
201
|
-
// Create generator
|
|
202
96
|
const generator = new OpenApiGenerator({
|
|
203
97
|
...config,
|
|
204
98
|
info: apiInfo,
|
|
205
99
|
});
|
|
206
|
-
// Add controllers
|
|
207
100
|
const controllersToDocument = controllers || registry.getControllers();
|
|
208
101
|
for (const controller of controllersToDocument) {
|
|
209
102
|
generator.addController(controller);
|
|
210
103
|
}
|
|
211
|
-
// Add security schemes
|
|
212
104
|
if (config?.securitySchemes) {
|
|
213
105
|
for (const [name, scheme] of Object.entries(config.securitySchemes)) {
|
|
214
106
|
generator.addSecurityScheme(name, scheme);
|
|
@@ -232,4 +124,3 @@ export function createSwaggerConfig(options = {}) {
|
|
|
232
124
|
specification,
|
|
233
125
|
};
|
|
234
126
|
}
|
|
235
|
-
//# sourceMappingURL=swagger.plugin.js.map
|