http-utils
HTTP implementation utility collection.
A collection of modules with one or more of the following characteristics:
- Common to many projects
- Too difficult to classify
- Too small
Each module will be split into a separate repository when it becomes classifiable.
equalsRequest
Check two Request
fields equality.
import { equalsRequest } from "https://deno.land/x/http_utils@$VERSION/mod.ts";
import { assertEquals } from "https://deno.land/std@$VERSION/testing/asserts.ts";
assertEquals(
await equalsRequest(
new Request("http://localhost"),
new Request("http://test"),
),
false,
);
assertEquals(
await equalsRequest(
new Request("http://test", { method: "POST" }),
new Request("http://test", { method: "PUT" }),
),
false,
);
isRequest
Whether the value is Request
or not.
import { isRequest } from "https://deno.land/x/http_utils@$VERSION/mod.ts";
import { assertEquals } from "https://deno.land/std@$VERSION/testing/asserts.ts";
assertEquals(isRequest(new Request("http://localhost")), true);
assertEquals(isRequest({}), false);
assertEquals(isRequest(null), false);
equalsHeaders
Check two Headers
field name and field value equality.
import { equalsHeaders } from "https://deno.land/x/http_utils@$VERSION/mod.ts";
import { assertEquals } from "https://deno.land/std@$VERSION/testing/asserts.ts";
assertEquals(
equalsHeaders(new Headers({ a: "b" }), new Headers({ a: "b" })),
true,
);
assertEquals(
equalsHeaders(new Headers({ a: "b" }), new Headers({ c: "d" })),
false,
);
mergeHeaders
Merge two Headers
object.
The first Headers
always takes precedence.
When fields conflict, the first Headers
takes precedence if it is a singleton
field.
If it is a list-based field and not empty, it is appended to the first Headers
field.
Invalid field names and field values are ignored.
No destructive operation is performed on the arguments and returns a new
Headers
object.
import { mergeHeaders } from "https://deno.land/x/http_utils@$VERSION/mod.ts";
import { assertEquals } from "https://deno.land/std@$VERSION/testing/asserts.ts";
assertEquals(
mergeHeaders(
new Headers({ accept: "text/html" }),
new Headers({ accept: "application/json", "content-type": "text/plain" }),
),
new Headers({
accept: "text/html, application/json",
"content-type": "text/plain",
}),
);
assertEquals(
mergeHeaders(
new Headers({ origin: "http://test.test" }),
new Headers({ origin: "http://example.test" }),
),
new Headers({ origin: "http://test.test" }),
);
// origin is singleton field
parseFieldValue
Parse the header field value.
Split field values by <quoted-string>
or <token>
.
import { parseFieldValue } from "https://deno.land/x/http_utils@$VERSION/mod.ts";
import { assertEquals } from "https://deno.land/std@$VERSION/testing/asserts.ts";
assertEquals(
parseFieldValue("text/html, image/webp;q=0.8"),
["text/html", "image/webp;q=0.8"],
);
assertEquals(parseFieldValue(`"Sat, 04 May 1996", "Wed, 14 Sep 2005"`), [
`"Sat, 04 May 1996"`,
`"Wed, 14 Sep 2005"`,
]);
isSingletonField
Weather the field is singleton field or not.
import { isSingletonField } from "https://deno.land/x/http_utils@$VERSION/mod.ts";
import { assertEquals } from "https://deno.land/std@$VERSION/testing/asserts.ts";
assertEquals(isSingletonField("Origin"), true);
assertEquals(isSingletonField("Vary"), false);
RepresentationHeader
HTTP representation data and metadata header fields.
Compliant with RFC 9110, 3.2. Representations.
- Content-Type
- Content-Encoding
- Content-Language
- Content-Length
- Content-Location
- Last-Modified
- ETag
import { RepresentationHeader } from "https://deno.land/x/http_utils@$VERSION/mod.ts";
import { assertEquals } from "https://deno.land/std@$VERSION/testing/asserts.ts";
assertEquals(RepresentationHeader.ContentType, "content-type");
CachingHeader
HTTP Caching header fields.
Compliant with RFC 9111, HTTP Caching.
- Age
- Cache-Control
- Expires
import { CachingHeader } from "https://deno.land/x/http_utils@$VERSION/mod.ts";
import { assertEquals } from "https://deno.land/std@$VERSION/testing/asserts.ts";
assertEquals(CachingHeader.CacheControl, "cache-control");
AuthenticationHeader
HTTP Authentication header fields.
Compliant with RFC 9110, 11. HTTP Authentication.
- WWW-Authenticate
- Authorization
- Authentication-Info
- Proxy-Authenticate
- Proxy-Authorization
- Proxy-Authentication-Info
import { AuthenticationHeader } from "https://deno.land/x/http_utils@$VERSION/mod.ts";
import { assertEquals } from "https://deno.land/std@$VERSION/testing/asserts.ts";
assertEquals(AuthenticationHeader.Authorization, "authorization");
ContentNegotiationHeader
HTTP content negotiation header fields.
Compliant with RFC 9110, 12. Content Negotiation.
- Accept
- Accept-Charset
- Accept-Encoding
- Accept-Language
- Vary
import { ContentNegotiationHeader } from "https://deno.land/x/http_utils@$VERSION/mod.ts";
import { assertEquals } from "https://deno.land/std@$VERSION/testing/asserts.ts";
assertEquals(ContentNegotiationHeader.Accept, "accept");
ConditionalHeader
HTTP conditional requests header fields.
Compliant with RFC 9110, 13. Conditional Requests.
- If-Match
- If-None-Match
- If-Modified-Since
- If-Unmodified-Since
- If-Range
import { ConditionalHeader } from "https://deno.land/x/http_utils@$VERSION/mod.ts";
import { assertEquals } from "https://deno.land/std@$VERSION/testing/asserts.ts";
assertEquals(ConditionalHeader.IfNoneMatch, "if-none-match");
RangeHeader
HTTP range requests header fields.
Compliant with RFC 9110, 14. Range Requests.
- Range
- Accept-Ranges
- Content-Range
import { RangeHeader } from "https://deno.land/x/http_utils@$VERSION/mod.ts";
import { assertEquals } from "https://deno.land/std@$VERSION/testing/asserts.ts";
assertEquals(RangeHeader.Range, "range");
equalsResponse
Check two Response
fields equality.
import { equalsResponse } from "https://deno.land/x/http_utils@$VERSION/mod.ts";
import { assert } from "https://deno.land/std@$VERSION/testing/asserts.ts";
assert(
equalsResponse(
new Response(null, { status: 204, headers: { "content-length": "0" } }),
new Response(null, { status: 204, headers: { "content-length": "0" } }),
),
);
If you also want to check the equivalence of the body, set the mode to strict.
import { equalsResponse } from "https://deno.land/x/http_utils@$VERSION/mod.ts";
import { assert } from "https://deno.land/std@$VERSION/testing/asserts.ts";
assert(
await equalsResponse(
new Response("test1", { status: 200, headers: { "content-length": "5" } }),
new Response("test2", { status: 200, headers: { "content-length": "5" } }),
true,
),
);
Throwing error
In strict mode, if response body has already been read.
import { equalsResponse } from "https://deno.land/x/http_utils@$VERSION/mod.ts";
import {
assert,
assertThrows,
} from "https://deno.land/std@$VERSION/testing/asserts.ts";
const response = new Response("");
await response.text();
assert(response.bodyUsed);
assertThrows(() => equalsResponse(response, response, true));
safeResponse
Safely returns a Response object.
Wraps operations that may cause errors and returns a 500 internal server error response if an error occurs.
import { safeResponse } from "https://deno.land/x/http_utils@$VERSION/mod.ts";
import { assertEquals } from "https://deno.land/std@$VERSION/testing/asserts.ts";
const successRes = await safeResponse(() => new Response());
assertEquals(successRes.status, 200);
const res = await safeResponse(() => {
throw Error();
});
assertEquals(res.status, 500);
debug
By default, the error information is not provided to response.
If debug
flag is true
, the response will includes error information.
isResponse
Whether the value is Response
or not.
import { isResponse } from "https://deno.land/x/http_utils@$VERSION/mod.ts";
import { assertEquals } from "https://deno.land/std@$VERSION/testing/asserts.ts";
assertEquals(isResponse(new Response()), true);
assertEquals(isResponse({}), false);
assertEquals(isResponse(null), false);
isSafeMethod
Whether the method is safe method or not.
Defined in RFC 9110, 9.2.1. Safe Methods.
import { isSafeMethod } from "https://deno.land/x/http_utils@$VERSION/mod.ts";
import { assert } from "https://deno.land/std@$VERSION/testing/asserts.ts";
assert(isSafeMethod("GET"));
assert(isSafeMethod("HEAD"));
assert(isSafeMethod("OPTIONS"));
assert(isSafeMethod("TRACE"));
isIdempotentMethod
Whether the method is idempotent method or not.
Defined in RFC 9110, 9.2.2 Idempotent Methods.
import { isIdempotentMethod } from "https://deno.land/x/http_utils@$VERSION/mod.ts";
import { assert } from "https://deno.land/std@$VERSION/testing/asserts.ts";
assert(isIdempotentMethod("GET"));
assert(isIdempotentMethod("PUT"));
assert(isIdempotentMethod("DELETE"));
isRetrieveMethod
Whether the method is retrieve method or not.
Retrieve method is following:
- GET
- HEAD
import { isRetrieveMethod } from "https://deno.land/x/http_utils@$VERSION/mod.ts";
import { assert } from "https://deno.land/std@$VERSION/testing/asserts.ts";
assert(isRetrieveMethod("GET"));
assert(isRetrieveMethod("HEAD"));
assert(!isRetrieveMethod("POST"));
License
Copyright © 2023-present httpland.
Released under the MIT license