mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-04-13 08:57:26 +08:00
Add generic types for plugins
This commit is contained in:
8
types/hapi/definitions/plugin/plugin.d.ts
vendored
8
types/hapi/definitions/plugin/plugin.d.ts
vendored
@@ -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);
|
||||
|
||||
@@ -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>;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
5
types/hapi/definitions/server/server.d.ts
vendored
5
types/hapi/definitions/server/server.d.ts
vendored
@@ -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:
|
||||
|
||||
@@ -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 = {
|
||||
|
||||
@@ -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'));
|
||||
|
||||
@@ -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');
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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}
|
||||
}
|
||||
]);
|
||||
|
||||
Reference in New Issue
Block a user