Skip to main content
Module

x/oak/etag.ts>ifMatch

A middleware framework for handling HTTP with Deno 🐿️ 🦕
Extremely Popular
Go to Latest
function ifMatch
import { ifMatch } from "https://deno.land/x/oak@v12.5.0/etag.ts";

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.

import {
  calculate,
  ifMatch,
} from "https://deno.land/std@0.221.0/http/etag.ts";
import { serve } from "https://deno.land/std@0.221.0/http/server.ts";
import { assert } from "https://deno.land/std@0.221.0/testing/asserts.ts"

const body = "hello deno!";

await serve(async (req) => {
  const ifMatchValue = req.headers.get("if-match");
  const etag = await calculate(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"});
  }
});

Parameters

value: string | null
etag: string | undefined

Returns

boolean