@forinda/kickjs-swagger 2.0.1 → 2.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/README.md +4 -0
- package/dist/index.d.mts +187 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +521 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +30 -12
- package/dist/decorators.d.ts +0 -33
- package/dist/decorators.d.ts.map +0 -1
- package/dist/index.d.ts +0 -6
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -345
- package/dist/openapi-builder.d.ts +0 -36
- package/dist/openapi-builder.d.ts.map +0 -1
- package/dist/schema-parser.d.ts +0 -42
- package/dist/schema-parser.d.ts.map +0 -1
- package/dist/swagger.adapter.d.ts +0 -44
- package/dist/swagger.adapter.d.ts.map +0 -1
- package/dist/ui.d.ts +0 -5
- package/dist/ui.d.ts.map +0 -1
package/README.md
CHANGED
|
@@ -31,11 +31,15 @@ bootstrap({
|
|
|
31
31
|
new SwaggerAdapter({
|
|
32
32
|
info: { title: 'My API', version: '1.0.0' },
|
|
33
33
|
bearerAuth: true,
|
|
34
|
+
disableInProd: true, // skip mounting docs when NODE_ENV=production
|
|
34
35
|
}),
|
|
35
36
|
],
|
|
36
37
|
})
|
|
37
38
|
```
|
|
38
39
|
|
|
40
|
+
Set `disableInProd: true` to skip mounting docs, the spec, and assets when
|
|
41
|
+
`NODE_ENV === 'production'`.
|
|
42
|
+
|
|
39
43
|
### Custom Schema Parser (Joi)
|
|
40
44
|
|
|
41
45
|
```typescript
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
|
|
2
|
+
import { AdapterContext, AppAdapter } from "@forinda/kickjs";
|
|
3
|
+
|
|
4
|
+
//#region src/schema-parser.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Interface for converting validation library schemas to JSON Schema.
|
|
7
|
+
*
|
|
8
|
+
* KickJS ships with a Zod parser by default. To use a different validation
|
|
9
|
+
* library (Yup, Joi, Valibot, ArkType, etc.), implement this interface and
|
|
10
|
+
* pass it to the SwaggerAdapter.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* import Joi from 'joi'
|
|
15
|
+
* import joiToJson from 'joi-to-json'
|
|
16
|
+
*
|
|
17
|
+
* const joiParser: SchemaParser = {
|
|
18
|
+
* name: 'joi',
|
|
19
|
+
* supports: (schema) => Joi.isSchema(schema),
|
|
20
|
+
* toJsonSchema: (schema) => joiToJson(schema),
|
|
21
|
+
* }
|
|
22
|
+
*
|
|
23
|
+
* new SwaggerAdapter({ schemaParser: joiParser })
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
interface SchemaParser {
|
|
27
|
+
/** Human-readable name for logging/debugging */
|
|
28
|
+
readonly name: string;
|
|
29
|
+
/**
|
|
30
|
+
* Return true if this parser can handle the given schema object.
|
|
31
|
+
* Called before `toJsonSchema` to allow graceful fallback.
|
|
32
|
+
*/
|
|
33
|
+
supports(schema: unknown): boolean;
|
|
34
|
+
/**
|
|
35
|
+
* Convert a validation schema to a JSON Schema object.
|
|
36
|
+
* Should return a plain object conforming to JSON Schema draft-07 or later.
|
|
37
|
+
* Must not include the top-level `$schema` key — the builder adds it.
|
|
38
|
+
*/
|
|
39
|
+
toJsonSchema(schema: unknown): Record<string, unknown>;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Default schema parser for Zod v4+.
|
|
43
|
+
* Uses Zod's built-in `.toJSONSchema()` instance method.
|
|
44
|
+
*/
|
|
45
|
+
declare const zodSchemaParser: SchemaParser;
|
|
46
|
+
//#endregion
|
|
47
|
+
//#region src/decorators.d.ts
|
|
48
|
+
interface ApiOperationOptions {
|
|
49
|
+
summary?: string;
|
|
50
|
+
description?: string;
|
|
51
|
+
operationId?: string;
|
|
52
|
+
deprecated?: boolean;
|
|
53
|
+
}
|
|
54
|
+
interface ApiResponseOptions {
|
|
55
|
+
status: number;
|
|
56
|
+
description?: string;
|
|
57
|
+
schema?: any;
|
|
58
|
+
/** Schema name in components/schemas (e.g., 'UserResponse', 'ErrorBody'). Auto-generated from handler name if omitted. */
|
|
59
|
+
name?: string;
|
|
60
|
+
}
|
|
61
|
+
/** Attach operation metadata to a route handler */
|
|
62
|
+
declare function ApiOperation(options: ApiOperationOptions): MethodDecorator;
|
|
63
|
+
/** Document a response status. Can be stacked multiple times. */
|
|
64
|
+
declare function ApiResponse(options: ApiResponseOptions): MethodDecorator;
|
|
65
|
+
/** Apply OpenAPI tags at class or method level */
|
|
66
|
+
declare function ApiTags(...tags: string[]): ClassDecorator & MethodDecorator;
|
|
67
|
+
/** Mark endpoint as requiring Bearer token auth */
|
|
68
|
+
declare function ApiBearerAuth(name?: string): ClassDecorator & MethodDecorator;
|
|
69
|
+
/** Exclude a controller or method from the OpenAPI spec */
|
|
70
|
+
declare function ApiExclude(): ClassDecorator & MethodDecorator;
|
|
71
|
+
//#endregion
|
|
72
|
+
//#region src/openapi-builder.d.ts
|
|
73
|
+
interface OpenAPIInfo {
|
|
74
|
+
title: string;
|
|
75
|
+
version: string;
|
|
76
|
+
description?: string;
|
|
77
|
+
}
|
|
78
|
+
interface SwaggerOptions {
|
|
79
|
+
info?: Partial<OpenAPIInfo>;
|
|
80
|
+
servers?: {
|
|
81
|
+
url: string;
|
|
82
|
+
description?: string;
|
|
83
|
+
}[];
|
|
84
|
+
bearerAuth?: boolean;
|
|
85
|
+
/**
|
|
86
|
+
* Pluggable schema parser for converting validation schemas to JSON Schema.
|
|
87
|
+
* Defaults to `zodSchemaParser` which handles Zod v4+ schemas.
|
|
88
|
+
*
|
|
89
|
+
* Override this to use Yup, Joi, Valibot, ArkType, or any other library.
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* ```ts
|
|
93
|
+
* new SwaggerAdapter({
|
|
94
|
+
* schemaParser: myYupParser,
|
|
95
|
+
* })
|
|
96
|
+
* ```
|
|
97
|
+
*/
|
|
98
|
+
schemaParser?: SchemaParser;
|
|
99
|
+
}
|
|
100
|
+
/** Register a controller for OpenAPI introspection (called by Application during route mounting) */
|
|
101
|
+
declare function registerControllerForDocs(controllerClass: any, mountPath: string): void;
|
|
102
|
+
/** Clear all registered routes (for HMR) */
|
|
103
|
+
declare function clearRegisteredRoutes(): void;
|
|
104
|
+
/** Build a full OpenAPI 3.0.3 spec from registered controllers and their decorators */
|
|
105
|
+
declare function buildOpenAPISpec(options?: SwaggerOptions): any;
|
|
106
|
+
//#endregion
|
|
107
|
+
//#region src/swagger.adapter.d.ts
|
|
108
|
+
interface SwaggerAdapterOptions extends SwaggerOptions {
|
|
109
|
+
/** Path to serve Swagger UI (default: '/docs') */
|
|
110
|
+
docsPath?: string;
|
|
111
|
+
/** Path to serve ReDoc (default: '/redoc') */
|
|
112
|
+
redocPath?: string;
|
|
113
|
+
/** Path to serve the raw JSON spec (default: '/openapi.json') */
|
|
114
|
+
specPath?: string;
|
|
115
|
+
/** Other adapters to discover (e.g., WsAdapter for WebSocket server URLs) */
|
|
116
|
+
adapters?: any[];
|
|
117
|
+
/**
|
|
118
|
+
* When true, the adapter is a no-op while `NODE_ENV === 'production'` —
|
|
119
|
+
* docs, spec, and assets are not mounted. Useful for keeping API docs
|
|
120
|
+
* out of production builds without conditionally constructing the adapter.
|
|
121
|
+
*/
|
|
122
|
+
disableInProd?: boolean;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Swagger adapter — auto-generates OpenAPI spec from decorators and serves docs.
|
|
126
|
+
*
|
|
127
|
+
* Assets are served locally from `swagger-ui-dist` (npm dependency) —
|
|
128
|
+
* no CDN required, works fully offline.
|
|
129
|
+
*
|
|
130
|
+
* @example
|
|
131
|
+
* ```ts
|
|
132
|
+
* bootstrap({
|
|
133
|
+
* modules,
|
|
134
|
+
* adapters: [
|
|
135
|
+
* new SwaggerAdapter({
|
|
136
|
+
* info: { title: 'My API', version: '1.0.0' },
|
|
137
|
+
* }),
|
|
138
|
+
* ],
|
|
139
|
+
* })
|
|
140
|
+
* ```
|
|
141
|
+
*
|
|
142
|
+
* Endpoints:
|
|
143
|
+
* GET /docs — Swagger UI (local assets, no CDN)
|
|
144
|
+
* GET /redoc — ReDoc (CDN — no local package available)
|
|
145
|
+
* GET /openapi.json — Raw OpenAPI 3.0.3 spec
|
|
146
|
+
*/
|
|
147
|
+
declare class SwaggerAdapter implements AppAdapter {
|
|
148
|
+
private readonly options;
|
|
149
|
+
name: string;
|
|
150
|
+
constructor(options?: SwaggerAdapterOptions);
|
|
151
|
+
/** Whether the adapter should skip mounting in the current environment */
|
|
152
|
+
private get disabled();
|
|
153
|
+
/** Auto-detect server URLs from the running HTTP server and peer adapters */
|
|
154
|
+
afterStart({
|
|
155
|
+
server
|
|
156
|
+
}: AdapterContext): void;
|
|
157
|
+
/** Collect controller metadata as routes are mounted */
|
|
158
|
+
onRouteMount(controllerClass: any, mountPath: string): void;
|
|
159
|
+
beforeMount({
|
|
160
|
+
app
|
|
161
|
+
}: AdapterContext): void;
|
|
162
|
+
}
|
|
163
|
+
//#endregion
|
|
164
|
+
//#region src/ui.d.ts
|
|
165
|
+
/**
|
|
166
|
+
* Generate Swagger UI HTML using local assets from swagger-ui-dist.
|
|
167
|
+
*
|
|
168
|
+
* Assets are served from `/_swagger-assets/` by the adapter's Express
|
|
169
|
+
* static middleware. Falls back to CDN if the local path is not provided.
|
|
170
|
+
* This ensures Swagger UI works fully offline in development.
|
|
171
|
+
*
|
|
172
|
+
* @param specUrl - Path to the OpenAPI JSON spec (e.g., '/openapi.json')
|
|
173
|
+
* @param title - Page title
|
|
174
|
+
* @param assetsPath - Base path for local swagger-ui-dist assets (e.g., '/_swagger-assets')
|
|
175
|
+
*/
|
|
176
|
+
declare function swaggerUIHtml(specUrl: string, title?: string, assetsPath?: string): string;
|
|
177
|
+
/**
|
|
178
|
+
* Generate ReDoc HTML.
|
|
179
|
+
*
|
|
180
|
+
* ReDoc doesn't publish a standalone npm package suitable for local serving,
|
|
181
|
+
* so it still loads from CDN. If offline support for ReDoc is needed,
|
|
182
|
+
* vendor the standalone bundle into the package's public/ directory.
|
|
183
|
+
*/
|
|
184
|
+
declare function redocHtml(specUrl: string, title?: string): string;
|
|
185
|
+
//#endregion
|
|
186
|
+
export { ApiBearerAuth, ApiExclude, ApiOperation, type ApiOperationOptions, ApiResponse, type ApiResponseOptions, ApiTags, type OpenAPIInfo, type SchemaParser, SwaggerAdapter, type SwaggerAdapterOptions, type SwaggerOptions, buildOpenAPISpec, clearRegisteredRoutes, redocHtml, registerControllerForDocs, swaggerUIHtml, zodSchemaParser };
|
|
187
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/schema-parser.ts","../src/decorators.ts","../src/openapi-builder.ts","../src/swagger.adapter.ts","../src/ui.ts"],"mappings":";;;;;;;AAqBA;;;;;;;;;;;;AAsBA;;;;;;UAtBiB,YAAA;;WAEN,IAAA;ECXyB;;;;EDiBlC,QAAA,CAAS,MAAA;ECdT;;;;AAIF;EDiBE,YAAA,CAAa,MAAA,YAAkB,MAAA;AAAA;;;;;cAOpB,eAAA,EAAiB,YAAA;;;UC/Bb,mBAAA;EACf,OAAA;EACA,WAAA;EACA,WAAA;EACA,UAAA;AAAA;AAAA,UAGe,kBAAA;EACf,MAAA;EACA,WAAA;EACA,MAAA;EDqB4B;ECnB5B,IAAA;AAAA;;iBAIc,YAAA,CAAa,OAAA,EAAS,mBAAA,GAAsB,eAAA;AAhB5D;AAAA,iBAuBgB,WAAA,CAAY,OAAA,EAAS,kBAAA,GAAqB,eAAA;;iBAY1C,OAAA,CAAA,GAAW,IAAA,aAAiB,cAAA,GAAiB,eAAA;;iBAW7C,aAAA,CAAc,IAAA,YAAsB,cAAA,GAAiB,eAAA;;iBAWrD,UAAA,CAAA,GAAc,cAAA,GAAiB,eAAA;;;UCxD9B,WAAA;EACf,KAAA;EACA,OAAA;EACA,WAAA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