http.mjs provides essential tools for HTTP servers and clients:
- Shortcuts for making requests via native
fetch
. - Cookie decoding and encoding.
- URL-based routing for SSR and SPA apps.
Also see http_deno
for Deno HTTP servers, http_srv
for generic tools for HTTP servers using native stream APIs, and live_deno
for live-reload tools for development.
import * as h from 'https://cdn.jsdelivr.net/npm/@mitranim/js@0.1.61/http.mjs'
const reqBody = {msg: `hello world`}
const resBody = await h.reqBui().to(`/api`).post().json(reqBody).fetchOkJson()
Links: source; test/example.
Signature: (res: Response | Promise<Response>) => Promise<Response>
.
Missing feature of the fetch API. If the response is OK (HTTP code between 200 and 299, .ok === true
), the resulting promise resolves to that response as-is. Otherwise the resulting promise is rejected with a descriptive #ErrHttp
which includes the response status code, the response body (if any) as the error message, and the response itself for introspection if needed.
import * as h from 'https://cdn.jsdelivr.net/npm/@mitranim/js@0.1.61/http.mjs'
// If response is unsuccessful, this will throw `h.ErrHttp`.
const res = await h.resOk(await fetch(someUrl, someOpt))
const body = res.json()
Links: source; test/example.
Sanity-checking wrapper for JSON.parse
. If the input is nil or an empty string, returns null
. Otherwise the input must be a primitive string. Throws on other inputs, without trying to stringify them.
Links: source; test/example.
Sanity-checking wrapper for JSON.stringify
. Equivalent to JSON.stringify(val ?? null)
. If the input is undefined
, returns 'null'
(string) rather than undefined
(nil). Output is always a valid JSON string.
Links: source; test/example.
Subclass of Error
for HTTP responses. The error message includes the HTTP status code, if any.
class ErrHttp extends Error {
message: string
status: int
res?: Response
constructor(message: string, status: int, res?: Response)
}
Links: source; test/example.
Simple router that uses only URL and pathname. Suitable for SPA. For servers, use #ReqRou
which supports requests and HTTP methods.
Basics:
const rou = new h.Rou(`https://example.com/path?query#hash`)
rou.url // Url { https://example.com/path?query#hash }
rou.url.href === `https://example.com/path?query#hash`
rou.pathname === `/path`
rou.groups === undefined
rou.pat(`/`) === false
rou.pat(`/blah`) === false
rou.pat(`/path`) === true
rou.pat(/^[/](?<key>[^/]+)$/) === true
rou.groups // {key: `path`}
Routing is imperative:
import * as h from 'https://cdn.jsdelivr.net/npm/@mitranim/js@0.1.61/http.mjs'
import * as l from 'https://cdn.jsdelivr.net/npm/@mitranim/js@0.1.61/lang.mjs'
const nextPage = route(window.location)
function route(loc) {
const rou = new h.Rou(loc)
if (rou.pat(`/`)) return PageIndex(rou)
if (rou.pat(`/articles`)) return PageArticles(rou)
if (rou.pat(/^[/]articles[/](?<key>[^/]+)$/)) return PageArticle(rou)
return Page404(rou)
}
function PageArticle(rou) {
const key = l.reqPk(rou.reqGroups().key)
return `page for article ${key}`
}
Links: source; test/example.
Subclass of built-in AbortController
. Features:
- Support for chaining/linking, like in Go.
- Subclassable without further breakage.
- Has workarounds for Safari bugs.
- Implements our "standard" interface
.deinit()
.- Enables automatic cleanup when using our proxies for deinitables and observables.
Optional chaining/linking:
const parent = new AbortController()
const child = new h.Ctx(parent.signal)
parent.abort()
parent.signal.aborted === true
child.signal.aborted === true
The following APIs are exported but undocumented. Check http.mjs.
const GET
const HEAD
const OPTIONS
const POST
const PUT
const PATCH
const DELETE
const HEADER_NAME_CACHE_CONTROL
const HEADER_NAME_CONTENT_TYPE
const HEADER_NAME_ACCEPT
const HEADER_NAME_ETAG
const HEADER_NAME_ORIGIN
const HEADER_NAME_HOST
const HEADER_NAME_CORS_CREDENTIALS
const HEADER_NAME_CORS_HEADERS
const HEADER_NAME_CORS_METHODS
const HEADER_NAME_CORS_ORIGIN
const MIME_TYPE_TEXT
const MIME_TYPE_HTML
const MIME_TYPE_JSON
const MIME_TYPE_FORM
const MIME_TYPE_MULTI
const HEADER_TEXT
const HEADER_HTML
const HEADER_JSON
const HEADER_JSON_ACCEPT
const HEADERS_JSON_INOUT
const HEADERS_CORS_PROMISCUOUS
function isStatusInfo
function isStatusOk
function isStatusRedir
function isStatusClientErr
function isStatusServerErr
function hasStatus
function getStatus
function isErrAbort
class AbortError
function toRou
function toReqRou
class ReqRou
function cookieSplitPairs
function cookieSplitPair
function cook
class Cookie
class Cookies