-
Notifications
You must be signed in to change notification settings - Fork 474
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2459 from nestjs/feat/readonly-visitors
feat(plugin): introduce readonly visitors
- Loading branch information
Showing
19 changed files
with
663 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,54 @@ | ||
import { Type } from '@nestjs/common'; | ||
import { DECORATORS } from '../constants'; | ||
import { ApiOperation } from '../decorators/api-operation.decorator'; | ||
import { METADATA_FACTORY_NAME } from '../plugin/plugin-constants'; | ||
|
||
export const exploreApiOperationMetadata = ( | ||
instance: object, | ||
prototype: Type<unknown>, | ||
method: object | ||
) => Reflect.getMetadata(DECORATORS.API_OPERATION, method); | ||
) => { | ||
applyMetadataFactory(prototype); | ||
return Reflect.getMetadata(DECORATORS.API_OPERATION, method); | ||
}; | ||
|
||
function applyMetadataFactory(prototype: Type<unknown>) { | ||
const classPrototype = prototype; | ||
do { | ||
if (!prototype.constructor) { | ||
return; | ||
} | ||
if (!prototype.constructor[METADATA_FACTORY_NAME]) { | ||
continue; | ||
} | ||
const metadata = prototype.constructor[METADATA_FACTORY_NAME](); | ||
const methodKeys = Object.keys(metadata); | ||
methodKeys.forEach((key) => { | ||
const operationMeta = {}; | ||
const { summary, deprecated, tags } = metadata[key]; | ||
|
||
applyIfNotNil(operationMeta, 'summary', summary); | ||
applyIfNotNil(operationMeta, 'deprecated', deprecated); | ||
applyIfNotNil(operationMeta, 'tags', tags); | ||
|
||
if (Object.keys(operationMeta).length === 0) { | ||
return; | ||
} | ||
ApiOperation(operationMeta, { overrideExisting: false })( | ||
classPrototype, | ||
key, | ||
Object.getOwnPropertyDescriptor(classPrototype, key) | ||
); | ||
}); | ||
} while ( | ||
(prototype = Reflect.getPrototypeOf(prototype) as Type<any>) && | ||
prototype !== Object.prototype && | ||
prototype | ||
); | ||
} | ||
|
||
function applyIfNotNil(target: Record<string, any>, key: string, value: any) { | ||
if (value !== undefined && value !== null) { | ||
target[key] = value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './compiler-plugin'; | ||
export * from './visitors/readonly.visitor'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.