|
| 1 | +# response-utils |
| 2 | + |
| 3 | +[](https://deno.land/x/response_utils) |
| 4 | +[](https://github.com/httpland/response-utils/releases) |
| 5 | +[](https://codecov.io/gh/httpland/response-utils) |
| 6 | +[](https://github.com/httpland/response-utils/blob/main/LICENSE) |
| 7 | + |
| 8 | +[](https://github.com/httpland/response-utils/actions/workflows/test.yaml) |
| 9 | +[](https://nodei.co/npm/@httpland/response-utils/) |
| 10 | + |
| 11 | +Response utility collection. |
| 12 | + |
| 13 | +## createResponse |
| 14 | + |
| 15 | +Create a new `Response` |
| 16 | + |
| 17 | +If you create a new `Response` from an existing `Response`, any options you set |
| 18 | +in an options argument for the new response replace any corresponding options |
| 19 | +set in the original `Response`. |
| 20 | + |
| 21 | +```ts |
| 22 | +import { createResponse } from "https://deno.land/x/response_utils@$VERSION/create.ts"; |
| 23 | +import { assertEquals } from "https://deno.land/std/testing/asserts.ts"; |
| 24 | + |
| 25 | +declare const init: Response; |
| 26 | +const response = createResponse(init, { status: 201 }); |
| 27 | + |
| 28 | +assertEquals(response.status, 201); |
| 29 | +``` |
| 30 | + |
| 31 | +## equalsResponse |
| 32 | + |
| 33 | +Check two `Response` fields equality. |
| 34 | + |
| 35 | +```ts |
| 36 | +import { equalsResponse } from "https://deno.land/x/response_utils@$VERSION/equal.ts"; |
| 37 | +import { assert } from "https://deno.land/std/testing/asserts.ts"; |
| 38 | + |
| 39 | +assert( |
| 40 | + equalsResponse( |
| 41 | + new Response(null, { status: 204, headers: { "content-length": "0" } }), |
| 42 | + new Response(null, { status: 204, headers: { "content-length": "0" } }), |
| 43 | + ), |
| 44 | +); |
| 45 | +``` |
| 46 | + |
| 47 | +If you also want to check the equivalence of the body, set the mode to strict. |
| 48 | + |
| 49 | +```ts |
| 50 | +import { equalsResponse } from "https://deno.land/x/response_utils@$VERSION/equal.ts"; |
| 51 | +import { assert } from "https://deno.land/std/testing/asserts.ts"; |
| 52 | + |
| 53 | +assert( |
| 54 | + await equalsResponse( |
| 55 | + new Response("test1", { status: 200, headers: { "content-length": "5" } }), |
| 56 | + new Response("test2", { status: 200, headers: { "content-length": "5" } }), |
| 57 | + true, |
| 58 | + ), |
| 59 | +); |
| 60 | +``` |
| 61 | + |
| 62 | +### Throwing error |
| 63 | + |
| 64 | +In strict mode, if response body has already been read. |
| 65 | + |
| 66 | +```ts |
| 67 | +import { equalsResponse } from "https://deno.land/x/response_utils@$VERSION/equal.ts"; |
| 68 | +import { assert, assertThrows } from "https://deno.land/std/testing/asserts.ts"; |
| 69 | + |
| 70 | +const response = new Response(""); |
| 71 | +await response.text(); |
| 72 | + |
| 73 | +assert(response.bodyUsed); |
| 74 | +assertThrows(() => equalsResponse(response, response, true)); |
| 75 | +``` |
| 76 | + |
| 77 | +## isResponse |
| 78 | + |
| 79 | +Whether the input is `Response` or not. |
| 80 | + |
| 81 | +```ts |
| 82 | +import { isResponse } from "https://deno.land/x/response_utils@$VERSION/is.ts"; |
| 83 | +import { assert, assertFalse } from "https://deno.land/std/testing/asserts.ts"; |
| 84 | + |
| 85 | +assert(isResponse(new Response())); |
| 86 | +assertFalse(isResponse({})); |
| 87 | +assertFalse(isResponse(null)); |
| 88 | +``` |
| 89 | + |
| 90 | +## License |
| 91 | + |
| 92 | +Copyright © 2023-present [httpland](https://github.com/httpland). |
| 93 | + |
| 94 | +Released under the [MIT](./LICENSE) license |
0 commit comments