Skip to content

Commit f616ec0

Browse files
committed
docs: add description of preconditions
1 parent 8f37b6b commit f616ec0

File tree

2 files changed

+109
-10
lines changed

2 files changed

+109
-10
lines changed

README.md

Lines changed: 100 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,39 +64,130 @@ representation.
6464

6565
You must provide a function to retrieve the representation.
6666

67-
Select representation is the following interface:
67+
Selecting representation is the following interface:
6868

6969
```ts
7070
interface SelectRepresentation {
7171
(request: Request): Response | Promise<Response>;
7272
}
7373
```
7474

75-
The select representation is executed prior to the handler when a request with a
76-
precondition header is received.
75+
It is executed prior to the handler when a request with a precondition header is
76+
received.
7777

78-
The select representation removes the pre-conditional header from the actual
79-
request header in order to satisfy the following requirement.
78+
The `Request` object passed to select representation has fileted the conditional
79+
header.
80+
81+
It satisfies the following requirement.
8082

8183
> A server MUST ignore all received preconditions if its response to the same
8284
> request without those conditions, prior to processing the request content,
8385
> would have been a status code other than a 2xx (Successful) or 412
8486
> (Precondition Failed).
8587
88+
## Preconditions
89+
90+
Middleware supports all preconditions compliant with
91+
[RFC 9110, 13.1. Preconditions](https://www.rfc-editor.org/rfc/rfc9110#section-13.1)
92+
by default.
93+
94+
If you want to adapt only some of the preconditions, give a list of them.
95+
96+
Example of middleware that handles only `If-None-Match` and `If-Modified-Since`
97+
headers:
98+
99+
```ts
100+
import {
101+
conditionalRequest,
102+
type Handler,
103+
IfModifiedSince,
104+
IfNoneMatch,
105+
} from "https://deno.land/x/conditional_request_middleware@$VERSION/mod.ts";
106+
107+
declare const selectRepresentation: Handler;
108+
109+
const middleware = conditionalRequest(selectRepresentation, {
110+
preconditions: [new IfNoneMatch(), new IfModifiedSince()],
111+
});
112+
```
113+
114+
Don't worry about the order of preconditions. They will be sorted appropriately
115+
based on the
116+
[13.2.2. Precedence of Preconditions](https://www.rfc-editor.org/rfc/rfc9110.html#section-13.2.2).
117+
118+
### Middleware default
119+
120+
The Middleware factory default values are as follows:
121+
122+
```ts
123+
import {
124+
BytesRange,
125+
conditionalRequest,
126+
type Handler,
127+
IfMatch,
128+
IfModifiedSince,
129+
IfNoneMatch,
130+
IfRange,
131+
IfUnmodifiedSince,
132+
} from "https://deno.land/x/conditional_request_middleware@$VERSION/mod.ts";
133+
134+
declare const selectRepresentation: Handler;
135+
const DEFAULT_PRECONDITIONS = [
136+
new IfMatch(),
137+
new IfNoneMatch(),
138+
new IfModifiedSince(),
139+
new IfUnmodifiedSince(),
140+
new IfRange([new BytesRange()]),
141+
];
142+
143+
const middleware = conditionalRequest(selectRepresentation, {
144+
preconditions: DEFAULT_PRECONDITIONS,
145+
});
146+
```
147+
86148
## Precondition
87149

150+
`Precondition` is following structured object.
151+
152+
```ts
153+
/** Precondition API. */
154+
export interface Precondition {
155+
/** Precondition header field name. */
156+
readonly field: string;
157+
158+
/** Definition of precondition evaluation.
159+
* If return value is void, it represents ignore this precondition.
160+
*/
161+
evaluate(
162+
request: Request,
163+
selectedRepresentation: Response,
164+
): boolean | void | Promise<boolean | void>;
165+
166+
/** Called after {@link Precondition.evaluate}.
167+
* If return response, it must not perform the requested method.
168+
* If return value is void, it represents ignore this precondition.
169+
*/
170+
respond(
171+
request: Request,
172+
selectedRepresentation: Response,
173+
result: boolean,
174+
): Response | void | Promise<Response | void>;
175+
}
176+
```
177+
178+
`Precondition` abstracts the evaluation of a precondition and its response.
179+
180+
Provide all preconditions compliant with
88181
[RFC 9110, 13.1. Preconditions](https://www.rfc-editor.org/rfc/rfc9110#section-13.1)
89-
compliant and supports the following precondition:
90182

91183
- [If-Match](#ifmatch)
92184
- [If-None-Match](#ifnonematch)
93185
- [If-Modified-Since](#ifmodifiedsince)
94186
- [If-Unmodified-Since](#ifunmodifiedsince)
95187
- [If-Range](#ifrange)
96188

97-
If multiple precondition headers are present, precondition is processed
98-
according to
99-
[precedence](https://www.rfc-editor.org/rfc/rfc9110.html#section-13.2.2).
189+
If you implement a `Precondition` that is not in the specification, make sure
190+
[extensibility of preconditions](https://www.rfc-editor.org/rfc/rfc9110.html#section-13.1-3).
100191

101192
### IfMatch
102193

middleware.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,15 @@ import { IfRange } from "./preconditions/if_range.ts";
1818

1919
/** Middleware options. */
2020
export interface Options {
21-
/** Precondition list. */
21+
/** Precondition list.
22+
*
23+
* Default preconditions are as follows:
24+
* - {@link IfMatch}
25+
* - {@link IfNoneMatch}
26+
* - {@link IfModifiedSince}
27+
* - {@link IfUnmodifiedSince}
28+
* - {@link IfRange}
29+
*/
2230
readonly preconditions?: Iterable<Precondition>;
2331
}
2432

0 commit comments

Comments
 (0)