From b7edd5c1db16c924636e7bfeeb3727de6bfe2eb9 Mon Sep 17 00:00:00 2001 From: Justin Simms Date: Thu, 8 Feb 2018 23:41:49 -0600 Subject: [PATCH] Add generic types for plugins --- types/hapi/definitions/plugin/plugin.d.ts | 8 +- .../definitions/server/server-register.d.ts | 21 +++++- types/hapi/definitions/server/server.d.ts | 5 +- types/hapi/test/server/server-bind.ts | 2 +- types/hapi/test/server/server-expose.ts | 4 +- types/hapi/test/server/server-options.ts | 2 +- types/hapi/test/server/server-path.ts | 2 +- types/hapi/test/server/server-plugins.ts | 74 ++++++++++++++++++- 8 files changed, 101 insertions(+), 17 deletions(-) diff --git a/types/hapi/definitions/plugin/plugin.d.ts b/types/hapi/definitions/plugin/plugin.d.ts index 4582bb152f..925ac02205 100644 --- a/types/hapi/definitions/plugin/plugin.d.ts +++ b/types/hapi/definitions/plugin/plugin.d.ts @@ -33,15 +33,17 @@ export interface PluginPackage { * certain properties. For example, setting a file path in one plugin doesn't affect the file path set * in another plugin. * [See docs](https://github.com/hapijs/hapi/blob/master/API.md#plugins) + * + * The type T is the type of the plugin options. */ -export interface PluginBase { +export interface PluginBase { /** * (required) the registration function with the signature async function(server, options) where: * * server - the server object with a plugin-specific server.realm. * * options - any options passed to the plugin during registration via server.register(). */ - register: (server: Server, options: any) => Promise; + register: (server: Server, options: T) => Promise; /** (optional) if true, allows the plugin to be registered multiple times with the same server. Defaults to false. */ multiple?: boolean; @@ -53,4 +55,4 @@ export interface PluginBase { once?: boolean; } -export type Plugin = PluginBase & (PluginNameVersion | PluginPackage); +export type Plugin = PluginBase & (PluginNameVersion | PluginPackage); diff --git a/types/hapi/definitions/server/server-register.d.ts b/types/hapi/definitions/server/server-register.d.ts index b4ce0614d6..7e7680edfb 100644 --- a/types/hapi/definitions/server/server-register.d.ts +++ b/types/hapi/definitions/server/server-register.d.ts @@ -37,14 +37,29 @@ export interface ServerRegisterOptions { * * * prefix - string added as prefix to any route path (must begin with '/'). If a plugin registers a child plugin the prefix is passed on to the child or is added in front of the child-specific prefix. * * * vhost - virtual host string (or array of strings) applied to every route. The outer-most vhost overrides the any nested configuration. * For reference [See docs](https://github.com/hapijs/hapi/blob/master/API.md#-await-serverregisterplugins-options) + * + * The type parameter T is the type of the plugin configuration options. */ -export interface ServerRegisterPluginObject extends ServerRegisterOptions { +export interface ServerRegisterPluginObject extends ServerRegisterOptions { /** * a plugin object. */ - plugin: Plugin; + plugin: Plugin; /** * options passed to the plugin during registration. */ - options?: any; + options?: T; } + +export interface ServerRegisterPluginObjectArray extends Array | undefined> { + 0: ServerRegisterPluginObject; + 1?: ServerRegisterPluginObject; + 2?: ServerRegisterPluginObject; + 3?: ServerRegisterPluginObject; + 4?: ServerRegisterPluginObject; + 5?: ServerRegisterPluginObject; + 6?: ServerRegisterPluginObject; +} + + + diff --git a/types/hapi/definitions/server/server.d.ts b/types/hapi/definitions/server/server.d.ts index 4753f96165..0133a35775 100644 --- a/types/hapi/definitions/server/server.d.ts +++ b/types/hapi/definitions/server/server.d.ts @@ -30,6 +30,7 @@ import { ServerRealm, ServerRegisterOptions, ServerRegisterPluginObject, + ServerRegisterPluginObjectArray, ServerRoute, ServerState, ServerStateCookieOptions, @@ -483,8 +484,8 @@ export class Server extends Podium { * @return Return value: none. * [See docs](https://github.com/hapijs/hapi/blob/master/API.md#-await-serverregisterplugins-options) */ - register(plugins: Plugin | Plugin[], options?: ServerRegisterOptions): Promise; - register(plugins: ServerRegisterPluginObject | ServerRegisterPluginObject[], options?: ServerRegisterOptions): Promise; + register(plugins: Plugin | Plugin[], options?: ServerRegisterOptions): Promise; + register(plugins: ServerRegisterPluginObject | ServerRegisterPluginObjectArray, options?: ServerRegisterOptions): Promise; /** * Adds a route where: diff --git a/types/hapi/test/server/server-bind.ts b/types/hapi/test/server/server-bind.ts index e68e248ad7..9bec0ec5f9 100644 --- a/types/hapi/test/server/server-bind.ts +++ b/types/hapi/test/server/server-bind.ts @@ -8,7 +8,7 @@ const handler = (request: Request, h: ResponseToolkit) => { return h.context.message; // Or h.context.message }; -const plugin: Plugin = { +const plugin: Plugin = { name: 'example', register: async (server: Server, options: ServerRegisterOptions) => { const bind = { diff --git a/types/hapi/test/server/server-expose.ts b/types/hapi/test/server/server-expose.ts index 4b1a5802dd..2f1424b2ba 100644 --- a/types/hapi/test/server/server-expose.ts +++ b/types/hapi/test/server/server-expose.ts @@ -1,14 +1,14 @@ // https://github.com/hapijs/hapi/blob/master/API.md#-serverplugins import { Plugin, Server, ServerRegisterOptions } from "hapi"; -const plugin1: Plugin = { +const plugin1: Plugin = { name: 'example1', register: async (server: Server, options: ServerRegisterOptions) => { server.expose('util', () => console.log('something')); } }; -const plugin2: Plugin = { +const plugin2: Plugin = { name: 'example2', register: async (server: Server, options: ServerRegisterOptions) => { server.expose('util', () => console.log('something')); diff --git a/types/hapi/test/server/server-options.ts b/types/hapi/test/server/server-options.ts index bb8643d5f2..ce487bbb20 100644 --- a/types/hapi/test/server/server-options.ts +++ b/types/hapi/test/server/server-options.ts @@ -30,7 +30,7 @@ const mimeOptions: MimosOptions = { } }; -const plugin: Plugin = { +const plugin: Plugin = { name: 'example', register: async (server: Server, options: ServerRegisterOptions) => { server.expose('key', 'value'); diff --git a/types/hapi/test/server/server-path.ts b/types/hapi/test/server/server-path.ts index 86363c5636..36f346cb10 100644 --- a/types/hapi/test/server/server-path.ts +++ b/types/hapi/test/server/server-path.ts @@ -13,7 +13,7 @@ const serverRouteOption: ServerRoute = { } }; -const plugin: Plugin = { +const plugin: Plugin = { name: 'example', register: async (server: Server, options: ServerRegisterOptions) => { // Assuming the Inert plugin was registered previously diff --git a/types/hapi/test/server/server-plugins.ts b/types/hapi/test/server/server-plugins.ts index a4bd629600..970b53fb6d 100644 --- a/types/hapi/test/server/server-plugins.ts +++ b/types/hapi/test/server/server-plugins.ts @@ -1,9 +1,21 @@ // https://github.com/hapijs/hapi/blob/master/API.md#-serverplugins import { Plugin, Server, ServerRegisterOptions } from "hapi"; -const plugin: Plugin = { - name: 'example', - register: async (server: Server, options: ServerRegisterOptions) => { +interface Plugin1 { + one: 1; +} + +interface Plugin2 { + two: 2; +} + +interface Plugin3 { + three: 3; +} + +const plugin1: Plugin = { + name: 'plugin1', + register: async (server: Server, options: Plugin1) => { server.expose('key', 'value'); server.plugins.example.other = 'other'; console.log(server.plugins.example.key); // 'value' @@ -11,9 +23,63 @@ const plugin: Plugin = { } }; +const plugin2: Plugin = { + name: 'plugin2', + register: async (server: Server, options: Plugin2) => {} +}; + +const plugin3: Plugin = { + name: 'plugin3', + register: async (server: Server, options: Plugin3) => {} +}; + const server = new Server({ port: 8000, }); server.start(); -server.register(plugin); +server.register(plugin1); + +server.register({ + plugin: plugin1, + options: {one: 1} +}); + +server.register([ + { + plugin: plugin2, + options: {two: 2} + }, + { + plugin: plugin3, + options: {three: 3} + }, + { + plugin: plugin1, + options: {one: 1} + }, + { + plugin: plugin2, + options: {two: 2} + }, + { + plugin: plugin3, + options: {three: 3} + }, + { + plugin: plugin1, + options: {one: 1} + }, + { + plugin: plugin2, + options: {two: 2} + }, + { + plugin: plugin3, + options: {three: 3} + }, + { + plugin: plugin1, + options: {one: 1} + } +]);