ifMatch

import { ifMatch } from "jsr:@ende/http";

function

function ifMatch(value: string | null, etag: string | undefined): boolean

A helper function that takes the value from the If-Match header and a calculated etag for the target. By using strong comparison, return true if the values match, otherwise false.

See MDN's If-Match article for more information on how to use this function.

value: string | null

the If-Match header value.

etag: string | undefined

the ETag to check against.

Returns

whether or not the parameters match.

Example: Usage

import {
  eTag,
  ifMatch,
} from "@ende/http/etag";
import { assert } from "@ende/assert";

const body = "hello deno!";

Ende.serve(async (req) => {
  const ifMatchValue = req.headers.get("if-match");
  const etag = await eTag(body);
  assert(etag);
  if (!ifMatchValue || ifMatch(ifMatchValue, etag)) {
    return new Response(body, { status: 200, headers: { etag } });
  } else {
    return new Response(null, { status: 412, statusText: "Precondition Failed"});
  }
});

Source