Skip to content

Commit edb978b

Browse files
committed
fix: remove the headers that caused the precondition to fail
1 parent 1c6d88a commit edb978b

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

.changeset/khaki-doors-stare.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@otterhttp/response": patch
3+
---
4+
5+
remove `etag`, `last-modified` headers before throwing 'precondition failed' errors

packages/response/src/preconditions/index.ts

+10
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ import { noneMatch } from "./if-none-match"
99
import { rangePrecondition } from "./if-range"
1010
import { isUnmodifiedSince } from "./if-unmodified-since"
1111

12+
function clearPreconditionHeadersFromResponse(response: HasOutgoingHeaders) {
13+
response.removeHeader("etag")
14+
response.removeHeader("last-modified")
15+
}
16+
1217
/**
1318
* Checks that the provided response's associated request passes its
1419
* [preconditions]{@link https://datatracker.ietf.org/doc/html/rfc9110#name-conditional-requests},
@@ -44,6 +49,7 @@ export function validatePreconditions(
4449
if (ifMatch != null) {
4550
if (!Object.hasOwnProperty.call(current, "etag")) current.etag = res.getHeader("etag")
4651
if (!someMatch(current.etag, ifMatch)) {
52+
clearPreconditionHeadersFromResponse(res)
4753
throw new ClientError("If-Match Precondition Failed", {
4854
statusCode: HttpStatus.PreconditionFailed,
4955
code: "ERR_PRECONDITION_FAILED_IF_MATCH",
@@ -60,6 +66,7 @@ export function validatePreconditions(
6066
if (ifMatch == null && ifUnmodifiedSince != null) {
6167
if (!Object.hasOwnProperty.call(current, "lastModified")) current.lastModified = res.getHeader("last-modified")
6268
if (!isUnmodifiedSince(current.lastModified, ifUnmodifiedSince)) {
69+
clearPreconditionHeadersFromResponse(res)
6370
throw new ClientError("If-Unmodified-Since Precondition Failed", {
6471
statusCode: HttpStatus.PreconditionFailed,
6572
code: "ERR_PRECONDITION_FAILED_IF_UNMODIFIED_SINCE",
@@ -77,13 +84,15 @@ export function validatePreconditions(
7784
const failed = !noneMatch(current.etag, ifNoneMatch)
7885

7986
if (failed && (method === "GET" || method === "HEAD")) {
87+
clearPreconditionHeadersFromResponse(res)
8088
throw new NotModifiedError("If-None-Match Precondition Failed", {
8189
code: "ERR_PRECONDITION_FAILED_IF_NONE_MATCH",
8290
expected: true,
8391
})
8492
}
8593

8694
if (failed) {
95+
clearPreconditionHeadersFromResponse(res)
8796
throw new ClientError("If-None-Match Precondition Failed", {
8897
statusCode: HttpStatus.PreconditionFailed,
8998
code: "ERR_PRECONDITION_FAILED_IF_NONE_MATCH",
@@ -100,6 +109,7 @@ export function validatePreconditions(
100109
if ((method === "GET" || method === "HEAD") && ifNoneMatch == null && ifModifiedSince != null) {
101110
if (!Object.hasOwnProperty.call(current, "lastModified")) current.lastModified = res.getHeader("last-modified")
102111
if (!hasBeenModifiedSince(current.lastModified, ifModifiedSince)) {
112+
clearPreconditionHeadersFromResponse(res)
103113
throw new NotModifiedError("If-Modified-Since Precondition Failed", {
104114
code: "ERR_PRECONDITION_FAILED_IF_MODIFIED_SINCE",
105115
expected: true,

0 commit comments

Comments
 (0)