[go: up one dir, main page]

Skip to content

httpland/http-utils

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

http-utils

deno land GitHub release (latest by date) codecov GitHub

test NPM

HTTP utility collection.

Compliant with RFC 9110.

Request

Utilities for Request object.

@deprecated Move to request-utils.

equalsRequest

Check two Request fields equality.

import { equalsRequest } from "https://deno.land/x/http_utils@$VERSION/request.ts";
import { assert } from "https://deno.land/std/testing/asserts.ts";

declare const url: URL;

assert(
  equalsRequest(
    new Request(url, { method: "HEAD" }),
    new Request(url, { method: "HEAD" }),
  ),
);

If you also want to check the equivalence of the body, set the mode to strict.

import { equalsRequest } from "https://deno.land/x/http_utils@$VERSION/request.ts";
import { assert } from "https://deno.land/std/testing/asserts.ts";

declare const url: URL;

assert(
  await equalsRequest(
    new Request(url, { body: "", method: "POST" }),
    new Request(url, { body: "", method: "POST" }),
    true,
  ),
);

Throwing error

In strict mode, if request body has already been read.

import { equalsRequest } from "https://deno.land/x/http_utils@$VERSION/request.ts";
import { assert, assertThrows } from "https://deno.land/std/testing/asserts.ts";

declare const url: URL;
const request = new Request(url, { body: "" });
await request.text();

assert(request.bodyUsed);
assertThrows(() => equalsRequest(request, request, true));

isRequest

Whether the input is Request or not.

import { isRequest } from "https://deno.land/x/http_utils@$VERSION/request.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);

Response

Utilities for Response object.

@deprecated Move to response-utils.

equalsResponse

Check two Response fields equality.

import { equalsResponse } from "https://deno.land/x/http_utils@$VERSION/response.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/response.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/response.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));

isResponse

Whether the input is Response or not.

import { isResponse } from "https://deno.land/x/http_utils@$VERSION/response.ts";
import { assertEquals } from "https://deno.land/std@$VERSION/testing/asserts.ts";

assertEquals(isResponse(new Response()), true);
assertEquals(isResponse({}), false);
assertEquals(isResponse(null), false);

Headers

Utilities for Headers object.

equalsHeaders

@deprecated Move to headers-utils.

Check two Headers field name and field value equality.

import { equalsHeaders } from "https://deno.land/x/http_utils@$VERSION/header.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,
);

filterKeys

@deprecated Move to headers-utils.

Returns a new Headers with all entries of the given headers except the ones that have a key(header name or field name) that does not match the given predicate.

import { filterKeys } from "https://deno.land/x/http_utils@$VERSION/header.ts";
import { assert } from "https://deno.land/std@$VERSION/testing/asserts.ts";

const headers = filterKeys(
  new Headers({
    "date": "<date>",
    "content-type": "<content-type>",
  }),
  (key) => key.startsWith("content"),
);

assert(headers.has("content-type"));
assert(!headers.has("date"));

isMessageMetadataHeader

Whether the input is MessageMetadataHeader or not.

import { isMessageMetadataHeader } from "https://deno.land/x/http_utils@$VERSION/header.ts";
import { assert } from "https://deno.land/std@$VERSION/testing/asserts.ts";

assert(isMessageMetadataHeader("date"));
assert(!isMessageMetadataHeader("<others>"));

isMessageForwardingHeader

Whether the input is MessageForwardingHeader or not.

import { isMessageForwardingHeader } from "https://deno.land/x/http_utils@$VERSION/header.ts";
import { assert } from "https://deno.land/std@$VERSION/testing/asserts.ts";

assert(isMessageForwardingHeader("connection"));
assert(!isMessageForwardingHeader("<others>"));

isRepresentationHeader

Whether the input is RepresentationHeader or not.

import { isRepresentationHeader } from "https://deno.land/x/http_utils@$VERSION/header.ts";
import { assert } from "https://deno.land/std@$VERSION/testing/asserts.ts";

assert(isRepresentationHeader("content-type"));
assert(!isRepresentationHeader("<others>"));

isAuthenticationHeader

Whether the input is AuthenticationHeader or not.

import { isAuthenticationHeader } from "https://deno.land/x/http_utils@$VERSION/header.ts";
import { assert } from "https://deno.land/std@$VERSION/testing/asserts.ts";

assert(isAuthenticationHeader("authorization"));
assert(!isAuthenticationHeader("<others>"));

isContentNegotiationHeader

Whether the input is ContentNegotiationHeader or not.

import { isContentNegotiationHeader } from "https://deno.land/x/http_utils@$VERSION/header.ts";
import { assert } from "https://deno.land/std@$VERSION/testing/asserts.ts";

assert(isContentNegotiationHeader("accept"));
assert(!isContentNegotiationHeader("<others>"));

isConditionalHeader

Whether the input is ConditionalHeader or not.

import { isConditionalHeader } from "https://deno.land/x/http_utils@$VERSION/header.ts";
import { assert } from "https://deno.land/std@$VERSION/testing/asserts.ts";

assert(isConditionalHeader("if-match"));
assert(!isConditionalHeader("<others>"));

isRangeHeader

Whether the input is RangeHeader or not.

import { isRangeHeader } from "https://deno.land/x/http_utils@$VERSION/header.ts";
import { assert } from "https://deno.land/std@$VERSION/testing/asserts.ts";

