🚀 Big News:Socket Has Acquired Secure Annex.Learn More →
Socket
Book a DemoSign in
Socket

@httpland/http-conditional-requests

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@httpland/http-conditional-requests

HTTP Conditional Requests middleware for Fetch API

beta
latest
Source
npmnpm
Version
1.0.0-beta.1
Version published
Maintainers
1
Created
Source

http-conditional-requests

HTTP Conditional Requests middleware for Fetch API.

deno land deno doc GitHub release (latest by date) codecov GitHub

test NPM

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

Keywords

http

FAQs

Package last updated on 06 Mar 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts