Skip to content
This repository has been archived by the owner on Mar 24, 2023. It is now read-only.

httpland/http-conditional-requests

Repository files navigation

http-conditional-requests

HTTP Conditional Request middleware for Fetch API.

What

Middleware for HTTP Conditional Requests.

It conditionally processes a HTTP request based on a precondition.

It compliant with RFC 9110, 13. Conditional Requests.

Middleware

For a definition of Universal HTTP middleware, see the http-middleware project.

Usage

Middleware factory is exported by default.

To evaluate precondition, you need to provide a function to retrieve the selected representation.

The following example evaluates the If-None-Match precondition and controls the handler.

import conditionalRequests from "https://deno.land/x/http_conditional_requests@$VERSION/mod.ts";
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
import { assertSpyCalls, spy } from "https://deno.land/std/testing/mock.ts";

const selectedRepresentation = new Response("<body>", {
  headers: { etag: "<etag>" },
});
const selectRepresentation = spy(() => selectedRepresentation);
const middleware = conditionalRequests(selectRepresentation);
const conditionalRequest = new Request("<uri>", {
  headers: { "if-none-match": "<etag>" },
});
const handler = spy(() => selectedRepresentation);

const response = await middleware(conditionalRequest, handler);

assertSpyCalls(handler, 0);
assertSpyCalls(selectRepresentation, 1);
assertEquals(response.status, 304);

Preconditions

RFC 9110, 13.1. Preconditions compliant and supports the following precondition:

  • If-Match
  • If-None-Match
  • If-Modified-Since
  • If-Unmodified-Since

If multiple precondition headers are present, precondition is processed according to precedence.

Effects

Middleware will effect following:

Conditions

Middleware will execute only if the following conditions are met:

  • The precondition header exists
    • If-Match
      • The ETag header exist
    • If-None-Match
      • The ETag header exist
    • If-Modified-Since
      • The Last-Modified header exist
    • If-Unmodified-Since
      • The Last-Modified header exist

License

Copyright © 2023-present httpland.

Released under the MIT license