Skip to content

add support to return web API's File from handler #1146

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/adapter/web-standard/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,9 @@ export const mapResponse = (

case 'ElysiaFile':
return handleFile((response as ElysiaFile).value as File)

case 'File':
return handleFile(response as File, set as any)

case 'Blob':
return handleFile(response as Blob, set as any)
Expand Down Expand Up @@ -542,6 +545,9 @@ export const mapEarlyResponse = (
case 'ElysiaFile':
return handleFile((response as ElysiaFile).value as File)

case 'File':
return handleFile(response as File, set as any)

case 'Blob':
return handleFile(response as File | Blob, set)

Expand Down Expand Up @@ -710,6 +716,9 @@ export const mapEarlyResponse = (
case 'ElysiaFile':
return handleFile((response as ElysiaFile).value as File)

case 'File':
return handleFile(response as File, set as any)

case 'Blob':
return handleFile(response as File | Blob, set)

Expand Down Expand Up @@ -858,6 +867,9 @@ export const mapCompactResponse = (
case 'ElysiaFile':
return handleFile((response as ElysiaFile).value as File)

case 'File':
return handleFile(response as File)

case 'Blob':
return handleFile(response as File | Blob)

Expand Down
10 changes: 10 additions & 0 deletions test/adapter/web-standard/map-compact-response.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ describe('Web Standard - Map Compact Response', () => {
expect(response.status).toBe(200)
})

it('map File', async () => {
const file = new File(['Hello'], 'hello.txt', { type: 'text/plain' })

const response = mapCompactResponse(file)

expect(response).toBeInstanceOf(Response)
expect(await response.text()).toEqual('Hello')
expect(response.status).toBe(200)
})

it('map Promise', async () => {
const body = {
name: 'Shiroko'
Expand Down
10 changes: 10 additions & 0 deletions test/adapter/web-standard/map-early-response.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,16 @@ describe('Web Standard - Map Early Response', () => {
expect(response?.status).toBe(200)
})

it('map File', async () => {
const file = new File(['Hello'], 'hello.txt', { type: 'text/plain' })

const response = mapEarlyResponse(file, defaultContext)

expect(response).toBeInstanceOf(Response)
expect(await response?.text()).toEqual('Hello')
expect(response?.status).toBe(200)
})

it('map Promise', async () => {
const body = {
name: 'Shiroko'
Expand Down
10 changes: 10 additions & 0 deletions test/adapter/web-standard/map-response.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,16 @@ describe('Web Standard - Map Response', () => {
expect(response.status).toBe(200)
})

it('map File', async () => {
const file = new File(['Hello'], 'hello.txt', { type: 'text/plain' })

const response = mapResponse(file, defaultContext)

expect(response).toBeInstanceOf(Response)
expect(await response.text()).toEqual('Hello')
expect(response.status).toBe(200)
})

it('map Promise', async () => {
const body = {
name: 'Shiroko'
Expand Down
12 changes: 12 additions & 0 deletions test/path/path.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,18 @@ describe('Path', () => {
expect(res.headers.get('Server')).toBe('Elysia')
})

it('return web api\'s File', async () => {
const app = new Elysia().get('/', () => new File(['Hello'], 'hello.txt', { type: 'text/plain' }))
const res = await app.handle(req('/'))

expect(res.headers.get('content-type')).toBe('text/plain;charset=utf-8')
expect(await res.text()).toBe('Hello')
expect(res.status).toBe(200)
expect(res.headers.get('accept-ranges')).toBe('bytes')
expect(res.headers.get('content-range')).toBe('bytes 0-4/5')

})

it('handle *', async () => {
const app = new Elysia().get('/*', () => 'Hi')
const get = await app.handle(req('/')).then((r) => r.text())
Expand Down