rong_http

HTTP client/server, fetch API

HTTP — fetch / Request / Response / SSE

Web-standard Fetch API and Server-Sent Events.

fetch

const response = await fetch("https://api.example.com/data");
const json = await response.json();

// POST
const response = await fetch("https://api.example.com/submit", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({ name: "rong" }),
});

Signature

fetch(input: string | Request | URL, init?: RequestInit): Promise<Response>

RequestInit

PropertyTypeDescription
methodstringHTTP method, default "GET"
headersHeaders | Record<string, string> | [string, string][]Request headers
bodystring | ArrayBuffer | Uint8Array | Blob | FormData | URLSearchParams | ReadableStreamRequest body
redirect"follow" | "error" | "manual"Redirect policy
signalAbortSignalCancellation signal

Request

const req = new Request("https://api.example.com", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({ key: "value" }),
});

const req2 = new Request(req); // clone from existing

Properties

PropertyTypeDescription
urlstringRequest URL
methodstringHTTP method
headersHeadersRequest headers
bodyReadableStream | nullBody stream
redirectstringRedirect policy
signalAbortSignalCancellation signal

Methods

MethodReturnsDescription
clone()RequestClone the request
text()Promise<string>Read body as text
json()Promise<any>Read body as JSON
arrayBuffer()Promise<ArrayBuffer>Read body as binary
blob()Promise<Blob>Read body as Blob

Response

// Manual construction
const res = new Response("Hello", {
  status: 200,
  headers: { "Content-Type": "text/plain" },
});

// From fetch
const res = await fetch(url);
if (res.ok) {
  const data = await res.json();
}

Properties

PropertyTypeDescription
urlstringResponse URL
statusnumberHTTP status code
statusTextstringStatus text
okbooleanStatus 200-299
headersHeadersResponse headers
bodyReadableStream | nullBody stream
bodyUsedbooleanWhether body is consumed
redirectedbooleanWhether redirected
typestringResponse type

Methods

MethodReturns
text()Promise<string>
json()Promise<any>
arrayBuffer()Promise<ArrayBuffer>
blob()Promise<Blob>
formData()Promise<FormData>
clone()Response

Headers

const h = new Headers({ "Content-Type": "application/json" });
const h2 = new Headers([["Accept", "text/html"], ["Accept", "application/json"]]);
const h3 = new Headers(existingHeaders);

h.set("Authorization", "Bearer token");
h.append("Accept", "text/html");
h.get("Content-Type"); // "application/json"
h.has("Authorization"); // true
h.delete("Authorization");

for (const [name, value] of h.entries()) {
  console.log(`${name}: ${value}`);
}
MethodDescription
get(name)Get value
set(name, value)Set value
append(name, value)Append value
delete(name)Delete
has(name)Check existence
getSetCookie()Get all Set-Cookie header values (Rong extension)
entries()Iterate [name, value]
keys()Iterate names
values()Iterate values
forEach(fn)For each entry

SSE (Server-Sent Events)

const sse = new Rong.SSE("https://api.example.com/events", {
  headers: { Authorization: "Bearer token" },
  signal: AbortSignal.timeout(60000),
  reconnect: {
    enabled: true,
    baseDelayMs: 1000,
    maxDelayMs: 30000,
    maxRetries: 10,
  },
  requestTimeoutMs: 60000,
});

for await (const event of sse) {
  console.log(event.type, event.data);
  if (done) break; // triggers close + cleanup
}

// Manual close
sse.close();

Properties

PropertyTypeDescription
urlstringConnection URL

Methods

MethodDescription
close()Close connection

Options

OptionTypeDescription
headersRecord<string, string>Custom request headers
signalAbortSignalCancellation signal
requestTimeoutMsnumberRequest timeout in milliseconds
reconnectobjectReconnect policy (enabled, maxRetries, baseDelayMs, maxDelayMs)

Yielded event shape

PropertyType
typestring
datastring
idstring
originstring