Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(schema): fix typing for operation decorators #2973

Merged
merged 1 commit into from
Jan 25, 2025
Merged
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 packages/specs/schema/src/decorators/generics/generics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {JsonEntityStore} from "../../domain/JsonEntityStore.js";
* @input
* @generics
*/
export function Generics(...generics: string[]) {
export function Generics(...generics: string[]): ClassDecorator {
return (target: any) => {
const storedSchema = JsonEntityStore.from(target);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ import {Produces} from "./produces.js";
* @operation
* @response
*/
export function AcceptMime(...mimes: string[]): Function {
export function AcceptMime(...mimes: string[]): ClassDecorator & MethodDecorator {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick (assertive)

LGTM! Consider adding @methodDecorator to JSDoc.

The return type correctly allows both class and method usage. Consider adding @methodDecorator to the JSDoc tags to match the implementation.

  * @decorator
  * @operation
  * @response
+ * @methodDecorator
  */

Committable suggestion skipped: line range outside the PR's diff.

return useDecorators(Produces(...mimes), StoreSet("acceptMimes", mimes));
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ describe("Consumes", () => {

let actualError: any;
try {
// @ts-ignore
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick (assertive)

Consider adding type annotation for better clarity.

The test case effectively validates the error handling. Consider adding a type annotation to make the intention clearer:

-// @ts-ignore
+// @ts-ignore - Testing invalid decorator usage on parameter
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// @ts-ignore
// @ts-ignore - Testing invalid decorator usage on parameter

Consumes("text/json")(Test.prototype, "test", 0);
} catch (er) {
actualError = er;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {JsonEntityFn} from "../common/jsonEntityFn.js";
* @classDecorator
* @operation
*/
export function Consumes(...consumes: string[]) {
export function Consumes(...consumes: string[]): ClassDecorator & MethodDecorator {
return JsonEntityFn((store, args) => {
switch (store.decoratorType) {
case DecoratorTypes.METHOD:
Expand Down
2 changes: 1 addition & 1 deletion packages/specs/schema/src/decorators/operations/header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ import {Returns} from "./returns.js";
* @operation
* @response
*/
export function Header(headers: string | number | JsonHeaders, value?: string | number | JsonHeader): Function {
export function Header(headers: string | number | JsonHeaders, value?: string | number | JsonHeader) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add explicit decorator return type.

While removing the generic Function return type is good, we should specify the exact decorator type for consistency with other decorators in the codebase.

-export function Header(headers: string | number | JsonHeaders, value?: string | number | JsonHeader) {
+export function Header(headers: string | number | JsonHeaders, value?: string | number | JsonHeader): MethodDecorator {

The MethodDecorator type is appropriate here as this decorator is only used on methods, as shown in the examples in the JSDoc.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
export function Header(headers: string | number | JsonHeaders, value?: string | number | JsonHeader) {
export function Header(headers: string | number | JsonHeaders, value?: string | number | JsonHeader): MethodDecorator {

if (value !== undefined) {
headers = {[headers as string]: value};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ describe("OperationId", () => {
it("should throw error for unsupported usage", () => {
let actualError: any;
try {
// @ts-ignore
OperationId("id")(class Test {});
} catch (er) {
actualError = er;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {JsonEntityFn} from "../common/jsonEntityFn.js";
* @schema
* @operation
*/
export function OperationId(operationId: string) {
export function OperationId(operationId: string): MethodDecorator {
return JsonEntityFn((store, args) => {
if (store.decoratorType !== DecoratorTypes.METHOD) {
throw new UnsupportedDecoratorType(OperationId, args);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {JsonEntityFn} from "../common/jsonEntityFn.js";
* ::: warning
* Don't use decorator with Ts.ED application.
*
* Use theses decorators instead:
* Use these decorators instead:
*
* <ApiList query="status.includes('decorator') && status.includes('httpMethod')" />
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ describe("Path", () => {

let actualError: any;
try {
// @ts-ignore
Path("/")(Test.prototype, "test", 0);
} catch (er) {
actualError = er;
Expand Down
2 changes: 1 addition & 1 deletion packages/specs/schema/src/decorators/operations/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {JsonEntityFn} from "../common/jsonEntityFn.js";
* @classDecorator
* @operation
*/
export function Path(path: string) {
export function Path(path: string): ClassDecorator {
return JsonEntityFn((store, args) => {
if (store.decoratorType !== DecoratorTypes.CLASS) {
throw new UnsupportedDecoratorType(Path, args);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ describe("Produces", () => {

let actualError: any;
try {
// @ts-ignore
Produces("text/json")(Test.prototype, "test", 0);
} catch (er) {
actualError = er;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {JsonEntityFn} from "../common/jsonEntityFn.js";
* @operation
* @response
*/
export function Produces(...produces: string[]) {
export function Produces(...produces: string[]): ClassDecorator & MethodDecorator {
return JsonEntityFn((store, args) => {
switch (store.decoratorType) {
case DecoratorTypes.METHOD:
Expand Down
6 changes: 3 additions & 3 deletions packages/specs/schema/src/decorators/operations/redirect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ import {Returns} from "./returns.js";
* @response
* @headers
*/
export function Redirect(url: string, meta?: JsonHeader): Function;
export function Redirect(status: number, url: string, meta?: JsonHeader): Function;
export function Redirect(...args: any[]): Function {
export function Redirect(url: string, meta?: JsonHeader): MethodDecorator;
export function Redirect(status: number, url: string, meta?: JsonHeader): MethodDecorator;
export function Redirect(...args: any[]): MethodDecorator {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick (assertive)

Consider adding type safety for the spread arguments.

The spread operator with any[] could be more strictly typed.

-export function Redirect(...args: any[]): MethodDecorator {
+export function Redirect(...args: Array<string | number | JsonHeader>): MethodDecorator {

Committable suggestion skipped: line range outside the PR's diff.

const {status, url, meta} = args.reduce(
(options: any, value: any) => {
if (isNumber(value)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ describe("Security", () => {

let actualError: any;
try {
// @ts-ignore
Security("POST", "/")(Test.prototype, "test", 0);
} catch (er) {
actualError = er;
Expand Down
6 changes: 3 additions & 3 deletions packages/specs/schema/src/decorators/operations/security.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {JsonEntityFn} from "../common/jsonEntityFn.js";
* @classDecorator
* @operation
*/
export function Security(name: string, ...scopes: string[]): Function;
export function Security(name: string, ...scopes: string[]): ClassDecorator & MethodDecorator;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick (assertive)

Type declarations accurately reflect decorator usage.

The return type ClassDecorator & MethodDecorator properly constrains the decorator to class and method usage, improving type safety and preventing misuse at compile time.

Also applies to: 60-61

/**
* Add security metadata on the decorated method.
*
Expand Down Expand Up @@ -57,8 +57,8 @@ export function Security(name: string, ...scopes: string[]): Function;
* @classDecorator
* @operation
*/
export function Security(security: OpenSpecSecurity): Function;
export function Security(nameOrSecurity: string | OpenSpecSecurity, ...scopes: string[]): Function {
export function Security(security: OpenSpecSecurity): ClassDecorator & MethodDecorator;
export function Security(nameOrSecurity: string | OpenSpecSecurity, ...scopes: string[]): ClassDecorator & MethodDecorator {
return JsonEntityFn((store, args) => {
switch (store.decoratorType) {
case DecoratorTypes.METHOD:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ describe("Summary", () => {
it("should throw error for unsupported usage", () => {
let actualError: any;
try {
// @ts-ignore
Summary("summary")(class Test {});
} catch (er) {
actualError = er;
Expand Down
2 changes: 1 addition & 1 deletion packages/specs/schema/src/decorators/operations/summary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {JsonEntityFn} from "../common/jsonEntityFn.js";
* @schema
* @operation
*/
export function Summary(summary: string) {
export function Summary(summary: string): MethodDecorator {
return JsonEntityFn((store, args) => {
if (store.decoratorType !== DecoratorTypes.METHOD) {
throw new UnsupportedDecoratorType(Summary, args);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ describe("Tags", () => {

let actualError: any;
try {
// @ts-ignore
Tags("tags")(Test.prototype, "test", 0);
} catch (er) {
actualError = er;
Expand Down
13 changes: 11 additions & 2 deletions packages/specs/schema/src/decorators/operations/tags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function mapTags(tags: (string | OpenSpecTag)[]) {
}

/**
* Add tags metadata on the decorated element.
* Add metadata tags to the decorated element (class or method).
*
* ## Examples
* ### On method
Expand All @@ -28,6 +28,15 @@ function mapTags(tags: (string | OpenSpecTag)[]) {
* get() {}
* }
* ```
* ### On Class
*
* ```typescript
* @Controller("/")
* @Tags("api")
* class MyController {
* get() {}
* }
* ```
*
* @param tags
* @decorator
Expand All @@ -36,7 +45,7 @@ function mapTags(tags: (string | OpenSpecTag)[]) {
* @classDecorator
* @operation
*/
export function Tags(...tags: (string | OpenSpecTag)[]) {
export function Tags(...tags: (string | OpenSpecTag)[]): ClassDecorator & MethodDecorator {
return JsonEntityFn((store, args) => {
switch (store.decoratorType) {
case DecoratorTypes.METHOD:
Expand Down
Loading