This repository has been archived by the owner on Jan 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
elmExpress.js
140 lines (106 loc) · 2.88 KB
/
elmExpress.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
class ElmExpress {
static create(window, routes) {
const app = new ElmExpress()
routes(app)
const OriginalXMLHttpRequest = window.XMLHttpRequest
window.XMLHttpRequest = function () {
const xhr = new OriginalXMLHttpRequest()
const originalOpen = xhr.open
xhr.open = function (method, url, async) {
if (!url.startsWith('elm:')) {
return originalOpen.apply(xhr, arguments)
}
['status', 'statusText', 'responseText', 'response', 'setRequestHeader']
.forEach(prop =>
Object.defineProperty(xhr, prop, {writable: true}))
//who cares about request headers here? :)
xhr.setRequestHeader = () => {
}
xhr.send = body => app.run(xhr, method, url, body)
}
return xhr
}
}
constructor() {
this.handlers = {}
}
use(method, funcName, func) {
this.handlers[this.toKey(method, funcName)] = func
}
toKey(method, funcName) {
return `${method.toLocaleLowerCase()}:${funcName}`
}
get(funcName, func) {
this.use('get', funcName, func)
}
put(funcName, func) {
this.use('put', funcName, func)
}
post(funcName, func) {
this.use('post', funcName, func)
}
patch(funcName, func) {
this.use('patch', funcName, func)
}
delete(funcName, func) {
this.use('delete', funcName, func)
}
run(xhr, method, url, body) {
return this.route(method, url, body)
.then(([code, responseText]) => {
console.log('ElmExpress response:', code, responseText)
xhr.status = code
xhr.statusText = `code:${code}`
xhr.response = xhr.responseText = responseText || ''
xhr.dispatchEvent(new Event('load'))
})
}
route(method, url, body) {
const parsed = new URL(url)
return new Promise(resolve => {
const key = this.toKey(method, parsed.pathname)
const handler = this.handlers[key]
if (!handler) {
return resolve([404, `Unknown function: ${method} ${parsed.pathname}`])
}
const json = tryJsonParse(body)
const params = parsed.searchParams
const req = {params, body, json, method, url}
const res = new Response(resolve)
try {
console.log('ElmExpress request:', req, res)
handler(req, res)
} catch (e) {
return resolve([500, `Internal JS error: ${e.toString()}`])
}
})
}
}
function tryJsonParse(body) {
if (!body) {
return
}
try {
return JSON.parse(body)
} catch (e) {
console.log('it is not a json', body)
}
}
class Response {
constructor(resolve) {
this.resolve = resolve
}
text(res) {
this.resolve([200, res])
}
ok() {
this.resolve([200, '"OK"'])
}
json(res) {
this.resolve([200, JSON.stringify(res)])
}
error(e = 'Internal error.') {
this.resolve([500, `${e.toString()}`])
}
}
module.exports = ElmExpress.create