@plumier/core
Advanced tools
Comparing version 1.0.0-dev11.3 to 1.0.0-dev11.4
@@ -5,5 +5,9 @@ import { ActionResult, Invocation, Middleware, RouteInfo, AuthorizeMetadataInfo } from "./types"; | ||
type: "plumier-meta:authorize"; | ||
authorize: string | ((info: AuthorizeMetadataInfo) => boolean | Promise<boolean>); | ||
authorize: string | AuthorizeCallback | Authorizer; | ||
tag: string; | ||
location: "Class" | "Parameter" | "Method"; | ||
} | ||
interface Authorizer { | ||
authorize(info: AuthorizeMetadataInfo, location: "Class" | "Parameter" | "Method"): boolean | Promise<boolean>; | ||
} | ||
declare type RoleField = string | ((value: any) => Promise<string[]>); | ||
@@ -20,2 +24,2 @@ declare function updateRouteAccess(routes: RouteInfo[], globals?: (...args: any[]) => void): void; | ||
} | ||
export { AuthorizeCallback, RoleField, updateRouteAccess, AuthorizeMiddleware, AuthorizeDecorator }; | ||
export { AuthorizeCallback, RoleField, Authorizer, updateRouteAccess, AuthorizeMiddleware, AuthorizeDecorator }; |
@@ -13,4 +13,10 @@ "use strict"; | ||
const authorize = decorator.authorize; | ||
const impl = typeof authorize === "string" ? info.ctx.config.authorizer[authorize] : authorize; | ||
return impl(info); | ||
let instance; | ||
if (typeof authorize === "function") | ||
instance = { authorize }; | ||
else if (common_1.hasKeyOf(authorize, "authorize")) | ||
instance = authorize; | ||
else | ||
instance = info.ctx.config.dependencyResolver.resolve(authorize); | ||
return instance.authorize(info, decorator.location); | ||
} | ||
@@ -17,0 +23,0 @@ function isAuthDecorator(decorator) { |
@@ -21,3 +21,3 @@ "use strict"; | ||
function hasKeyOf(opt, key) { | ||
return key in opt; | ||
return !!opt[key]; | ||
} | ||
@@ -24,0 +24,0 @@ exports.hasKeyOf = hasKeyOf; |
@@ -1,4 +0,7 @@ | ||
import { AuthorizeCallback } from "./authorization"; | ||
import { AuthorizeCallback, Authorizer } from "./authorization"; | ||
declare class AuthDecoratorImpl { | ||
custom(authorize: string | AuthorizeCallback, tag?: string): (...args: any[]) => void; | ||
custom(callback: AuthorizeCallback, tag?: string): (...args: any[]) => void; | ||
custom(authorizer: Authorizer, tag?: string): (...args: any[]) => void; | ||
custom(id: string, tag?: string): (...args: any[]) => void; | ||
custom(id: symbol, tag?: string): (...args: any[]) => void; | ||
/** | ||
@@ -5,0 +8,0 @@ * Authorize controller/action to public |
@@ -9,6 +9,5 @@ "use strict"; | ||
return tinspector_1.decorate((...args) => { | ||
const type = args.length === 1 ? "Class" : args.length === 2 ? "Method" : "Parameter"; | ||
const location = args.length === 1 ? "Class" : args.length === 2 ? "Method" : "Parameter"; | ||
return { | ||
type: "plumier-meta:authorize", tag, | ||
authorize: typeof authorize === "string" ? authorize : (info) => authorize(info, type), | ||
type: "plumier-meta:authorize", tag, authorize, location | ||
}; | ||
@@ -15,0 +14,0 @@ }, ["Class", "Parameter", "Method", "Property"]); |
@@ -1,6 +0,9 @@ | ||
import { KoaMiddleware, Middleware } from "./types"; | ||
import { MiddlewareFunction, Middleware } from "./types"; | ||
declare function domain(): (target: any) => void; | ||
declare namespace middleware { | ||
function use(...middleware: (Middleware | KoaMiddleware)[]): (...args: any[]) => void; | ||
function use(middleware: MiddlewareFunction): (...args: any[]) => void; | ||
function use(middleware: Middleware): (...args: any[]) => void; | ||
function use(id: string): (...args: any[]) => void; | ||
function use(id: symbol): (...args: any[]) => void; | ||
} | ||
export { middleware, domain }; |
@@ -5,3 +5,2 @@ "use strict"; | ||
const tinspector_1 = tslib_1.__importStar(require("tinspector")); | ||
const types_1 = require("./types"); | ||
// --------------------------------------------------------------------- // | ||
@@ -18,4 +17,3 @@ // ------------------------------- DOMAIN ------------------------------ // | ||
function use(...middleware) { | ||
const mdw = middleware.map(x => typeof x == "function" ? types_1.MiddlewareUtil.fromKoa(x) : x).reverse(); | ||
const value = { name: "Middleware", value: mdw }; | ||
const value = { name: "Middleware", value: middleware }; | ||
return tinspector_1.decorate(value, ["Class", "Method"]); | ||
@@ -22,0 +20,0 @@ } |
import { val } from "typedconverter"; | ||
export { val }; | ||
export { AuthorizeCallback, AuthorizeMiddleware, RoleField, updateRouteAccess } from "./authorization"; | ||
export { AuthorizeCallback, AuthorizeMiddleware, RoleField, updateRouteAccess, Authorizer } from "./authorization"; | ||
export { HeaderPart, RequestPart, BindingDecorator, binder } from "./binder"; | ||
@@ -16,2 +16,2 @@ export { invoke } from "./middleware-pipeline"; | ||
export { HttpStatus } from "./http-status"; | ||
export { ActionResult, Application, AuthorizeMetadataInfo, AuthorizeStore, Configuration, DefaultFacility, DependencyResolver, Facility, FileParser, FileUploadInfo, HttpMethod, HttpStatusError, Invocation, KoaMiddleware, Middleware, MiddlewareDecorator, MiddlewareUtil, PlumierApplication, PlumierConfiguration, RedirectActionResult, RouteContext, RouteInfo, RouteAnalyzerFunction, RouteAnalyzerIssue, ValidatorDecorator, ValidatorFunction, ValidatorInfo, ValidatorStore, ValidationError, errorMessage } from "./types"; | ||
export { ActionResult, Application, AuthorizeMetadataInfo, Configuration, DefaultFacility, CustomValidator, DependencyResolver, Facility, FileParser, FileUploadInfo, HttpMethod, HttpStatusError, Invocation, KoaMiddleware, Middleware, MiddlewareFunction, MiddlewareDecorator, MiddlewareUtil, PlumierApplication, PlumierConfiguration, RedirectActionResult, RouteContext, RouteInfo, RouteAnalyzerFunction, RouteAnalyzerIssue, ValidatorDecorator, ValidatorFunction, ValidatorInfo, ValidationError, errorMessage, AsyncValidatorResult, DefaultDependencyResolver } from "./types"; |
@@ -49,1 +49,2 @@ "use strict"; | ||
exports.errorMessage = types_1.errorMessage; | ||
exports.DefaultDependencyResolver = types_1.DefaultDependencyResolver; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const common_1 = require("./common"); | ||
const types_1 = require("./types"); | ||
@@ -22,3 +23,13 @@ function getMiddleware(global, route) { | ||
proceed() { | ||
return this.middleware.execute(this.next); | ||
let middleware; | ||
if (typeof this.middleware === "function") { | ||
middleware = { execute: this.middleware }; | ||
} | ||
else if (common_1.hasKeyOf(this.middleware, "execute")) { | ||
middleware = this.middleware; | ||
} | ||
else { | ||
middleware = this.context.config.dependencyResolver.resolve(this.middleware); | ||
} | ||
return middleware.execute(this.next); | ||
} | ||
@@ -69,4 +80,3 @@ } | ||
for (let i = middlewares.length; i--;) { | ||
const mdw = middlewares[i]; | ||
invocationStack = new MiddlewareInvocation(mdw, context, invocationStack); | ||
invocationStack = new MiddlewareInvocation(middlewares[i], context, invocationStack); | ||
} | ||
@@ -73,0 +83,0 @@ return invocationStack.proceed(); |
@@ -67,4 +67,5 @@ import Koa, { Context } from "koa"; | ||
name: "Middleware"; | ||
value: Middleware[]; | ||
value: (string | symbol | MiddlewareFunction | Middleware)[]; | ||
} | ||
export declare type MiddlewareFunction = (invocation: Readonly<Invocation>) => Promise<ActionResult>; | ||
export interface Middleware { | ||
@@ -75,28 +76,32 @@ execute(invocation: Readonly<Invocation>): Promise<ActionResult>; | ||
function fromKoa(middleware: KoaMiddleware): Middleware; | ||
function extractDecorators(route: RouteInfo): Middleware[]; | ||
function extractDecorators(route: RouteInfo): (string | symbol | MiddlewareFunction | Middleware)[]; | ||
} | ||
export interface DependencyResolver { | ||
resolve(type: (new (...args: any[]) => any)): any; | ||
resolve(type: Class | string | symbol): any; | ||
} | ||
export declare class DefaultDependencyResolver implements DependencyResolver { | ||
private readonly registry; | ||
register(id: string | symbol): (target: any) => void; | ||
resolve(type: Class | string | symbol): any; | ||
} | ||
export interface Application { | ||
/** | ||
* Use Koa middleware | ||
* Use plumier middleware registered from the registry | ||
``` | ||
use(KoaBodyParser()) | ||
use("myMiddleware") | ||
``` | ||
* Use inline Koa middleware | ||
``` | ||
use(async (ctx, next) => { }) | ||
``` | ||
*/ | ||
use(middleware: KoaMiddleware): Application; | ||
use(middleware: string | symbol): Application; | ||
/** | ||
* Use plumier middleware by class instance inherited from Middleware | ||
* Use plumier middleware | ||
``` | ||
use(new MyMiddleware()) | ||
``` | ||
* Use plumier middleware by inline object | ||
*/ | ||
use(middleware: Middleware): Application; | ||
/** | ||
* Use plumier middleware | ||
``` | ||
use({ execute: x => x.proceed()}) | ||
use({ execute: async x => { | ||
use(x => x.proceed()) | ||
use(async x => { | ||
return new ActionResult({ json: "body" }, 200) | ||
@@ -106,3 +111,3 @@ }) | ||
*/ | ||
use(middleware: Middleware): Application; | ||
use(middleware: MiddlewareFunction): Application; | ||
/** | ||
@@ -145,3 +150,3 @@ * Set facility (advanced configuration) | ||
type: "ValidatorDecorator"; | ||
validator: ValidatorFunction; | ||
validator: ValidatorFunction | string | symbol; | ||
} | ||
@@ -162,8 +167,5 @@ export interface ValidatorInfo { | ||
export declare type ValidatorFunction = (value: any, info: ValidatorInfo) => undefined | string | AsyncValidatorResult[] | Promise<AsyncValidatorResult[] | string | undefined>; | ||
export declare type ValidatorStore = { | ||
[key: string]: ValidatorFunction; | ||
}; | ||
export declare type AuthorizeStore = { | ||
[key: string]: (info: AuthorizeMetadataInfo) => boolean | Promise<boolean>; | ||
}; | ||
export interface CustomValidator { | ||
validate(value: any, info: ValidatorInfo): undefined | string | AsyncValidatorResult[] | Promise<AsyncValidatorResult[] | string | undefined>; | ||
} | ||
export interface AuthorizeMetadataInfo { | ||
@@ -191,3 +193,3 @@ value?: any; | ||
*/ | ||
middlewares: Middleware[]; | ||
middlewares: (string | symbol | MiddlewareFunction | Middleware)[]; | ||
/** | ||
@@ -215,9 +217,4 @@ * Specify controller path (absolute or relative to entry point) or the controller classes array. | ||
/** | ||
* Key-value pair to store validator logic. Separate decorator and validation logic | ||
* Set custom route analyser functions | ||
*/ | ||
validators?: ValidatorStore; | ||
/** | ||
* Key-value pair to store authorization logic. Separate decorator and authorization logic | ||
*/ | ||
authorizer?: AuthorizeStore; | ||
analyzers?: RouteAnalyzerFunction[]; | ||
@@ -252,2 +249,3 @@ } | ||
const PublicNotInParameter = "PLUM1008: @authorize.public() can not be applied to parameter"; | ||
const ObjectNotFound = "PLUM1009: Object with id {0} not found in Object registry"; | ||
const UnableToInstantiateModel = "PLUM2000: Unable to instantiate {0}. Domain model should not throw error inside constructor"; | ||
@@ -254,0 +252,0 @@ const UnableToConvertValue = "Unable to convert \"{0}\" into {1}"; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const tinspector_1 = require("tinspector"); | ||
const http_status_1 = require("./http-status"); | ||
@@ -91,2 +92,25 @@ // --------------------------------------------------------------------- // | ||
})(MiddlewareUtil = exports.MiddlewareUtil || (exports.MiddlewareUtil = {})); | ||
class DefaultDependencyResolver { | ||
constructor() { | ||
this.registry = new Map(); | ||
} | ||
register(id) { | ||
return tinspector_1.decorateClass(cls => { | ||
this.registry.set(id, cls); | ||
return { type: "RegistryDecorator", id }; | ||
}); | ||
} | ||
resolve(type) { | ||
if (typeof type === "function") { | ||
return new type(); | ||
} | ||
else { | ||
const Type = this.registry.get(type); | ||
if (!Type) | ||
throw new Error(errorMessage.ObjectNotFound.format(type)); | ||
return new Type(); | ||
} | ||
} | ||
} | ||
exports.DefaultDependencyResolver = DefaultDependencyResolver; | ||
// --------------------------------------------------------------------- // | ||
@@ -126,2 +150,3 @@ // ------------------------------- ERROR ------------------------------- // | ||
errorMessage.PublicNotInParameter = "PLUM1008: @authorize.public() can not be applied to parameter"; | ||
errorMessage.ObjectNotFound = "PLUM1009: Object with id {0} not found in Object registry"; | ||
//PLUM2XXX internal app error | ||
@@ -128,0 +153,0 @@ errorMessage.UnableToInstantiateModel = `PLUM2000: Unable to instantiate {0}. Domain model should not throw error inside constructor`; |
@@ -1,5 +0,9 @@ | ||
import { ActionResult, Invocation, Middleware, ValidatorFunction, AsyncValidatorResult } from "./types"; | ||
import { ActionResult, AsyncValidatorResult, Invocation, Middleware, ValidatorFunction, CustomValidator } from "./types"; | ||
declare module "typedconverter" { | ||
namespace val { | ||
function custom(val: ValidatorFunction | string): (...arg: any[]) => void; | ||
function custom(validator: ValidatorFunction): (...arg: any[]) => void; | ||
function custom(validator: CustomValidator): (...arg: any[]) => void; | ||
function custom(id: string): (...arg: any[]) => void; | ||
function custom(id: symbol): (...arg: any[]) => void; | ||
function custom(val: ValidatorFunction | string | symbol | CustomValidator): (...arg: any[]) => void; | ||
function result(path: string, messages: string | string[]): AsyncValidatorResult[]; | ||
@@ -6,0 +10,0 @@ } |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const tslib_1 = require("tslib"); | ||
const tinspector_1 = require("tinspector"); | ||
const tc = tslib_1.__importStar(require("typedconverter")); | ||
const binder_1 = require("./binder"); | ||
const common_1 = require("./common"); | ||
const types_1 = require("./types"); | ||
const binder_1 = require("./binder"); | ||
const tinspector_1 = require("tinspector"); | ||
function createVisitor(items) { | ||
@@ -35,7 +36,10 @@ return (i) => { | ||
return []; | ||
const validators = ctx.config.validators || {}; | ||
if (typeof x.validator === "string" && !validators[x.validator]) | ||
throw new Error(`No validation implementation found for ${x.validator}`); | ||
const validator = typeof x.validator === "function" ? x.validator : validators[x.validator]; | ||
const message = await validator(x.value, info); | ||
let validator; | ||
if (typeof x.validator === "function") | ||
validator = { validate: x.validator }; | ||
else if (common_1.hasKeyOf(x.validator, "validate")) | ||
validator = x.validator; | ||
else | ||
validator = ctx.config.dependencyResolver.resolve(x.validator); | ||
const message = await validator.validate(x.value, info); | ||
if (!message) | ||
@@ -42,0 +46,0 @@ return []; |
{ | ||
"name": "@plumier/core", | ||
"version": "1.0.0-dev11.3+4023ad0", | ||
"version": "1.0.0-dev11.4+abaf721", | ||
"description": "Delightful Node.js Rest Framework", | ||
@@ -41,3 +41,3 @@ "main": "lib/index.js", | ||
}, | ||
"gitHead": "4023ad0ad94e738ed1fd7149e070ef9d71694d26", | ||
"gitHead": "abaf7219e8bddd92eae914e23cd069d122e9d048", | ||
"devDependencies": { | ||
@@ -44,0 +44,0 @@ "upath": "^1.2.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
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
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
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
124201
2839