From 6d9f08eda8b2f7e8560095801b3a3d88b5c11ca6 Mon Sep 17 00:00:00 2001 From: Shinya Ohira Date: Fri, 7 Nov 2014 23:31:02 +0900 Subject: [PATCH] Fix server.method --- hapi/hapi-tests.ts | 30 ++++++++++++++++++++++++++++++ hapi/hapi.d.ts | 4 ++-- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/hapi/hapi-tests.ts b/hapi/hapi-tests.ts index da28b97a37..586d3d29f8 100644 --- a/hapi/hapi-tests.ts +++ b/hapi/hapi-tests.ts @@ -25,6 +25,36 @@ server.pack.register([plugin], (err: Object) => { if (err) { throw err; } }); +// Add server method +var add = function (a: number, b: number, next: (err: any, result?: any, ttl?: number) => void) { + next(null, a + b); +}; + +server.method('sum', add, { cache: { expiresIn: 2000 } }); + +server.methods.sum(4, 5, (err: any, result: any) => { + console.log(result); +}); + +var addArray = function (array: Array, next: (err: any, result?: any, ttl?: number) => void) { + var sum: number = 0; + array.forEach((item: number) => { + sum += item; + }); + next(null, sum); +}; + +server.method('sumObj', addArray, { + cache: { expiresIn: 2000 }, + generateKey: (array: Array) => { + return array.join(','); + } +}); + +server.methods.sumObj([5, 6], (err: any, result: any) => { + console.log(result); +}); + // Add the route server.route({ method: 'GET', diff --git a/hapi/hapi.d.ts b/hapi/hapi.d.ts index 09453737ad..de65efcdc8 100644 --- a/hapi/hapi.d.ts +++ b/hapi/hapi.d.ts @@ -279,7 +279,7 @@ declare module Hapi { export class Server { app: any; - methods: Array<() => void>; + methods: any; info: { port: number; host?: string; @@ -336,7 +336,7 @@ declare module Hapi { }; ext(event: any, method: string, options?: any): void; method(method: Array<{name: string; fn: () => void; options: any}>): void; - method(name: string, fn: () => void, options: any): void; + method(name: string, fn: Function, options: any): void; inject(options: any, callback: any): void; handler(name: string, method: (name: string, options: any) => void): void; }