Skip to content

Commit 48b7414

Browse files
committed
docs: exported symbol + 2.1.4
1 parent 734d60b commit 48b7414

File tree

3 files changed

+133
-1
lines changed

3 files changed

+133
-1
lines changed

decorators.ts

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,18 @@ type DecoratorFunction = (
99
descriptor?: PropertyDescriptor,
1010
) => void;
1111

12+
/**
13+
* Metadata key to store the property schema
14+
*/
1215
export const API_PROPERTY = 'api-property';
1316

17+
18+
/**
19+
* Decorator to define API property metadata for a class property.
20+
*
21+
* @param property - Optional Swagger schema to define the property metadata.
22+
* @returns A decorator function that sets the API property metadata.
23+
*/
1424
export function ApiProperty(property?: Swagger.Schema): DecoratorFunction {
1525
return (
1626
// deno-lint-ignore ban-types
@@ -22,8 +32,23 @@ export function ApiProperty(property?: Swagger.Schema): DecoratorFunction {
2232
};
2333
}
2434

35+
36+
/**
37+
* Metadata key to mark a property as optional
38+
*/
2539
export const OPTIONAL_KEY = 'optional';
2640

41+
/**
42+
* Decorator that marks a property or method as optional.
43+
*
44+
* @example
45+
* ```typescript
46+
* class Example {
47+
* @Optional()
48+
* optionalProperty?: string;
49+
* }
50+
* ```
51+
*/
2752
export function Optional(): DecoratorFunction {
2853
return (
2954
// deno-lint-ignore ban-types
@@ -35,8 +60,17 @@ export function Optional(): DecoratorFunction {
3560
};
3661
}
3762

63+
/**
64+
* Metadata key to store the returned type of a method
65+
*/
3866
export const RETURNED_TYPE_KEY = 'returntype';
3967

68+
/**
69+
* Decorator to set metadata for the returned type of a method.
70+
*
71+
* @param returnedType - The type of the value that the method or property returns.
72+
* @param isArray - Optional boolean indicating if the returned type is an array.
73+
*/
4074
export function ReturnedType(returnedType: unknown, isArray?: boolean): DecoratorFunction {
4175
return (
4276
target: Object,
@@ -55,6 +89,11 @@ export function ReturnedType(returnedType: unknown, isArray?: boolean): Decorato
5589
};
5690
}
5791

92+
/**
93+
* Decorator to indicate the body type of a request.
94+
*
95+
* @param type - The constructor of the type to be used as the body type.
96+
*/
5897
export function BodyType(type: Constructor): DecoratorFunction {
5998
return (
6099
target: Object,
@@ -65,6 +104,11 @@ export function BodyType(type: Constructor): DecoratorFunction {
65104
};
66105
}
67106

107+
/**
108+
* Decorator to indicate the query type.
109+
*
110+
* @param type - The constructor function of the type to be set as metadata.
111+
*/
68112
export function QueryType(type: Constructor) : DecoratorFunction {
69113
return (
70114
target: Object,
@@ -75,8 +119,29 @@ return (
75119
};
76120
}
77121

122+
/**
123+
* A constant key used to store or retrieve tags metadata.
124+
* This key is typically used in decorators to annotate
125+
* classes or methods with specific tags for documentation.
126+
*/
78127
export const TAGS_KEY = 'tags';
79128

129+
/**
130+
* A decorator function to add an openAPI tag to a class or a method.
131+
*
132+
* @param tagName - The name of the tag to be added.
133+
*
134+
* @example
135+
* ```typescript
136+
* @Tag('exampleTag')
137+
* class ExampleClass {
138+
* @Tag('methodTag')
139+
* exampleMethod() {
140+
* // method implementation
141+
* }
142+
* }
143+
* ```
144+
*/
80145
export function Tag(tagName: string) : DecoratorFunction {
81146
return (
82147
target: Object,
@@ -91,14 +156,52 @@ return (
91156
};
92157
}
93158

159+
/**
160+
* A constant key used to store or retrieve api-security metadata.
161+
*/
94162
export const API_SECURITY = 'api-security';
163+
164+
/**
165+
* A constant key used to store or retrieve api-security data metadata.
166+
*/
95167
export const API_SECURITY_DATA = 'api-security-data';
96168

169+
/**
170+
* Indicate that the endpoint use basic authentication security.
171+
*
172+
* This function is a shorthand for applying the 'basic' security scheme using the `ApiSecurity` decorator.
173+
*
174+
*/
97175
export function ApiBasicAuth(): DecoratorFunction { return ApiSecurity('basic') }
176+
/**
177+
* Indicate that the endpoint use api bearer auth security.
178+
*
179+
* This function is a shorthand for applying the 'bearer' security scheme using the `ApiSecurity` decorator.
180+
*
181+
*/
98182
export function ApiBearerAuth(): DecoratorFunction { return ApiSecurity('bearer') }
183+
/**
184+
* Indicate that the endpoint use cookie security.
185+
*
186+
* This function is a shorthand for applying the 'cookie' security scheme using the `ApiSecurity` decorator.
187+
*
188+
*/
99189
export function ApiCookieAuth(): DecoratorFunction { return ApiSecurity('cookie') }
190+
/**
191+
* Indicate that the endpoint use oauth2 security.
192+
*
193+
* This function is a shorthand for applying the 'oauth2' security scheme using the `ApiSecurity` decorator.
194+
*
195+
*/
100196
export function ApiOAuth2(data: string[]): DecoratorFunction { return ApiSecurity('oauth2', data) }
101197

198+
/**
199+
* Decorator that indicate that an endpoint use a security mechanism.
200+
*
201+
* @param name - The name of the security scheme.
202+
* @param data - An optional array of strings representing the security requirements.
203+
*
204+
*/
102205
export function ApiSecurity(name: string, data: string[] = []) : DecoratorFunction {
103206
return (
104207
// deno-lint-ignore ban-types

deno.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@danet/swagger",
3-
"version": "2.1.3",
3+
"version": "2.1.4",
44
"exports": {
55
".":"./mod.ts",
66
"./decorators": "./decorators.ts"

mod.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,24 @@ import Schema = Swagger.Schema;
1111
import Path = Swagger.Path;
1212
import { MethodDefiner } from './method-definer.ts';
1313

14+
/**
15+
* Generic constructor type
16+
*/
1417
export type Constructor<T = unknown> = new (...args: any[]) => T;
1518

19+
/**
20+
* Responsible for generating and setting up Swagger documentation
21+
* for a Danet application. It provides methods to create a Swagger document based on the application's
22+
* modules and controllers, and to set up routes to serve the Swagger UI and JSON specification.
23+
*/
1624
export class SwaggerModule {
25+
/**
26+
* Creates a Swagger document for the given Danet application.
27+
*
28+
* @param app - The Danet application instance.
29+
* @param spec - The initial Swagger specification object.
30+
* @returns A promise that resolves to the updated Swagger specification.
31+
*/
1732
static async createDocument(app: DanetApplication, spec: Swagger.Spec): Promise<Swagger.Spec> {
1833
const definition = await this.generateModuleDefinition(app.entryModule);
1934
spec.paths = definition.paths;
@@ -76,6 +91,20 @@ export class SwaggerModule {
7691
return { paths, schemas };
7792
}
7893

94+
/**
95+
* Sets up the API documentation routes for the given Danet application.
96+
*
97+
* @param apiPath - The base path for the API documentation.
98+
* @param app - The Danet application instance.
99+
* @param document - The Swagger specification document.
100+
*
101+
* This method registers two routes:
102+
* 1. `GET /{apiPath}`: Serves an HTML page displaying the API documentation.
103+
* 2. `GET /{apiPath}/json`: Serves the raw Swagger specification document in JSON format.
104+
*
105+
* The HTML page includes a script tag that loads the Swagger specification and a script from
106+
* a CDN to render the API documentation.
107+
*/
79108
static async setup(
80109
apiPath: string,
81110
app: DanetApplication,

0 commit comments

Comments
 (0)