Skip to content

Commit

Permalink
fix(schema): fix typing for operation decorators
Browse files Browse the repository at this point in the history
  • Loading branch information
Romakita committed Jan 25, 2025
1 parent 7060d65 commit 3d4751a
Show file tree
Hide file tree
Showing 19 changed files with 33 additions and 17 deletions.
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 {
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
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) {
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 {
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;
/**
* 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

0 comments on commit 3d4751a

Please sign in to comment.