@apex-pub/convector-core-controller
Advanced tools
Comparing version 1.4.1-2.0 to 1.4.1-4-alpha.6064f94c
@@ -1,18 +0,14 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Controller = exports.controllerMetadataKey = void 0; | ||
const g = require("window-or-global"); | ||
const convector_core_errors_1 = require("@apex-pub/convector-core-errors"); | ||
require("reflect-metadata"); | ||
exports.controllerMetadataKey = g.ConvectorControllerMetadataKey || Symbol('controller'); | ||
g.ConvectorControllerMetadataKey = exports.controllerMetadataKey; | ||
function Controller(namespace) { | ||
import * as g from 'window-or-global'; | ||
import { ControllerInvalidError } from '@apex-pub/convector-core-errors'; | ||
import 'reflect-metadata'; | ||
export const controllerMetadataKey = g.ConvectorControllerMetadataKey || Symbol('controller'); | ||
g.ConvectorControllerMetadataKey = controllerMetadataKey; | ||
export function Controller(namespace) { | ||
return ctor => { | ||
if (typeof ctor !== 'function') { | ||
throw new convector_core_errors_1.ControllerInvalidError(namespace); | ||
throw new ControllerInvalidError(namespace); | ||
} | ||
Reflect.defineMetadata(exports.controllerMetadataKey, namespace, ctor); | ||
Reflect.defineMetadata(controllerMetadataKey, namespace, ctor); | ||
}; | ||
} | ||
exports.Controller = Controller; | ||
//# sourceMappingURL=controller.decorator.js.map |
@@ -1,7 +0,3 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.ConvectorController = void 0; | ||
class ConvectorController { | ||
export class ConvectorController { | ||
} | ||
exports.ConvectorController = ConvectorController; | ||
//# sourceMappingURL=convector-controller.js.map |
@@ -1,9 +0,6 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const tslib_1 = require("tslib"); | ||
tslib_1.__exportStar(require("./param.decorator"), exports); | ||
tslib_1.__exportStar(require("./invokable.decorator"), exports); | ||
tslib_1.__exportStar(require("./optional.decorator"), exports); | ||
tslib_1.__exportStar(require("./controller.decorator"), exports); | ||
tslib_1.__exportStar(require("./convector-controller"), exports); | ||
export * from './param.decorator'; | ||
export * from './invokable.decorator'; | ||
export * from './optional.decorator'; | ||
export * from './controller.decorator'; | ||
export * from './convector-controller'; | ||
//# sourceMappingURL=index.js.map |
@@ -1,86 +0,85 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getInvokables = exports.Invokable = exports.invokableMetadataKey = void 0; | ||
const tslib_1 = require("tslib"); | ||
const g = require("window-or-global"); | ||
const convector_core_errors_1 = require("@apex-pub/convector-core-errors"); | ||
require("reflect-metadata"); | ||
const param_decorator_1 = require("./param.decorator"); | ||
const optional_decorator_1 = require("./optional.decorator"); | ||
const controller_decorator_1 = require("./controller.decorator"); | ||
exports.invokableMetadataKey = g.ConvectorInvokableMetadataKey || Symbol('invokable'); | ||
g.ConvectorInvokableMetadataKey = exports.invokableMetadataKey; | ||
function Invokable() { | ||
import * as g from 'window-or-global'; | ||
import { ControllerNamespaceMissingError, ControllerInvokablesMissingError, ControllerInvalidInvokeError, ControllerInvalidArgumentError, ControllerArgumentParseError, ControllerInvalidFunctionError, ControllerUndefinedArgumentError } from '@apex-pub/convector-core-errors'; | ||
import 'reflect-metadata'; | ||
import { paramMetadataKey } from './param.decorator'; | ||
import { optionalMetadataKey } from './optional.decorator'; | ||
import { controllerMetadataKey } from './controller.decorator'; | ||
export const invokableMetadataKey = g.ConvectorInvokableMetadataKey || Symbol('invokable'); | ||
g.ConvectorInvokableMetadataKey = invokableMetadataKey; | ||
export function Invokable() { | ||
return (target, key, descriptor) => { | ||
const fn = descriptor.value; | ||
if (typeof fn !== 'function') { | ||
throw new convector_core_errors_1.ControllerInvalidFunctionError(); | ||
throw new ControllerInvalidFunctionError(); | ||
} | ||
const invokables = Reflect.getMetadata(exports.invokableMetadataKey, target.constructor) || {}; | ||
Reflect.defineMetadata(exports.invokableMetadataKey, Object.assign(Object.assign({}, invokables), { [key]: true }), target.constructor); | ||
descriptor.value = function internalFn(_, args, extras, ...rest) { | ||
return tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
let internalCall = false; | ||
if ('_internal_invokable' in this) { | ||
args = [_, args, extras, ...rest].filter(param => param !== undefined); | ||
internalCall = true; | ||
extras = {}; | ||
const invokables = Reflect.getMetadata(invokableMetadataKey, target.constructor) || {}; | ||
Reflect.defineMetadata(invokableMetadataKey, { | ||
...invokables, | ||
[key]: true | ||
}, target.constructor); | ||
descriptor.value = async function internalFn(_, args, extras, ...rest) { | ||
let internalCall = false; | ||
if ('_internal_invokable' in this) { | ||
args = [_, args, extras, ...rest].filter(param => param !== undefined); | ||
internalCall = true; | ||
extras = {}; | ||
} | ||
const schemas = Reflect.getOwnMetadata(paramMetadataKey, target, key); | ||
if (schemas) { | ||
const optionals = Reflect.getOwnMetadata(optionalMetadataKey, target, key) || []; | ||
if (schemas.length - optionals.length > args.length) { | ||
throw new ControllerInvalidInvokeError(key, args.length, schemas.length - optionals.length); | ||
} | ||
const schemas = Reflect.getOwnMetadata(param_decorator_1.paramMetadataKey, target, key); | ||
if (schemas) { | ||
const optionals = Reflect.getOwnMetadata(optional_decorator_1.optionalMetadataKey, target, key) || []; | ||
if (schemas.length - optionals.length > args.length) { | ||
throw new convector_core_errors_1.ControllerInvalidInvokeError(key, args.length, schemas.length - optionals.length); | ||
args = await schemas.reduce(async (result, [schema, opts, model], index) => { | ||
let paramResult; | ||
if (args[index] === undefined) { | ||
if (optionals.indexOf(index) === -1) { | ||
throw new ControllerUndefinedArgumentError(index); | ||
} | ||
return [...await result, undefined]; | ||
} | ||
args = yield schemas.reduce((result, [schema, opts, model], index) => tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
let paramResult; | ||
if (args[index] === undefined) { | ||
if (optionals.indexOf(index) === -1) { | ||
throw new convector_core_errors_1.ControllerUndefinedArgumentError(index); | ||
} | ||
return [...yield result, undefined]; | ||
try { | ||
if (opts.update) { | ||
paramResult = schema.cast(args[index], opts); | ||
} | ||
else { | ||
paramResult = await schema.validate(args[index], opts); | ||
} | ||
} | ||
catch (e) { | ||
throw new ControllerInvalidArgumentError(e, index, args[index]); | ||
} | ||
if (model) { | ||
try { | ||
if (opts.update) { | ||
paramResult = schema.cast(args[index], opts); | ||
} | ||
else { | ||
paramResult = yield schema.validate(args[index], opts); | ||
} | ||
paramResult = new model(JSON.parse(args[index])); | ||
} | ||
catch (e) { | ||
throw new convector_core_errors_1.ControllerInvalidArgumentError(e, index, args[index]); | ||
throw new ControllerArgumentParseError(e, index, args[index]); | ||
} | ||
if (model) { | ||
try { | ||
paramResult = new model(JSON.parse(args[index])); | ||
} | ||
catch (e) { | ||
throw new convector_core_errors_1.ControllerArgumentParseError(e, index, args[index]); | ||
} | ||
} | ||
return [...yield result, paramResult]; | ||
}), Promise.resolve([])); | ||
} | ||
const namespace = Reflect.getMetadata(controller_decorator_1.controllerMetadataKey, target.constructor); | ||
const ctx = Object.create(internalCall ? this : this[namespace], Object.assign(Object.assign({}, (extras || {})), { _internal_invokable: { value: true } })); | ||
try { | ||
const response = yield fn.call(ctx, ...args); | ||
return typeof response === 'object' ? JSON.parse(JSON.stringify(response)) : response; | ||
} | ||
catch (e) { | ||
const error = new Error(e.message); | ||
error.stack = e.stack; | ||
throw error; | ||
} | ||
} | ||
return [...await result, paramResult]; | ||
}, Promise.resolve([])); | ||
} | ||
const namespace = Reflect.getMetadata(controllerMetadataKey, target.constructor); | ||
const ctx = Object.create(internalCall ? this : this[namespace], { | ||
...(extras || {}), | ||
_internal_invokable: { value: true } | ||
}); | ||
try { | ||
const response = await fn.call(ctx, ...args); | ||
return typeof response === 'object' ? JSON.parse(JSON.stringify(response)) : response; | ||
} | ||
catch (e) { | ||
const error = new Error(e.message); | ||
error.stack = e.stack; | ||
throw error; | ||
} | ||
}; | ||
}; | ||
} | ||
exports.Invokable = Invokable; | ||
function getInvokables(controller) { | ||
export function getInvokables(controller) { | ||
let namespace; | ||
let invokables; | ||
try { | ||
namespace = Reflect.getMetadata(controller_decorator_1.controllerMetadataKey, controller); | ||
namespace = Reflect.getMetadata(controllerMetadataKey, controller); | ||
if (!namespace) { | ||
@@ -91,6 +90,6 @@ throw new TypeError(); | ||
catch (e) { | ||
throw new convector_core_errors_1.ControllerNamespaceMissingError(e, controller.name); | ||
throw new ControllerNamespaceMissingError(e, controller.name); | ||
} | ||
try { | ||
invokables = Reflect.getMetadata(exports.invokableMetadataKey, controller); | ||
invokables = Reflect.getMetadata(invokableMetadataKey, controller); | ||
if (!invokables) { | ||
@@ -101,3 +100,3 @@ throw new TypeError(); | ||
catch (e) { | ||
throw new convector_core_errors_1.ControllerInvokablesMissingError(e, namespace); | ||
throw new ControllerInvokablesMissingError(e, namespace); | ||
} | ||
@@ -109,3 +108,2 @@ return { | ||
} | ||
exports.getInvokables = getInvokables; | ||
//# sourceMappingURL=invokable.decorator.js.map |
@@ -1,16 +0,12 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.OptionalParam = exports.optionalMetadataKey = void 0; | ||
const g = require("window-or-global"); | ||
require("reflect-metadata"); | ||
exports.optionalMetadataKey = g.ConvectorOptionalParamMetadataKey || Symbol('optional-param'); | ||
g.ConvectorOptionalParamMetadataKey = exports.optionalMetadataKey; | ||
function OptionalParam() { | ||
import * as g from 'window-or-global'; | ||
import 'reflect-metadata'; | ||
export const optionalMetadataKey = g.ConvectorOptionalParamMetadataKey || Symbol('optional-param'); | ||
g.ConvectorOptionalParamMetadataKey = optionalMetadataKey; | ||
export function OptionalParam() { | ||
return (target, propertyKey, parameterIndex) => { | ||
const optionals = Reflect.getOwnMetadata(exports.optionalMetadataKey, target, propertyKey) || []; | ||
const optionals = Reflect.getOwnMetadata(optionalMetadataKey, target, propertyKey) || []; | ||
optionals.push(parameterIndex); | ||
Reflect.defineMetadata(exports.optionalMetadataKey, optionals, target, propertyKey); | ||
Reflect.defineMetadata(optionalMetadataKey, optionals, target, propertyKey); | ||
}; | ||
} | ||
exports.OptionalParam = OptionalParam; | ||
//# sourceMappingURL=optional.decorator.js.map |
@@ -1,20 +0,16 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Param = exports.paramMetadataKey = void 0; | ||
const g = require("window-or-global"); | ||
const yup_1 = require("yup"); | ||
require("reflect-metadata"); | ||
import * as g from 'window-or-global'; | ||
import { object } from 'yup'; | ||
import 'reflect-metadata'; | ||
const isSchema = (schema) => 'validate' in schema; | ||
exports.paramMetadataKey = g.ConvectorParamMetadataKey || Symbol('param'); | ||
g.ConvectorParamMetadataKey = exports.paramMetadataKey; | ||
function Param(model, opts = { context: {} }) { | ||
export const paramMetadataKey = g.ConvectorParamMetadataKey || Symbol('param'); | ||
g.ConvectorParamMetadataKey = paramMetadataKey; | ||
export function Param(model, opts = { context: {} }) { | ||
return (target, propertyKey, parameterIndex) => { | ||
const schemas = Reflect.getOwnMetadata(exports.paramMetadataKey, target, propertyKey) || []; | ||
const schema = isSchema(model) ? model : (0, yup_1.object)() | ||
const schemas = Reflect.getOwnMetadata(paramMetadataKey, target, propertyKey) || []; | ||
const schema = isSchema(model) ? model : object() | ||
.transform(value => value instanceof model ? value : new model(value)); | ||
schemas[parameterIndex] = [schema, opts, !isSchema(model) && model]; | ||
Reflect.defineMetadata(exports.paramMetadataKey, schemas, target, propertyKey); | ||
Reflect.defineMetadata(paramMetadataKey, schemas, target, propertyKey); | ||
}; | ||
} | ||
exports.Param = Param; | ||
//# sourceMappingURL=param.decorator.js.map |
{ | ||
"name": "@apex-pub/convector-core-controller", | ||
"version": "1.4.1-2.0", | ||
"version": "1.4.1-4-alpha.6064f94c", | ||
"description": "Convector Controller base class", | ||
@@ -53,4 +53,4 @@ "license": "Apache-2.0", | ||
"dependencies": { | ||
"@apex-pub/convector-core-errors": "^1.4.1-2.0", | ||
"@apex-pub/convector-core-model": "^1.4.1-2.0", | ||
"@apex-pub/convector-core-errors": "1.4.1-4-alpha.6064f94c", | ||
"@apex-pub/convector-core-model": "1.4.1-4-alpha.6064f94c", | ||
"window-or-global": "^1.0.1", | ||
@@ -57,0 +57,0 @@ "yup": "^0.26.10" |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
28242
183
+ Added@apex-pub/convector-core-errors@1.4.1-4-alpha.6064f94c(transitive)
+ Added@apex-pub/convector-core-model@1.4.1-4-alpha.6064f94c(transitive)
+ Added@apex-pub/convector-core-storage@1.4.1-4-alpha.6064f94c(transitive)
- Removed@apex-pub/convector-core-errors@1.5.0(transitive)
- Removed@apex-pub/convector-core-model@1.5.0(transitive)
- Removed@apex-pub/convector-core-storage@1.5.0(transitive)
- Removedtslib@1.14.1(transitive)
Updated@apex-pub/convector-core-errors@1.4.1-4-alpha.6064f94c
Updated@apex-pub/convector-core-model@1.4.1-4-alpha.6064f94c