moleculer-decorators
Advanced tools
Comparing version 1.0.14 to 1.0.15
@@ -1,12 +0,14 @@ | ||
import { ServiceSchema, Action, ActionHandler, LoggerInstance, ServiceMethods, ServiceEvents, Actions, ServiceSettingSchema, GenericObject, ServiceEvent, ServiceEventHandler, ServiceLocalEventHandler } from 'moleculer'; | ||
import { ServiceSchema, Action, ActionHandler, LoggerInstance, ServiceMethods, ServiceEvents, Actions, ServiceSettingSchema, GenericObject, ServiceBroker, ServiceEvent, ServiceEventHandler } from 'moleculer'; | ||
export declare class BaseSchema { | ||
[x: string]: any; | ||
logger: LoggerInstance; | ||
name: string; | ||
version?: string | number; | ||
settings?: ServiceSettingSchema; | ||
metadata?: GenericObject; | ||
mixins?: Array<ServiceSchema>; | ||
actions?: Actions; | ||
methods?: ServiceMethods; | ||
events?: ServiceEvents; | ||
broker: ServiceBroker; | ||
version: string | number; | ||
settings: ServiceSettingSchema; | ||
metadata: GenericObject; | ||
mixins: Array<ServiceSchema>; | ||
actions: Actions; | ||
methods: ServiceMethods; | ||
events: ServiceEvents; | ||
} | ||
@@ -25,3 +27,3 @@ export interface Options extends Partial<ServiceSchema> { | ||
group?: string; | ||
handler?: ServiceEventHandler | ServiceLocalEventHandler; | ||
handler?: ServiceEventHandler; | ||
} | ||
@@ -28,0 +30,0 @@ export declare function Method(target: any, key: any, descriptor: any): void; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const _ = require("lodash"); | ||
const blacklist = ['created', 'started', 'stopped', 'actions', 'methods', 'events']; | ||
const blacklist = [ | ||
'created', | ||
'started', | ||
'stopped', | ||
'actions', | ||
'methods', | ||
'events', | ||
'broker', | ||
'logger' | ||
]; | ||
const blacklist2 = ['metadata', 'settings', 'mixins', 'name', 'version'].concat(blacklist); | ||
const defaultServiceOptions = { | ||
constructOverride: true | ||
constructOverride: true, | ||
skipHandler: false | ||
}; | ||
class BaseSchema { | ||
} | ||
exports.BaseSchema = BaseSchema; | ||
function Method(target, key, descriptor) { | ||
@@ -18,3 +25,4 @@ (target.methods || (target.methods = {}))[key] = descriptor.value; | ||
return function (target, key, descriptor) { | ||
(target.events || (target.events = {}))[key] = (options ? Object.assign({}, options, { handler: descriptor.value }) : descriptor.value); | ||
(target.events || (target.events = {}))[key] = options | ||
? Object.assign({}, options, { handler: descriptor.value }) : descriptor.value; | ||
}; | ||
@@ -28,3 +36,9 @@ } | ||
} | ||
(target.actions || (target.actions = {}))[key] = (options ? Object.assign({}, options) : (options.skipHandler ? '' : descriptor.value)); | ||
else { | ||
delete options.skipHandler; | ||
} | ||
(target.actions || (target.actions = {}))[key] = options | ||
? Object.assign({}, options) : options.skipHandler | ||
? '' | ||
: descriptor.value; | ||
}; | ||
@@ -51,5 +65,6 @@ } | ||
if (_options.constructOverride) { | ||
const ServiceClass = new target.prototype[key]; | ||
const ServiceClass = new target.prototype[key](); | ||
Object.getOwnPropertyNames(ServiceClass).forEach(function (key) { | ||
if (blacklist.indexOf(key) === -1 && !_.isFunction(ServiceClass[key])) { | ||
if (blacklist.indexOf(key) === -1 && | ||
!_.isFunction(ServiceClass[key])) { | ||
base[key] = Object.getOwnPropertyDescriptor(ServiceClass, key).value; | ||
@@ -88,3 +103,5 @@ if (blacklist2.indexOf(key) === -1) { | ||
if (key === 'events' || key === 'methods' || key === 'actions') { | ||
(base[key] ? Object.assign(base[key], descriptor.value) : base[key] = descriptor.value); | ||
base[key] | ||
? Object.assign(base[key], descriptor.value) | ||
: (base[key] = descriptor.value); | ||
return; | ||
@@ -91,0 +108,0 @@ } |
{ | ||
"name": "moleculer-decorators", | ||
"version": "1.0.14", | ||
"version": "1.0.15", | ||
"description": "decorators for moleculer", | ||
@@ -33,3 +33,3 @@ "main": "dist/index.js", | ||
"lodash": "^4.17.4", | ||
"moleculer": "^0.12.1" | ||
"moleculer": "^0.13.2" | ||
}, | ||
@@ -41,4 +41,4 @@ "devDependencies": { | ||
"rimraf": "^2.6.1", | ||
"typescript": "^2.7.2" | ||
"typescript": "^3.0.3" | ||
} | ||
} |
152
src/index.ts
@@ -1,34 +0,63 @@ | ||
import { ServiceSchema, Action, ActionHandler, LoggerInstance, ServiceMethods, ServiceEvents, Actions, Context, ServiceSettingSchema, GenericObject, ServiceBroker, ServiceEvent, ServiceEventHandler, ServiceLocalEventHandler} from 'moleculer'; | ||
import { | ||
ServiceSchema, | ||
Action, | ||
ActionHandler, | ||
LoggerInstance, | ||
ServiceMethods, | ||
ServiceEvents, | ||
Actions, | ||
ServiceSettingSchema, | ||
GenericObject, | ||
ServiceBroker, | ||
ServiceEvent, | ||
ServiceEventHandler | ||
} from 'moleculer'; | ||
import * as _ from 'lodash'; | ||
import Bluebird = require('bluebird'); | ||
const blacklist = ['created', 'started', 'stopped', 'actions', 'methods', 'events']; | ||
const blacklist2 = ['metadata', 'settings', 'mixins', 'name', 'version'].concat(blacklist); | ||
const blacklist = [ | ||
'created', | ||
'started', | ||
'stopped', | ||
'actions', | ||
'methods', | ||
'events', | ||
'broker', | ||
'logger' | ||
]; | ||
const blacklist2 = ['metadata', 'settings', 'mixins', 'name', 'version'].concat( | ||
blacklist | ||
); | ||
const defaultServiceOptions: Options = { | ||
constructOverride: true | ||
} | ||
constructOverride: true, | ||
skipHandler: false // not needed, just for clarity | ||
}; | ||
// Needed for intellisense only pretty much. | ||
export class BaseSchema { | ||
export declare class BaseSchema { | ||
[x: string]: any; | ||
logger: LoggerInstance; | ||
name: string; | ||
version?: string | number; | ||
settings?: ServiceSettingSchema; | ||
metadata?: GenericObject; | ||
mixins?: Array<ServiceSchema>; | ||
broker: ServiceBroker; | ||
actions?: Actions; | ||
methods?: ServiceMethods; | ||
events?: ServiceEvents; | ||
version: string | number; | ||
settings: ServiceSettingSchema; | ||
metadata: GenericObject; | ||
mixins: Array<ServiceSchema>; | ||
actions: Actions; | ||
methods: ServiceMethods; | ||
events: ServiceEvents; | ||
} | ||
export interface Options extends Partial<ServiceSchema> { | ||
name?: string | ||
constructOverride?: boolean | ||
name?: string; | ||
constructOverride?: boolean; | ||
} | ||
export interface ActionOptions extends Partial<Action> { | ||
name?: string, | ||
handler?: ActionHandler<any>, // Not really used | ||
skipHandler?: boolean | ||
name?: string; | ||
handler?: ActionHandler<any>; // Not really used | ||
skipHandler?: boolean; | ||
} | ||
@@ -39,7 +68,7 @@ | ||
group?: string; | ||
handler?: ServiceEventHandler | ServiceLocalEventHandler; // not really used | ||
handler?: ServiceEventHandler; // not really used | ||
} | ||
export function Method(target, key, descriptor) { | ||
(target.methods || (target.methods = {}))[key] = descriptor.value | ||
(target.methods || (target.methods = {}))[key] = descriptor.value; | ||
} | ||
@@ -49,7 +78,9 @@ | ||
return function(target, key, descriptor) { | ||
(target.events || (target.events = {}))[key] = (options ? { | ||
...options, | ||
handler: descriptor.value | ||
} : descriptor.value); | ||
} | ||
(target.events || (target.events = {}))[key] = options | ||
? { | ||
...options, | ||
handler: descriptor.value | ||
} | ||
: descriptor.value; | ||
}; | ||
} | ||
@@ -61,13 +92,19 @@ | ||
options.handler = descriptor.value; | ||
} else { | ||
delete options.skipHandler; | ||
} | ||
(target.actions || (target.actions = {}))[key] = (options ? { | ||
...options, | ||
} : (options.skipHandler ? '' : descriptor.value)); | ||
} | ||
(target.actions || (target.actions = {}))[key] = options | ||
? { | ||
...options | ||
} | ||
: options.skipHandler | ||
? '' | ||
: descriptor.value; | ||
}; | ||
} | ||
export function Service(options: Options = {}) : any { | ||
export function Service(options: Options = {}): any { | ||
return function(target) { | ||
let base = {} | ||
let base = {}; | ||
const _options = _.extend({}, defaultServiceOptions, options); | ||
@@ -89,22 +126,34 @@ | ||
const vars = []; | ||
Object.getOwnPropertyNames(proto).forEach(function (key) { | ||
Object.getOwnPropertyNames(proto).forEach(function(key) { | ||
if (key === 'constructor') { | ||
if (_options.constructOverride) { // Override properties defined in @Service | ||
const ServiceClass = new target.prototype[key]; | ||
if (_options.constructOverride) { | ||
// Override properties defined in @Service | ||
const ServiceClass = new target.prototype[key](); | ||
Object.getOwnPropertyNames(ServiceClass).forEach(function(key) { | ||
if (blacklist.indexOf(key) === -1 && !_.isFunction(ServiceClass[key])) { | ||
base[key] = Object.getOwnPropertyDescriptor(ServiceClass, key)!.value | ||
if (blacklist2.indexOf(key) === -1) { // Needed, otherwize if the service is used as a mixin, these variables will overwrite the toplevel's | ||
vars[key] = Object.getOwnPropertyDescriptor(ServiceClass, key)!.value | ||
if ( | ||
blacklist.indexOf(key) === -1 && | ||
!_.isFunction(ServiceClass[key]) | ||
) { | ||
base[key] = Object.getOwnPropertyDescriptor( | ||
ServiceClass, | ||
key | ||
)!.value; | ||
if (blacklist2.indexOf(key) === -1) { | ||
// Needed, otherwize if the service is used as a mixin, these variables will overwrite the toplevel's | ||
vars[key] = Object.getOwnPropertyDescriptor( | ||
ServiceClass, | ||
key | ||
)!.value; | ||
} | ||
} | ||
}); | ||
/* Insane hack below :D | ||
* It's needed since moleculer don't transfer all defined props in the schema to the actual service, so we have to do it. | ||
* Side note: This is quite hacky and would be a performance loss if the created function would be called over and over, since it's called once, it's more than fine :) | ||
*/ | ||
*/ | ||
const bypass: any = Object.defineProperty, // typescript fix | ||
obj: any = {}; // placeholder | ||
obj: any = {}; // placeholder | ||
@@ -114,9 +163,12 @@ // Defining our 'own' created function | ||
value: function created(broker: ServiceBroker) { | ||
for (let key in vars) { | ||
for (let key in vars) { | ||
this[key] = vars[key]; | ||
} | ||
// Check if user defined a created function, if so, we need to call it after ours. | ||
if (!_.isNil(Object.getOwnPropertyDescriptor(proto, 'created'))) { | ||
Object.getOwnPropertyDescriptor(proto, 'created').value.call(this, broker); | ||
Object.getOwnPropertyDescriptor(proto, 'created').value.call( | ||
this, | ||
broker | ||
); | ||
} | ||
@@ -134,3 +186,3 @@ }, | ||
const descriptor = Object.getOwnPropertyDescriptor(proto, key)! | ||
const descriptor = Object.getOwnPropertyDescriptor(proto, key)!; | ||
@@ -147,3 +199,5 @@ if (key === 'created' && !_options.constructOverride) { | ||
if (key === 'events' || key === 'methods' || key === 'actions') { | ||
(base[key] ? Object.assign(base[key], descriptor.value) : base[key] = descriptor.value) | ||
base[key] | ||
? Object.assign(base[key], descriptor.value) | ||
: (base[key] = descriptor.value); | ||
return; | ||
@@ -154,3 +208,3 @@ } | ||
return base; | ||
} | ||
} | ||
}; | ||
} |
@@ -16,3 +16,3 @@ import moleculer = require('moleculer'); | ||
Login(ctx) { | ||
} | ||
@@ -19,0 +19,0 @@ |
import moleculer = require('moleculer'); | ||
import { Service, Action, Method, BaseSchema } from '../index'; | ||
const broker = new moleculer.ServiceBroker({ | ||
logger: console, | ||
logLevel: "debug", | ||
@@ -10,5 +9,7 @@ }); | ||
class mixin12 extends BaseSchema { | ||
@Action() | ||
@Action({ | ||
}) | ||
Login3(ctx) { | ||
} | ||
@@ -15,0 +16,0 @@ } |
Sorry, the diff of this file is not supported yet
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
13
549
74103
+ Addedargs@5.0.3(transitive)
+ Addedbluebird@3.7.2(transitive)
+ Addedchalk@2.4.2(transitive)
+ Addedfastest-validator@0.6.19(transitive)
+ Addedglob@7.1.6(transitive)
+ Addedipaddr.js@1.9.1(transitive)
+ Addedlru-cache@5.1.1(transitive)
+ Addedmoleculer@0.13.13(transitive)
+ Addedmri@1.1.4(transitive)
+ Addedyallist@3.1.1(transitive)
- Removedargs@4.0.0(transitive)
- Removedbluebird@3.5.1(transitive)
- Removedchalk@2.3.22.4.1(transitive)
- Removedfastest-validator@0.6.9(transitive)
- Removedglob@7.1.2(transitive)
- Removedipaddr.js@1.7.0(transitive)
- Removedlodash@4.17.10(transitive)
- Removedmoleculer@0.12.8(transitive)
- Removedmri@1.1.0(transitive)
- Removednode-object-hash@1.4.1(transitive)
Updatedmoleculer@^0.13.2