From 5d61f02af8e08fe84b0c004693674a3103dde9ed Mon Sep 17 00:00:00 2001 From: Jakub Olek Date: Tue, 10 Jun 2014 20:26:51 +0200 Subject: [PATCH 1/3] Working on hapi.d.ts --- CONTRIBUTORS.md | 1 + hapi/hapi-tests.ts | 18 ++ hapi/hapi.d.ts | 430 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 449 insertions(+) create mode 100644 hapi/hapi-tests.ts create mode 100644 hapi/hapi.d.ts diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index bb0933af5..e122304b6 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -117,6 +117,7 @@ All definitions files include a header with the author and editors, so at some p * [Google Translate API](https://developers.google.com/translate/) (by [Frank M](https://github.com/sgtfrankieboy)) * [Google Url Shortener](https://developers.google.com/url-shortener/) (by [Frank M](https://github.com/sgtfrankieboy)) * [Hammer.js](http://eightmedia.github.com/hammer.js/) (by [Boris Yankov](https://github.com/borisyankov)) +* [Hapi](http://github.com/spumko/hapi) (by [Hakubo](http://github.com/hakubo)) * [Handlebars](http://handlebarsjs.com/) (by [Boris Yankov](https://github.com/borisyankov)) * [HashMap](https://github.com/flesler/hashmap) (by [RafaƂ Wrzeszcz](https://wrzasq.pl)) * [HashSet](http://www.timdown.co.uk/jshashtable/jshashset.html) (by [Sergey Gerasimov](https://github.com/gerich-home)) diff --git a/hapi/hapi-tests.ts b/hapi/hapi-tests.ts new file mode 100644 index 000000000..6617387d8 --- /dev/null +++ b/hapi/hapi-tests.ts @@ -0,0 +1,18 @@ +/// + +import Hapi = require('hapi'); + +// Create a server with a host and port +var server = Hapi.createServer('localhost', 8000); + +// Add the route +server.route({ + method: 'GET', + path: '/hello', + handler: function (request, reply) { + reply('hello world'); + } +}); + +// Start the server +server.start(); diff --git a/hapi/hapi.d.ts b/hapi/hapi.d.ts new file mode 100644 index 000000000..0914a5367 --- /dev/null +++ b/hapi/hapi.d.ts @@ -0,0 +1,430 @@ +// Type definitions for hapi +// Project: http://github.com/spumko/hapi +// Definitions by: Hakubo +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +/// + +declare module Hapi { + export interface ServerOptions { + app?: any; + cache?: string; + cache?: { + engine: any; + }; + cors?: boolean; + cors?: { + origin?: Array; + isOriginExposed?: boolean; + matchOrigin?: boolean; + maxAge?: number; + headers?: Array; + additionalHeaders?: Array; + methods?: Array; + additionalMethods?: Array; + exposedHeaders?: Array; + additionalExposedHeaders?: Array; + credentials?: boolean; + }; + security?: boolean; + security?: { + hsts?: boolean; + hsts?: { + maxAge: number; + includeSubdomains: boolean; + }; + xframe?: boolean; + xframe?: string; + xframe?: { + rule: string; + source: any; + }; + xss?: boolean; + noOpen?: boolean; + noSniff?: boolean; + }; + debug?: { + request: Array; + }; + files?: { + relativeTo: string; + etagsCacheMaxSize: number; + }; + json?: { + replacer?: Function; + replacer?: Array; + space?: number; + }; + labels?: Array; + load?: { + maxHeapUsedBytes?: number; + maxRssBytes?: number; + maxEventLoopDelay?: number; + sampleInterval?: number; + }; + location?: string; + payload?: { + maxBytes: number; + uploads: string; + }; + plugins?: any; + router?: { + isCaseSensitive?: boolean; + stripTrailingSlash?: boolean; + }; + state?: { + cookies: { + parse?: boolean; + failAction?: string; + clearInvalid?: boolean; + strictHeader?: boolean; + } + }; + timeout?: { + server?: boolean; + server?: number; + client?: boolean; + client?: number; + socket?: boolean; + socket?: number; + }; + tls?: any; //This should be Node.tls + maxSockets?: number; + validation?: any; + views?: ServerView; + } + + export class Pack { + require(name: string, options: {}, callback: Function): void; + } + + interface ServerView { + engines: { + module: string; + compile: (template: string, options: any): (context: any, options: any); + compile: (template: string, options: any, callback: (err: any, compiled: (context: any, options: any, callback: (err: any, rendered: boolean)))): void; + }; + defaultExtension: string; + path?: string; + partialsPath?: string; + helpersPath?: string; + basePath: string; + layout?: boolean; + layoutPath?: string; + layoutKeyword?: string; + encoding?: string; + isCached?: boolean; + allowAbsolutePaths?: boolean; + allowInsecureAccess?: boolean; + compileOptions?: any; + runtimeOptions?: any; + contentType?: string; + compileMode?: string; + } + + interface RouteOptions { + path: string; + method: string; + vhost?: string; + vhost?: Array; + handler: string; + handler: (); + handler: { + file: string; + file: (request: Request); + file: { + path: string; + filename?: string; + mode?: boolean; + mode?: string; + lookupCompressed: boolean; + }; + directory: { + path: string; + path: Array; + path: (request: Request): string; + path: (request: Request): Array; + index?: boolean; + listing?: boolean; + showHidden?: boolean; + redirectToSlash?: boolean; + lookupCompressed?: boolean; + defaultExtension?: string; + }; + proxy?: { + host?: string; + port?: number; + protocol?: string; + uri?: string; + passThrough?: boolean; + rejectUnauthorized?: boolean; + xforward?: boolean; + redirects?: boolean; + redirects?: number; + timeout?: number; + + mapUri?: (request: Request, callback: (err: any, uri: string, headers: {[key: string]: string}): void): void; + onResponse: ( + err: any, + res: any,//Node Response + req: any,//Node Request + reply: (): void, + settings: any, + ttl: number + ); + ttl: number; + }; + view: string; + view: { + template: string; + context: { + payload: any; + params: any; + query: any; + pre: any; + } + }; + config: { + handler: any; + bind: any; + app: any; + plugins: { + [name: string]: any; + }; + pre: Array<()>; + validate: { + headers: any; + params: any; + query: any; + payload: any; + errorFields?: any; + failAction?: string; + failAction?: (source: string, error: any, next: ()); + }; + payload: { + output: { + data: any; + stream: any; + file: any; + }; + parse?: any; + allow?: string; + allow?: Array; + override?: string; + maxBytes?: number; + uploads?: number; + failAction?: string; + }; + response: { + schema: any; + sample: number; + failAction: string; + }; + cache: { + privacy: string; + expiresIn: number; + expiresAt: number; + }; + auth: string; + auth: boolean; + auth: { + mode: string; + strategies: Array; + payload?: boolean; + payload?: string; + tos?: boolean; + tos?: string; + scope?: string; + scope?: Array; + entity: string; + }; + cors?: boolean; + jsonp?: string; + description?: string; + notes?: string; + notes?: Array; + tags?: Array; + } + }; + } + + export class Server { + app: any; + methods: Array; + info: { + port: number; + host?: string; + protocol?: string; + uri?: string; + }; + listener: any;// Node Http server + load: { + eventLoopDelay: number; + heapUsed: number; + rss: number; + }; + pack: Pack; + plugins: { + [pluginName: string]: any; + }; + + + start(callback?: ()): void; + stop(options?: {timeout: number;}, callback?: ()): void; + route(options: RouteOptions): void; + route(routes: Array): void; + table(host?: string): Array; + log(tags: string, data?: string, timestamp?: number): void; + log(tags: Array, data?: string, timestamp?: number): void; + log(tags: string, data?: any, timestamp?: number): void; + log(tags: Array, data?: any, timestamp?: number): void; + state(name: string, options?: { + ttl: number; + isSecure: boolean; + isHttpOnly: boolean; + path: string; + domain: string; + autoValue: (request: Request, next: (err: any, value: any): void): void; + encoding?: string; + sign: any; + password: string; + iron: any; + }); + views: (options: ServerView): void; + cache: (name: string, options: { + expiresIn: number; + expiresAt: number; + staleIn: number; + staleTimeout: number; + cache: string; + }): void; + + auth: { + scheme: (name: string, scheme: { + name: string; + scheme: (server: Server, options: any): (authenticate: any, payload: any, response: any); + }); + strategy: any; + }; + ext: (event: any, method: string, options?: any): void; + method: (method: Array<{name: string; fn: (); options: any}>): void; + method: (name: string, fn: (), options: any): void; + inject: (options: any, callback: any): void; + handler: (name: string, method: (name: string, options: any): void): void; + } + + export interface Request { + app: any; + auth: { + isAuthenticated: boolean; + credentials: Object; + artifacts: Object; + session: Object + }; + domain: any; + headers: Object; + id: number; + info: { + received: number; + remoteAddress: string; + remotePort: number; + referrer: string; + host: string; + }; + method: string; + mime?: string; + params: any; + path: string; + payload: any; + plugins: Object; + pre: Object; + response: Object; + responses: Object; + query: Object; + raw: { + req: any; //http.ClientRequest + res: any; //http.ClientResponse + }; + route: string; + server: Server; + session: any; + state: Object; + url: Object; + + setUrl? (url: string): void; + setMethod? (method: string): void; + log (tags: string, data?: string, timestamp?: number): void; + log (tags: string, data?: Object, timestamp?: number): void; + log (tags: string[], data?: string, timestamp?: number): void; + log (tags: string[], data?: Object, timestamp?: number): void; + getLog(): string[]; + getLog(tag: string): string[]; + getLog(tags: string[]): string[]; + tail(name?: string): Function; + } + + export interface Response { + statusCode: number; + headers: Object; + source: any; + variety: string; + app: any; + plugins: Object; + settings: { + encoding: string; + charset: string; + location: string; + ttl: number; + stringify: any; + passThrough: boolean; + } + + code (statusCode: number): void; + header (name: string, value: string, options?: { + append: boolean; + separator: string; + override: boolean; + }): void; + type (mimeType: string): void; + bytes (length: number): void; + vary (header: string): void; + location (location: string): void; + created (location: string): void; + redirect (location: string): void; + encoding (encoding: string): void; + charset (charset: string): void; + ttl (ttl: number): void; + state (name: string, value: string, options?: any): void; + unstate (name: string): void; + + replacer (method: Function): void; + replacer (method: Array): void; + spaces (count: number): void; + + temporary (isTemporary: boolean): void; + permanent (isPermanent: boolean): void; + rewritable (isRewritable: boolean): void; + } + + export module reply { + function file(path: string, options: { + filePath: string; + options: { + filename: string; + mode: string + } + }): void; + + function view(template: string, context?: Object, options?: Object): Response; + function close(options?: Object): void; + function proxy(options: Object): void; + + export function reply(result: any): any; + } + + export function createServer (host: string, port: number, options?: ServerOptions): Server; +} + +declare module "hapi" { + export = Hapi; +} From 60bc96cefb2304db0d2f3ad198696157fab065ae Mon Sep 17 00:00:00 2001 From: Jakub Olek Date: Tue, 10 Jun 2014 21:42:36 +0200 Subject: [PATCH 2/3] I have no idea how to do union types for properties and how to use node modules --- hapi/hapi-tests.ts | 10 +- hapi/hapi.d.ts | 394 ++++++++++++++++++++++++--------------------- 2 files changed, 219 insertions(+), 185 deletions(-) diff --git a/hapi/hapi-tests.ts b/hapi/hapi-tests.ts index 6617387d8..fcc870af6 100644 --- a/hapi/hapi-tests.ts +++ b/hapi/hapi-tests.ts @@ -9,10 +9,18 @@ var server = Hapi.createServer('localhost', 8000); server.route({ method: 'GET', path: '/hello', - handler: function (request, reply) { + handler: function (request: Hapi.Request, reply: Function) { reply('hello world'); } }); +server.route([{ + method: 'GET', + path: '/hello2', + handler: function (request: Hapi.Request, reply: Function) { + reply('hello world2'); + } +}]); + // Start the server server.start(); diff --git a/hapi/hapi.d.ts b/hapi/hapi.d.ts index 0914a5367..cf2d966a0 100644 --- a/hapi/hapi.d.ts +++ b/hapi/hapi.d.ts @@ -8,41 +8,44 @@ declare module Hapi { export interface ServerOptions { app?: any; - cache?: string; - cache?: { - engine: any; - }; - cors?: boolean; - cors?: { - origin?: Array; - isOriginExposed?: boolean; - matchOrigin?: boolean; - maxAge?: number; - headers?: Array; - additionalHeaders?: Array; - methods?: Array; - additionalMethods?: Array; - exposedHeaders?: Array; - additionalExposedHeaders?: Array; - credentials?: boolean; - }; - security?: boolean; - security?: { - hsts?: boolean; - hsts?: { - maxAge: number; - includeSubdomains: boolean; - }; - xframe?: boolean; - xframe?: string; - xframe?: { - rule: string; - source: any; - }; - xss?: boolean; - noOpen?: boolean; - noSniff?: boolean; - }; +// cache?: string; +// cache?: { +// engine: any; +// }; + cache?: any; +// cors?: boolean; +// cors?: { +// origin?: Array; +// isOriginExposed?: boolean; +// matchOrigin?: boolean; +// maxAge?: number; +// headers?: Array; +// additionalHeaders?: Array; +// methods?: Array; +// additionalMethods?: Array; +// exposedHeaders?: Array; +// additionalExposedHeaders?: Array; +// credentials?: boolean; +// }; + cors?: any; +// security?: boolean; +// security?: { +// hsts?: boolean; +// hsts?: { +// maxAge: number; +// includeSubdomains: boolean; +// }; +// xframe?: boolean; +// xframe?: string; +// xframe?: { +// rule: string; +// source: any; +// }; +// xss?: boolean; +// noOpen?: boolean; +// noSniff?: boolean; +// }; + security?: any; debug?: { request: Array; }; @@ -51,8 +54,9 @@ declare module Hapi { etagsCacheMaxSize: number; }; json?: { - replacer?: Function; - replacer?: Array; +// replacer?: () => void; +// replacer?: Array<() => void>; + replacer?: any; space?: number; }; labels?: Array; @@ -81,12 +85,15 @@ declare module Hapi { } }; timeout?: { - server?: boolean; - server?: number; - client?: boolean; - client?: number; - socket?: boolean; - socket?: number; +// server?: boolean; +// server?: number; + server?: any; +// client?: boolean; +// client?: number; + client?: any; +// socket?: boolean; +// socket?: number; + socket?: any; }; tls?: any; //This should be Node.tls maxSockets?: number; @@ -98,11 +105,29 @@ declare module Hapi { require(name: string, options: {}, callback: Function): void; } - interface ServerView { + export interface ServerView { engines: { module: string; - compile: (template: string, options: any): (context: any, options: any); - compile: (template: string, options: any, callback: (err: any, compiled: (context: any, options: any, callback: (err: any, rendered: boolean)))): void; +// compile: ( +// template: string, +// options: any +// ) => void; +// compile: ( +// template: string, +// options: any, +// callback: ( +// err: any, +// compiled: ( +// context: any, +// options: any, +// callback: ( +// err: any, +// rendered: boolean +// ) => void +// ) => void +// ) => void +// ) => void; + compile: any; }; defaultExtension: string; path?: string; @@ -122,135 +147,137 @@ declare module Hapi { compileMode?: string; } - interface RouteOptions { + export interface RouteOptions { path: string; method: string; - vhost?: string; - vhost?: Array; - handler: string; - handler: (); - handler: { - file: string; - file: (request: Request); - file: { - path: string; - filename?: string; - mode?: boolean; - mode?: string; - lookupCompressed: boolean; - }; - directory: { - path: string; - path: Array; - path: (request: Request): string; - path: (request: Request): Array; - index?: boolean; - listing?: boolean; - showHidden?: boolean; - redirectToSlash?: boolean; - lookupCompressed?: boolean; - defaultExtension?: string; - }; - proxy?: { - host?: string; - port?: number; - protocol?: string; - uri?: string; - passThrough?: boolean; - rejectUnauthorized?: boolean; - xforward?: boolean; - redirects?: boolean; - redirects?: number; - timeout?: number; - - mapUri?: (request: Request, callback: (err: any, uri: string, headers: {[key: string]: string}): void): void; - onResponse: ( - err: any, - res: any,//Node Response - req: any,//Node Request - reply: (): void, - settings: any, - ttl: number - ); - ttl: number; - }; - view: string; - view: { - template: string; - context: { - payload: any; - params: any; - query: any; - pre: any; - } - }; - config: { - handler: any; - bind: any; - app: any; - plugins: { - [name: string]: any; - }; - pre: Array<()>; - validate: { - headers: any; - params: any; - query: any; - payload: any; - errorFields?: any; - failAction?: string; - failAction?: (source: string, error: any, next: ()); - }; - payload: { - output: { - data: any; - stream: any; - file: any; - }; - parse?: any; - allow?: string; - allow?: Array; - override?: string; - maxBytes?: number; - uploads?: number; - failAction?: string; - }; - response: { - schema: any; - sample: number; - failAction: string; - }; - cache: { - privacy: string; - expiresIn: number; - expiresAt: number; - }; - auth: string; - auth: boolean; - auth: { - mode: string; - strategies: Array; - payload?: boolean; - payload?: string; - tos?: boolean; - tos?: string; - scope?: string; - scope?: Array; - entity: string; - }; - cors?: boolean; - jsonp?: string; - description?: string; - notes?: string; - notes?: Array; - tags?: Array; - } - }; +// vhost?: string; +// vhost?: Array; + vhost?: any; +// handler: string; +// handler: (request: Request, reply: Function) => void; +// handler: { +// file: string; +// file: (request: Request) => void; +// file: { +// path: string; +// filename?: string; +// mode?: boolean; +// mode?: string; +// lookupCompressed: boolean; +// }; +// directory: { +// path: string; +// path: Array; +// path: (request: Request) => string; +// path: (request: Request) => Array; +// index?: boolean; +// listing?: boolean; +// showHidden?: boolean; +// redirectToSlash?: boolean; +// lookupCompressed?: boolean; +// defaultExtension?: string; +// }; +// proxy?: { +// host?: string; +// port?: number; +// protocol?: string; +// uri?: string; +// passThrough?: boolean; +// rejectUnauthorized?: boolean; +// xforward?: boolean; +// redirects?: boolean; +// redirects?: number; +// timeout?: number; +// +// mapUri?: (request: Request, callback: (err: any, uri: string, headers: {[key: string]: string}) => void) => void; +// onResponse: ( +// err: any, +// res: any,//Node Response +// req: any,//Node Request +// reply: () => void, +// settings: any, +// ttl: number +// ) => void; +// ttl: number; +// }; +// view: string; +// view: { +// template: string; +// context: { +// payload: any; +// params: any; +// query: any; +// pre: any; +// } +// }; +// config: { +// handler: any; +// bind: any; +// app: any; +// plugins: { +// [name: string]: any; +// }; +// pre: Array<() => void>; +// validate: { +// headers: any; +// params: any; +// query: any; +// payload: any; +// errorFields?: any; +// failAction?: string; +// failAction?: (source: string, error: any, next: () => void) => void; +// }; +// payload: { +// output: { +// data: any; +// stream: any; +// file: any; +// }; +// parse?: any; +// allow?: string; +// allow?: Array; +// override?: string; +// maxBytes?: number; +// uploads?: number; +// failAction?: string; +// }; +// response: { +// schema: any; +// sample: number; +// failAction: string; +// }; +// cache: { +// privacy: string; +// expiresIn: number; +// expiresAt: number; +// }; +// auth: string; +// auth: boolean; +// auth: { +// mode: string; +// strategies: Array; +// payload?: boolean; +// payload?: string; +// tos?: boolean; +// tos?: string; +// scope?: string; +// scope?: Array; +// entity: string; +// }; +// cors?: boolean; +// jsonp?: string; +// description?: string; +// notes?: string; +// notes?: Array; +// tags?: Array; +// } +// }; + handler: any; } export class Server { app: any; - methods: Array; + methods: Array<() => void>; info: { port: number; host?: string; @@ -268,9 +295,8 @@ declare module Hapi { [pluginName: string]: any; }; - - start(callback?: ()): void; - stop(options?: {timeout: number;}, callback?: ()): void; + start(callback?: () => void): void; + stop(options?: {timeout: number;}, callback?: () => void): void; route(options: RouteOptions): void; route(routes: Array): void; table(host?: string): Array; @@ -284,14 +310,14 @@ declare module Hapi { isHttpOnly: boolean; path: string; domain: string; - autoValue: (request: Request, next: (err: any, value: any): void): void; + autoValue: (request: Request, next: (err: any, value: any) => void) => void; encoding?: string; sign: any; password: string; iron: any; - }); - views: (options: ServerView): void; - cache: (name: string, options: { + }): void; + views(options: ServerView): void; + cache(name: string, options: { expiresIn: number; expiresAt: number; staleIn: number; @@ -300,17 +326,17 @@ declare module Hapi { }): void; auth: { - scheme: (name: string, scheme: { + scheme(name: string, scheme: { name: string; - scheme: (server: Server, options: any): (authenticate: any, payload: any, response: any); - }); + scheme: (server: Server, options: any) => (authenticate: any, payload: any, response: any) => void; + }): void; strategy: any; }; - ext: (event: any, method: string, options?: any): void; - method: (method: Array<{name: string; fn: (); options: any}>): void; - method: (name: string, fn: (), options: any): void; - inject: (options: any, callback: any): void; - handler: (name: string, method: (name: string, options: any): void): void; + 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; + inject(options: any, callback: any): void; + handler(name: string, method: (name: string, options: any) => void): void; } export interface Request { From cc4e279c3712229f24bd534f0748c67bb55bfa75 Mon Sep 17 00:00:00 2001 From: Jakub Olek Date: Wed, 11 Jun 2014 22:18:55 +0200 Subject: [PATCH 3/3] Fixes for createServer --- hapi/hapi.d.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/hapi/hapi.d.ts b/hapi/hapi.d.ts index cf2d966a0..68b366d6d 100644 --- a/hapi/hapi.d.ts +++ b/hapi/hapi.d.ts @@ -107,7 +107,8 @@ declare module Hapi { export interface ServerView { engines: { - module: string; + [extension: string]: string; + module?: string; // compile: ( // template: string, // options: any @@ -127,13 +128,13 @@ declare module Hapi { // ) => void // ) => void // ) => void; - compile: any; + compile?: any; }; - defaultExtension: string; + defaultExtension?: string; path?: string; partialsPath?: string; helpersPath?: string; - basePath: string; + basePath?: string; layout?: boolean; layoutPath?: string; layoutKeyword?: string;