-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathopine.ts
57 lines (48 loc) · 1.46 KB
/
opine.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { app as application } from "./application.ts";
import { WrappedRequest } from "./request.ts";
import { Response as ServerResponse } from "./response.ts";
import { mergeDescriptors } from "./utils/mergeDescriptors.ts";
import { EventEmitter } from "../deps.ts";
import type {
NextFunction,
Opine,
OpineRequest,
OpineResponse,
} from "./types.ts";
/**
* Response prototype.
*
* @public
*/
export const response: OpineResponse = Object.create(ServerResponse.prototype);
export const request: OpineRequest = Object.create(WrappedRequest.prototype);
/**
* Create an Opine application.
*
* @return {Opine}
* @public
*/
export function opine(): Opine {
const app = function (
req: OpineRequest,
res: OpineResponse = new ServerResponse(),
next: NextFunction,
): void {
app.handle(req, res, next);
} as Opine;
const eventEmitter = new EventEmitter();
app.emit = (event: string, ...args: any[]) =>
eventEmitter.emit(event, ...args);
app.on = (event: string, arg: any) => eventEmitter.on(event, arg);
mergeDescriptors(app, application, false);
// expose the prototype that will get set on requests
app.request = Object.create(request, {
app: { configurable: true, enumerable: true, writable: true, value: app },
});
// expose the prototype that will get set on responses
app.response = Object.create(response, {
app: { configurable: true, enumerable: true, writable: true, value: app },
});
app.init();
return app;
}