mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2025-10-16 12:05:41 +00:00
Add events (server and request) test files.
This commit is contained in:
parent
81a8109fed
commit
bbfeda585b
5
types/hapi/v17/test/index.test.d.ts
vendored
5
types/hapi/v17/test/index.test.d.ts
vendored
@ -17,10 +17,15 @@ export * from './reply/continue';
|
||||
export * from './reply/redirect';
|
||||
export * from './reply/reply';
|
||||
export * from './reply/state_cookie';
|
||||
|
||||
export * from './request/event-types';
|
||||
|
||||
export * from './response/error';
|
||||
|
||||
export * from './server/app';
|
||||
export * from './server/info';
|
||||
export * from './server/server-events-once';
|
||||
export * from './server/table';
|
||||
|
||||
|
||||
|
||||
|
||||
71
types/hapi/v17/test/request/event-types.ts
Normal file
71
types/hapi/v17/test/request/event-types.ts
Normal file
@ -0,0 +1,71 @@
|
||||
// From https://github.com/hapijs/hapi/blob/master/API.md#-servereventevents
|
||||
// From https://github.com/hapijs/hapi/blob/master/API.md#-requestevents
|
||||
import {
|
||||
Request, RequestExtPointFunction, ResponseToolkit, RouteOptions, Server, ServerOptions,
|
||||
ServerRoute
|
||||
} from "hapi";
|
||||
import * as Crypto from 'crypto';
|
||||
|
||||
const options: ServerOptions = {
|
||||
port: 8000,
|
||||
};
|
||||
|
||||
const serverRoute: ServerRoute = {
|
||||
path: '/',
|
||||
method: 'GET',
|
||||
handler: function (request: Request, h: ResponseToolkit) {
|
||||
return 'ok: ' + request.path;
|
||||
}
|
||||
};
|
||||
|
||||
const onRequest: RequestExtPointFunction = function (request: Request, h: ResponseToolkit) {
|
||||
|
||||
/*
|
||||
* Server events
|
||||
*/
|
||||
request.server.events.on('request', (request: Request, event: any, tags: any) => {
|
||||
console.log(request.paramsArray);
|
||||
console.log(event);
|
||||
console.log(tags);
|
||||
});
|
||||
|
||||
request.server.events.on('response', (request: Request) => {
|
||||
console.log('Response sent for request: ' + request.path);
|
||||
});
|
||||
|
||||
request.server.events.on('start', (route: RouteOptions) => {
|
||||
console.log('Server started');
|
||||
});
|
||||
|
||||
request.server.events.once('stop', (route: RouteOptions) => {
|
||||
console.log('Server stoped');
|
||||
});
|
||||
|
||||
/*
|
||||
* Request events
|
||||
*/
|
||||
const hash = Crypto.createHash('sha1');
|
||||
|
||||
request.events.on("peek", (chunk: any) => {
|
||||
hash.update(chunk);
|
||||
});
|
||||
|
||||
request.events.once("finish", () => {
|
||||
console.log(hash.digest('hex'));
|
||||
});
|
||||
|
||||
request.events.once("disconnect", () => {
|
||||
console.error('request aborted');
|
||||
});
|
||||
|
||||
return h.continue;
|
||||
|
||||
};
|
||||
|
||||
const server = new Server(options);
|
||||
server.route(serverRoute);
|
||||
server.ext('onRequest', onRequest);
|
||||
|
||||
server.start();
|
||||
console.log('Server started at: ' + server.info.uri);
|
||||
|
||||
26
types/hapi/v17/test/server/server-events-once.ts
Normal file
26
types/hapi/v17/test/server/server-events-once.ts
Normal file
@ -0,0 +1,26 @@
|
||||
// from https://github.com/hapijs/hapi/blob/master/API.md#-servereventsoncecriteria-listener
|
||||
import {Request, ResponseToolkit, Server, ServerOptions, ServerRoute} from "hapi";
|
||||
|
||||
const options: ServerOptions = {
|
||||
port: 8000,
|
||||
};
|
||||
|
||||
const serverRoute: ServerRoute = {
|
||||
path: '/',
|
||||
method: 'GET',
|
||||
handler: function (request: Request, h: ResponseToolkit) {
|
||||
return 'oks: ' + request.path;
|
||||
}
|
||||
};
|
||||
|
||||
const server = new Server(options);
|
||||
server.route(serverRoute);
|
||||
server.event('test1');
|
||||
server.event('test2');
|
||||
server.events.once('test1', function(update: any) {console.log(update);});
|
||||
server.events.once('test2', function(...args: any[]) {console.log(args);});
|
||||
server.events.emit('test1', 'hello-1');
|
||||
server.events.emit('test2', 'hello-2'); // Ignored
|
||||
|
||||
server.start();
|
||||
console.log('Server started at: ' + server.info.uri);
|
||||
Loading…
Reference in New Issue
Block a user