diff --git a/amqp-rpc/amqp-rpc-tests.ts b/amqp-rpc/amqp-rpc-tests.ts new file mode 100644 index 0000000000..30d740d3e5 --- /dev/null +++ b/amqp-rpc/amqp-rpc-tests.ts @@ -0,0 +1,42 @@ +/// +import amqp_rpc = require('amqp-rpc'); +var rpc = amqp_rpc.factory(); + +interface Name { + name?: string; +} + +rpc.on('inc', function (param, cb) { + var prevVal = param; + var nextVal = param + 2; + cb(++param, prevVal, nextVal); +}); + +rpc.on('say.*', function (param, cb, inf) { + var arr = inf.cmd.split('.'); + var name = (param && param.name) ? param.name : 'world'; + cb(arr[1] + ' ' + name + '!'); +}); + +rpc.on('withoutCB', function (param, cb, inf) { + if (cb) { + cb('please run function without cb parameter') + } + else { + console.log('this is function withoutCB'); + } +}); + +rpc.call('inc', 5, function (param1, param2, param3) { + console.log(param1, param2, param3); +}); + +rpc.call('say.Hello', { name: 'John' }, function (msg) { + console.log('results of say.Hello:', msg); //output: Hello John! +}); + +rpc.call('withoutCB', {}, function (msg) { + console.log('withoutCB results:', msg); //output: please run function without cb parameter +}); + +rpc.call('withoutCB', {}); //output message on server side console \ No newline at end of file diff --git a/amqp-rpc/amqp-rpc.d.ts b/amqp-rpc/amqp-rpc.d.ts new file mode 100644 index 0000000000..a848fd4c40 --- /dev/null +++ b/amqp-rpc/amqp-rpc.d.ts @@ -0,0 +1,68 @@ +// Type definitions for amqp-rpc v0.0.8 +// Project: https://github.com/demchenkoe/node-amqp-rpc/ +// Definitions by: Wonshik Kim +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +/// + +declare module "amqp-rpc" { + + export interface Options { + connection?: any; + url?: string; + exchangeInstance?: any; + exchange?: string; + exchange_options?: { + exclusive?: boolean; + autoDelete?: boolean; + }; + ipml_options?: { + defaultExchangeName?: string; + } + conn_options?: any; + } + + export interface CallOptions { + correlationId?: string; + autoDeleteCallback?: any; + } + + export interface HandlerOptions { + queueName?: string; + durable?: boolean; + exclusive?: boolean; + autoDelete?: boolean; + } + + export interface BroadcastOptions { + ttl?: boolean; + onResponse?: any; + context?: any; + onComplete?: any; + } + + export interface CommandInfo { + cmd?: string; + exchange?: string; + contentType?: string; + size?: number; + } + + export interface Callback { + (...args: any[]): void; + } + + export function factory(opt?: Options): amqpRPC; + + export class amqpRPC { + constructor(opt?: Options); + generateQueueName(type: string): string; + disconnect(): void; + call(cmd: string, params: T, cb?: Callback, context?: any, options?: CallOptions): string; + on(cmd: string, cb: (param?: T, cb?: Callback, info?: CommandInfo) => void, context?: any, options?: HandlerOptions): boolean; + off(cmd: string): boolean; + callBroadcast(cmd: string, params: any, options: BroadcastOptions): void; + onBroadcast(cmd: string, cb: (err: any) => void, context: any, options?: any): boolean; + offBroadcast(cmd: string): boolean; + } +} \ No newline at end of file