From 8c2cd931c17406d00364e3dfa1ed88a15c214cfd Mon Sep 17 00:00:00 2001 From: Tomoki Miyauchi Date: Fri, 5 May 2023 10:40:13 +0900 Subject: [PATCH] chore: initial commit --- .editorconfig | 13 ++++++ .gitignore | 4 ++ .vscode/extensions.json | 5 +++ .vscode/settings.json | 23 ++++++++++ LICENSE | 20 +++++++++ README.md | 94 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 159 insertions(+) create mode 100644 .editorconfig create mode 100644 .gitignore create mode 100644 .vscode/extensions.json create mode 100644 .vscode/settings.json create mode 100644 LICENSE create mode 100644 README.md diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..5d12634 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,13 @@ +# editorconfig.org +root = true + +[*] +indent_style = space +indent_size = 2 +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true + +[*.md] +trim_trailing_whitespace = false diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..79d0efb --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.DS_Store +Thumbs.db +npm/ +coverage diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..09cf720 --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,5 @@ +{ + "recommendations": [ + "denoland.vscode-deno" + ] +} diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..bf81c9c --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,23 @@ +{ + "editor.defaultFormatter": "denoland.vscode-deno", + "editor.formatOnSave": true, + "editor.codeActionsOnSave": { + "source.fixAll": true + }, + "deno.enable": true, + "deno.codeLens.referencesAllFunctions": true, + "deno.internalDebug": true, + "deno.lint": true, + "deno.suggest.completeFunctionCalls": true, + "deno.unstable": true, + "deno.codeLens.references": true, + "deno.suggest.autoImports": true, + "deno.suggest.names": true, + "deno.suggest.imports.autoDiscover": true, + "deno.suggest.paths": true, + "deno.codeLens.test": true, + "deno.codeLens.implementations": true, + "deno.codeLens.testArgs": [ + "--allow-all" + ] +} diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..797920f --- /dev/null +++ b/LICENSE @@ -0,0 +1,20 @@ +MIT License + +Copyright (c) 2023 httpland + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..b4f6ded --- /dev/null +++ b/README.md @@ -0,0 +1,94 @@ +# response-utils + +[![deno land](http://img.shields.io/badge/available%20on-deno.land/x-lightgrey.svg?logo=deno)](https://deno.land/x/response_utils) +[![GitHub release (latest by date)](https://img.shields.io/github/v/release/httpland/response-utils)](https://github.com/httpland/response-utils/releases) +[![codecov](https://codecov.io/github/httpland/response-utils/branch/main/graph/badge.svg)](https://codecov.io/gh/httpland/response-utils) +[![GitHub](https://img.shields.io/github/license/httpland/response-utils)](https://github.com/httpland/response-utils/blob/main/LICENSE) + +[![test](https://github.com/httpland/response-utils/actions/workflows/test.yaml/badge.svg)](https://github.com/httpland/response-utils/actions/workflows/test.yaml) +[![NPM](https://nodei.co/npm/@httpland/response-utils.png?mini=true)](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)); +``` + +## License + +Copyright © 2023-present [httpland](https://github.com/httpland). + +Released under the [MIT](./LICENSE) license