-
-
Notifications
You must be signed in to change notification settings - Fork 96
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 #548 from samchon/features/TypedException
`@TypedException()` decorator for swagger documents
- Loading branch information
Showing
56 changed files
with
2,268 additions
and
153 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import "reflect-metadata"; | ||
|
||
/** | ||
* > You must configure the generic argument `T` | ||
* | ||
* Exception decorator. | ||
* | ||
* `TypedException` is a decorator function describing HTTP exception and its type | ||
* which could be occured in the method. | ||
* | ||
* For reference, this decorator function does not affect to the method's behavior, | ||
* but only affects to the swagger documents generation. Also, it does not affect to | ||
* the SDK library generation yet, but will be used in the future. | ||
* | ||
* @param status Status number or pattern like "2XX", "3XX", "4XX", "5XX" | ||
* @param description Description about the exception | ||
* @returns Method decorator | ||
* | ||
* @author Jeongho Nam - https://github.com/samchon | ||
* @deprecated | ||
*/ | ||
export function TypedException( | ||
status: number | "2XX" | "3XX" | "4XX" | "5XX", | ||
description?: string | undefined, | ||
): never; | ||
|
||
/** | ||
* Exception decorator. | ||
* | ||
* `TypedException` is a decorator function describing HTTP exception and its type | ||
* which could be occured in the method. | ||
* | ||
* For reference, this decorator function does not affect to the method's behavior, | ||
* but only affects to the swagger documents generation. Also, it does not affect to | ||
* the SDK library generation yet, but will be used in the future. | ||
* | ||
* @template T Type of the exception | ||
* @param status Status number or pattern like "2XX", "3XX", "4XX", "5XX" | ||
* @param description Description about the exception | ||
* @returns Method decorator | ||
* | ||
* @author Jeongho Nam - https://github.com/samchon | ||
*/ | ||
export function TypedException<T extends object>( | ||
status: number | "2XX" | "3XX" | "4XX" | "5XX", | ||
description?: string | undefined, | ||
): MethodDecorator; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export function TypedException<T extends object>( | ||
status: number | "2XX" | "3XX" | "4XX" | "5XX", | ||
description?: string | undefined, | ||
type?: string | undefined, | ||
): MethodDecorator { | ||
return function TypedException( | ||
target: Object | T, | ||
propertyKey: string | symbol, | ||
descriptor: TypedPropertyDescriptor<any>, | ||
) { | ||
const array: IProps[] = (() => { | ||
const oldbie: IProps[] | undefined = Reflect.getMetadata( | ||
`swagger/TypedException`, | ||
(target as any)[propertyKey], | ||
); | ||
if (oldbie !== undefined) return oldbie; | ||
|
||
const newbie: IProps[] = []; | ||
Reflect.defineMetadata( | ||
`swagger/TypedException`, | ||
newbie, | ||
(target as any)[propertyKey], | ||
); | ||
return newbie; | ||
})(); | ||
array.push({ | ||
status, | ||
description, | ||
type: type!, | ||
}); | ||
return descriptor; | ||
}; | ||
} | ||
|
||
interface IProps { | ||
status: number | "2XX" | "3XX" | "4XX" | "5XX"; | ||
description?: string | undefined; | ||
type: string; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import ts from "typescript"; | ||
|
||
import { INestiaTransformProject } from "../options/INestiaTransformProject"; | ||
|
||
export namespace TypedExceptionProgrammer { | ||
export const generate = | ||
({ checker }: INestiaTransformProject) => | ||
(expression: ts.CallExpression): ts.CallExpression => { | ||
// CHECK GENERIC ARGUMENT EXISTENCE | ||
if (!expression.typeArguments?.[0]) throw new Error(NOT_SPECIFIED); | ||
|
||
// GET TYPE INFO | ||
const node: ts.TypeNode = expression.typeArguments[0]; | ||
const type: ts.Type = checker.getTypeFromTypeNode(node); | ||
|
||
if (type.isTypeParameter()) throw new Error(NO_GENERIC_ARGUMENT); | ||
|
||
// CHECK DUPLICATED TRNASFORMATION | ||
if (expression.arguments.length === 3) return expression; | ||
|
||
// DO TRANSFORM | ||
const name: string = node.getFullText().trim(); | ||
return ts.factory.updateCallExpression( | ||
expression, | ||
expression.expression, | ||
expression.typeArguments, | ||
[ | ||
expression.arguments[0], | ||
expression.arguments[1] ?? | ||
ts.factory.createIdentifier("undefined"), | ||
ts.factory.createStringLiteral(name), | ||
], | ||
); | ||
}; | ||
} | ||
|
||
const NOT_SPECIFIED = | ||
"Error on @nestia.core.TypedException(): generic argument is not specified."; | ||
const NO_GENERIC_ARGUMENT = | ||
"Error on @nestia.core.TypedException(): non-specified generic argument."; |
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
51 changes: 51 additions & 0 deletions
51
packages/core/src/transformers/TypedExceptionTransformer.ts
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import path from "path"; | ||
import ts from "typescript"; | ||
|
||
import { INestiaTransformProject } from "../options/INestiaTransformProject"; | ||
import { TypedExceptionProgrammer } from "../programmers/TypedExceptionProgrammer"; | ||
|
||
export namespace TypedExceptionTransformer { | ||
export const transform = | ||
(project: INestiaTransformProject) => | ||
(decorator: ts.Decorator): ts.Decorator => { | ||
if (!ts.isCallExpression(decorator.expression)) return decorator; | ||
|
||
// CHECK SIGNATURE | ||
const signature: ts.Signature | undefined = | ||
project.checker.getResolvedSignature(decorator.expression); | ||
if (!signature || !signature.declaration) return decorator; | ||
|
||
// CHECK TO BE TRANSFORMED | ||
const done: boolean = (() => { | ||
// CHECK FILENAME | ||
const location: string = path.resolve( | ||
signature.declaration.getSourceFile().fileName, | ||
); | ||
if (location.indexOf(LIB_PATH) === -1 && location !== SRC_PATH) | ||
return false; | ||
|
||
// CHECK DUPLICATED | ||
return decorator.expression.arguments.length !== 3; | ||
})(); | ||
if (done === false) return decorator; | ||
|
||
// DO TRANSFORM | ||
return ts.factory.createDecorator( | ||
TypedExceptionProgrammer.generate(project)( | ||
decorator.expression, | ||
), | ||
); | ||
}; | ||
|
||
const LIB_PATH = path.join( | ||
"node_modules", | ||
"@nestia", | ||
"core", | ||
"lib", | ||
"decorators", | ||
`TypedException.d.ts`, | ||
); | ||
const SRC_PATH = path.resolve( | ||
path.join(__dirname, "..", "decorators", `TypedException.ts`), | ||
); | ||
} |
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 |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
"openapi": "3.0.0", | ||
"info": { | ||
"title": "Fireblocks API", | ||
"version": "1.6.3", | ||
"version": "1.0.0", | ||
"contact": { | ||
"email": "[email protected]" | ||
} | ||
|
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.