Skip to content

Commit

Permalink
🎉 feat: add more test case
Browse files Browse the repository at this point in the history
  • Loading branch information
SaltyAom committed Dec 15, 2023
1 parent db2fce4 commit 2f5ec09
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 18 deletions.
10 changes: 5 additions & 5 deletions example/a.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { Elysia, t } from '../src'
import { req } from '../test/utils'

const app = new Elysia()
.mapResponse(() => {
return new Response('hello')
})
.get('/', Bun.file('test/kyuukurarin.mp4'))
.mapResponse(async ({ response }) => {})
.get('/', async () => 'NOOP')
.compile()

const res = await app.handle(req('/')).then((x) => x.text())

app.handle(req('/'))
.then((x) => x.text())
.then(console.log)

console.log(app.routes[0]?.composed?.toString())
// console.log(app.routes[0]?.composed?.toString())
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "elysia",
"description": "Ergonomic Framework for Human",
"version": "0.8.0-rc.1",
"version": "0.8.0-rc.2",
"author": {
"name": "saltyAom",
"url": "https://github.com/SaltyAom",
Expand Down
9 changes: 7 additions & 2 deletions src/compose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ export const composeHandler = ({

const hasCookie =
hasUnknownContext ||
validator.cookie ||
!!validator.cookie ||
lifeCycleLiteral.some((fn) => isFnUse('cookie', fn))

// @ts-ignore
Expand Down Expand Up @@ -801,6 +801,7 @@ export const composeHandler = ({
hasBody ||
hasTraceSet ||
isAsyncHandler ||
!!hooks.mapResponse.length ||
hooks.parse.length > 0 ||
hooks.afterHandle.some(isAsync) ||
hooks.beforeHandle.some(isAsync) ||
Expand Down Expand Up @@ -1238,6 +1239,7 @@ export const composeHandler = ({
for (let i = 0; i < hooks.mapResponse.length; i++) {
fnLiteral += `\nif(mr === undefined) {
mr = onMapResponse[${i}](c)
if(mr instanceof Promise) mr = await mr
if(mr !== undefined) c.response = mr
}\n`
}
Expand Down Expand Up @@ -1319,7 +1321,8 @@ export const composeHandler = ({
fnLiteral += `c.response = be`

for (let i = 0; i < hooks.mapResponse.length; i++) {
fnLiteral += `\nmr = onMapResponse[${i}](c)\n
fnLiteral += `\nmr = onMapResponse[${i}](c)
if(mr instanceof Promise) mr = await mr
if(mr !== undefined) c.response = mr\n`
}
}
Expand Down Expand Up @@ -1347,6 +1350,7 @@ export const composeHandler = ({
for (let i = 0; i < hooks.mapResponse.length; i++) {
fnLiteral += `\nif(mr === undefined) {
mr = onMapResponse[${i}](c)
if(mr instanceof Promise) mr = await mr
if(mr !== undefined) r = c.response = mr
}\n`
}
Expand All @@ -1373,6 +1377,7 @@ export const composeHandler = ({
for (let i = 0; i < hooks.mapResponse.length; i++) {
fnLiteral += `\nif(mr === undefined) {
mr = onMapResponse[${i}](c)
if(mr instanceof Promise) mr = await mr
if(mr !== undefined) r = c.response = mr
}\n`
}
Expand Down
42 changes: 32 additions & 10 deletions test/lifecycle/map-response.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ describe('Map Response', () => {
it('work global', async () => {
const app = new Elysia()
.mapResponse(() => new Response('A'))
.get('/', () => 'NOOP')
.get('/', () => 'Hutao')

const res = await app.handle(req('/')).then((x) => x.text())

expect(res).toBe('A')
})

it('work local', async () => {
const app = new Elysia().get('/', () => 'NOOP', {
const app = new Elysia().get('/', () => 'Hutao', {
mapResponse() {
return new Response('A')
}
Expand Down Expand Up @@ -52,7 +52,7 @@ describe('Map Response', () => {
})

it('map response only once', async () => {
const app = new Elysia().get('/', () => 'NOOP', {
const app = new Elysia().get('/', () => 'Hutao', {
mapResponse: [
() => {},
() => {
Expand All @@ -72,7 +72,8 @@ describe('Map Response', () => {
it('inherit response', async () => {
const app = new Elysia().get('/', () => 'Hu', {
mapResponse({ response }) {
if (typeof response === 'string') return new Response(response + 'tao')
if (typeof response === 'string')
return new Response(response + 'tao')
}
})

Expand All @@ -86,17 +87,38 @@ describe('Map Response', () => {
mapResponse({ response, set }) {
set.headers['X-Powered-By'] = 'Elysia'

if (typeof response === 'string') return new Response(response + 'tao', {
headers: {
'X-Series': 'Genshin'
}
})
if (typeof response === 'string')
return new Response(response + 'tao', {
headers: {
'X-Series': 'Genshin'
}
})
}
})

const res = await app.handle(req('/')).then(x => x.headers)
const res = await app.handle(req('/')).then((x) => x.headers)

expect(res.get('X-Powered-By')).toBe('Elysia')
expect(res.get('X-Series')).toBe('Genshin')
})

it('return async', async () => {
const app = new Elysia()
.mapResponse(async () => new Response('A'))
.get('/', () => 'Hutao')

const res = await app.handle(req('/')).then((x) => x.text())

expect(res).toBe('A')
})

it('skip async', async () => {
const app = new Elysia()
.mapResponse(async () => {})
.get('/', () => 'Hutao')

const res = await app.handle(req('/')).then((x) => x.text())

expect(res).toBe('Hutao')
})
})

0 comments on commit 2f5ec09

Please sign in to comment.