-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
application.ts
119 lines (113 loc) · 3.52 KB
/
application.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import { ServeInit, Server } from "./deps.ts"
import { handler } from "./handler.ts"
import { middleware } from "./middleware.ts"
import { router } from "./router.ts"
import { dependency } from "./container.ts"
import {
Dependency,
HandlerArgument,
MiddlewareArgument,
PathArgument,
} from "./types.ts"
interface Application {
getDeps(key: string): unknown
serve(options?: ServeInit): Promise<void>
get(path: PathArgument, ...handlers: HandlerArgument[]): Application
post(path: PathArgument, ...handlers: HandlerArgument[]): Application
put(path: PathArgument, ...handlers: HandlerArgument[]): Application
delete(path: PathArgument, ...handlers: HandlerArgument[]): Application
head(path: PathArgument, ...handlers: HandlerArgument[]): Application
options(path: PathArgument, ...handlers: HandlerArgument[]): Application
patch(path: PathArgument, ...handlers: HandlerArgument[]): Application
use(...middlewares: MiddlewareArgument[]): Application
close(): void
}
const appHandler = handler()
export const { getParams, getParam } = appHandler
export function application(): Application {
const appRouter = router()
const appMiddleware = middleware()
let appDeps: Dependency = dependency()
let server: Server
function containDeps(array: MiddlewareArgument[]) {
for (let index = 0; index < array.length; index++) {
const element = array[index]
if (isDeps(element)) {
return true
}
}
return false
}
function isDeps(first: MiddlewareArgument) {
const deps = <Dependency>first
return deps.deps !== undefined
}
const app = {
deps: appDeps.deps,
getDeps: (key: string) => {
return appDeps.get(key)
},
setDeps: (key: string, val: unknown) => {
appDeps.set(key, val)
return app
},
close: () => {
return server.close()
},
serve: (options: ServeInit = {}) => {
server = new Server({
onError: options.onError,
hostname: options.hostname,
port: options.port,
handler: appHandler.createHandler(
appRouter.routes,
appMiddleware.middlewares,
),
})
const port = options.port ?? 8000
const listener = Deno.listen({ port })
return server.serve(listener)
},
get: (path: PathArgument, ...handlers: HandlerArgument[]) => {
appRouter.get(path, ...handlers)
return app
},
post: (path: PathArgument, ...handlers: HandlerArgument[]) => {
appRouter.post(path, ...handlers)
return app
},
put: (path: PathArgument, ...handlers: HandlerArgument[]) => {
appRouter.put(path, ...handlers)
return app
},
delete: (path: PathArgument, ...handlers: HandlerArgument[]) => {
appRouter.delete(path, ...handlers)
return app
},
head: (path: PathArgument, ...handlers: HandlerArgument[]) => {
appRouter.head(path, ...handlers)
return app
},
patch: (path: PathArgument, ...handlers: HandlerArgument[]) => {
appRouter.patch(path, ...handlers)
return app
},
options: (path: PathArgument, ...handlers: HandlerArgument[]) => {
appRouter.options(path, ...handlers)
return app
},
use: (...middlewares: MiddlewareArgument[]) => {
const [first, ...rest] = middlewares
if (isDeps(first)) {
appDeps = <Dependency>first
if (containDeps(rest)) {
throw Error("Dependency only used at first argument")
}
} else {
appMiddleware.useMiddleware(...middlewares)
}
return app
},
}
return app
}