provider-fetch
Fetch data with HTML. Pair it with Quark to render the data. Zero app JS.
Features
- Provides data Use Quark to render that data
- Auto-fetch Fetches whenever
api-urlis set or changes - Re-fetch on demand The
--fetchcommand (<button command="--fetch" commandfor="…">) forces a re-fetch - Pausable
is-pausedholds off auto-fetch without removing state - Highly configurable Headers, method,
form-ref, credentials, redirect, etc
Installation
This package is available in the
<script src="https://unpkg.com/@excom/kit-utils@0.3.0/dist/index.umd.min.js"></script>
<script src="https://unpkg.com/@excom/neutron@0.2.0/dist/index.umd.min.js"></script>
<script src="https://unpkg.com/@excom/provider-fetch@0.1.5/dist/index.umd.min.js"></script>npm install @excom/provider-fetch<!-- import path to `node_modules` will depend on your build setup -->
<script type="module" src="/node_modules/@excom/provider-fetch"></script>
<link rel="stylesheet" href="/node_modules/@excom/provider-fetch">import "@excom/provider-fetch";Peer dependencies (0)
Packages a consumer must install alongside this one. Workspace deps are bundled.
| Package | Version |
|---|---|
|
Usage
<provider-fetch api-url="/api/todos"></provider-fetch><provider-fetch api-url="/api/todos"></provider-fetch>Hook the lifecycle state with CSS:
provider-fetch[is-loading] { /* show loading spinner */ }
provider-fetch[is-error]::before { content: "An error occurred." }provider-fetch[is-loading] { /* show loading spinner */ }
provider-fetch[is-error]::before { content: "An error occurred." }did-load is set on the first success, kept through a refresh (where is-loading replaces is-success) and cleared on an error: gate content on [did-load] to keep it on screen during a refresh.
Or Quark:
provider-fetch[is-success] {
$todo: prop("provision").body;
span { content: $todo.title; }
}provider-fetch[is-success] {
$todo: prop("provision").body;
span { content: $todo.title; }
}API Reference
Attributes
Role —option is configurable, state
is managed by the element (read-only), or hybrid which is both.
Provision
Thisprovision property lives on the DOM node.
Read / watch it with Quark’s prop("provision"), or listen
for the neutron-provision event from app JS.
Events
Type
command event (HTML Command API): from a
<button command commandfor>, an <event-handler
command-name>, or a CommandEvent.
Recognized Elements
Child or descendant elements are recognized by and relevant to its functionality.Styles
@custom-selector synonyms so custom elements can share
semantic styles. Element aliases match the host tag / .tag-* class; state
aliases match attributes (nested under the element alias).
provider-fetch
Attributes (13)
Role —option is configurable, state
is managed by the element (read-only), or hybrid which is both.
api-method
HTTP method. Always uppercased before the request is sent.
fetchable-element
apiMethod
string
"GET"api-url
Endpoint URL. When the request has no body, the JSON payload (from fetchable-element
form-ref or custom doFetch() args) is merged in as query params instead.
apiUrl
string
""fetch-credentials
RequestInit.credentials mode.
fetchCredentials
string
"omit" | "same-origin" | "include""include"fetch-redirect
RequestInit.redirect mode. Unset defers to the browser default (follow).
fetchRedirect
string
"follow" | "error" | "manual"nullform-ref
CSS selector for a fetchable-element
<form> to source the request from — its action (URL), method, enctype (Content-Type), and field values (as the JSON payload) all take priority over the matching attributes below. Omit to build the request entirely from attributes / custom doFetch() args.
formRef
string
<CSS Selector>nullhas-body
Force a request body even for methods that don't imply one (fetchable-element
GET / HEAD). Already implied for POST / PUT / PATCH.
hasBody
boolean
falseheader-accept
Accept request header.
headerAccept
string
"application/json"header-cache-control
Cache-Control request header. Unset by default (browser default caching applies).
headerCacheControl
string
nullheader-content-type
Content-Type request header. Dropped entirely when the request has no body.
headerContentType
string
"application/json"is-paused
Pause auto-fetch. While set,
api-url changes are ignored; the --fetch command still works.
isPaused
boolean
falseis-error
The most recent request failed (status 400 or above, network error, or a thrown error other than fetchable-element
AbortError). Fires with the error event.
isError
boolean
falseis-loading
A request is currently in flight.
fetchable-element
isLoading
boolean
falseis-success
The most recent request resolved successfully. Mutually exclusive with fetchable-element
is-loading and is-error.
isSuccess
boolean
falseProvision (1)
Thisprovision property lives on the DOM node.
Read / watch it with Quark’s prop("provision"), or listen
for the neutron-provision event from app JS.
provision
Response payload on success, or error payload on failure. Success shape: fetchable-element
{ status, statusText, ok, headers, url, redirected, bodyUsed, type, body }. Failure shape is either that same response shape (server responded with an error status) or { message, stack } (request never completed). Not reflected as an attribute.
provision
FetchResponse
Events (5)
A "default action" is subsequent logic executed by the element if
e.preventDefault() is not
synchronously called on the event.
Type
provider-fetch-error
Dispatched when the request fails — status 400 or above, network error, or a thrown error. fetchable-element
event.detail is the error payload (see provision). Not dispatched for aborted requests.
Type
FetchableErrorEvent
provider-fetch-loading
Dispatched immediately before the request is sent.
fetchable-element
Type
FetchableLoadingEvent
provider-fetch-submit
Internal — dispatched whenever a fetch is about to run (auto-fetch or
--fetch). Built by getFetchArgs(). Cancelable; default action calls doFetch().
Type
ProviderFetchSubmitEvent
doFetch([url, requestInit]) with the event's detail.provider-fetch-success
Dispatched when the request resolves successfully. fetchable-element
event.detail is the parsed response (see provision).
Type
FetchableSuccessEvent
command event (HTML Command API): from a
<button command commandfor>, an <event-handler
command-name>, or a CommandEvent.
--fetchis-paused.
Recognized Elements (0)
Child or descendant elements are recognized by provider-fetch and relevant to its functionality.Styles (0)
If you wish to opt-out of these styles on a case-to-case basis, use property
all: revert-layer.
Valence.css ships this as .unstyled and .unstyled-all for the entire tree.
@custom-selector synonyms so custom elements can share
semantic styles. Element aliases match the host tag / .tag-* class; state
aliases match attributes (nested under the element alias).
Release notes (1)
0.1.1
- Ship only dist (and declared extras) in the npm tarball; drop build logs, tests and sources
Release notes
A "default action" is subsequent logic executed by the element if
e.preventDefault() is not
synchronously called on the event.
If you wish to opt-out of these styles on a case-to-case basis, use property
all: revert-layer.
Valence.css ships this as .unstyled and .unstyled-all for the entire tree.
Examples
Comprehensive
This example shows loading state, error state, rendering, and refetching.