Skip to main content

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);

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" } }),
    false,
  ),
);

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"));

License

Copyright © 2023-present httpland.

Released under the MIT license