Add generic types for plugins

This commit is contained in:
Justin Simms
2018-02-08 23:41:49 -06:00
parent d6324664f5
commit b7edd5c1db
8 changed files with 101 additions and 17 deletions

View File

@@ -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<T> {
/**
* (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<void>;
register: (server: Server, options: T) => Promise<void>;
/** (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<T> = PluginBase<T> & (PluginNameVersion | PluginPackage);

View File

@@ -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<T> extends ServerRegisterOptions {
/**
* a plugin object.
*/
plugin: Plugin;
plugin: Plugin<T>;
/**
* options passed to the plugin during registration.
*/
options?: any;
options?: T;
}
export interface ServerRegisterPluginObjectArray<T, U, V, W, X, Y, Z> extends Array<ServerRegisterPluginObject<T | U | V | W | X | Y | Z> | undefined> {
0: ServerRegisterPluginObject<T>;
1?: ServerRegisterPluginObject<U>;
2?: ServerRegisterPluginObject<V>;
3?: ServerRegisterPluginObject<W>;
4?: ServerRegisterPluginObject<X>;
5?: ServerRegisterPluginObject<Y>;
6?: ServerRegisterPluginObject<Z>;
}

View File

@@ -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<void>;
register(plugins: ServerRegisterPluginObject | ServerRegisterPluginObject[], options?: ServerRegisterOptions): Promise<void>;
register(plugins: Plugin<any> | Plugin<any>[], options?: ServerRegisterOptions): Promise<void>;
register<T, U, V, W, X, Y, Z>(plugins: ServerRegisterPluginObject<T> | ServerRegisterPluginObjectArray<T, U, V, W, X, Y, Z>, options?: ServerRegisterOptions): Promise<void>;
/**
* Adds a route where:

View File

@@ -8,7 +8,7 @@ const handler = (request: Request, h: ResponseToolkit) => {
return h.context.message; // Or h.context.message
};
const plugin: Plugin = {
const plugin: Plugin<any> = {
name: 'example',
register: async (server: Server, options: ServerRegisterOptions) => {
const bind = {

View File

@@ -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<any> = {
name: 'example1',
register: async (server: Server, options: ServerRegisterOptions) => {
server.expose('util', () => console.log('something'));
}
};
const plugin2: Plugin = {
const plugin2: Plugin<any> = {
name: 'example2',
register: async (server: Server, options: ServerRegisterOptions) => {
server.expose('util', () => console.log('something'));

View File

@@ -30,7 +30,7 @@ const mimeOptions: MimosOptions = {
}
};
const plugin: Plugin = {
const plugin: Plugin<any> = {
name: 'example',
register: async (server: Server, options: ServerRegisterOptions) => {
server.expose('key', 'value');

View File

@@ -13,7 +13,7 @@ const serverRouteOption: ServerRoute = {
}
};
const plugin: Plugin = {
const plugin: Plugin<any> = {
name: 'example',
register: async (server: Server, options: ServerRegisterOptions) => {
// Assuming the Inert plugin was registered previously

View File

@@ -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<Plugin1> = {
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<Plugin2> = {
name: 'plugin2',
register: async (server: Server, options: Plugin2) => {}
};
const plugin3: Plugin<Plugin3> = {
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}
}
]);