← Back to Usage Guide Index
Copy‑paste recipes for common m7Fetch tasks. Adjust import paths to match your project.
- Basic GET (full response)
- POST JSON
- POST urlencoded (login)
- Spec: load + call by
operationId - Batch: config/lang/health
- Batch: custom handler (normalize)
- Dynamic module load
- File upload (FormData)
- Binary download
- Abort & timeout
- Retry with backoff
- Auth header defaults
- Absolute URL override
- Cookies / credentials include
- Global fetch defaults (subclass)
- Progress UI with streaming batch
- Multiple specs side‑by‑side
- Centralized error/log helper
import Net from "./vendor/m7Fetch/src/index.js";
const net = new Net();
const res = await net.http.get("/config.json", { format: "full" });
if (!res.ok) console.error("config failed", res.status, res.body);
console.log(res.body);const save = await net.http.post("/api/save", { a: 1, b: 2 }, { format: "full" });
console.log(save.status, save.body);const login = await net.http.post("/api/login", { user: "u", pass: "p" }, {
urlencoded: true,
format: "full",
});
console.log(login.ok, login.status);await net.specs.load("/specs/pets.json", { id: "petsAPI" });
const pets = await net.specs.call("petsAPI", "listPets", { query: { limit: 10 }, format: "full" });
console.log(pets.status, pets.body);const { results } = await net.batch.run([
{ id: "cfg", url: "/config.json", opts: { format: "full" } },
{ id: "lang", url: "/i18n/en.json", opts: { format: "full" } },
{ id: "ping", url: "/health", opts: { format: "full" } },
],
(prepend) => console.log("done", Object.keys(prepend.context)),
(prepend) => console.warn("had failures"),
{ awaitAll: true, limit: 8 }
);
console.log(results.cfg.body);function normalize(obj, id, handler) {
return (res) => {
if (res?.ok === false) return false; // mark fail
const body = res.body ?? res;
const out = { data: body?.data ?? body, t: Date.now() };
obj.context[id] = out;
return out;
};
}
net.batch.setBatchHandler(normalize);
const { results } = await net.batch.run([
{ id: "a", url: "/a.json", opts: { format: "full" } },
{ id: "b", url: "/b.json", opts: { format: "full" } },
]);
console.log(results.a, results.b);const math = await net.modules.load("mathTools", "/modules/math.js");
console.log("2+3=", math.add(2, 3));const fd = new FormData();
fd.append("file", fileInput.files[0]);
const up = await net.http.post("/upload", fd, { format: "full" });
console.log(up.status);const resp = await net.http.get("/file.bin", { format: "raw" });
const buf = await resp.arrayBuffer();// Timeout
const slow = await net.http.get("/slow", { format: "full", timeout: 5000 });
// Abort
const ctrl = new AbortController();
const p = net.http.get("/stream", { format: "raw", signal: ctrl.signal });
ctrl.abort();
try { await p; } catch (e) { console.warn("aborted", e.name); }async function retry(fn, tries = 3, base = 200) {
let last;
for (let i = 0; i < tries; i++) {
try { return await fn(); } catch (e) { last = e; }
await new Promise(r => setTimeout(r, base * 2 ** i));
}
throw last;
}
const res = await retry(() => net.http.get("/ping", { format: "full" }));const net = new Net({ headers: { Authorization: `Bearer ${token}` } });
const me = await net.http.get("/me", { format: "full" });await net.http.get("https://other.example.com/health", { absolute: true, format: "full" });// Per request
await net.http.get("/profile", { format: "full", credentials: "include" });import HTTP from "./vendor/m7Fetch/src/core/HTTP.js";
class MyHTTP extends HTTP {
static FETCH_DEFAULTS = { credentials: "include", mode: "cors", cache: "no-cache" };
}const loadList = [ /* items */ ];
const { sync } = await net.batch.run(loadList, null, null, { awaitAll: false, limit: 5 });
const total = loadList.length;
const timer = setInterval(() => {
const done = Object.keys(sync.controller.run).length;
console.log(`${done}/${total}`);
if (sync.loaded()) clearInterval(timer);
}, 100);await net.specs.load("/specs/pets.json", { id: "petsAPI" });
await net.specs.load("/specs/store.json", { id: "storeAPI" });
const pets = await net.specs.call("petsAPI", "listPets");
const order = await net.specs.call("storeAPI", "createOrder", { body: { sku: "X" } });async function call(route, fn) {
try {
const res = await fn();
if (res?.ok === false) console.warn("HTTP", { route, status: res.status, msg: res.body?.message });
return res;
} catch (e) {
console.error("FATAL", { route, err: String(e) });
throw e;
}
}
await call("/config", () => net.http.get("/config", { format: "full" }));- HTTP_GUIDE.md — request/response formats
- BATCHING_AND_COORDINATION.md — handlers & limits
- SPEC_MANAGER.md — spec‑driven calls
- AUTHENTICATION_AND_SECURITY.md — tokens, cookies, CORS