Skip to content

Commit

Permalink
feat(context): add Context-Type types to c.header (#3255)
Browse files Browse the repository at this point in the history
* feat(context): add Context-Type types to `c.header`

* chore: add JSDoc

* chore: format code

* use strict mimetype types and change HeaderRecord
  • Loading branch information
nakasyou authored Aug 11, 2024
1 parent 8ba0227 commit ac069d1
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 4 deletions.
10 changes: 8 additions & 2 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ import type {
JSONValue,
SimplifyDeepArray,
} from './utils/types'
import type { BaseMime } from './utils/mime'

type HeaderRecord = Record<ResponseHeader, string | string[]> | Record<string, string | string[]>
type HeaderRecord =
| Record<'Content-Type', BaseMime>
| Record<ResponseHeader, string | string[]>
| Record<string, string | string[]>

/**
* Data type can be a string, ArrayBuffer, or ReadableStream.
Expand Down Expand Up @@ -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<string, string>
| Record<'Content-Type', BaseMime>
| Record<ResponseHeader, string>
| Record<string, string>
| Headers

interface ResponseInit {
Expand Down
62 changes: 60 additions & 2 deletions src/utils/mime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
* MIME utility.
*/

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

export { baseMimes as mimes }
const baseMimes: Record<string, string> = {

/**
* 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<string, BaseMime> = {
aac: 'audio/aac',
avi: 'video/x-msvideo',
avif: 'image/avif',
Expand Down

0 comments on commit ac069d1

Please sign in to comment.