mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-04-23 12:56:46 +08:00
Update hapi.d.ts
This commit is contained in:
@@ -110,3 +110,51 @@ server.route([{
|
||||
|
||||
// Start the server
|
||||
server.start();
|
||||
|
||||
// server startup may now return a promise
|
||||
server.start()
|
||||
.then(() => {
|
||||
console.log('Started!');
|
||||
});
|
||||
|
||||
//inject a request into connection
|
||||
server.inject({
|
||||
method: 'GET',
|
||||
url: '/hello'
|
||||
}).then(response => {
|
||||
console.log(response.statusCode);
|
||||
});
|
||||
|
||||
//the same but this time using callback
|
||||
server.inject({
|
||||
method: 'GET',
|
||||
url: '/hello'
|
||||
}, response => {
|
||||
console.log(response.statusCode);
|
||||
});
|
||||
|
||||
//tests for server initialization
|
||||
server.initialize()
|
||||
.then(() => {
|
||||
console.log('Initialized!')
|
||||
});
|
||||
|
||||
//and the same but with callback
|
||||
server.initialize(err => {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
}
|
||||
});
|
||||
|
||||
//server stopping may now return a promise
|
||||
server.stop()
|
||||
.then(() => {
|
||||
console.log('Stopped!');
|
||||
});
|
||||
|
||||
//decorate can take an optional options argument
|
||||
server.decorate('hello', 'world', () => {}, {
|
||||
apply: true
|
||||
});
|
||||
|
||||
server.decorate('hello2', 'world2', () => {});
|
||||
|
||||
140
hapi/hapi.d.ts
vendored
140
hapi/hapi.d.ts
vendored
@@ -1,9 +1,9 @@
|
||||
// Type definitions for hapi 12.0.1
|
||||
// Type definitions for hapi 13.0.0
|
||||
// Project: http://github.com/spumko/hapi
|
||||
// Definitions by: Jason Swearingen <http://github.com/jasonswearingen>
|
||||
// Definitions: https://github.com/borisyankov/DefinitelyTyped
|
||||
|
||||
//Note/Disclaimer: This .d.ts was created against hapi v8.x but has been incrementally upgraded to 12.x. Some newer features/changes may be missing. YMMV.
|
||||
//Note/Disclaimer: This .d.ts was created against hapi v8.x but has been incrementally upgraded to 13.x. Some newer features/changes may be missing. YMMV.
|
||||
|
||||
|
||||
/// <reference path="../node/node.d.ts" />
|
||||
@@ -513,8 +513,8 @@ declare module "hapi" {
|
||||
pre?: any[];
|
||||
/** validation rules for the outgoing response payload (response body).Can only validate object response: */
|
||||
response?: {
|
||||
/** the default HTTP status code when the payload is empty. Value can be 200 or 204.
|
||||
Note that a 200 status code is converted to a 204 only at the time or response transmission
|
||||
/** the default HTTP status code when the payload is empty. Value can be 200 or 204.
|
||||
Note that a 200 status code is converted to a 204 only at the time or response transmission
|
||||
(the response status code will remain 200 throughout the request lifecycle unless manually set). Defaults to 200. */
|
||||
emptyStatusCode?: number;
|
||||
/** the default response object validation rules (for all non-error responses) expressed as one of:
|
||||
@@ -935,7 +935,28 @@ declare module "hapi" {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**the response object where:
|
||||
statusCode - the HTTP status code.
|
||||
headers - an object containing the headers set.
|
||||
payload - the response payload string.
|
||||
rawPayload - the raw response payload buffer.
|
||||
raw - an object with the injection request and response objects:
|
||||
req - the simulated node request object.
|
||||
res - the simulated node response object.
|
||||
result - the raw handler response (e.g. when not a stream or a view) before it is serialized for transmission. If not available, the value is set to payload. Useful for inspection and reuse of the internal objects returned (instead of parsing the response string).
|
||||
request - the request object.*/
|
||||
export interface IServerInjectResponse {
|
||||
statusCode: number;
|
||||
headers: IDictionary<string>;
|
||||
payload: string;
|
||||
rawPayload: Buffer;
|
||||
raw: {
|
||||
req: http.ClientRequest;
|
||||
res: http.ServerResponse
|
||||
};
|
||||
result: string;
|
||||
request: Request;
|
||||
}
|
||||
|
||||
export interface IServerInject {
|
||||
(options: string | {
|
||||
@@ -969,19 +990,8 @@ declare module "hapi" {
|
||||
end: boolean;
|
||||
};
|
||||
},
|
||||
callback: (
|
||||
/**the response object where:
|
||||
statusCode - the HTTP status code.
|
||||
headers - an object containing the headers set.
|
||||
payload - the response payload string.
|
||||
rawPayload - the raw response payload buffer.
|
||||
raw - an object with the injection request and response objects:
|
||||
req - the simulated node request object.
|
||||
res - the simulated node response object.
|
||||
result - the raw handler response (e.g. when not a stream or a view) before it is serialized for transmission. If not available, the value is set to payload. Useful for inspection and reuse of the internal objects returned (instead of parsing the response string).
|
||||
request - the request object.*/
|
||||
res: { statusCode: number; headers: IDictionary<string>; payload: string; rawPayload: Buffer; raw: { req: http.ClientRequest; res: http.ServerResponse }; result: string; request: Request }) => void
|
||||
): void;
|
||||
callback?: (res: IServerInjectResponse) => void
|
||||
): IPromise<IServerInjectResponse>;
|
||||
|
||||
}
|
||||
|
||||
@@ -1114,8 +1124,6 @@ declare module "hapi" {
|
||||
mode: any;
|
||||
/** the authentication error is failed and mode set to 'try'.*/
|
||||
error: any;
|
||||
/** an object used by the ['cookie' authentication scheme] https://github.com/hapijs/hapi-auth-cookie */
|
||||
session: any
|
||||
};
|
||||
/** the connection used by this request*/
|
||||
connection: ServerConnection;
|
||||
@@ -1127,21 +1135,26 @@ declare module "hapi" {
|
||||
id: number;
|
||||
/** request information */
|
||||
info: {
|
||||
/** request reception timestamp. */
|
||||
received: number;
|
||||
/** request response timestamp (0 is not responded yet). */
|
||||
responded: number;
|
||||
/** remote client IP address. */
|
||||
|
||||
remoteAddress: string;
|
||||
/** remote client port. */
|
||||
remotePort: number;
|
||||
/** content of the HTTP 'Referrer' (or 'Referer') header. */
|
||||
referrer: string;
|
||||
/** the request preferred encoding. */
|
||||
acceptEncoding: string;
|
||||
/** if CORS is enabled for the route, contains the following: */
|
||||
cors: {
|
||||
isOriginMatch: boolean; /** true if the request 'Origin' header matches the configured CORS restrictions. Set to false if no 'Origin' header is found or if it does not match. Note that this is only available after the 'onRequest' extension point as CORS is configured per-route and no routing decisions are made at that point in the request lifecycle. */
|
||||
};
|
||||
/** content of the HTTP 'Host' header (e.g. 'example.com:8080'). */
|
||||
host: string;
|
||||
/** the hostname part of the 'Host' header (e.g. 'example.com').*/
|
||||
hostname: string;
|
||||
/** request reception timestamp. */
|
||||
received: number;
|
||||
/** content of the HTTP 'Referrer' (or 'Referer') header. */
|
||||
referrer: string;
|
||||
/** remote client IP address. */
|
||||
remoteAddress: string;
|
||||
/** remote client port. */
|
||||
remotePort: number;
|
||||
/** request response timestamp (0 is not responded yet). */
|
||||
responded: number;
|
||||
};
|
||||
/** the request method in lower case (e.g. 'get', 'post'). */
|
||||
method: string;
|
||||
@@ -1180,8 +1193,6 @@ declare module "hapi" {
|
||||
route: IRoute;
|
||||
/** the server object. */
|
||||
server: Server;
|
||||
/** Special key reserved for plugins implementing session support. Plugins utilizing this key must check for null value to ensure there is no conflict with another similar server. */
|
||||
session: any;
|
||||
/** an object containing parsed HTTP state information (cookies) where each key is the cookie name and value is the matching cookie content after processing using any registered cookie definition. */
|
||||
state: any;
|
||||
/** complex object contining details on the url */
|
||||
@@ -1541,7 +1552,7 @@ Notes: 1. Default value. 2. Proposed code, not supported by all clients. */
|
||||
states: {
|
||||
settings: any; cookies: any; names: any[]
|
||||
};
|
||||
auth: { connection: ServerConnection; _schemes: any; _strategies: any; settings: any; };
|
||||
auth: { connection: ServerConnection; _schemes: any; _strategies: any; settings: any; api: any; };
|
||||
_router: any;
|
||||
MSPluginsCollection: any;
|
||||
applicationCache: any;
|
||||
@@ -1716,6 +1727,35 @@ Notes: 1. Default value. 2. Proposed code, not supported by all clients. */
|
||||
after(method: (plugin: any, next: (err: any) => void) => void, dependencies: string | string[]): void;
|
||||
|
||||
auth: {
|
||||
/** server.auth.api
|
||||
An object where each key is a strategy name and the value is the exposed strategy API. Available on when the authentication scheme exposes an API by returning an api key in the object returned from its implementation function.
|
||||
When the server contains more than one connection, each server.connections array member provides its own connection.auth.api object.
|
||||
const server = new Hapi.Server();
|
||||
server.connection({ port: 80 });
|
||||
const scheme = function (server, options) {
|
||||
return {
|
||||
api: {
|
||||
settings: {
|
||||
x: 5
|
||||
}
|
||||
},
|
||||
authenticate: function (request, reply) {
|
||||
const req = request.raw.req;
|
||||
const authorization = req.headers.authorization;
|
||||
if (!authorization) {
|
||||
return reply(Boom.unauthorized(null, 'Custom'));
|
||||
}
|
||||
return reply.continue({ credentials: { user: 'john' } });
|
||||
}
|
||||
};
|
||||
};
|
||||
server.auth.scheme('custom', scheme);
|
||||
server.auth.strategy('default', 'custom');
|
||||
console.log(server.auth.api.default.settings.x); // 5
|
||||
*/
|
||||
api: {
|
||||
[index: string]: any;
|
||||
}
|
||||
/** server.auth.default(options)
|
||||
Sets a default strategy which is applied to every route where:
|
||||
options - a string with the default strategy name or an object with a specified strategy or strategies using the same format as the route auth handler options.
|
||||
@@ -1861,13 +1901,15 @@ Notes: 1. Default value. 2. Proposed code, not supported by all clients. */
|
||||
// web.connections.length === 1
|
||||
// admin.connections.length === 1 */
|
||||
connection(options: IServerConnectionOptions): Server;
|
||||
/** server.decorate(type, property, method)
|
||||
/** server.decorate(type, property, method, [options])
|
||||
Extends various framework interfaces with custom methods where:
|
||||
type - the interface being decorated. Supported types:
|
||||
'reply' - adds methods to the reply interface.
|
||||
'server' - adds methods to the Server object.
|
||||
property - the object decoration key name.
|
||||
method - the extension function.
|
||||
options - if the type is 'request', supports the following optional settings:
|
||||
'apply' - if true, the method function is invoked using the signature function(request) where request is the current request object and the returned value is assigned as the decoration.
|
||||
Note that decorations apply to the entire server and all its connections regardless of current selection.
|
||||
var Hapi = require('hapi');
|
||||
var server = new Hapi.Server();
|
||||
@@ -1882,7 +1924,7 @@ Notes: 1. Default value. 2. Proposed code, not supported by all clients. */
|
||||
return reply.success();
|
||||
}
|
||||
});*/
|
||||
decorate(type: string, property: string, method: Function): void;
|
||||
decorate(type: string, property: string, method: Function, options?: { apply: boolean }): void;
|
||||
|
||||
/** server.dependency(dependencies, [after])
|
||||
Used within a plugin to declares a required dependency on other plugins where:
|
||||
@@ -1987,7 +2029,27 @@ Notes: 1. Default value. 2. Proposed code, not supported by all clients. */
|
||||
};
|
||||
server.handler('test', handler);*/
|
||||
handler<THandlerConfig>(name: string, method: (route: IRoute, options: THandlerConfig) => ISessionHandler): void;
|
||||
/** When the server contains exactly one connection, injects a request into the sole connection simulating an incoming HTTP request without making an actual socket connection.
|
||||
|
||||
/** server.initialize([callback])
|
||||
Initializes the server (starts the caches, finalizes plugin registration) but does not start listening
|
||||
on the connection ports, where:
|
||||
- `callback` - the callback method when server initialization is completed or failed with the signature
|
||||
`function(err)` where:
|
||||
- `err` - any initialization error condition.
|
||||
|
||||
If no `callback` is provided, a `Promise` object is returned.
|
||||
|
||||
Note that if the method fails and the callback includes an error, the server is considered to be in
|
||||
an undefined state and should be shut down. In most cases it would be impossible to fully recover as
|
||||
the various plugins, caches, and other event listeners will get confused by repeated attempts to
|
||||
start the server or make assumptions about the healthy state of the environment. It is recommended
|
||||
to assert that no error has been returned after calling `initialize()` to abort the process when the
|
||||
server fails to start properly. If you must try to resume after an error, call `server.stop()`
|
||||
first to reset the server state.
|
||||
*/
|
||||
initialize(callback?: (error: any) => void): IPromise<void>;
|
||||
|
||||
/** When the server contains exactly one connection, injects a request into the sole connection simulating an incoming HTTP request without making an actual socket connection.
|
||||
Injection is useful for testing purposes as well as for invoking routing logic internally without the overhead or limitations of the network stack.
|
||||
Utilizes the [shot module | https://github.com/hapijs/shot ] for performing injections, with some additional options and response properties
|
||||
* When the server contains more than one connection, each server.connections array member provides its own connection.inject().
|
||||
@@ -2233,7 +2295,7 @@ Notes: 1. Default value. 2. Proposed code, not supported by all clients. */
|
||||
server.start(function (err) {
|
||||
console.log('Server started at: ' + server.info.uri);
|
||||
});*/
|
||||
start(callback?: (err: any) => void): void;
|
||||
start(callback?: (err: any) => void): IPromise<void>;
|
||||
/** server.state(name, [options])
|
||||
HTTP state management uses client cookies to persist a state across multiple requests. Registers a cookie definitions
|
||||
State defaults can be modified via the server connections.routes.state configuration option.
|
||||
@@ -2278,7 +2340,7 @@ Notes: 1. Default value. 2. Proposed code, not supported by all clients. */
|
||||
server.stop({ timeout: 60 * 1000 }, function () {
|
||||
console.log('Server stopped');
|
||||
});*/
|
||||
stop(options?: { timeout: number }, callback?: () => void): void;
|
||||
stop(options?: { timeout: number }, callback?: () => void): IPromise<void>;
|
||||
/**server.table([host])
|
||||
Returns a copy of the routing table where:
|
||||
host - optional host to filter routes matching a specific virtual host. Defaults to all virtual hosts.
|
||||
|
||||
Reference in New Issue
Block a user