11# http-utils
22
3- HTTP utility collection for Fetch API.
3+ [ ![ deno land] ( http://img.shields.io/badge/available%20on-deno.land/x-lightgrey.svg?logo=deno )] ( https://deno.land/x/http_utils )
4+ [ ![ GitHub release (latest by date)] ( https://img.shields.io/github/v/release/httpland/http_utils )] ( https://github.com/httpland/http_utils/releases )
5+ [ ![ codecov] ( https://codecov.io/github/httpland/http-utils/branch/main/graph/badge.svg )] ( https://codecov.io/gh/httpland/http-utils )
6+ [ ![ GitHub] ( https://img.shields.io/github/license/httpland/http-utils )] ( https://github.com/httpland/http-utils/blob/main/LICENSE )
7+
8+ [ ![ test] ( https://github.com/httpland/http-utils/actions/workflows/test.yaml/badge.svg )] ( https://github.com/httpland/http-utils/actions/workflows/test.yaml )
9+ [ ![ NPM] ( https://nodei.co/npm/@httpland/http-utils.png?mini=true )] ( https://nodei.co/npm/@httpland/http-utils/ )
410
5- A collection of modules with one or more of the following characteristics:
11+ HTTP utility collection for Fetch API.
612
7- - Common to many projects
8- - Too difficult to classify
9- - Too small
13+ ## Request
1014
11- Each module will be split into a separate repository when it becomes
12- classifiable.
15+ Utilities for ` Request ` object.
1316
14- ## equalsRequest
17+ ### equalsRequest
1518
1619Check two ` Request ` fields equality.
1720
@@ -46,7 +49,7 @@ assert(
4649);
4750```
4851
49- ### Throwing error
52+ #### Throwing error
5053
5154In strict mode, if request body has already been read.
5255
@@ -62,7 +65,7 @@ assert(request.bodyUsed);
6265assertThrows (() => equalsRequest (request , request , true ));
6366```
6467
65- ## isRequest
68+ ### isRequest
6669
6770Whether the input is ` Request ` or not.
6871
@@ -75,7 +78,77 @@ assertEquals(isRequest({}), false);
7578assertEquals (isRequest (null ), false );
7679```
7780
78- ## equalsHeaders
81+ ## Response
82+
83+ Utilities for ` Response ` object.
84+
85+ ### equalsResponse
86+
87+ Check two ` Response ` fields equality.
88+
89+ ``` ts
90+ import { equalsResponse } from " https://deno.land/x/http_utils@$VERSION/response.ts" ;
91+ import { assert } from " https://deno.land/std@$VERSION/testing/asserts.ts" ;
92+
93+ assert (
94+ equalsResponse (
95+ new Response (null , { status: 204 , headers: { " content-length" : " 0" } }),
96+ new Response (null , { status: 204 , headers: { " content-length" : " 0" } }),
97+ ),
98+ );
99+ ```
100+
101+ If you also want to check the equivalence of the body, set the mode to strict.
102+
103+ ``` ts
104+ import { equalsResponse } from " https://deno.land/x/http_utils@$VERSION/response.ts" ;
105+ import { assert } from " https://deno.land/std@$VERSION/testing/asserts.ts" ;
106+
107+ assert (
108+ await equalsResponse (
109+ new Response (" test1" , { status: 200 , headers: { " content-length" : " 5" } }),
110+ new Response (" test2" , { status: 200 , headers: { " content-length" : " 5" } }),
111+ true ,
112+ ),
113+ );
114+ ```
115+
116+ #### Throwing error
117+
118+ In strict mode, if response body has already been read.
119+
120+ ``` ts
121+ import { equalsResponse } from " https://deno.land/x/http_utils@$VERSION/response.ts" ;
122+ import {
123+ assert ,
124+ assertThrows ,
125+ } from " https://deno.land/std@$VERSION/testing/asserts.ts" ;
126+
127+ const response = new Response (" " );
128+ await response .text ();
129+
130+ assert (response .bodyUsed );
131+ assertThrows (() => equalsResponse (response , response , true ));
132+ ```
133+
134+ ### isResponse
135+
136+ Whether the input is ` Response ` or not.
137+
138+ ``` ts
139+ import { isResponse } from " https://deno.land/x/http_utils@$VERSION/response.ts" ;
140+ import { assertEquals } from " https://deno.land/std@$VERSION/testing/asserts.ts" ;
141+
142+ assertEquals (isResponse (new Response ()), true );
143+ assertEquals (isResponse ({}), false );
144+ assertEquals (isResponse (null ), false );
145+ ```
146+
147+ ## Headers
148+
149+ Utilities for ` Headers ` object.
150+
151+ ### equalsHeaders
79152
80153Check two ` Headers ` field name and field value equality.
81154
@@ -93,7 +166,7 @@ assertEquals(
93166);
94167```
95168
96- ## filterKeys
169+ ### filterKeys
97170
98171Returns a new ` Headers ` with all entries of the given headers except the ones
99172that have a key(header name or field name) that does not match the given
@@ -115,7 +188,7 @@ assert(headers.has("content-type"));
115188assert (! headers .has (" date" ));
116189```
117190
118- ## isMessageMetadataHeader
191+ ### isMessageMetadataHeader
119192
120193Whether the input is [ MessageMetadataHeader] ( #messagemetadataheader ) or not.
121194
@@ -127,7 +200,7 @@ assert(isMessageMetadataHeader("date"));
127200assert (! isMessageMetadataHeader (" <others>" ));
128201```
129202
130- ## isMessageForwardingHeader
203+ ### isMessageForwardingHeader
131204
132205Whether the input is [ MessageForwardingHeader] ( #messageforwardingheader ) or not.
133206
@@ -139,7 +212,7 @@ assert(isMessageForwardingHeader("connection"));
139212assert (! isMessageForwardingHeader (" <others>" ));
140213```
141214
142- ## isRepresentationHeader
215+ ### isRepresentationHeader
143216
144217Whether the input is [ RepresentationHeader] ( #representationheader ) or not.
145218
@@ -151,7 +224,7 @@ assert(isRepresentationHeader("content-type"));
151224assert (! isRepresentationHeader (" <others>" ));
152225```
153226
154- ## isAuthenticationHeader
227+ ### isAuthenticationHeader
155228
156229Whether the input is [ AuthenticationHeader] ( #authenticationheader ) or not.
157230
@@ -163,7 +236,7 @@ assert(isAuthenticationHeader("authorization"));
163236assert (! isAuthenticationHeader (" <others>" ));
164237```
165238
166- ## isContentNegotiationHeader
239+ ### isContentNegotiationHeader
167240
168241Whether the input is [ ContentNegotiationHeader] ( #contentnegotiationheader ) or
169242not.
@@ -176,7 +249,7 @@ assert(isContentNegotiationHeader("accept"));
176249assert (! isContentNegotiationHeader (" <others>" ));
177250```
178251
179- ## isConditionalHeader
252+ ### isConditionalHeader
180253
181254Whether the input is [ ConditionalHeader] ( #conditionalheader ) or not.
182255
@@ -188,7 +261,7 @@ assert(isConditionalHeader("if-match"));
188261assert (! isConditionalHeader (" <others>" ));
189262```
190263
191- ## isRangeHeader
264+ ### isRangeHeader
192265
193266Whether the input is [ RangeHeader] ( #rangeheader ) or not.
194267
@@ -200,7 +273,7 @@ assert(isRangeHeader("range"));
200273assert (! isRangeHeader (" <others>" ));
201274```
202275
203- ## isCachingHeader
276+ ### isCachingHeader
204277
205278Whether the input is [ CachingHeader] ( #cachingheader ) or not.
206279
@@ -212,7 +285,7 @@ assert(isCachingHeader("age"));
212285assert (! isCachingHeader (" <others>" ));
213286```
214287
215- ## MessageMetadataHeader
288+ ### MessageMetadataHeader
216289
217290HTTP Message Metadata header fields.
218291
@@ -229,7 +302,7 @@ import { assertEquals } from "https://deno.land/std@$VERSION/testing/asserts.ts"
229302assertEquals (MessageMetadataHeader .Date , " date" );
230303```
231304
232- ## MessageForwardingHeader
305+ ### MessageForwardingHeader
233306
234307HTTP Message Forwarding header fields.
235308
@@ -247,7 +320,7 @@ import { assertEquals } from "https://deno.land/std@$VERSION/testing/asserts.ts"
247320assertEquals (MessageForwardingHeader .Via , " via" );
248321```
249322
250- ## RepresentationHeader
323+ ### RepresentationHeader
251324
252325HTTP representation data and metadata header fields.
253326
@@ -269,7 +342,7 @@ import { assertEquals } from "https://deno.land/std@$VERSION/testing/asserts.ts"
269342assertEquals (RepresentationHeader .ContentType , " content-type" );
270343```
271344
272- ## AuthenticationHeader
345+ ### AuthenticationHeader
273346
274347HTTP Authentication header fields.
275348
@@ -290,7 +363,7 @@ import { assertEquals } from "https://deno.land/std@$VERSION/testing/asserts.ts"
290363assertEquals (AuthenticationHeader .Authorization , " authorization" );
291364```
292365
293- ## ContentNegotiationHeader
366+ ### ContentNegotiationHeader
294367
295368HTTP content negotiation header fields.
296369
@@ -310,7 +383,7 @@ import { assertEquals } from "https://deno.land/std@$VERSION/testing/asserts.ts"
310383assertEquals (ContentNegotiationHeader .Accept , " accept" );
311384```
312385
313- ## ConditionalHeader
386+ ### ConditionalHeader
314387
315388HTTP conditional requests header fields.
316389
@@ -330,7 +403,7 @@ import { assertEquals } from "https://deno.land/std@$VERSION/testing/asserts.ts"
330403assertEquals (ConditionalHeader .IfNoneMatch , " if-none-match" );
331404```
332405
333- ## RangeHeader
406+ ### RangeHeader
334407
335408HTTP range requests header fields.
336409
@@ -348,7 +421,7 @@ import { assertEquals } from "https://deno.land/std@$VERSION/testing/asserts.ts"
348421assertEquals (RangeHeader .Range , " range" );
349422```
350423
351- ## CachingHeader
424+ ### CachingHeader
352425
353426HTTP Caching header fields.
354427
@@ -365,69 +438,11 @@ import { assertEquals } from "https://deno.land/std@$VERSION/testing/asserts.ts"
365438assertEquals (CachingHeader .CacheControl , " cache-control" );
366439```
367440
368- ## equalsResponse
369-
370- Check two ` Response ` fields equality.
371-
372- ``` ts
373- import { equalsResponse } from " https://deno.land/x/http_utils@$VERSION/response.ts" ;
374- import { assert } from " https://deno.land/std@$VERSION/testing/asserts.ts" ;
375-
376- assert (
377- equalsResponse (
378- new Response (null , { status: 204 , headers: { " content-length" : " 0" } }),
379- new Response (null , { status: 204 , headers: { " content-length" : " 0" } }),
380- ),
381- );
382- ```
383-
384- If you also want to check the equivalence of the body, set the mode to strict.
385-
386- ``` ts
387- import { equalsResponse } from " https://deno.land/x/http_utils@$VERSION/response.ts" ;
388- import { assert } from " https://deno.land/std@$VERSION/testing/asserts.ts" ;
389-
390- assert (
391- await equalsResponse (
392- new Response (" test1" , { status: 200 , headers: { " content-length" : " 5" } }),
393- new Response (" test2" , { status: 200 , headers: { " content-length" : " 5" } }),
394- true ,
395- ),
396- );
397- ```
441+ ## Method
398442
399- ### Throwing error
443+ Utilities for HTTP method.
400444
401- In strict mode, if response body has already been read.
402-
403- ``` ts
404- import { equalsResponse } from " https://deno.land/x/http_utils@$VERSION/response.ts" ;
405- import {
406- assert ,
407- assertThrows ,
408- } from " https://deno.land/std@$VERSION/testing/asserts.ts" ;
409-
410- const response = new Response (" " );
411- await response .text ();
412-
413- assert (response .bodyUsed );
414- assertThrows (() => equalsResponse (response , response , true ));
415- ```
416-
417- ## isResponse
418-
419- Whether the input is ` Response ` or not.
420-
421- ``` ts
422- import { isResponse } from " https://deno.land/x/http_utils@$VERSION/response.ts" ;
423- import { assertEquals } from " https://deno.land/std@$VERSION/testing/asserts.ts" ;
424-
425- assertEquals (isResponse (new Response ()), true );
426- assertEquals (isResponse ({}), false );
427- assertEquals (isResponse (null ), false );
428- ```
429-
430- ## isSafeMethod
445+ ### isSafeMethod
431446
432447Whether the method is safe method or not.
433448
@@ -444,7 +459,7 @@ assert(isSafeMethod("OPTIONS"));
444459assert (isSafeMethod (" TRACE" ));
445460```
446461
447- ## isIdempotentMethod
462+ ### isIdempotentMethod
448463
449464Whether the method is idempotent method or not.
450465
@@ -460,7 +475,7 @@ assert(isIdempotentMethod("PUT"));
460475assert (isIdempotentMethod (" DELETE" ));
461476```
462477
463- ## isRetrieveMethod
478+ ### isRetrieveMethod
464479
465480Whether the method is retrieve method or not.
466481
@@ -478,7 +493,16 @@ assert(isRetrieveMethod("HEAD"));
478493assert (! isRetrieveMethod (" POST" ));
479494```
480495
481- ## withHeader
496+ ## Message
497+
498+ Utilities for HTTP message.
499+
500+ HTTP message is following union types:
501+
502+ - ` Request `
503+ - ` Response `
504+
505+ ### withHeader
482506
483507Return an instance with the provided value replacing the specified header. There
484508are no side effects on the original target.
0 commit comments