Skip to content

Commit 54b8930

Browse files
committed
Use constants for internal key names
1 parent e0b6bb2 commit 54b8930

File tree

1 file changed

+70
-47
lines changed

1 file changed

+70
-47
lines changed

packages/headers/src/lib/super-headers.ts

+70-47
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ import { type HeaderValue } from './header-value.ts';
1010
import { type SetCookieInit, SetCookie } from './set-cookie.ts';
1111
import { isIterable } from './utils.ts';
1212

13-
const CRLF = '\r\n';
14-
const SetCookieKey = 'set-cookie';
15-
1613
type DateInit = number | Date;
1714

1815
interface SuperHeadersPropertyInit {
@@ -114,6 +111,32 @@ export type SuperHeadersInit =
114111
| Iterable<[string, string]>
115112
| (SuperHeadersPropertyInit & Record<string, string | HeaderValue>);
116113

114+
const CRLF = '\r\n';
115+
116+
const AcceptKey = 'accept';
117+
const AcceptEncodingKey = 'accept-encoding';
118+
const AcceptLanguageKey = 'accept-language';
119+
const AcceptRangesKey = 'accept-ranges';
120+
const AgeKey = 'age';
121+
const CacheControlKey = 'cache-control';
122+
const ConnectionKey = 'connection';
123+
const ContentDispositionKey = 'content-disposition';
124+
const ContentEncodingKey = 'content-encoding';
125+
const ContentLanguageKey = 'content-language';
126+
const ContentLengthKey = 'content-length';
127+
const ContentTypeKey = 'content-type';
128+
const CookieKey = 'cookie';
129+
const DateKey = 'date';
130+
const ETagKey = 'etag';
131+
const ExpiresKey = 'expires';
132+
const HostKey = 'host';
133+
const IfModifiedSinceKey = 'if-modified-since';
134+
const IfUnmodifiedSinceKey = 'if-unmodified-since';
135+
const LastModifiedKey = 'last-modified';
136+
const LocationKey = 'location';
137+
const RefererKey = 'referer';
138+
const SetCookieKey = 'set-cookie';
139+
117140
/**
118141
* An enhanced JavaScript `Headers` interface with type-safe access.
119142
*
@@ -335,11 +358,11 @@ export class SuperHeaders extends Headers {
335358
* [HTTP/1.1 Specification](https://datatracker.ietf.org/doc/html/rfc7231#section-5.3.2)
336359
*/
337360
get accept(): Accept {
338-
return this.#getHeaderValue('accept', Accept);
361+
return this.#getHeaderValue(AcceptKey, Accept);
339362
}
340363

341364
set accept(value: string | AcceptInit | undefined | null) {
342-
this.#setHeaderValue('accept', Accept, value);
365+
this.#setHeaderValue(AcceptKey, Accept, value);
343366
}
344367

345368
/**
@@ -351,11 +374,11 @@ export class SuperHeaders extends Headers {
351374
* [HTTP/1.1 Specification](https://datatracker.ietf.org/doc/html/rfc7231#section-5.3.4)
352375
*/
353376
get acceptEncoding(): AcceptEncoding {
354-
return this.#getHeaderValue('accept-encoding', AcceptEncoding);
377+
return this.#getHeaderValue(AcceptEncodingKey, AcceptEncoding);
355378
}
356379

357380
set acceptEncoding(value: string | AcceptEncodingInit | undefined | null) {
358-
this.#setHeaderValue('accept-encoding', AcceptEncoding, value);
381+
this.#setHeaderValue(AcceptEncodingKey, AcceptEncoding, value);
359382
}
360383

361384
/**
@@ -367,11 +390,11 @@ export class SuperHeaders extends Headers {
367390
* [HTTP/1.1 Specification](https://datatracker.ietf.org/doc/html/rfc7231#section-5.3.5)
368391
*/
369392
get acceptLanguage(): AcceptLanguage {
370-
return this.#getHeaderValue('accept-language', AcceptLanguage);
393+
return this.#getHeaderValue(AcceptLanguageKey, AcceptLanguage);
371394
}
372395

373396
set acceptLanguage(value: string | AcceptLanguageInit | undefined | null) {
374-
this.#setHeaderValue('accept-language', AcceptLanguage, value);
397+
this.#setHeaderValue(AcceptLanguageKey, AcceptLanguage, value);
375398
}
376399

377400
/**
@@ -382,11 +405,11 @@ export class SuperHeaders extends Headers {
382405
* [HTTP/1.1 Specification](https://datatracker.ietf.org/doc/html/rfc7233#section-2.3)
383406
*/
384407
get acceptRanges(): string | null {
385-
return this.#getStringValue('accept-ranges');
408+
return this.#getStringValue(AcceptRangesKey);
386409
}
387410

388411
set acceptRanges(value: string | undefined | null) {
389-
this.#setStringValue('accept-ranges', value);
412+
this.#setStringValue(AcceptRangesKey, value);
390413
}
391414

392415
/**
@@ -397,11 +420,11 @@ export class SuperHeaders extends Headers {
397420
* [HTTP/1.1 Specification](https://datatracker.ietf.org/doc/html/rfc7234#section-5.1)
398421
*/
399422
get age(): number | null {
400-
return this.#getNumberValue('age');
423+
return this.#getNumberValue(AgeKey);
401424
}
402425

403426
set age(value: string | number | undefined | null) {
404-
this.#setNumberValue('age', value);
427+
this.#setNumberValue(AgeKey, value);
405428
}
406429

407430
/**
@@ -412,11 +435,11 @@ export class SuperHeaders extends Headers {
412435
* [HTTP/1.1 Specification](https://datatracker.ietf.org/doc/html/rfc7234#section-5.2)
413436
*/
414437
get cacheControl(): CacheControl {
415-
return this.#getHeaderValue('cache-control', CacheControl);
438+
return this.#getHeaderValue(CacheControlKey, CacheControl);
416439
}
417440

418441
set cacheControl(value: string | CacheControlInit | undefined | null) {
419-
this.#setHeaderValue('cache-control', CacheControl, value);
442+
this.#setHeaderValue(CacheControlKey, CacheControl, value);
420443
}
421444

422445
/**
@@ -428,11 +451,11 @@ export class SuperHeaders extends Headers {
428451
* [HTTP/1.1 Specification](https://datatracker.ietf.org/doc/html/rfc7230#section-6.1)
429452
*/
430453
get connection(): string | null {
431-
return this.#getStringValue('connection');
454+
return this.#getStringValue(ConnectionKey);
432455
}
433456

434457
set connection(value: string | undefined | null) {
435-
this.#setStringValue('connection', value);
458+
this.#setStringValue(ConnectionKey, value);
436459
}
437460

438461
/**
@@ -443,11 +466,11 @@ export class SuperHeaders extends Headers {
443466
* [RFC 6266](https://datatracker.ietf.org/doc/html/rfc6266)
444467
*/
445468
get contentDisposition(): ContentDisposition {
446-
return this.#getHeaderValue('content-disposition', ContentDisposition);
469+
return this.#getHeaderValue(ContentDispositionKey, ContentDisposition);
447470
}
448471

449472
set contentDisposition(value: string | ContentDispositionInit | undefined | null) {
450-
this.#setHeaderValue('content-disposition', ContentDisposition, value);
473+
this.#setHeaderValue(ContentDispositionKey, ContentDisposition, value);
451474
}
452475

453476
/**
@@ -461,11 +484,11 @@ export class SuperHeaders extends Headers {
461484
* [HTTP/1.1 Specification](https://httpwg.org/specs/rfc9110.html#field.content-encoding)
462485
*/
463486
get contentEncoding(): string | null {
464-
return this.#getStringValue('content-encoding');
487+
return this.#getStringValue(ContentEncodingKey);
465488
}
466489

467490
set contentEncoding(value: string | string[] | undefined | null) {
468-
this.#setStringValue('content-encoding', Array.isArray(value) ? value.join(', ') : value);
491+
this.#setStringValue(ContentEncodingKey, Array.isArray(value) ? value.join(', ') : value);
469492
}
470493

471494
/**
@@ -479,11 +502,11 @@ export class SuperHeaders extends Headers {
479502
* [HTTP/1.1 Specification](https://httpwg.org/specs/rfc9110.html#field.content-language)
480503
*/
481504
get contentLanguage(): string | null {
482-
return this.#getStringValue('content-language');
505+
return this.#getStringValue(ContentLanguageKey);
483506
}
484507

485508
set contentLanguage(value: string | string[] | undefined | null) {
486-
this.#setStringValue('content-language', Array.isArray(value) ? value.join(', ') : value);
509+
this.#setStringValue(ContentLanguageKey, Array.isArray(value) ? value.join(', ') : value);
487510
}
488511

489512
/**
@@ -494,11 +517,11 @@ export class SuperHeaders extends Headers {
494517
* [HTTP/1.1 Specification](https://datatracker.ietf.org/doc/html/rfc7230#section-3.3.2)
495518
*/
496519
get contentLength(): number | null {
497-
return this.#getNumberValue('content-length');
520+
return this.#getNumberValue(ContentLengthKey);
498521
}
499522

500523
set contentLength(value: string | number | undefined | null) {
501-
this.#setNumberValue('content-length', value);
524+
this.#setNumberValue(ContentLengthKey, value);
502525
}
503526

504527
/**
@@ -509,11 +532,11 @@ export class SuperHeaders extends Headers {
509532
* [HTTP/1.1 Specification](https://datatracker.ietf.org/doc/html/rfc7231#section-3.1.1.5)
510533
*/
511534
get contentType(): ContentType {
512-
return this.#getHeaderValue('content-type', ContentType);
535+
return this.#getHeaderValue(ContentTypeKey, ContentType);
513536
}
514537

515538
set contentType(value: string | ContentTypeInit | undefined | null) {
516-
this.#setHeaderValue('content-type', ContentType, value);
539+
this.#setHeaderValue(ContentTypeKey, ContentType, value);
517540
}
518541

519542
/**
@@ -525,11 +548,11 @@ export class SuperHeaders extends Headers {
525548
* [HTTP/1.1 Specification](https://datatracker.ietf.org/doc/html/rfc6265#section-5.4)
526549
*/
527550
get cookie(): Cookie {
528-
return this.#getHeaderValue('cookie', Cookie);
551+
return this.#getHeaderValue(CookieKey, Cookie);
529552
}
530553

531554
set cookie(value: string | CookieInit | undefined | null) {
532-
this.#setHeaderValue('cookie', Cookie, value);
555+
this.#setHeaderValue(CookieKey, Cookie, value);
533556
}
534557

535558
/**
@@ -540,11 +563,11 @@ export class SuperHeaders extends Headers {
540563
* [HTTP/1.1 Specification](https://datatracker.ietf.org/doc/html/rfc7231#section-7.1.1.2)
541564
*/
542565
get date(): Date | null {
543-
return this.#getDateValue('date');
566+
return this.#getDateValue(DateKey);
544567
}
545568

546569
set date(value: string | DateInit | undefined | null) {
547-
this.#setDateValue('date', value);
570+
this.#setDateValue(DateKey, value);
548571
}
549572

550573
/**
@@ -555,12 +578,12 @@ export class SuperHeaders extends Headers {
555578
* [HTTP/1.1 Specification](https://datatracker.ietf.org/doc/html/rfc7232#section-2.3)
556579
*/
557580
get etag(): string | null {
558-
return this.#getStringValue('etag');
581+
return this.#getStringValue(ETagKey);
559582
}
560583

561584
set etag(value: string | undefined | null) {
562585
this.#setStringValue(
563-
'etag',
586+
ETagKey,
564587
typeof value === 'string' && !/^(W\/)?".*"$/.test(value) ? `"${value}"` : value,
565588
);
566589
}
@@ -573,11 +596,11 @@ export class SuperHeaders extends Headers {
573596
* [HTTP/1.1 Specification](https://datatracker.ietf.org/doc/html/rfc7234#section-5.3)
574597
*/
575598
get expires(): Date | null {
576-
return this.#getDateValue('expires');
599+
return this.#getDateValue(ExpiresKey);
577600
}
578601

579602
set expires(value: string | DateInit | undefined | null) {
580-
this.#setDateValue('expires', value);
603+
this.#setDateValue(ExpiresKey, value);
581604
}
582605

583606
/**
@@ -588,11 +611,11 @@ export class SuperHeaders extends Headers {
588611
* [HTTP/1.1 Specification](https://datatracker.ietf.org/doc/html/rfc7230#section-5.4)
589612
*/
590613
get host(): string | null {
591-
return this.#getStringValue('host');
614+
return this.#getStringValue(HostKey);
592615
}
593616

594617
set host(value: string | undefined | null) {
595-
this.#setStringValue('host', value);
618+
this.#setStringValue(HostKey, value);
596619
}
597620

598621
/**
@@ -604,11 +627,11 @@ export class SuperHeaders extends Headers {
604627
* [HTTP/1.1 Specification](https://datatracker.ietf.org/doc/html/rfc7232#section-3.3)
605628
*/
606629
get ifModifiedSince(): Date | null {
607-
return this.#getDateValue('if-modified-since');
630+
return this.#getDateValue(IfModifiedSinceKey);
608631
}
609632

610633
set ifModifiedSince(value: string | DateInit | undefined | null) {
611-
this.#setDateValue('if-modified-since', value);
634+
this.#setDateValue(IfModifiedSinceKey, value);
612635
}
613636

614637
/**
@@ -620,11 +643,11 @@ export class SuperHeaders extends Headers {
620643
* [HTTP/1.1 Specification](https://datatracker.ietf.org/doc/html/rfc7232#section-3.4)
621644
*/
622645
get ifUnmodifiedSince(): Date | null {
623-
return this.#getDateValue('if-unmodified-since');
646+
return this.#getDateValue(IfUnmodifiedSinceKey);
624647
}
625648

626649
set ifUnmodifiedSince(value: string | DateInit | undefined | null) {
627-
this.#setDateValue('if-unmodified-since', value);
650+
this.#setDateValue(IfUnmodifiedSinceKey, value);
628651
}
629652

630653
/**
@@ -635,11 +658,11 @@ export class SuperHeaders extends Headers {
635658
* [HTTP/1.1 Specification](https://datatracker.ietf.org/doc/html/rfc7232#section-2.2)
636659
*/
637660
get lastModified(): Date | null {
638-
return this.#getDateValue('last-modified');
661+
return this.#getDateValue(LastModifiedKey);
639662
}
640663

641664
set lastModified(value: string | DateInit | undefined | null) {
642-
this.#setDateValue('last-modified', value);
665+
this.#setDateValue(LastModifiedKey, value);
643666
}
644667

645668
/**
@@ -650,11 +673,11 @@ export class SuperHeaders extends Headers {
650673
* [HTTP/1.1 Specification](https://datatracker.ietf.org/doc/html/rfc7231#section-7.1.2)
651674
*/
652675
get location(): string | null {
653-
return this.#getStringValue('location');
676+
return this.#getStringValue(LocationKey);
654677
}
655678

656679
set location(value: string | undefined | null) {
657-
this.#setStringValue('location', value);
680+
this.#setStringValue(LocationKey, value);
658681
}
659682

660683
/**
@@ -666,11 +689,11 @@ export class SuperHeaders extends Headers {
666689
* [HTTP/1.1 Specification](https://datatracker.ietf.org/doc/html/rfc7231#section-5.5.2)
667690
*/
668691
get referer(): string | null {
669-
return this.#getStringValue('referer');
692+
return this.#getStringValue(RefererKey);
670693
}
671694

672695
set referer(value: string | undefined | null) {
673-
this.#setStringValue('referer', value);
696+
this.#setStringValue(RefererKey, value);
674697
}
675698

676699
/**

0 commit comments

Comments
 (0)