https://github.com/httpland/response-utils
Response utility collection
https://github.com/httpland/response-utils
collection fetch-api http response utilities utility
Last synced: 25 days ago
JSON representation
Response utility collection
- Host: GitHub
- URL: https://github.com/httpland/response-utils
- Owner: httpland
- License: mit
- Created: 2023-05-05T01:42:42.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2023-05-09T01:12:10.000Z (about 3 years ago)
- Last Synced: 2025-10-06T13:56:39.666Z (9 months ago)
- Topics: collection, fetch-api, http, response, utilities, utility
- Language: TypeScript
- Homepage: https://deno.land/x/response_utils
- Size: 16.6 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# response-utils
[](https://deno.land/x/response_utils)
[](https://github.com/httpland/response-utils/releases)
[](https://codecov.io/gh/httpland/response-utils)
[](https://github.com/httpland/response-utils/blob/main/LICENSE)
[](https://github.com/httpland/response-utils/actions/workflows/test.yaml)
[](https://nodei.co/npm/@httpland/response-utils/)
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`.
```ts
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.
```ts
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.
```ts
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.
```ts
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.
```ts
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`.
```ts
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](https://deno.land/x/response_utils?doc).
## License
Copyright © 2023-present [httpland](https://github.com/httpland).
Released under the [MIT](./LICENSE) license