Skip to content

Commit

Permalink
🧹 chore: merge main
Browse files Browse the repository at this point in the history
  • Loading branch information
SaltyAom committed Nov 5, 2024
2 parents a6182e8 + e715e00 commit 8a36533
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 4 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# 1.1.24 - 31 Oct 2024
Security:
- [#891](https://github.com/elysiajs/elysia/pull/891) Upgrade Cookie to 0.7.x to fix CVE-2024-47764

Bug fix:
- [#885](https://github.com/elysiajs/elysia/pull/885) unwrap transform errors
- [#903](https://github.com/elysiajs/elysia/pull/903) typebox object schemas without properties key

# 1.1.23 - 22 Oct 2024
Bug fix:
- Handle object with `.then` even if it's not promise (looking at you, Drizzle)
Expand Down
Binary file modified bun.lockb
Binary file not shown.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@
"devDependencies": {
"@types/benchmark": "^2.1.5",
"@types/bun": "^1.1.2",
"@types/cookie": "^1.0.0",
"@typescript-eslint/eslint-plugin": "^6.17.0",
"@typescript-eslint/parser": "^6.17.0",
"benchmark": "^2.1.4",
Expand Down
2 changes: 2 additions & 0 deletions src/compose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export const hasAdditionalProperties = (
const properties = schema.properties as Record<string, TAnySchema>

if ('additionalProperties' in schema) return schema.additionalProperties
if ('patternProperties' in schema) return false

for (const key of Object.keys(properties)) {
const property = properties[key]
Expand Down Expand Up @@ -2226,6 +2227,7 @@ export const composeErrorHandler = (app: AnyElysia) => {

fnLiteral +=
`if(error.constructor.name==="ValidationError"||error.constructor.name==="TransformDecodeError"){` +
`if(error.error)error=error.error\n` +
`set.status = error.status??422\n` +
adapter.validationError +
`}else{` +
Expand Down
11 changes: 7 additions & 4 deletions src/dynamic-handle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { redirect, signCookie, StatusMap } from './utils'
import { parseCookie } from './cookies'

import type { Handler, LifeCycleStore, SchemaValidator } from './types'
import { TransformDecodeError } from '@sinclair/typebox/value'

// JIT Handler
export type DynamicHandler = {
Expand Down Expand Up @@ -423,11 +424,13 @@ export const createDynamicHandler = (app: AnyElysia) => {
// @ts-expect-error
return (context.response = mapResponse(response, context.set))
} catch (error) {
if ((error as ElysiaErrors).status)
set.status = (error as ElysiaErrors).status

const reportedError = (error instanceof TransformDecodeError && error.error)
? error.error
: error
if ((reportedError as ElysiaErrors).status)
set.status = (reportedError as ElysiaErrors).status
// @ts-expect-error private
return app.handleError(context, error)
return app.handleError(context, reportedError)
} finally {
for (const afterResponse of app.event.afterResponse)
await afterResponse.fn(context as any)
Expand Down
14 changes: 14 additions & 0 deletions test/core/compose.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { describe, expect, it } from 'bun:test'
import { Type } from '@sinclair/typebox'
import { hasAdditionalProperties } from '../../src/compose'

describe('hasAdditionalProperties', () => {
it('should handle object schemas without properties key', () => {
const schema = Type.Intersect([
Type.Object({ a: Type.String() }),
// Record schemas does not have properties key, instead it has patternProperties
Type.Record(Type.Number(), Type.String())
])
expect(hasAdditionalProperties(schema)).toBe(false)
})
})
25 changes: 25 additions & 0 deletions test/core/handle-error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,4 +223,29 @@ describe('Handle Error', () => {
'x-test': 'Nagi'
})
})

it('handle error in Transform', async () => {
const route = new Elysia().get('/', ({ query: { aid } }) => aid, {
query: t.Object({
aid: t
.Transform(t.String())
.Decode((value) => {
throw new NotFoundError('foo')
})
.Encode((value) => `1`)
})
})

let response = await new Elysia({ aot: false })
.use(route)
.handle(req('/?aid=a'))
expect(response.status).toEqual(404)
expect(await response.text()).toEqual('foo')

response = await new Elysia({ aot: true })
.use(route)
.handle(req('/?aid=a'))
expect(response.status).toEqual(404)
expect(await response.text()).toEqual('foo')
})
})

0 comments on commit 8a36533

Please sign in to comment.