@moostjs/swagger 0.5.33 → 0.6.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 CHANGED
@@ -1 +1,80 @@
1
1
  # @moostjs/swagger
2
+
3
+ Swagger/OpenAPI integration for [Moost](https://moost.org). Automatically generates an OpenAPI spec from your controllers and serves the Swagger UI. Provides decorators for adding tags, descriptions, response schemas, and request body definitions.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @moostjs/swagger swagger-ui-dist
9
+ ```
10
+
11
+ ## Quick Start
12
+
13
+ ```ts
14
+ import { Moost } from 'moost'
15
+ import { MoostHttp } from '@moostjs/event-http'
16
+ import { SwaggerController } from '@moostjs/swagger'
17
+
18
+ const app = new Moost()
19
+ const http = new MoostHttp()
20
+
21
+ app.adapter(http).controllers(SwaggerController).init()
22
+
23
+ http.listen(3000)
24
+ // Swagger UI available at http://localhost:3000/api-docs/
25
+ // JSON spec at http://localhost:3000/api-docs/spec.json
26
+ // YAML spec at http://localhost:3000/api-docs/spec.yaml
27
+ ```
28
+
29
+ ## Decorators
30
+
31
+ - `@SwaggerTag(tag)` — Add an OpenAPI tag to a controller or handler. Tags used on endpoints are auto-collected into the top-level `tags` array. Provide `tags` in options to add descriptions or control ordering.
32
+ - `@SwaggerDescription(text)` — Set a description for a handler.
33
+ - `@SwaggerResponse(opts)` / `@SwaggerResponse(code, opts)` — Define response schemas. Supports optional `description` to document the status code; when omitted, a standard HTTP reason phrase is used automatically.
34
+ - `@SwaggerRequestBody(opts)` — Define request body schema.
35
+ - `@SwaggerParam(opts)` — Define a parameter.
36
+ - `@SwaggerExample(example)` — Attach an example value.
37
+ - `@SwaggerOperationId(id)` — Override the auto-generated operationId. Falls back to `@Id()` from moost core, then to the auto-generated value.
38
+ - `@SwaggerDeprecated()` — Mark a controller or handler as deprecated.
39
+ - `@SwaggerExclude()` — Exclude a controller or handler from the spec.
40
+ - `@SwaggerLink(name, opts)` / `@SwaggerLink(code, name, opts)` — Add an OpenAPI link to a response, describing how response values feed into another operation. Supports `operationId`, `operationRef`, or type-safe `handler: [Class, 'method']` references.
41
+ - `@SwaggerCallback(name, opts)` — Document an OpenAPI callback (webhook) on an operation. Describes a request your server sends to a client-provided URL.
42
+ - `@SwaggerPublic()` — Mark an endpoint or controller as public (no auth required in docs).
43
+ - `@SwaggerSecurity(schemeName, scopes?)` — Explicitly set a security requirement.
44
+ - `@SwaggerSecurityAll(requirement)` — Set security with AND semantics.
45
+
46
+ ## Return Type Inference
47
+
48
+ When no `@SwaggerResponse` is declared for the success status code, the generator falls back to the method's return type (via `emitDecoratorMetadata`). This works for simple class types like Atscript-generated DTOs but has limitations: `Promise<T>`, type aliases (`type X = Y[]`), and other generics lose their type arguments at emit time. For reliable response documentation — especially with async handlers — always use `@SwaggerResponse` explicitly.
49
+
50
+ ## Security Schemes
51
+
52
+ Security schemes are auto-discovered from `@Authenticate()` guards (provided by `@moostjs/event-http`). All four OpenAPI transport types are supported: bearer, basic, apiKey, and cookie.
53
+
54
+ ```ts
55
+ import { Authenticate, defineAuthGuard } from '@moostjs/event-http'
56
+ import { SwaggerPublic } from '@moostjs/swagger'
57
+
58
+ const jwtGuard = defineAuthGuard({ bearer: { format: 'JWT' } }, (transports) => {
59
+ // verify transports.bearer token
60
+ })
61
+
62
+ @Authenticate(jwtGuard)
63
+ @Controller('users')
64
+ class UsersController {
65
+ @SwaggerPublic() // overrides controller auth for this endpoint
66
+ @Get()
67
+ list() { ... }
68
+
69
+ @Get(':id')
70
+ find(@Param('id') id: string) { ... } // inherits bearer auth from controller
71
+ }
72
+ ```
73
+
74
+ The generated spec will include `components.securitySchemes` with the auto-discovered schemes and per-operation `security` arrays. Use `@SwaggerPublic()` to mark endpoints as public or `@SwaggerSecurity()` for explicit overrides.
75
+
76
+ ## [Official Documentation](https://moost.org/swagger/)
77
+
78
+ ## License
79
+
80
+ MIT