Skip to content

Commit 29928d2

Browse files
authored
Merge pull request #1146 from kravetsone/handle-File
add support to return web API's File from handler
2 parents 3a6f951 + 28670ef commit 29928d2

File tree

5 files changed

+54
-0
lines changed

5 files changed

+54
-0
lines changed

src/adapter/web-standard/handler.ts

+12
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,9 @@ export const mapResponse = (
341341

342342
case 'ElysiaFile':
343343
return handleFile((response as ElysiaFile).value as File)
344+
345+
case 'File':
346+
return handleFile(response as File, set as any)
344347

345348
case 'Blob':
346349
return handleFile(response as Blob, set as any)
@@ -542,6 +545,9 @@ export const mapEarlyResponse = (
542545
case 'ElysiaFile':
543546
return handleFile((response as ElysiaFile).value as File)
544547

548+
case 'File':
549+
return handleFile(response as File, set as any)
550+
545551
case 'Blob':
546552
return handleFile(response as File | Blob, set)
547553

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

719+
case 'File':
720+
return handleFile(response as File, set as any)
721+
713722
case 'Blob':
714723
return handleFile(response as File | Blob, set)
715724

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

870+
case 'File':
871+
return handleFile(response as File)
872+
861873
case 'Blob':
862874
return handleFile(response as File | Blob)
863875

test/adapter/web-standard/map-compact-response.test.ts

+10
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,16 @@ describe('Web Standard - Map Compact Response', () => {
8787
expect(response.status).toBe(200)
8888
})
8989

90+
it('map File', async () => {
91+
const file = new File(['Hello'], 'hello.txt', { type: 'text/plain' })
92+
93+
const response = mapCompactResponse(file)
94+
95+
expect(response).toBeInstanceOf(Response)
96+
expect(await response.text()).toEqual('Hello')
97+
expect(response.status).toBe(200)
98+
})
99+
90100
it('map Promise', async () => {
91101
const body = {
92102
name: 'Shiroko'

test/adapter/web-standard/map-early-response.test.ts

+10
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,16 @@ describe('Web Standard - Map Early Response', () => {
8686
expect(response?.status).toBe(200)
8787
})
8888

89+
it('map File', async () => {
90+
const file = new File(['Hello'], 'hello.txt', { type: 'text/plain' })
91+
92+
const response = mapEarlyResponse(file, defaultContext)
93+
94+
expect(response).toBeInstanceOf(Response)
95+
expect(await response?.text()).toEqual('Hello')
96+
expect(response?.status).toBe(200)
97+
})
98+
8999
it('map Promise', async () => {
90100
const body = {
91101
name: 'Shiroko'

test/adapter/web-standard/map-response.test.ts

+10
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,16 @@ describe('Web Standard - Map Response', () => {
105105
expect(response.status).toBe(200)
106106
})
107107

108+
it('map File', async () => {
109+
const file = new File(['Hello'], 'hello.txt', { type: 'text/plain' })
110+
111+
const response = mapResponse(file, defaultContext)
112+
113+
expect(response).toBeInstanceOf(Response)
114+
expect(await response.text()).toEqual('Hello')
115+
expect(response.status).toBe(200)
116+
})
117+
108118
it('map Promise', async () => {
109119
const body = {
110120
name: 'Shiroko'

test/path/path.test.ts

+12
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,18 @@ describe('Path', () => {
310310
expect(res.headers.get('Server')).toBe('Elysia')
311311
})
312312

313+
it('return web api\'s File', async () => {
314+
const app = new Elysia().get('/', () => new File(['Hello'], 'hello.txt', { type: 'text/plain' }))
315+
const res = await app.handle(req('/'))
316+
317+
expect(res.headers.get('content-type')).toBe('text/plain;charset=utf-8')
318+
expect(await res.text()).toBe('Hello')
319+
expect(res.status).toBe(200)
320+
expect(res.headers.get('accept-ranges')).toBe('bytes')
321+
expect(res.headers.get('content-range')).toBe('bytes 0-4/5')
322+
323+
})
324+
313325
it('handle *', async () => {
314326
const app = new Elysia().get('/*', () => 'Hi')
315327
const get = await app.handle(req('/')).then((r) => r.text())

0 commit comments

Comments
 (0)