-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathRouteKeeper.swift
122 lines (102 loc) · 3.14 KB
/
RouteKeeper.swift
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
120
121
122
//
// RouteKeeper.swift
// Noze.io
//
// Created by Helge Heß on 6/2/16.
// Copyright © 2016 ZeeZide GmbH. All rights reserved.
//
import http
import connect
public protocol RouteKeeper {
func add(route e: Route)
}
// MARK: - Add Middleware
public extension RouteKeeper {
@discardableResult
func use(_ cb: @escaping Middleware) -> Self {
add(route: Route(pattern: nil, method: nil, middleware: [cb]))
return self
}
@discardableResult
func use(_ p: String, _ cb: @escaping Middleware) -> Self {
add(route: Route(pattern: p, method: nil, middleware: [cb]))
return self
}
@discardableResult
func all(_ p: String, _ cb: @escaping Middleware) -> Self {
add(route: Route(pattern: p, method: nil, middleware: [cb]))
return self
}
@discardableResult
func get(_ p: String, _ cb: @escaping Middleware) -> Self {
add(route: Route(pattern: p, method: .GET, middleware: [cb]))
return self
}
@discardableResult
func post(_ p: String, _ cb: @escaping Middleware) -> Self {
add(route: Route(pattern: p, method: .POST, middleware: [cb]))
return self
}
@discardableResult
func head(_ p: String, _ cb: @escaping Middleware) -> Self {
add(route: Route(pattern: p, method: .HEAD, middleware: [cb]))
return self
}
@discardableResult
func put(_ p: String, _ cb: @escaping Middleware) -> Self {
add(route: Route(pattern: p, method: .PUT, middleware: [cb]))
return self
}
@discardableResult
func del(_ p: String, _ cb: @escaping Middleware) -> Self {
add(route: Route(pattern: p, method: .DELETE, middleware: [cb]))
return self
}
@discardableResult
func patch(_ p: String, _ cb: @escaping Middleware) -> Self {
add(route: Route(pattern: p, method: .PATCH, middleware: [cb]))
return self
}
}
public extension RouteKeeper {
// Directly attach MiddlewareObject's as Middleware. That is:
// let app = express()
// let admin = express()
// app.use("/admin", admin)
@discardableResult
func use(_ middleware: MiddlewareObject) -> Self {
return use(middleware.middleware)
}
@discardableResult
func use(_ p: String, _ middleware: MiddlewareObject) -> Self {
return use(p, middleware.middleware)
}
@discardableResult
func all(_ p: String, _ middleware: MiddlewareObject) -> Self {
return all(p, middleware.middleware)
}
@discardableResult
func get(_ p: String, _ middleware: MiddlewareObject) -> Self {
return get(p, middleware.middleware)
}
@discardableResult
func post(_ p: String, _ middleware: MiddlewareObject) -> Self {
return post(p, middleware.middleware)
}
@discardableResult
func head(_ p: String, _ middleware: MiddlewareObject) -> Self {
return head(p, middleware.middleware)
}
@discardableResult
func put(_ p: String, _ middleware: MiddlewareObject) -> Self {
return put(p, middleware.middleware)
}
@discardableResult
func del(_ p: String, _ middleware: MiddlewareObject) -> Self {
return del(p, middleware.middleware)
}
@discardableResult
func patch(_ p: String, _ middleware: MiddlewareObject) -> Self {
return patch(p, middleware.middleware)
}
}