Skip to content

Commit 0fed7b1

Browse files
committed
docs: add readme
1 parent b1aa56f commit 0fed7b1

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed

README.md

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# conditional-request-middleware
2+
3+
HTTP conditional request middleware.
4+
5+
Compliant with
6+
[RFC 9110, 13. Conditional Requests](https://www.rfc-editor.org/rfc/rfc9110#section-13)
7+
8+
[![deno land](http://img.shields.io/badge/available%20on-deno.land/x-lightgrey.svg?logo=deno)](https://deno.land/x/conditional_request_middleware)
9+
[![deno doc](https://doc.deno.land/badge.svg)](https://doc.deno.land/https/deno.land/x/conditional_request_middleware/mod.ts)
10+
[![GitHub release (latest by date)](https://img.shields.io/github/v/release/httpland/conditional-request-middleware)](https://github.com/httpland/conditional-request-middleware/releases)
11+
[![codecov](https://codecov.io/gh/httpland/conditional-request-middleware/branch/main/graph/badge.svg)](https://codecov.io/gh/httpland/conditional-request-middleware)
12+
[![GitHub](https://img.shields.io/github/license/httpland/conditional-request-middleware)](https://github.com/httpland/conditional-request-middleware/blob/main/LICENSE)
13+
14+
[![test](https://github.com/httpland/conditional-request-middleware/actions/workflows/test.yaml/badge.svg)](https://github.com/httpland/conditional-request-middleware/actions/workflows/test.yaml)
15+
[![NPM](https://nodei.co/npm/@httpland/conditional-request-middleware.png?mini=true)](https://nodei.co/npm/@httpland/conditional-request-middleware/)
16+
17+
## What
18+
19+
Middleware for HTTP Conditional Requests.
20+
21+
It conditionally processes a HTTP request based on a precondition.
22+
23+
It compliant with
24+
[RFC 9110, 13. Conditional Requests](https://www.rfc-editor.org/rfc/rfc9110#section-13).
25+
26+
## Middleware
27+
28+
For a definition of Universal HTTP middleware, see the
29+
[http-middleware](https://github.com/httpland/http-middleware) project.
30+
31+
## Usage
32+
33+
Middleware factory is exported by default.
34+
35+
To evaluate precondition, you need to provide a function to retrieve the
36+
selected representation.
37+
38+
The following example evaluates the `If-None-Match` precondition and controls
39+
the handler.
40+
41+
```ts
42+
import { conditionalRequest } from "https://deno.land/x/conditional_request_middleware@$VERSION/mod.ts";
43+
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
44+
import { assertSpyCalls, spy } from "https://deno.land/std/testing/mock.ts";
45+
46+
const selectedRepresentation = new Response("<body>", {
47+
headers: { etag: "<etag>" },
48+
});
49+
const selectRepresentation = spy(() => selectedRepresentation);
50+
const middleware = conditionalRequest(selectRepresentation);
51+
const request = new Request("<uri>", {
52+
headers: { "if-none-match": "<etag>" },
53+
});
54+
const handler = spy(() => selectedRepresentation);
55+
56+
const response = await middleware(request, handler);
57+
58+
assertSpyCalls(handler, 0);
59+
assertSpyCalls(selectRepresentation, 1);
60+
assertEquals(response.status, 304);
61+
```
62+
63+
## Preconditions
64+
65+
[RFC 9110, 13.1. Preconditions](https://www.rfc-editor.org/rfc/rfc9110#section-13.1)
66+
compliant and supports the following precondition:
67+
68+
- If-Match
69+
- If-None-Match
70+
- If-Modified-Since
71+
- If-Unmodified-Since
72+
- If-Range
73+
74+
If multiple precondition headers are present, precondition is processed
75+
according to
76+
[precedence](https://www.rfc-editor.org/rfc/rfc9110.html#section-13.2.2).
77+
78+
## Effects
79+
80+
Middleware will effect following:
81+
82+
- HTTP response status
83+
- [304 (Not Modified)](https://www.rfc-editor.org/rfc/rfc9110#section-15.4.5)
84+
- [412 (Precondition Failed)](https://www.rfc-editor.org/rfc/rfc9110#section-15.5.13)
85+
86+
## Conditions
87+
88+
Middleware will execute only if the following conditions are met:
89+
90+
- The precondition header exists
91+
- `If-Match`
92+
- The `ETag` header exist
93+
- `If-None-Match`
94+
- The `ETag` header exist
95+
- `If-Modified-Since`
96+
- The `Last-Modified` header exist
97+
- `If-Unmodified-Since`
98+
- The `Last-Modified` header exist
99+
100+
## License
101+
102+
Copyright © 2023-present [httpland](https://github.com/httpland).
103+
104+
Released under the [MIT](./LICENSE) license

0 commit comments

Comments
 (0)