@plumier/core
Advanced tools
Comparing version 1.0.0-beta.3 to 1.0.0-beta.4
@@ -6,2 +6,3 @@ /// <reference types="koa-bodyparser" /> | ||
import { ClassReflection, MethodReflection, ParameterReflection } from "tinspector"; | ||
import { HttpStatus } from './http-status'; | ||
export declare type HttpMethod = "post" | "get" | "put" | "delete" | "patch" | "head" | "trace" | "options"; | ||
@@ -48,6 +49,15 @@ export declare type KoaMiddleware = (ctx: Context, next: () => Promise<void>) => Promise<any>; | ||
} | ||
export interface AuthDecorator { | ||
type: "authorize:public" | "authorize:role"; | ||
value: string[]; | ||
export interface AuthorizeMetadataInfo { | ||
role: string[]; | ||
user: any; | ||
ctx: Koa.Context; | ||
route: RouteInfo; | ||
parameters: any[]; | ||
value?: any; | ||
} | ||
export interface AuthorizeDecorator { | ||
type: "plumier-meta:authorize"; | ||
authorize: (info: AuthorizeMetadataInfo) => Promise<boolean>; | ||
tag: string; | ||
} | ||
export interface RouteInfo { | ||
@@ -238,4 +248,4 @@ url: string; | ||
export declare class HttpStatusError extends Error { | ||
status: number; | ||
constructor(status: number, message?: string); | ||
status: HttpStatus; | ||
constructor(status: HttpStatus, message?: string); | ||
} | ||
@@ -620,2 +630,3 @@ export declare class ConversionError extends HttpStatusError { | ||
export declare class AuthDecoratorImpl { | ||
custom(authorize: (info: AuthorizeMetadataInfo, location: "Class" | "Parameter" | "Method") => Promise<boolean>, tag?: string): (...args: any[]) => void; | ||
/** | ||
@@ -626,3 +637,3 @@ * Authorize controller/action to public | ||
/** | ||
* Authorize controller/action accessible by sepecific role | ||
* Authorize controller/action accessible by specific role | ||
* @param roles List of roles allowed | ||
@@ -629,0 +640,0 @@ */ |
@@ -508,2 +508,11 @@ "use strict"; | ||
class AuthDecoratorImpl { | ||
custom(authorize, tag = "Custom") { | ||
return tinspector_1.decorate((...args) => { | ||
const type = args.length === 1 ? "Class" : args.length === 2 ? "Method" : "Parameter"; | ||
return { | ||
type: "plumier-meta:authorize", tag, | ||
authorize: (info) => authorize(info, type), | ||
}; | ||
}, ["Class", "Parameter", "Method", "Property"]); | ||
} | ||
/** | ||
@@ -516,14 +525,20 @@ * Authorize controller/action to public | ||
throw new Error(errorMessage.PublicNotInParameter); | ||
return { type: "authorize:public", value: [] }; | ||
}, ["Class", "Parameter", "Method"]); | ||
return { type: "plumier-meta:authorize", tag: "Public" }; | ||
}, ["Class", "Parameter", "Method", "Property"]); | ||
} | ||
/** | ||
* Authorize controller/action accessible by sepecific role | ||
* Authorize controller/action accessible by specific role | ||
* @param roles List of roles allowed | ||
*/ | ||
role(...roles) { | ||
return tinspector_1.mergeDecorator(tinspector_1.decorate({ type: "authorize:role", value: roles }, ["Class", "Parameter", "Method"]), (...args) => { | ||
const roleDecorator = this.custom(async (info, location) => { | ||
const { role, value } = info; | ||
const isAuthorized = roles.some(x => role.some(y => x === y)); | ||
return location === "Parameter" ? !!value && isAuthorized : isAuthorized; | ||
}, roles.join(", ")); | ||
const optionalDecorator = (...args) => { | ||
if (args.length === 3 && typeof args[2] === "number") | ||
tinspector_1.decorateParameter({ type: "ValidatorDecorator", validator: ValidatorId.optional })(args[0], args[1], args[2]); | ||
}); | ||
}; | ||
return tinspector_1.mergeDecorator(roleDecorator, optionalDecorator); | ||
} | ||
@@ -530,0 +545,0 @@ } |
export * from "./core"; | ||
export * from "./common"; | ||
export * from "./http-status"; |
@@ -6,1 +6,2 @@ "use strict"; | ||
tslib_1.__exportStar(require("./common"), exports); | ||
tslib_1.__exportStar(require("./http-status"), exports); |
{ | ||
"name": "@plumier/core", | ||
"version": "1.0.0-beta.3", | ||
"version": "1.0.0-beta.4", | ||
"description": "Pleasant TypeScript Web Api Framework", | ||
@@ -23,3 +23,3 @@ "main": "lib/index.js", | ||
"@types/koa": "^2.0.48", | ||
"tinspector": "^2.1.2", | ||
"tinspector": "^2.2.0", | ||
"tslib": "^1.9.3" | ||
@@ -34,3 +34,3 @@ }, | ||
}, | ||
"gitHead": "ce2b47b0e9ca50f330741125a90c54b55661c1ad", | ||
"gitHead": "803ec103d9b72a40e53e806182228676a03bb0ce", | ||
"devDependencies": { | ||
@@ -37,0 +37,0 @@ "upath": "^1.1.0" |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
78461
10
2018
Updatedtinspector@^2.2.0