Skip to content

Repository files navigation

response-utils

deno landGitHub release (latest by date)codecovGitHub

testNPM

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";declareconstinit: Response;constresponse=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(newResponse(null,{status: 204,headers: {"content-length": "0"}}),newResponse(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(awaitequalsResponse(newResponse("test1",{status: 200,headers: {"content-length": "5"}}),newResponse("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";constresponse=newResponse("");awaitresponse.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(newResponse()));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";declareconstinit: Response;declareconstfieldName: string;declareconstfieldValue: string;constresponse=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

Releases

Used by

Contributors

Languages