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.

TypescriptResilience

Code

typescriptsnippet
41 lines
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 === 0 short-circuits the flow with an early throw when 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

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.