assert(isRangeHeader("range"));
assert(!isRangeHeader("<others>"));

isCachingHeader

Whether the input is CachingHeader or not.

import { isCachingHeader } from "https://deno.land/x/http_utils@$VERSION/header.ts";
import { assert } from "https://deno.land/std@$VERSION/testing/asserts.ts";

assert(isCachingHeader("age"));
assert(!isCachingHeader("<others>"));

MessageMetadataHeader

HTTP Message Metadata header fields.

Compliant with RFC 9110, 6.6. Message Metadata.

  • Date
  • Trailer
import { MessageMetadataHeader } from "https://deno.land/x/http_utils@$VERSION/header.ts";
import { assertEquals } from "https://deno.land/std@$VERSION/testing/asserts.ts";

assertEquals(MessageMetadataHeader.Date, "date");

MessageForwardingHeader

HTTP Message Forwarding header fields.

Compliant with RFC 9110, 7.6. Message Forwarding.

  • Connection
  • Max-Forwards
  • Via
import { MessageForwardingHeader } from "https://deno.land/x/http_utils@$VERSION/header.ts";
import { assertEquals } from "https://deno.land/std@$VERSION/testing/asserts.ts";

assertEquals(MessageForwardingHeader.Via, "via");

RepresentationHeader

HTTP representation data and metadata header fields.

Compliant with RFC 9110, 8. Representations.

  • Content-Type
  • Content-Encoding
  • Content-Language
  • Content-Length
  • Content-Location
  • Last-Modified
  • ETag
import { RepresentationHeader } from "https://deno.land/x/http_utils@$VERSION/header.ts";
import { assertEquals } from "https://deno.land/std@$VERSION/testing/asserts.ts";

assertEquals(RepresentationHeader.ContentType, "content-type");

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/header.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/header.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/header.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/header.ts";
import { assertEquals } from "https://deno.land/std@$VERSION/testing/asserts.ts";

assertEquals(RangeHeader.Range, "range");

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/header.ts";
import { assertEquals } from "https://deno.land/std@$VERSION/testing/asserts.ts";

assertEquals(CachingHeader.CacheControl, "cache-control");

Method

Utilities for HTTP method.

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/method.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/method.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/method.ts";
import { assert } from "https://deno.land/std@$VERSION/testing/asserts.ts";

assert(isRetrieveMethod("GET"));
assert(isRetrieveMethod("HEAD"));
assert(!isRetrieveMethod("POST"));

Message

Utilities for HTTP message.

HTTP message is following union types:

  • Request
  • Response

withHeader

@deprecated Move to request-utils. @deprecated Move to response-utils.

Return an instance with the provided value replacing the specified header. There are no side effects on the original target.

This was inspired by PSR-7: HTTP message interfaces.

Request:

import { withHeader } from "https://deno.land/x/http_utils@$VERSION/message.ts";
import { assert } from "https://deno.land/std@$VERSION/testing/asserts.ts";

declare const init: Request;
declare const header: string;
declare const value: string;

const request = withHeader(init, header, value);

assert(request.headers.get(header), value);
assert(init !== request);

Response:

import { withHeader } from "https://deno.land/x/http_utils@$VERSION/message.ts";
import { assert } from "https://deno.land/std@$VERSION/testing/asserts.ts";

declare const init: Response;
declare const header: string;
declare const value: string;

const response = withHeader(init, header, value);

assert(response.headers.get(header), value);
assert(init !== response);

Lists

Compliant with RFC 9110, 5.6.1. Lists.

parseListFields

Parse list-based fields into array.

Strings enclosed in double quotes are safely handled.

import { parseListFields } from "https://deno.land/x/http_utils@$VERSION/list.ts";
import { assertEquals } from "https://deno.land/std@$VERSION/testing/asserts.ts";

assertEquals(parseListFields("foo , ,bar,charlie"), [
  "foo",
  "bar",
  "charlie",
]);
assertEquals(parseListFields(`"Sat, 04 May 1996", "Wed, 14 Sep 2005"`), [
  `"Sat, 04 May 1996"`,
  `"Wed, 14 Sep 2005"`,
]);

Tokens

Compliant with RFC 9110, 5.6.2. Tokens.

isTchar

Whether the input is tchar or not.

import { isTchar } from "https://deno.land/x/http_utils@$VERSION/token.ts";
import {
  assert,
  assertFalse,
} from "https://deno.land/std@$VERSION/testing/asserts.ts";

assert(isTchar("!"));
assert(isTchar("a"));
assert(isTchar("Z"));
assertFalse(isTchar(""));

isToken

Whether the input is token or not.

import { isToken } from "https://deno.land/x/http_utils@$VERSION/token.ts";
import {
  assert,
  assertFalse,
} from "https://deno.land/std@$VERSION/testing/asserts.ts";

assert(isToken("token"));
assert(isToken("*!~"));
assertFalse(isToken(""));

Quoted Strings

Compliant with RFC 9110, 5.6.4. Quoted Strings.

isQdtext

Whether the input is qdtext or not.

import { isQdtext } from "https://deno.land/x/http_utils@$VERSION/quoted_string.ts";
import {
  assert,
  assertFalse,
} from "https://deno.land/std@$VERSION/testing/asserts.ts";

assert(isQdtext("\t"));
assert(isQdtext("\xFF"));
assertFalse(isQdtext(`"`));

isQuotedPair