@imqueue/rpc
Version:
RPC-like client-service implementation over messaging queue
118 lines • 3.39 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.property = property;
/*!
* IMQ-RPC Decorators: property
*
* Copyright (c) 2018, imqueue.com <support@imqueue.com>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
const __1 = require("..");
/**
* Implements '@property' decorator factory
* This is used to specify complex service types to be exposed
*
* @example
* ~~~typescript
* import { property, expose, IMQService } from '@imqueue/rpc';
*
* class Address {
* @property('string')
* country: string;
*
* @property('string')
* city: string;
*
* @property('string')
* address: string;
*
* @property('string', true)
* zipCode?: string; // this is optional
* }
*
* class User {
* @property('string')
* firstName: string;
*
* @property('string')
* lastName: string;
*
* @property('string')
* email: string;
*
* @property('Array<Address>', true)
* address?: Array<Address>;
* }
*
* // now we can use those complex types as service methods args
* // and them will be properly exposed to service clients
*
* class UserService extends IMQService {
*
* @expose()
* public save(user: User) {
* // do smth with given user data to persis it
* }
*
* @expose()
* public find(id: number): User {
* // find and return user
* }
*
* }
* ~~~
*
* @return {(
* target: any,
* methodName: (string),
* descriptor: TypedPropertyDescriptor<(...args: any[]) => any>
* ) => void}
*/
function property(type, isOptional = false) {
// istanbul ignore if
if (!type) {
return;
}
return function (target, propertyKey) {
const typeName = target.constructor.name;
let typeDef;
if (typeof type === 'function' && !type.name) {
type = type();
}
typeDef = type;
if (Array.isArray(typeDef)) {
typeDef = typeDef[0];
}
if (typeDef && typeof typeDef !== 'string') {
typeDef = typeDef.name;
}
if (Array.isArray(type)) {
typeDef += '[]';
}
// istanbul ignore if
if (!typeDef) {
typeDef = String(type);
}
__1.IMQRPCDescription.typesDescription[typeName] =
__1.IMQRPCDescription.typesDescription[typeName] || {
properties: {},
inherits: Object.getPrototypeOf(target.constructor).name,
};
__1.IMQRPCDescription.typesDescription[typeName].properties[propertyKey] = {
type: typeDef,
isOptional,
};
};
}
//# sourceMappingURL=property.js.map