diff --git a/src/context.ts b/src/context.ts index bb9029ca0..e3dbc3c5c 100644 --- a/src/context.ts +++ b/src/context.ts @@ -18,8 +18,12 @@ import type { JSONValue, SimplifyDeepArray, } from './utils/types' +import type { BaseMime } from './utils/mime' -type HeaderRecord = Record | Record +type HeaderRecord = + | Record<'Content-Type', BaseMime> + | Record + | Record /** * Data type can be a string, ArrayBuffer, or ReadableStream. @@ -304,14 +308,16 @@ type ResponseHeader = | 'X-XSS-Protection' interface SetHeaders { + (name: 'Content-Type', value?: BaseMime, options?: SetHeadersOptions): void (name: ResponseHeader, value?: string, options?: SetHeadersOptions): void (name: string, value?: string, options?: SetHeadersOptions): void } type ResponseHeadersInit = | [string, string][] - | Record + | Record<'Content-Type', BaseMime> | Record + | Record | Headers interface ResponseInit { diff --git a/src/utils/mime.ts b/src/utils/mime.ts index 26b481aba..afef10487 100644 --- a/src/utils/mime.ts +++ b/src/utils/mime.ts @@ -3,7 +3,10 @@ * MIME utility. */ -export const getMimeType = (filename: string, mimes = baseMimes): string | undefined => { +export const getMimeType = ( + filename: string, + mimes: Record = baseMimes +): string | undefined => { const regexp = /\.([a-zA-Z0-9]+?)$/ const match = filename.match(regexp) if (!match) { @@ -25,7 +28,62 @@ export const getExtension = (mimeType: string): string | undefined => { } export { baseMimes as mimes } -const baseMimes: Record = { + +/** + * Union types for BaseMime + */ +export type BaseMime = + | 'audio/aac' + | 'video/x-msvideo' + | 'image/avif' + | 'video/av1' + | 'application/octet-stream' + | 'image/bmp' + | 'text/css' + | 'text/csv' + | 'application/vnd.ms-fontobject' + | 'application/epub+zip' + | 'image/gif' + | 'application/gzip' + | 'text/html' + | 'image/x-icon' + | 'text/calendar' + | 'image/jpeg' + | 'text/javascript' + | 'application/json' + | 'application/ld+json' + | 'audio/x-midi' + | 'audio/mpeg' + | 'video/mp4' + | 'video/mpeg' + | 'audio/ogg' + | 'video/ogg' + | 'application/ogg' + | 'audio/opus' + | 'font/otf' + | 'application/pdf' + | 'image/png' + | 'application/rtf' + | 'image/svg+xml' + | 'image/tiff' + | 'video/mp2t' + | 'font/ttf' + | 'text/plain' + | 'application/wasm' + | 'video/webm' + | 'audio/webm' + | 'image/webp' + | 'font/woff' + | 'font/woff2' + | 'application/xhtml+xml' + | 'application/xml' + | 'application/zip' + | 'video/3gpp' + | 'video/3gpp2' + | 'model/gltf+json' + | 'model/gltf-binary' + +const baseMimes: Record = { aac: 'audio/aac', avi: 'video/x-msvideo', avif: 'image/avif',