Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ npm-debug.log
dist
index.js
index.d.ts
/sample
/sample
1 change: 1 addition & 0 deletions lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const DECORATORS = {
API_MODEL_PROPERTIES_ARRAY: `${DECORATORS_PREFIX}/apiModelPropertiesArray`,
API_SECURITY: `${DECORATORS_PREFIX}/apiSecurity`,
API_EXCLUDE_ENDPOINT: `${DECORATORS_PREFIX}/apiExcludeEndpoint`,
API_INCLUDE_ENDPOINT: `${DECORATORS_PREFIX}/apiIncludeEndpoint`,
API_EXCLUDE_CONTROLLER: `${DECORATORS_PREFIX}/apiExcludeController`,
API_EXTRA_MODELS: `${DECORATORS_PREFIX}/apiExtraModels`,
API_EXTENSION: `${DECORATORS_PREFIX}/apiExtension`,
Expand Down
11 changes: 11 additions & 0 deletions lib/decorators/api-include-endpoint.decorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { DECORATORS } from '../constants';
import { createMethodDecorator } from './helpers';

/**
* @publicApi
*/
export function ApiIncludeEndpoint(disable = true): MethodDecorator {
return createMethodDecorator(DECORATORS.API_INCLUDE_ENDPOINT, {
disable
});
}
1 change: 1 addition & 0 deletions lib/decorators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export * from './api-consumes.decorator';
export * from './api-cookie.decorator';
export * from './api-default-getter.decorator';
export * from './api-exclude-endpoint.decorator';
export * from './api-include-endpoint.decorator';
export * from './api-exclude-controller.decorator';
export * from './api-extra-models.decorator';
export * from './api-header.decorator';
Expand Down
8 changes: 8 additions & 0 deletions lib/explorers/api-include-endpoint.explorer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Type } from '@nestjs/common';
import { DECORATORS } from '../constants';

export const exploreApiIncludeEndpointMetadata = (
instance: object,
prototype: Type<unknown>,
method: object
) => Reflect.getMetadata(DECORATORS.API_INCLUDE_ENDPOINT, method);
3 changes: 3 additions & 0 deletions lib/extra/swagger-shim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ export function ApiCookieAuth() {
export function ApiExcludeEndpoint() {
return () => {};
}
export function ApiIncludeEndpoint() {
return () => {};
}
export function ApiExcludeController() {
return () => {};
}
Expand Down
6 changes: 6 additions & 0 deletions lib/interfaces/swagger-document-options.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,10 @@ export interface SwaggerDocumentOptions {
* @default true
*/
autoTagControllers?: boolean;

/**
* If `true`, swagger will only include routes that are decorated with the `@ApiIncludeEndpoint()` decorator
* @default false
*/
onlyIncludeDecoratedEndpoints?: boolean;
}
11 changes: 11 additions & 0 deletions lib/swagger-explorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { DECORATORS } from './constants';
import { exploreApiCallbacksMetadata } from './explorers/api-callbacks.explorer';
import { exploreApiExcludeControllerMetadata } from './explorers/api-exclude-controller.explorer';
import { exploreApiExcludeEndpointMetadata } from './explorers/api-exclude-endpoint.explorer';
import { exploreApiIncludeEndpointMetadata } from './explorers/api-include-endpoint.explorer';
import {
exploreApiExtraModelsMetadata,
exploreGlobalApiExtraModelsMetadata
Expand Down Expand Up @@ -113,6 +114,7 @@ export class SwaggerExplorer {
fieldKey: string
) => string;
autoTagControllers?: boolean;
onlyIncludeDecoratedEndpoints?: boolean;
}
) {
const { operationIdFactory, linkNameFactory } = options;
Expand Down Expand Up @@ -167,6 +169,7 @@ export class SwaggerExplorer {
modulePath?: string;
globalPrefix?: string;
autoTagControllers?: boolean;
onlyIncludeDecoratedEndpoints?: boolean;
}
): DenormalizedDoc[] {
// eslint-disable-next-line @typescript-eslint/no-this-alias
Expand All @@ -187,6 +190,14 @@ export class SwaggerExplorer {
DenormalizedDoc[]
>(instance, prototype, (name) => {
const targetCallback = prototype[name];
const includeEndpoint = exploreApiIncludeEndpointMetadata(
instance,
prototype,
targetCallback
);
if (options.onlyIncludeDecoratedEndpoints && !includeEndpoint) {
return;
}
const excludeEndpoint = exploreApiExcludeEndpointMetadata(
instance,
prototype,
Expand Down
10 changes: 7 additions & 3 deletions lib/swagger-scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ export class SwaggerScanner {
ignoreGlobalPrefix = false,
operationIdFactory,
linkNameFactory,
autoTagControllers = true
autoTagControllers = true,
onlyIncludeDecoratedEndpoints = false
} = options;

const untypedApp = app as any;
Expand Down Expand Up @@ -80,7 +81,8 @@ export class SwaggerScanner {
globalPrefix,
operationIdFactory,
linkNameFactory,
autoTagControllers
autoTagControllers,
onlyIncludeDecoratedEndpoints
})
);
});
Expand All @@ -92,7 +94,8 @@ export class SwaggerScanner {
globalPrefix,
operationIdFactory,
linkNameFactory,
autoTagControllers
autoTagControllers,
onlyIncludeDecoratedEndpoints
})
);
}
Expand Down Expand Up @@ -122,6 +125,7 @@ export class SwaggerScanner {
fieldKey: string
) => string;
autoTagControllers?: boolean;
onlyIncludeDecoratedEndpoints?: boolean;
}
): ModuleRoute[] {
const denormalizedArray = [...controller.values()].map((ctrl) =>
Expand Down