↑ Usage Guide Index | ← Core API OVERVIEW
The low-level HTTP client used by Net. It wraps fetch() with:
- ergonomic method helpers (
get,post,put,patch,delete,head,options) - normalized request options (base URL, headers,
json/urlencoded, timeouts,signal) - predictable response formats (
body|full|raw) - a small set of validated fetch enums and extendable
FETCH_DEFAULTS
Use
HTTPstandalone or vianew Net().http. See also HTTP_GUIDE.md for narrative walkthroughs.
Creates a new HTTP client with per-instance defaults.
-
Parameters
opts(object, optional) — Per-instance defaults merged withHTTP.FETCH_DEFAULTSand overridden by per-request options.
-
Returns
HTTPinstance exposing method helpers.
All helpers share a common signature and option parsing. Unknown options are forwarded to fetch if valid.
// canonical shape (TypeScript-ish, informal)
interface RequestOpts {
baseURL?: string; // "https://api.example.com"
url?: string; // optional when calling via spec layer
headers?: Record<string, string>; // merged (lowercased keys on send)
query?: Record<string, any>; // appended to URL as ?key=value
// body helpers (mutually exclusive - first truthy wins)
json?: any; // JSON.stringify + Content-Type: application/json
urlencoded?: Record<string, any>; // application/x-www-form-urlencoded
body?: BodyInit | null; // pass-through (FormData/Blob/ArrayBuffer/etc.)
// control
format?: 'body' | 'full' | 'raw'; // default: 'body'
timeout?: number; // ms; implemented via AbortController
signal?: AbortSignal; // external AbortSignal; merged with timeout
// fetch() options (validated against FETCH_CONSTANTS)
method?: string; // validated on helpers
mode?: RequestMode;
cache?: RequestCache;
credentials?: RequestCredentials;
redirect?: RequestRedirect;
referrerPolicy?: ReferrerPolicy;
}get(url: string, opts?: RequestOpts)
post(url: string, opts?: RequestOpts)
put(url: string, opts?: RequestOpts)
patch(url: string, opts?: RequestOpts)
delete(url: string, opts?: RequestOpts)
head(url: string, opts?: RequestOpts)
options(url: string, opts?: RequestOpts)Validation: Method helpers guard against unsupported/typoed methods and normalize casing.
- If
urlis absolute, it is used as-is. - If
urlis relative andbaseURLis set (on instance or per-request), the final URL isnew URL(url, baseURL). queryobject is serialized using standard rules (array-> repeated keys, primitives -> strings) and appended to the final URL.
{ ...FETCH_DEFAULTS.headers, ...instance.headers, ...perRequest.headers }
- Keys are treated case-insensitively when merging.
- Helper flags (like
json) may set/overrideContent-Typeif absent.
json:JSON.stringify(json)+Content-Type: application/json.urlencoded:application/x-www-form-urlencodedusingURLSearchParams.body: pass-through; e.g.,FormData,Blob,ArrayBuffer,ReadableStream(where supported).- If both
jsonandbodyare provided,jsonwins. Ifurlencodedandjsonprovided, first truthy wins following the orderjson→urlencoded→body.
timeoutcreates an internalAbortControllerthat races the request.- If
signalis provided, it is composed with the internal timeout signal (aborts if either fires).
Select the shape you need via format (default body).
Returns parsed body only:
application/json→await res.json()text/*→await res.text()*/*;charset=binary/blob-like →await res.blob()when available
Returns an object { ok, status, statusText, url, headers, body }, where body is parsed as above.
Returns the native Response instance, unparsed.
For debugging and error flows, prefer
format: "full"to inspectok/statusand headers.
A mutable baseline extended at the module level. Typical fields: mode, cache, credentials, redirect, referrerPolicy, and default headers.
Merge precedence: FETCH_DEFAULTS ← instance opts ← per-request opts.
Values for mode, cache, credentials, redirect, referrerPolicy are validated against a known-safe set (see FETCH_CONSTANTS). Unknown values are rejected early with a descriptive error.
import Net from 'm7Fetch';
const net = new Net({ http: { baseURL: 'https://api.example.com' } });
const user = await net.http.get('/v1/users/me', { format: 'body' });
const create = await net.http.post('/v1/users', {
json: { email: 'a@b.co', name: 'Ada' },
headers: { 'X-Trace': 'demo-1' },
format: 'full'
});
console.log(create.status, create.body.id);const search = await net.http.get('/v1/search', {
query: { q: 'kitties', limit: 25, tags: ['cute', 'fluffy'] }
});
const token = await net.http.post('/oauth/token', {
urlencoded: { grant_type: 'client_credentials', scope: 'public' }
});const ac = new AbortController();
setTimeout(() => ac.abort(), 250);
try {
await net.http.get('/slow', { timeout: 5000, signal: ac.signal, format: 'full' });
} catch (e) {
// e.name === 'AbortError' on timeout or external abort
}const res = await net.http.get('/report.csv', { format: 'raw' });
const text = await res.text(); // or res.blob()- Unsupported method →
E_HTTP_UNSUPPORTED_METHOD(thrown beforefetch). - Invalid enum value →
E_HTTP_INVALID_FETCH_OPTION. - Abort/Timeout → native
AbortError(wrap or inspect as needed). - When
format: 'body', exceptions bubble from the chosen parser (json()/text()/blob()). Preferformat: 'full'when diagnosing.
- The client is intentionally light; retries/backoff, streaming helpers, and HTTP/2 are in the roadmap and can be layered externally.
- Use instance-level defaults for cross-cutting concerns (credentials mode, cache policy) and override per request.
- For uploads, pass
FormDataviabodyand let the browser setContent-Typeboundaries.
class HTTP {
static FETCH_DEFAULTS: RequestInit & { headers?: Record<string, string> };
constructor(opts?: RequestOpts);
get(url: string, opts?: RequestOpts): Promise<any>;
post(url: string, opts?: RequestOpts): Promise<any>;
put(url: string, opts?: RequestOpts): Promise<any>;
patch(url: string, opts?: RequestOpts): Promise<any>;
delete(url: string, opts?: RequestOpts): Promise<any>;
head(url: string, opts?: RequestOpts): Promise<any>;
options(url: string, opts?: RequestOpts): Promise<any>;
}- HTTP_GUIDE.md — step‑by‑step patterns and troubleshooting.
- CORE_API_FETCH_CONSTANTS.md — allowed enums and validation rules.
- CONFIGURATION_AND_DEFAULTS.md — deep dive on
FETCH_DEFAULTSand merge precedence.