HTTP Method Semantics: Safe, Idempotent, Cacheable
Classify HTTP methods as safe, idempotent and cacheable by default, and decide whether a failed request may be retried.
Code
export type HttpMethod =
| "GET" | "HEAD" | "OPTIONS" | "TRACE"
| "POST" | "PUT" | "PATCH" | "DELETE" | "CONNECT";
export interface MethodSemantics {
method: HttpMethod;
safe: boolean;
idempotent: boolean;
cacheableByDefault: boolean;
}
const SAFE: ReadonlySet<HttpMethod> = new Set(["GET", "HEAD", "OPTIONS", "TRACE"]);
const IDEMPOTENT: ReadonlySet<HttpMethod> = new Set([
"GET", "HEAD", "OPTIONS", "TRACE", "PUT", "DELETE",
]);
const CACHEABLE: ReadonlySet<HttpMethod> = new Set(["GET", "HEAD"]);
const KNOWN: ReadonlySet<HttpMethod> = new Set([
"GET", "HEAD", "OPTIONS", "TRACE", "POST", "PUT", "PATCH", "DELETE", "CONNECT",
]);
export function methodSemantics(method: string): MethodSemantics {
if (typeof method !== "string" || method.length === 0) {
throw new TypeError("method must be a non-empty string");
}
const normalized = method.toUpperCase() as HttpMethod;
if (!KNOWN.has(normalized)) throw new RangeError("unknown HTTP method: " + method);
return {
method: normalized,
safe: SAFE.has(normalized),
idempotent: IDEMPOTENT.has(normalized),
cacheableByDefault: CACHEABLE.has(normalized),
};
}
export function isRetryable(method: string, status: number): boolean {
if (!Number.isInteger(status) || status < 100 || status > 599) {
throw new RangeError("status must be a valid HTTP status code");
}
const semantics = methodSemantics(method);
return status >= 500 && (semantics.idempotent || status === 503);
}What the code does
HTTP Method Semantics: Safe, Idempotent, Cacheable is built around the functions methodSemantics and isRetryable.
TypeError and RangeError is the guard’s signal — thrown at the branch, before any partial work happens.
The result comes back as an object carrying normalized.
What to watch for
- The guard
typeof method !== "string" || method.length === 0short-circuits the flow with an earlythrowwhen its condition holds.
Applying it
The main signature reads export function methodSemantics(method: string)
The surface worth importing is methodSemantics; the helpers around it are private to the snippet.
The call returns normalized.
Statements that matter
const SAFE: ReadonlySet<HttpMethod> = new Set(["GET", "HEAD", "OPTIONS", "TRACE"]);
if (!KNOWN.has(normalized)) throw new RangeError("unknown HTTP method: " + method);
const CACHEABLE: ReadonlySet<HttpMethod> = new Set(["GET", "HEAD"]);
Interactive Tools for This Topic
HTTP Retry & Backoff CalculatorSimulate exponential backoff retry schedules with Full, Equal, Decorrelated, or No Jitter to inspect per-attempt wait times and cumulative latency.Accept Header ParserParse an Accept header into media ranges with their quality values and specificity, sorted by preference for content negotiation debugging.Basic & Bearer Auth Header ToolEncode and decode HTTP Authorization Basic and Bearer headers locally with UTF-8 Base64 validation and automatic JWT claim and expiry inspection.Set-Cookie ParserParse one or more Set-Cookie header values into the cookie name, value, and attributes like Path, Domain, Expires, Max-Age, Secure, HttpOnly, and SameSite.
FAQ
- Does `methodSemantics` modify its input?
- No. Nothing passed in is written to, which makes the call safe on shared data.
- What happens on invalid input?
- The code throws `TypeError` and `RangeError` — the guard `typeof method !== "string" || method.length === 0` rejects bad states before the main logic runs. Handle it at the call site or let it propagate deliberately.
- Anything to download besides the language toolchain?
- Nothing beyond the toolchain you already have for this language. The file defines `methodSemantics`, `isRetryable` at load time.
Report a problem with this exampleOpens a pre-filled issue on GitHub.