Skip to content

Commit 00627cf

Browse files
committed
refactor: make emitter type-safe and rename request finished event
Rename http:request_finished event to http:request_completed
1 parent 2480546 commit 00627cf

File tree

3 files changed

+17
-9
lines changed

3 files changed

+17
-9
lines changed

src/server/main.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,18 @@
1010
import onFinished from 'on-finished'
1111
import Middleware from '@poppinss/middleware'
1212
import type { Logger } from '@adonisjs/logger'
13-
import type { Emitter } from '@adonisjs/events'
1413
import type { Encryption } from '@adonisjs/encryption'
1514
import type { Server as HttpsServer } from 'node:https'
1615
import type { Application } from '@adonisjs/application'
16+
import type { EmitterLike } from '@adonisjs/events/types'
1717
import { ContainerResolver, moduleCaller, moduleImporter } from '@adonisjs/fold'
1818
import type { ServerResponse, IncomingMessage, Server as HttpServer } from 'node:http'
1919

2020
import type { LazyImport } from '../types/base.js'
2121
import type { MiddlewareAsClass, ParsedGlobalMiddleware } from '../types/middleware.js'
2222
import type {
2323
ServerConfig,
24+
HttpServerEvents,
2425
ServerErrorHandler,
2526
ErrorHandlerAsAClass,
2627
TestingMiddlewarePipeline,
@@ -73,7 +74,7 @@ export class Server {
7374
/**
7475
* Emitter is required to notify when a request finishes
7576
*/
76-
#emitter: Emitter<any>
77+
#emitter: EmitterLike<HttpServerEvents>
7778

7879
/**
7980
* The application instance to be shared with the router
@@ -138,7 +139,7 @@ export class Server {
138139
constructor(
139140
app: Application<any>,
140141
encryption: Encryption,
141-
emitter: Emitter<any>,
142+
emitter: EmitterLike<HttpServerEvents>,
142143
logger: Logger,
143144
config: ServerConfig
144145
) {
@@ -330,7 +331,7 @@ export class Server {
330331
/**
331332
* Setup for the "http:request_finished" event
332333
*/
333-
const hasRequestListener = this.#emitter.hasListeners('http:request_finished')
334+
const hasRequestListener = this.#emitter.hasListeners('http:request_completed')
334335
const startTime = hasRequestListener ? process.hrtime() : null
335336

336337
/**
@@ -348,7 +349,7 @@ export class Server {
348349
*/
349350
if (startTime) {
350351
onFinished(res, () => {
351-
this.#emitter.emit('http:request_finished', {
352+
this.#emitter.emit('http:request_completed', {
352353
ctx: ctx,
353354
duration: process.hrtime(startTime),
354355
})

src/types/server.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,20 @@ export type StatusPageRange = `${number}..${number}` | `${number}` | number
5252
export type StatusPageRenderer = (error: HttpError, ctx: HttpContext) => any | Promise<any>
5353

5454
/**
55-
* Data type for the "http:request_finished" event
55+
* Data type for the "http:request_completed" event
5656
*/
5757
export type HttpRequestFinishedPayload = {
5858
ctx: HttpContext
5959
duration: [number, number]
6060
}
6161

62+
/**
63+
* Events emitted by the HttpServer
64+
*/
65+
export type HttpServerEvents = {
66+
'http:request_completed': HttpRequestFinishedPayload
67+
}
68+
6269
/**
6370
* Error handler to handle HTTP errors
6471
*/

tests/server.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { Router } from '../src/router/main.js'
2020
import { HttpContext } from '../src/http_context/main.js'
2121
import { ServerFactory } from '../factories/server_factory.js'
2222
import { defineNamedMiddleware } from '../src/define_middleware.js'
23-
import { HttpRequestFinishedPayload } from '../src/types/server.js'
23+
import { HttpRequestFinishedPayload, HttpServerEvents } from '../src/types/server.js'
2424

2525
const BASE_URL = new URL('./app/', import.meta.url)
2626

@@ -46,7 +46,7 @@ test.group('Server', () => {
4646

4747
test('emit request finished route handler', async ({ assert }, done) => {
4848
const app = new AppFactory().create(BASE_URL, () => {})
49-
const emitter = new Emitter(app)
49+
const emitter = new Emitter<HttpServerEvents>(app)
5050
const server = new ServerFactory().merge({ app, emitter }).create()
5151
const httpServer = createServer(server.handle.bind(server))
5252

@@ -56,7 +56,7 @@ test.group('Server', () => {
5656
server.getRouter().get('/', async ({ response }) => response.send('handled'))
5757
await server.boot()
5858

59-
emitter.on('http:request_finished', (event: HttpRequestFinishedPayload) => {
59+
emitter.on('http:request_completed', (event: HttpRequestFinishedPayload) => {
6060
assert.instanceOf(event.ctx, HttpContext)
6161
assert.isArray(event.duration)
6262
done()

0 commit comments

Comments
 (0)