Skip to content

Commit d6ef560

Browse files
committed
feat: export decorators
1 parent e0c1fe1 commit d6ef560

File tree

2 files changed

+61
-40
lines changed

2 files changed

+61
-40
lines changed

decorators.ts

Lines changed: 57 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,46 @@ import { BODY_TYPE_KEY, MetadataHelper, QUERY_TYPE_KEY } from './deps.ts';
22
import { Constructor } from './mod.ts';
33
import { Swagger } from "./swagger.ts";
44

5-
export const API_PROPERTY = 'api-property';
6-
7-
export const ApiProperty = (property?: Swagger.Schema) =>
8-
(
5+
type DecoratorFunction = (
96
// deno-lint-ignore ban-types
107
target: Object,
11-
propertyKey: string | symbol,
12-
) => {
13-
MetadataHelper.setMetadata(API_PROPERTY, property ?? null, target, propertyKey);
14-
};
8+
propertyKey?: string | symbol,
9+
descriptor?: PropertyDescriptor,
10+
) => void;
11+
12+
export const API_PROPERTY = 'api-property';
13+
14+
export function ApiProperty(property?: Swagger.Schema): DecoratorFunction {
15+
return (
16+
// deno-lint-ignore ban-types
17+
target: Object,
18+
propertyKey?: string | symbol,
19+
descriptor?: PropertyDescriptor,
20+
) => {
21+
MetadataHelper.setMetadata(API_PROPERTY, property ?? null, target, propertyKey);
22+
};
23+
}
1524

1625
export const OPTIONAL_KEY = 'optional';
1726

18-
export const Optional = () =>
19-
(
20-
// deno-lint-ignore ban-types
21-
target: Object,
22-
propertyKey: string | symbol,
23-
) => {
24-
MetadataHelper.setMetadata(OPTIONAL_KEY, true, target, propertyKey);
25-
};
27+
export function Optional(): DecoratorFunction {
28+
return (
29+
// deno-lint-ignore ban-types
30+
target: Object,
31+
propertyKey?: string | symbol,
32+
descriptor?: PropertyDescriptor,
33+
) => {
34+
MetadataHelper.setMetadata(OPTIONAL_KEY, true, target, propertyKey);
35+
};
36+
}
2637

2738
export const RETURNED_TYPE_KEY = 'returntype';
2839

29-
export const ReturnedType = (returnedType: unknown, isArray?: boolean) =>
30-
(
40+
export function ReturnedType(returnedType: unknown, isArray?: boolean): DecoratorFunction {
41+
return (
3142
target: Object,
32-
propertyKey: string | symbol,
33-
descriptor: any,
43+
propertyKey?: string | symbol,
44+
descriptor?: any,
3445
) => {
3546
MetadataHelper.setMetadata(
3647
RETURNED_TYPE_KEY,
@@ -42,27 +53,32 @@ export const ReturnedType = (returnedType: unknown, isArray?: boolean) =>
4253
propertyKey,
4354
);
4455
};
56+
}
4557

46-
export const BodyType = (type: Constructor) =>
47-
(
48-
target: Object,
49-
propertyKey: string | symbol,
50-
) => {
51-
MetadataHelper.setMetadata(BODY_TYPE_KEY, type, target, propertyKey);
52-
};
58+
export function BodyType(type: Constructor): DecoratorFunction {
59+
return (
60+
target: Object,
61+
propertyKey?: string | symbol,
62+
descriptor?: PropertyDescriptor,
63+
) => {
64+
MetadataHelper.setMetadata(BODY_TYPE_KEY, type, target, propertyKey);
65+
};
66+
}
5367

54-
export const QueryType = (type: Constructor) =>
55-
(
68+
export function QueryType(type: Constructor) : DecoratorFunction {
69+
return (
5670
target: Object,
57-
propertyKey: string | symbol,
71+
propertyKey?: string | symbol,
72+
descriptor?: PropertyDescriptor,
5873
) => {
5974
MetadataHelper.setMetadata(QUERY_TYPE_KEY, type, target, propertyKey);
6075
};
76+
}
6177

6278
export const TAGS_KEY = 'tags';
6379

64-
export const Tag = (tagName: string) =>
65-
(
80+
export function Tag(tagName: string) : DecoratorFunction {
81+
return (
6682
target: Object,
6783
propertyKey?: string | symbol,
6884
descriptor?: PropertyDescriptor,
@@ -73,17 +89,18 @@ export const Tag = (tagName: string) =>
7389
MetadataHelper.setMetadata(TAGS_KEY, tagName, target);
7490
}
7591
};
92+
}
7693

7794
export const API_SECURITY = 'api-security';
7895
export const API_SECURITY_DATA = 'api-security-data';
7996

80-
export const ApiBasicAuth = () => ApiSecurity('basic')
81-
export const ApiBearerAuth = () => ApiSecurity('bearer')
82-
export const ApiCookieAuth = () => ApiSecurity('cookie')
83-
export const ApiOAuth2 = (data: string[]) => ApiSecurity('oauth2', data)
97+
export function ApiBasicAuth(): DecoratorFunction { return ApiSecurity('basic') }
98+
export function ApiBearerAuth(): DecoratorFunction { return ApiSecurity('bearer') }
99+
export function ApiCookieAuth(): DecoratorFunction { return ApiSecurity('cookie') }
100+
export function ApiOAuth2(data: string[]): DecoratorFunction { return ApiSecurity('oauth2', data) }
84101

85-
export const ApiSecurity = (name: string, data: string[] = []) =>
86-
(
102+
export function ApiSecurity(name: string, data: string[] = []) : DecoratorFunction {
103+
return (
87104
// deno-lint-ignore ban-types
88105
target: Object,
89106
propertyKey?: string | symbol,
@@ -98,4 +115,5 @@ export const ApiSecurity = (name: string, data: string[] = []) =>
98115
[name]: data
99116
}, target);
100117
}
101-
};
118+
};
119+
}

deno.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
{
22
"name": "@danet/swagger",
33
"version": "2.1.0",
4-
"exports": "./mod.ts",
4+
"exports": {
5+
".":"./mod.ts",
6+
"./decorators": "./decorators.ts"
7+
},
58
"lint": {
69
"exclude": [
710
"spec"

0 commit comments

Comments
 (0)