-
Notifications
You must be signed in to change notification settings - Fork 1
/
ajax.js
164 lines (145 loc) · 5.47 KB
/
ajax.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
export async function wrappedFetch(url, options, type, target) {
const { onWait, waitTime, onSuccess, onError, retryDelay = 1000 } = options
let waitTimeout = null
onWait && (waitTimeout = setTimeout(() => onWait(), waitTime || 250))
try {
const response = await fetch(url, options)
const data = await handleResponse(response, type, target, options)
onSuccess && requestIdleCallback(() => onSuccess(data))
return data
} catch (error) {
if (options.retries > 0) {
const newOptions = {
...options,
retries: options.retries - 1,
retryDelay: options.retryDelay * 2,
}
await new Promise((resolve) => setTimeout(resolve, retryDelay))
return wrappedFetch(url, newOptions, type, target)
}
const errorMessage = error.message || error || `Failed to load ${type}`
onError
? requestIdleCallback(() => onError(error))
: target.forEach((el) => (el.innerHTML = errorMessage))
} finally {
clearTimeout(waitTimeout)
}
}
export function send(element, options = {}, target) {
let { url, method = "POST", json = false, body, event, headers } = options
event && event.preventDefault()
url = url || getAction(element)
headers = headers ? new Headers(headers) : new Headers()
body = body || getBody(element, options)
if (method === "GET" || method === "HEAD") {
body = null
}
if (json) {
headers.append("Content-Type", "application/json")
body =
body instanceof FormData
? JSON.stringify(Object.fromEntries(body))
: typeof body === "object"
? JSON.stringify(body)
: JSON.stringify({ body })
}
const fetchOptions = {
...options,
method,
headers,
body,
}
return wrappedFetch(url, fetchOptions, "text", target)
}
export function fetchElements(type, url, options = {}, target) {
if (type === "sse") {
const eventSource = new EventSource(url)
eventSource.onmessage = (event) => {
target.forEach((el) => {
if (options.add) {
options.toTop
? sanitizeOrNot(el, event.data + "<br />" + el.innerHTML, options)
: sanitizeOrNot(el, el.innerHTML + "<br />" + event.data, options)
} else {
sanitizeOrNot(el, event.data, options)
}
})
options.onSuccess && requestIdleCallback(() => options.onSuccess(event))
options.runScripts && runScripts(target)
}
eventSource.onerror = (error) => options.onError && options.onError(error)
return
}
wrappedFetch(url, options, type, target).then((data) => {
if (!data) throw new Error(`No data received from ${url}`)
if (type === "text") {
sanitizeOrNot(target, data, options)
}
options.runScripts && runScripts(target)
})
}
function handleResponse(response, type, target, options) {
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`)
if (type === "json") return response.json()
if (type === "stream") {
const reader = response.body.getReader()
return recursiveRead(reader, target, type, options)
}
return response.text()
}
function recursiveRead(reader, target, type, options, chunks = []) {
return reader.read().then(({ done, value }) => {
const decodedChunk = new TextDecoder("utf-8").decode(value)
const allChunks = [...chunks, decodedChunk]
target.forEach((el) => {
sanitizeOrNot(el, allChunks.join(""), options)
})
return done
? allChunks.join("")
: recursiveRead(reader, target, type, options, allChunks)
})
}
function getBody(element, options = {}) {
const { serializer } = options
const tagName = element.tagName
const form = element.form || element.closest("form")
return tagName === "FORM" // If the element is a form
? serializer // & a serializer is passed
? serializer(element) // use the serializer
: new FormData(element) // otherwise use FormData on the form
: // If the element is an input, select, or textarea
tagName === "INPUT" || tagName === "SELECT" || tagName === "TEXTAREA"
? element.value // use the value
: form // If the element is not a form, but has a form ancestor
? serializer // & a serializer is passed
? serializer(form) // use the serializer
: new FormData(form) // otherwise use FormData on the ancestor form
: element.textContent // If nothing else, just use the text content
}
function getAction(element) {
let form = element.form || element.closest("form")
return element.formAction && element.formAction !== window.location.href
? element.formAction // If there is a formaction, but it is not the same as the current URL, use it
: element.action // If there is an action attribute
? element.action // use it
: form && form.action // If there is no action, but there is a form ancestor with an action
? form.action // use it
: window.location.href // If there is no formAction, no action, and no form ancestor with an action, use the current URL
}
function sanitizeOrNot(target, data, options) {
const { sanitize = true, sanitizer } = options
const targetElements = Array.isArray(target) ? target : [target]
targetElements.forEach((el) => {
sanitize ? el.setHTML(data, { sanitizer }) : (el.innerHTML = data)
})
}
function runScripts(target) {
target.forEach((el) =>
el.querySelectorAll("script").forEach((script) => {
const newScript = document.createElement("script")
newScript.textContent = script.textContent
newScript.type = script.type
script.replaceWith(newScript)
})
)
}