feat: complate @ccms/web package

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
2020-06-30 16:02:39 +08:00
parent e28af3fbbc
commit bbfc70fdd6
7 changed files with 327 additions and 56 deletions

View File

@ -1,30 +1,80 @@
export const Controller = () => {
return <TFunction extends Function>(target: TFunction): ClassDecorator => {
import { decorate, injectable } from "@ccms/container"
import { METADATA_KEY, PARAM_TYPE } from '../constants'
import { interfaces } from "../interfaces"
import { addControllerMetadata, addControllerAction, addActionParam } from "./utils"
export const Controller = (metadata?: string | interfaces.ControllerMetadata) => {
return (target: any) => {
if (!metadata) { metadata = target.name.toLowerCase().replace('controller', '') }
if (typeof metadata === "string") { metadata = { path: metadata } }
metadata.name = metadata.name || target.name
metadata.path = metadata.path ?? `/${metadata}`
metadata.path = metadata.path.startsWith('/') ? metadata.path : `/${metadata.path}`
decorate(injectable(), target)
Reflect.defineMetadata(METADATA_KEY.Controller, metadata, target)
addControllerMetadata(metadata)
return
}
}
export const Post = () => {
return <T>(target: Object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<T>): MethodDecorator => {
return
function action(method: interfaces.Method) {
return (metadata?: string | interfaces.ActionMetadata) => {
return (target: any, propertyKey: string) => {
if (!metadata) { metadata = propertyKey.toLowerCase() }
if (typeof metadata === "string") { metadata = { path: metadata } }
metadata.path = metadata.path ?? `/${propertyKey}`
metadata.path = metadata.path.startsWith('/') ? metadata.path : `/${metadata.path}`
metadata.method = method
metadata.executor = propertyKey
Reflect.defineMetadata(METADATA_KEY.Action, metadata, target[propertyKey])
addControllerAction(target, propertyKey)
return
}
}
}
export const Get = () => {
return <T>(target: Object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<T>): MethodDecorator => {
return
export const Action = action('ALL')
export const Get = action('GET')
export const Post = action('POST')
export const Put = action('PUT')
export const Patch = action('PATCH')
export const Head = action('HEAD')
export const Delete = action('DELETE')
function param(type: PARAM_TYPE) {
return (metadata?: string | interfaces.ParamMetadata) => {
return (target: any, propertyKey: string, index: number) => {
if (!metadata) { metadata = `${propertyKey}-${index}` }
if (typeof metadata === "string") { metadata = { name: metadata } }
metadata.type = type
metadata.index = index
metadata.paramtype = Reflect.getMetadata("design:paramtypes", target, propertyKey)[index]
addActionParam(target, propertyKey, metadata)
return
}
}
}
export const Header = () => {
return <T>(target: Object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<T>): MethodDecorator => {
return
}
}
export const Param = () => {
return (target: Object, propertyKey: string | symbol, parameterIndex: number): ParameterDecorator => {
return
}
}
export const RequestBody = () => {
return (target: Object, propertyKey: string | symbol, parameterIndex: number): ParameterDecorator => {
return
export const Request = param(PARAM_TYPE.REQUEST)
export const Response = param(PARAM_TYPE.RESPONSE)
export const Header = param(PARAM_TYPE.HEADER)
export const Cookie = param(PARAM_TYPE.COOKIE)
export const Query = param(PARAM_TYPE.QUERY)
export const Param = param(PARAM_TYPE.QUERY)
export const Body = param(PARAM_TYPE.BODY)
function Middleware() {
return (metadata?: string | interfaces.ActionMetadata) => {
return (target: any, propertyKey: string) => {
if (!metadata) { metadata = propertyKey.toLowerCase() }
if (typeof metadata === "string") { metadata = { path: metadata } }
metadata.path = metadata.path ?? `/${propertyKey}`
metadata.path = metadata.path.startsWith('/') ? metadata.path : `/${metadata.path}`
metadata.executor = propertyKey
Reflect.defineMetadata(METADATA_KEY.Action, metadata, target[propertyKey])
addControllerAction(target, propertyKey)
return
}
}
}
export * from './utils'

View File

@ -0,0 +1,27 @@
import { interfaces } from '../interfaces'
import { METADATA_KEY } from '../constants'
export function getControllerMetadatas(): interfaces.ControllerMetadata[] {
return Reflect.getMetadata(METADATA_KEY.Controller, Reflect) || []
}
export function addControllerMetadata(metadata: interfaces.ControllerMetadata) {
Reflect.defineMetadata(METADATA_KEY.Controller, [metadata, ...getControllerMetadatas()], Reflect)
}
export function getControllerActions(target: any): string[] {
return Reflect.getMetadata(METADATA_KEY.Action, target.constructor) || []
}
export function addControllerAction(target: any, propertyKey: string) {
Reflect.defineMetadata(METADATA_KEY.Action, [propertyKey, ...getControllerActions(target)], target.constructor)
}
export function getControllerMetadata(target: any): interfaces.ControllerMetadata {
return Reflect.getMetadata(METADATA_KEY.Controller, target)
}
export function getActionMetadata(target: any, propertyKey: string): interfaces.ActionMetadata {
return Reflect.getMetadata(METADATA_KEY.Action, target[propertyKey])
}
export function getActionParams(target: any, propertyKey: string): interfaces.ParamMetadata[] {
return Reflect.getMetadata(METADATA_KEY.Param, target[propertyKey]) || []
}
export function addActionParam(target: any, propertyKey: string, metadata: interfaces.ParamMetadata) {
Reflect.defineMetadata(METADATA_KEY.Param, [metadata, ...getActionParams(target, propertyKey)], target[propertyKey])
}