Response utility collection.
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);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,),);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));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));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);All APIs can be found in the deno doc.
Copyright © 2023-present httpland.
Released under the MIT license
