Vanilla TypeScript
A todo list and a lookup form built with plain DOM code. @hyper-fetch/core does not need React or any other framework:
you create a client, describe requests, and call send().
beginnervanillasendtypescript
Opens the example in StackBlitz in a new tab. The first start installs packages and takes a few seconds.
import "@picocss/pico/css/pico.min.css";
import "./styles.css";
import { getTodo, getTodos } from "./api";
// Plain DOM: a static skeleton, then `textContent` for everything that comes from the API.
const root = document.getElementById("root")!;
root.innerHTML = `
<main class="container">
<hgroup>
<h1>Todos</h1>
<p>HyperFetch without a framework: <code>request.send()</code> and the response object.</p>
</hgroup>
<div class="grid-2">
<section>
<h2>Latest todos</h2>
<ul id="todos" class="list" aria-busy="true"></ul>
</section>
<section>
<h2>Look up a todo</h2>
<form id="lookup" role="group">
<input name="todoId" type="number" min="1" value="1" aria-label="Todo id" required />
<button type="submit">Send</button>
</form>
<small class="muted">Ids above 254 do not exist. Try 9999 to see an error response.</small>
<article id="result">
<dl class="response">
<dt>success</dt><dd data-field="success">-</dd>
<dt>status</dt><dd data-field="status">-</dd>
<dt>data</dt><dd data-field="data">-</dd>
<dt>error</dt><dd data-field="error">-</dd>
<dt>time</dt><dd data-field="time">-</dd>
</dl>
</article>
</section>
</div>
</main>
`;
const todoList = root.querySelector<HTMLUListElement>("#todos")!;
const form = root.querySelector<HTMLFormElement>("#lookup")!;
const result = root.querySelector<HTMLElement>("#result")!;
const field = (name: string) => result.querySelector<HTMLElement>(`[data-field="${name}"]`)!;
const listItem = (text: string) => {
const item = document.createElement("li");
item.textContent = text;
return item;
};
const loadTodos = async () => {
// send() never rejects. Network errors, timeouts and 4xx/5xx come back in `error`.
const { data, error } = await getTodos.send({ queryParams: { limit: 5 } });
todoList.removeAttribute("aria-busy");
if (error || !data) {
todoList.replaceChildren(listItem(`Could not load todos: ${error?.message}`));
return;
}
todoList.replaceChildren(...data.todos.map((todo) => listItem(`${todo.completed ? "✓" : "○"} ${todo.todo}`)));
};
form.addEventListener("submit", async (event) => {
event.preventDefault();
const todoId = Number(new FormData(form).get("todoId"));
result.setAttribute("aria-busy", "true");
// `:todoId` in the endpoint makes `params` required and typed here.
const response = await getTodo.send({ params: { todoId } });
result.removeAttribute("aria-busy");
field("success").textContent = String(response.success);
field("status").textContent = String(response.status);
field("data").textContent = response.data ? `#${response.data.id} ${response.data.todo}` : "null";
field("error").textContent = response.error ? response.error.message : "null";
field("time").textContent = `${response.responseTimestamp - response.requestTimestamp} ms`;
});
loadTodos();
Run locally
npx tiged BetterTyped/hyper-fetch/examples/vanilla/basic hyper-fetch-vanilla-basic && cd hyper-fetch-vanilla-basic && npm install && npm run devWhat it shows
createClientandcreateRequestwork the same with or without a UI framework.send()takes typedparamsandqueryParamsand returns a promise with the response.- The response object carries
data,error,success,statusand timestamps for every call. - Errors resolve, they do not throw. A 404 comes back as a response with a typed
error.
How it works
api.tscreates one client withcreateClient({ url })and two requests.getTodoalso sets theerrorgeneric, soresponse.erroris typed as the{ message }body dummyjson returns for a missing todo.main.tswrites a static HTML skeleton and fills it withtextContent, so nothing from the API is parsed as HTML.loadTodos()callsgetTodos.send({ queryParams: { limit: 5 } })on start. The values passed tosend()apply to that call only.- The lookup form calls
getTodo.send({ params: { todoId } }).:todoIdin the endpoint makesparamsrequired, so TypeScript flags a call without it. - Every field of the response is printed:
successandstatus, thendataorerror, and the time betweenrequestTimestampandresponseTimestamp. Try id9999to see a 404 come back as a normal response.
const response = await getTodo.send({ params: { todoId } });
if (response.error) console.log(response.status, response.error.message);
send() also accepts lifecycle callbacks such as onRequestStart, onDownloadProgress and onResponse when you need
to react before the promise settles.
When to use it
- Scripts, web components or apps built with a framework that has no HyperFetch bindings.
- Code that runs outside components, such as route loaders, workers or Node scripts.
Related
Summary
You sent typed requests with send() from plain TypeScript and read data, error, success and status from the
response. The same core API powers the React hooks.
