forked from rajtatata/cloudflare-worker-rest-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
146 lines (125 loc) · 3.82 KB
/
index.js
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
const res = require('./response')
const req = require('./request')
module.exports = class App {
constructor() {
this.routes = []
this.middlewares = []
}
async handleRequest(request) {
this.response = new res(request)
this.request = new req(request)
let method = request.method
let url = '/' + request.url.split('/').slice(3).join('/').split('?')[0]
let route = this.routes.find(elem => this.routeCheck(elem.url, url) && elem.method === method)
// if no route found, try to find with any method
if (!route) route = this.routes.find(elem => this.routeCheck(elem.url, url) && elem.method === '*')
if (route) {
// run the middlewares first
for (var i = 0; i < this.middlewares.length; i++) {
await this.middlewares[i].callback(this.request, this.response)
}
// run the callback function
return await route.callback(this.request, this.response)
}
return this.response.send({ status: 0, message: "Method not found!" }, 404)
}
get(url, callback) {
this.routes.push({
url: url,
method: 'GET',
callback
})
}
post(url, callback) {
this.routes.push({
url: url,
method: 'POST',
callback
})
}
put(url, callback) {
this.routes.push({
url: url,
method: 'PUT',
callback
})
}
patch(url, callback) {
this.routes.push({
url: url,
method: 'PATCH',
callback
})
}
delete(url, callback) {
this.routes.push({
url: url,
method: 'DELETE',
callback
})
}
any(url, callback) {
this.routes.push({
url: url,
method: '*',
callback
})
}
use(var1, var2) {
if (arguments.length == 2) {
this.useRouter(var1, var2)
} else if (arguments.length === 1) {
this.useMiddleware(var1)
}
}
useMiddleware(callback) {
arguments.length
this.middlewares.push({
callback
})
}
useRouter(path, router) {
router.routes.forEach(element => {
this.routes.push({
url: path + (element.url === '/' ? '' : element.url),
method: element.method,
callback: element.callback
})
})
router.middlewares.forEach(element => {
this.middlewares.push({
callback: element.callback
})
})
}
routeCheck(route, requestRoute) {
// implementing route params
// split actual route from this.routes
// and the route from the request
let routeArray = route.split('/')
let requestRouteArray = requestRoute.split('/')
if (routeArray.length !== requestRouteArray.length) {
return false
}
try {
let flag = true
// compare each element from both routes
routeArray.forEach((elem, index) => {
// check if we have url parameters
// and if there is actually a value in request url
// then insert to request.params
if (elem.includes(':') && requestRouteArray[index] && requestRouteArray[index] !== "") {
this.request.params[elem.substring(1)] = requestRouteArray[index]
} else {
if (elem !== requestRouteArray[index]) {
flag = false
return
}
}
})
return flag
} catch (error) {
return false
}
}
}