UNPKG

rajt

Version:

A serverless bundler layer, fully typed for AWS Lambda (Node.js and LLRT) and Cloudflare Workers.

135 lines (112 loc) 4.14 kB
import { MiddlewareType } from './middleware' import response from './response' import { GET_REQUEST } from './request' import { Ability } from './auth' import mergeMiddleware from './utils/merge-middleware' import type { Context, Next, IRequest, DescribeRouteOptions, } from './types' function method(method: string, ...args: any[]): void | ClassDecorator { if (args.length == 1 && typeof args[0] == 'function') return _method(method, '/', args[0]) const path = typeof args[0] == 'string' ? args[0] : '/' return (target: Function) => _method(method, path, target) } function _method(method: string, path = '/', target: Function | any) { target.m = method target.p = path target.mw = [] } export function Get(): ClassDecorator export function Get(target: Function): void export function Get(path: string): ClassDecorator export function Get(...args: any[]): void | ClassDecorator { return method('get', ...args) } export function Post(): ClassDecorator export function Post(target: Function): void export function Post(path: string): ClassDecorator export function Post(...args: any[]): void | ClassDecorator { return method('post', ...args) } export function Put(): ClassDecorator export function Put(target: Function): void export function Put(path: string): ClassDecorator export function Put(...args: any[]): void | ClassDecorator { return method('put', ...args) } export function Patch(): ClassDecorator export function Patch(target: Function): void export function Patch(path: string): ClassDecorator export function Patch(...args: any[]): void | ClassDecorator { return method('patch', ...args) } export function Delete(): ClassDecorator export function Delete(target: Function): void export function Delete(path: string): ClassDecorator export function Delete(...args: any[]): void | ClassDecorator { return method('delete', ...args) } export function Head(): ClassDecorator export function Head(target: Function): void export function Head(path: string): ClassDecorator export function Head(...args: any[]): void | ClassDecorator { return method('head', ...args) } export function Options(): ClassDecorator export function Options(target: Function): void export function Options(path: string): ClassDecorator export function Options(...args: any[]): void | ClassDecorator { return method('options', ...args) } export function Connect(): ClassDecorator export function Connect(target: Function): void export function Connect(path: string): ClassDecorator export function Connect(...args: any[]): void | ClassDecorator { return method('connect', ...args) } export function Trace(): ClassDecorator export function Trace(target: Function): void export function Trace(path: string): ClassDecorator export function Trace(...args: any[]): void | ClassDecorator { return method('trace', ...args) } export function Middleware(...handlers: MiddlewareType[]) { return function (target: any) { mergeMiddleware(target, ...handlers) } } export function Middlewares(...handlers: MiddlewareType[]) { return Middleware(...handlers) } export function Auth(target: Function): void export function Auth(): ClassDecorator export function Auth(...args: any[]): void | ClassDecorator { if (args.length == 1 && typeof args[0] == 'function') return _auth(args[0]) return (target: any) => _auth(target) } function _auth(target: Function | any) { if (!target?.d) target.d = {} if (!target.d?.security) target.d.security = [] target.d.security.push({JWT: []}) if (!target.d?.responses) target.d.responses = {} target.d.responses[401] = {description: 'Unauthorized'} mergeMiddleware(target, async (c: Context, next: Next) => { const req = c.get(GET_REQUEST as unknown as string) as IRequest const ability = Ability.fromAction(target) if (!req?.user || !ability || req.cant(ability)) return response.unauthorized() await next() }) } function _describe(spec: DescribeRouteOptions): ClassDecorator{ return (target: any) => { target.d = spec } } export const OpenApi = _describe export const Describe = _describe export const Desc = _describe