-
-
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.
- Loading branch information
Showing
7 changed files
with
149 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import "reflect-metadata"; | ||
|
||
export function TypedException( | ||
status: number, | ||
description?: string | undefined, | ||
): never; | ||
|
||
export function TypedException<T extends object>( | ||
status: number, | ||
description?: string | undefined, | ||
): MethodDecorator; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export function TypedException<T extends object>( | ||
status: number, | ||
description?: string | undefined, | ||
type?: string | undefined, | ||
): MethodDecorator { | ||
return function TypedException( | ||
target: Object, | ||
propertyKey: string | symbol, | ||
descriptor: TypedPropertyDescriptor<any>, | ||
) { | ||
Reflect.defineMetadata( | ||
`swagger/TypedException`, | ||
{ | ||
status, | ||
description, | ||
type, | ||
}, | ||
(target as any)[propertyKey], | ||
); | ||
return descriptor; | ||
}; | ||
} | ||
export namespace TypedException { | ||
export interface IProps { | ||
status: number; | ||
description?: string | undefined; | ||
/** | ||
* @internal | ||
*/ | ||
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