Skip to content

Commit

Permalink
docs: exported symbol + 2.1.4
Browse files Browse the repository at this point in the history
  • Loading branch information
Sorikairox committed Oct 3, 2024
1 parent 734d60b commit 48b7414
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 1 deletion.
103 changes: 103 additions & 0 deletions decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,18 @@ type DecoratorFunction = (
descriptor?: PropertyDescriptor,
) => void;

/**
* Metadata key to store the property schema
*/
export const API_PROPERTY = 'api-property';


/**
* Decorator to define API property metadata for a class property.
*
* @param property - Optional Swagger schema to define the property metadata.
* @returns A decorator function that sets the API property metadata.
*/
export function ApiProperty(property?: Swagger.Schema): DecoratorFunction {
return (
// deno-lint-ignore ban-types
Expand All @@ -22,8 +32,23 @@ export function ApiProperty(property?: Swagger.Schema): DecoratorFunction {
};
}


/**
* Metadata key to mark a property as optional
*/
export const OPTIONAL_KEY = 'optional';

/**
* Decorator that marks a property or method as optional.
*
* @example
* ```typescript
* class Example {
* @Optional()
* optionalProperty?: string;
* }
* ```
*/
export function Optional(): DecoratorFunction {
return (
// deno-lint-ignore ban-types
Expand All @@ -35,8 +60,17 @@ export function Optional(): DecoratorFunction {
};
}

/**
* Metadata key to store the returned type of a method
*/
export const RETURNED_TYPE_KEY = 'returntype';

/**
* Decorator to set metadata for the returned type of a method.
*
* @param returnedType - The type of the value that the method or property returns.
* @param isArray - Optional boolean indicating if the returned type is an array.
*/
export function ReturnedType(returnedType: unknown, isArray?: boolean): DecoratorFunction {
return (
target: Object,
Expand All @@ -55,6 +89,11 @@ export function ReturnedType(returnedType: unknown, isArray?: boolean): Decorato
};
}

/**
* Decorator to indicate the body type of a request.
*
* @param type - The constructor of the type to be used as the body type.
*/
export function BodyType(type: Constructor): DecoratorFunction {
return (
target: Object,
Expand All @@ -65,6 +104,11 @@ export function BodyType(type: Constructor): DecoratorFunction {
};
}

/**
* Decorator to indicate the query type.
*
* @param type - The constructor function of the type to be set as metadata.
*/
export function QueryType(type: Constructor) : DecoratorFunction {
return (
target: Object,
Expand All @@ -75,8 +119,29 @@ return (
};
}

/**
* A constant key used to store or retrieve tags metadata.
* This key is typically used in decorators to annotate
* classes or methods with specific tags for documentation.
*/
export const TAGS_KEY = 'tags';

/**
* A decorator function to add an openAPI tag to a class or a method.
*
* @param tagName - The name of the tag to be added.
*
* @example
* ```typescript
* @Tag('exampleTag')
* class ExampleClass {
* @Tag('methodTag')
* exampleMethod() {
* // method implementation
* }
* }
* ```
*/
export function Tag(tagName: string) : DecoratorFunction {
return (
target: Object,
Expand All @@ -91,14 +156,52 @@ return (
};
}

/**
* A constant key used to store or retrieve api-security metadata.
*/
export const API_SECURITY = 'api-security';

/**
* A constant key used to store or retrieve api-security data metadata.
*/
export const API_SECURITY_DATA = 'api-security-data';

/**
* Indicate that the endpoint use basic authentication security.
*
* This function is a shorthand for applying the 'basic' security scheme using the `ApiSecurity` decorator.
*
*/
export function ApiBasicAuth(): DecoratorFunction { return ApiSecurity('basic') }
/**
* Indicate that the endpoint use api bearer auth security.
*
* This function is a shorthand for applying the 'bearer' security scheme using the `ApiSecurity` decorator.
*
*/
export function ApiBearerAuth(): DecoratorFunction { return ApiSecurity('bearer') }
/**
* Indicate that the endpoint use cookie security.
*
* This function is a shorthand for applying the 'cookie' security scheme using the `ApiSecurity` decorator.
*
*/
export function ApiCookieAuth(): DecoratorFunction { return ApiSecurity('cookie') }
/**
* Indicate that the endpoint use oauth2 security.
*
* This function is a shorthand for applying the 'oauth2' security scheme using the `ApiSecurity` decorator.
*
*/
export function ApiOAuth2(data: string[]): DecoratorFunction { return ApiSecurity('oauth2', data) }

/**
* Decorator that indicate that an endpoint use a security mechanism.
*
* @param name - The name of the security scheme.
* @param data - An optional array of strings representing the security requirements.
*
*/
export function ApiSecurity(name: string, data: string[] = []) : DecoratorFunction {
return (
// deno-lint-ignore ban-types
Expand Down
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@danet/swagger",
"version": "2.1.3",
"version": "2.1.4",
"exports": {
".":"./mod.ts",
"./decorators": "./decorators.ts"
Expand Down
29 changes: 29 additions & 0 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,24 @@ import Schema = Swagger.Schema;
import Path = Swagger.Path;
import { MethodDefiner } from './method-definer.ts';

/**
* Generic constructor type
*/
export type Constructor<T = unknown> = new (...args: any[]) => T;

/**
* Responsible for generating and setting up Swagger documentation
* for a Danet application. It provides methods to create a Swagger document based on the application's
* modules and controllers, and to set up routes to serve the Swagger UI and JSON specification.
*/
export class SwaggerModule {
/**
* Creates a Swagger document for the given Danet application.
*
* @param app - The Danet application instance.
* @param spec - The initial Swagger specification object.
* @returns A promise that resolves to the updated Swagger specification.
*/
static async createDocument(app: DanetApplication, spec: Swagger.Spec): Promise<Swagger.Spec> {
const definition = await this.generateModuleDefinition(app.entryModule);
spec.paths = definition.paths;
Expand Down Expand Up @@ -76,6 +91,20 @@ export class SwaggerModule {
return { paths, schemas };
}

/**
* Sets up the API documentation routes for the given Danet application.
*
* @param apiPath - The base path for the API documentation.
* @param app - The Danet application instance.
* @param document - The Swagger specification document.
*
* This method registers two routes:
* 1. `GET /{apiPath}`: Serves an HTML page displaying the API documentation.
* 2. `GET /{apiPath}/json`: Serves the raw Swagger specification document in JSON format.
*
* The HTML page includes a script tag that loads the Swagger specification and a script from
* a CDN to render the API documentation.
*/
static async setup(
apiPath: string,
app: DanetApplication,
Expand Down

0 comments on commit 48b7414

Please sign in to comment.