Skip to content

Commit

Permalink
Bring back m.request - it's too useful.
Browse files Browse the repository at this point in the history
It mostly delegates to `fetch`.

Also, kill `m.withProgress` as that only existed because there was no `m.request`.
  • Loading branch information
dead-claudia committed Oct 31, 2024
1 parent a991694 commit e21c89f
Show file tree
Hide file tree
Showing 5 changed files with 2,080 additions and 97 deletions.
4 changes: 2 additions & 2 deletions src/entry/mithril.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import {debouncer, throttler} from "../std/rate-limit.js"
import {link, route} from "../std/router.js"
import {match, p, query} from "../std/path-query.js"
import {tracked, trackedList} from "../std/tracked.js"
import fetch from "../std/fetch.js"
import init from "../std/init.js"
import lazy from "../std/lazy.js"
import withProgress from "../std/with-progress.js"

m.route = route
m.link = link
m.p = p
m.query = query
m.match = match
m.withProgress = withProgress
m.fetch = fetch
m.lazy = lazy
m.init = init
m.tracked = tracked
Expand Down
71 changes: 71 additions & 0 deletions src/std/fetch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/* global fetch */

import {checkCallback} from "../util.js"

var mfetch = async (url, opts = {}) => {
checkCallback(opts.onprogress, true, "opts.onprogress")
checkCallback(opts.extract, true, "opts.extract")

try {
var response = await fetch(url, opts)

if (opts.onprogress && response.body) {
var reader = response.body.getReader()
var rawLength = response.headers.get("content-length") || ""
// This is explicit coercion, but ESLint is frequently too dumb to detect it correctly.
// Another example: https://github.com/eslint/eslint/issues/14623
// eslint-disable-next-line no-implicit-coercion
var total = (/^\d+$/).test(rawLength) ? +rawLength : -1
var current = 0

response = new Response(new ReadableStream({
type: "bytes",
start: (ctrl) => reader || ctrl.close(),
cancel: (reason) => reader.cancel(reason),
async pull(ctrl) {
var result = await reader.read()
if (result.done) {
ctrl.close()
} else {
current += result.value.length
ctrl.enqueue(result.value)
opts.onprogress(current, total)
}
},
}), response)
}

if (response.ok) {
if (opts.extract) {
return await opts.extract(response)
}

switch (opts.responseType || "json") {
case "json": return await response.json()
case "formdata": return await response.formData()
case "arraybuffer": return await response.arrayBuffer()
case "blob": return await response.blob()
case "text": return await response.text()
case "document":
// eslint-disable-next-line no-undef
return new DOMParser()
.parseFromString(await response.text(), response.headers.get("content-type") || "text/html")
default:
throw new TypeError(`Unknown response type: ${opts.responseType}`)
}
}

var message = (await response.text()) || response.statusText
} catch (e) {
var cause = e
var message = e.message
}

var e = new Error(message)
e.status = response ? response.status : 0
e.response = response
e.cause = cause
throw e
}

export {mfetch as default}
28 changes: 0 additions & 28 deletions src/std/with-progress.js

This file was deleted.

Loading

0 comments on commit e21c89f

Please sign in to comment.