/response-utils

Response utility collection

Primary LanguageTypeScriptMIT LicenseMIT

response-utils

deno land GitHub release (latest by date) codecov GitHub

test NPM

Response utility collection.

createResponse

Create a new Response

If you create a new Response from an existing Response, any options you set in an options argument for the new response replace any corresponding options set in the original Response.

import { createResponse } from "https://deno.land/x/response_utils@$VERSION/create.ts";
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";

declare const init: Response;
const response = createResponse(init, { status: 201 });

assertEquals(response.status, 201);

equalsResponse

Check two Response fields equality.

import { equalsResponse } from "https://deno.land/x/response_utils@$VERSION/equal.ts";
import { assert } from "https://deno.land/std/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/response_utils@$VERSION/equal.ts";
import { assert } from "https://deno.land/std/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/response_utils@$VERSION/equal.ts";
import { assert, assertThrows } from "https://deno.land/std/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/response_utils@$VERSION/is.ts";
import { assert, assertFalse } from "https://deno.land/std/testing/asserts.ts";

assert(isResponse(new Response()));
assertFalse(isResponse({}));
assertFalse(isResponse(null));

withHeader

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

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

declare const init: Response;
declare const fieldName: string;
declare const fieldValue: string;

const response = withHeader(init, fieldName, fieldValue);

assert(response.headers.get(fieldName), fieldValue);
assert(init !== response);

API

All APIs can be found in the deno doc.

License

Copyright © 2023-present httpland.

Released under the MIT license