Skip to content

Commit ac069d1

Browse files
authored
feat(context): add Context-Type types to c.header (#3255)
* feat(context): add Context-Type types to `c.header` * chore: add JSDoc * chore: format code * use strict mimetype types and change HeaderRecord
1 parent 8ba0227 commit ac069d1

2 files changed

Lines changed: 68 additions & 4 deletions

File tree

src/context.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@ import type {
1818
JSONValue,
1919
SimplifyDeepArray,
2020
} from './utils/types'
21+
import type { BaseMime } from './utils/mime'
2122

22-
type HeaderRecord = Record<ResponseHeader, string | string[]> | Record<string, string | string[]>
23+
type HeaderRecord =
24+
| Record<'Content-Type', BaseMime>
25+
| Record<ResponseHeader, string | string[]>
26+
| Record<string, string | string[]>
2327

2428
/**
2529
* Data type can be a string, ArrayBuffer, or ReadableStream.
@@ -304,14 +308,16 @@ type ResponseHeader =
304308
| 'X-XSS-Protection'
305309

306310
interface SetHeaders {
311+
(name: 'Content-Type', value?: BaseMime, options?: SetHeadersOptions): void
307312
(name: ResponseHeader, value?: string, options?: SetHeadersOptions): void
308313
(name: string, value?: string, options?: SetHeadersOptions): void
309314
}
310315

311316
type ResponseHeadersInit =
312317
| [string, string][]
313-
| Record<string, string>
318+
| Record<'Content-Type', BaseMime>
314319
| Record<ResponseHeader, string>
320+
| Record<string, string>
315321
| Headers
316322

317323
interface ResponseInit {

src/utils/mime.ts

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
* MIME utility.
44
*/
55

6-
export const getMimeType = (filename: string, mimes = baseMimes): string | undefined => {
6+
export const getMimeType = (
7+
filename: string,
8+
mimes: Record<string, string> = baseMimes
9+
): string | undefined => {
710
const regexp = /\.([a-zA-Z0-9]+?)$/
811
const match = filename.match(regexp)
912
if (!match) {
@@ -25,7 +28,62 @@ export const getExtension = (mimeType: string): string | undefined => {
2528
}
2629

2730
export { baseMimes as mimes }
28-
const baseMimes: Record<string, string> = {
31+
32+
/**
33+
* Union types for BaseMime
34+
*/
35+
export type BaseMime =
36+
| 'audio/aac'
37+
| 'video/x-msvideo'
38+
| 'image/avif'
39+
| 'video/av1'
40+
| 'application/octet-stream'
41+
| 'image/bmp'
42+
| 'text/css'
43+
| 'text/csv'
44+
| 'application/vnd.ms-fontobject'
45+
| 'application/epub+zip'
46+
| 'image/gif'
47+
| 'application/gzip'
48+
| 'text/html'
49+
| 'image/x-icon'
50+
| 'text/calendar'
51+
| 'image/jpeg'
52+
| 'text/javascript'
53+
| 'application/json'
54+
| 'application/ld+json'
55+
| 'audio/x-midi'
56+
| 'audio/mpeg'
57+
| 'video/mp4'
58+
| 'video/mpeg'
59+
| 'audio/ogg'
60+
| 'video/ogg'
61+
| 'application/ogg'
62+
| 'audio/opus'
63+
| 'font/otf'
64+
| 'application/pdf'
65+
| 'image/png'
66+
| 'application/rtf'
67+
| 'image/svg+xml'
68+
| 'image/tiff'
69+
| 'video/mp2t'
70+
| 'font/ttf'
71+
| 'text/plain'
72+
| 'application/wasm'
73+
| 'video/webm'
74+
| 'audio/webm'
75+
| 'image/webp'
76+
| 'font/woff'
77+
| 'font/woff2'
78+
| 'application/xhtml+xml'
79+
| 'application/xml'
80+
| 'application/zip'
81+
| 'video/3gpp'
82+
| 'video/3gpp2'
83+
| 'model/gltf+json'
84+
| 'model/gltf-binary'
85+
86+
const baseMimes: Record<string, BaseMime> = {
2987
aac: 'audio/aac',
3088
avi: 'video/x-msvideo',
3189
avif: 'image/avif',

0 commit comments

Comments
 (0)