Skip to content

Commit

Permalink
feat: export decorators
Browse files Browse the repository at this point in the history
  • Loading branch information
Sorikairox committed Oct 1, 2024
1 parent e0c1fe1 commit d6ef560
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 40 deletions.
96 changes: 57 additions & 39 deletions decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,46 @@ import { BODY_TYPE_KEY, MetadataHelper, QUERY_TYPE_KEY } from './deps.ts';
import { Constructor } from './mod.ts';
import { Swagger } from "./swagger.ts";

export const API_PROPERTY = 'api-property';

export const ApiProperty = (property?: Swagger.Schema) =>
(
type DecoratorFunction = (
// deno-lint-ignore ban-types
target: Object,
propertyKey: string | symbol,
) => {
MetadataHelper.setMetadata(API_PROPERTY, property ?? null, target, propertyKey);
};
propertyKey?: string | symbol,
descriptor?: PropertyDescriptor,
) => void;

export const API_PROPERTY = 'api-property';

export function ApiProperty(property?: Swagger.Schema): DecoratorFunction {
return (
// deno-lint-ignore ban-types
target: Object,
propertyKey?: string | symbol,
descriptor?: PropertyDescriptor,
) => {
MetadataHelper.setMetadata(API_PROPERTY, property ?? null, target, propertyKey);
};
}

export const OPTIONAL_KEY = 'optional';

export const Optional = () =>
(
// deno-lint-ignore ban-types
target: Object,
propertyKey: string | symbol,
) => {
MetadataHelper.setMetadata(OPTIONAL_KEY, true, target, propertyKey);
};
export function Optional(): DecoratorFunction {
return (
// deno-lint-ignore ban-types
target: Object,
propertyKey?: string | symbol,
descriptor?: PropertyDescriptor,
) => {
MetadataHelper.setMetadata(OPTIONAL_KEY, true, target, propertyKey);
};
}

export const RETURNED_TYPE_KEY = 'returntype';

export const ReturnedType = (returnedType: unknown, isArray?: boolean) =>
(
export function ReturnedType(returnedType: unknown, isArray?: boolean): DecoratorFunction {
return (
target: Object,
propertyKey: string | symbol,
descriptor: any,
propertyKey?: string | symbol,
descriptor?: any,
) => {
MetadataHelper.setMetadata(
RETURNED_TYPE_KEY,
Expand All @@ -42,27 +53,32 @@ export const ReturnedType = (returnedType: unknown, isArray?: boolean) =>
propertyKey,
);
};
}

export const BodyType = (type: Constructor) =>
(
target: Object,
propertyKey: string | symbol,
) => {
MetadataHelper.setMetadata(BODY_TYPE_KEY, type, target, propertyKey);
};
export function BodyType(type: Constructor): DecoratorFunction {
return (
target: Object,
propertyKey?: string | symbol,
descriptor?: PropertyDescriptor,
) => {
MetadataHelper.setMetadata(BODY_TYPE_KEY, type, target, propertyKey);
};
}

export const QueryType = (type: Constructor) =>
(
export function QueryType(type: Constructor) : DecoratorFunction {
return (
target: Object,
propertyKey: string | symbol,
propertyKey?: string | symbol,
descriptor?: PropertyDescriptor,
) => {
MetadataHelper.setMetadata(QUERY_TYPE_KEY, type, target, propertyKey);
};
}

export const TAGS_KEY = 'tags';

export const Tag = (tagName: string) =>
(
export function Tag(tagName: string) : DecoratorFunction {
return (
target: Object,
propertyKey?: string | symbol,
descriptor?: PropertyDescriptor,
Expand All @@ -73,17 +89,18 @@ export const Tag = (tagName: string) =>
MetadataHelper.setMetadata(TAGS_KEY, tagName, target);
}
};
}

export const API_SECURITY = 'api-security';
export const API_SECURITY_DATA = 'api-security-data';

export const ApiBasicAuth = () => ApiSecurity('basic')
export const ApiBearerAuth = () => ApiSecurity('bearer')
export const ApiCookieAuth = () => ApiSecurity('cookie')
export const ApiOAuth2 = (data: string[]) => ApiSecurity('oauth2', data)
export function ApiBasicAuth(): DecoratorFunction { return ApiSecurity('basic') }
export function ApiBearerAuth(): DecoratorFunction { return ApiSecurity('bearer') }
export function ApiCookieAuth(): DecoratorFunction { return ApiSecurity('cookie') }
export function ApiOAuth2(data: string[]): DecoratorFunction { return ApiSecurity('oauth2', data) }

export const ApiSecurity = (name: string, data: string[] = []) =>
(
export function ApiSecurity(name: string, data: string[] = []) : DecoratorFunction {
return (
// deno-lint-ignore ban-types
target: Object,
propertyKey?: string | symbol,
Expand All @@ -98,4 +115,5 @@ export const ApiSecurity = (name: string, data: string[] = []) =>
[name]: data
}, target);
}
};
};
}
5 changes: 4 additions & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
{
"name": "@danet/swagger",
"version": "2.1.0",
"exports": "./mod.ts",
"exports": {
".":"./mod.ts",
"./decorators": "./decorators.ts"
},
"lint": {
"exclude": [
"spec"
Expand Down

0 comments on commit d6ef560

Please sign in to comment.