-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
57 lines (45 loc) · 1.32 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
import { retrieve, ResponseError } from './dist/retrieve.js'
/** @import { RetrieveConfig } from './src/retrieve.js' */
Object.defineProperty(window, 'retrieve', { value: retrieve })
Object.defineProperty(window, 'ResponseError', { value: ResponseError })
const forms = Array.from(document.forms)
forms.filter((form) => form.method === 'get')
.forEach((form) => form.addEventListener('submit', handleGetSubmit))
forms.filter((form) => form.method === 'post')
.forEach((form) => form.addEventListener('submit', handlePostSubmit))
async function handleGetSubmit (event) {
event.preventDefault()
const form = /** @type {HTMLFormElement} */ (event.target)
console.log(form)
/** @type {RetrieveConfig} */ const config = {
url: form.action,
params: new URLSearchParams(/** @type {any} */(new FormData(form))),
init: {
method: form.method,
},
}
console.log(config)
try {
await retrieve(config)
} catch (error) {
console.dir(error)
}
}
async function handlePostSubmit (event) {
event.preventDefault()
const form = /** @type {HTMLFormElement} */ (event.target)
console.log(form)
/** @type {RetrieveConfig} */ const config = {
url: form.action,
data: new FormData(form),
init: {
method: form.method,
},
}
console.log(config)
try {
await retrieve(config)
} catch (error) {
console.dir(error)
}
}