Skip to content

Commit

Permalink
🎉 release: 1.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
SaltyAom committed Mar 18, 2024
1 parent 023af44 commit bdde62b
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 51 deletions.
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
# 1.0.3 - 18 Mar 2024
Improvement:
- Reduce instruction for static resource

Bug fix:
- Fix returning mulitple status code using `error` doesn't accept the response

# 1.0.2 - 18 Mar 2024
Feature:
- add `scoped` support for `derive` and `resolve`

Improvement:
- Type soundness
- type inference performance improvement

# 1.0.1 - 18 Mar 2024
Improvement:
- `mapHandler` now check passthrough once instead of twice
Expand Down
24 changes: 9 additions & 15 deletions example/a.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
import { Elysia } from '../src'
import { Elysia, error, t } from '../src'

const child = new Elysia()
// ? This is only in local
.derive(() => ({
hello: 'world'
}))
/**
* ? Since hello is only in local
* ? It might not be available in global
*
**/
.mapDerive(({ hello }) => ({
hello
}))
.get('/child', ({ hello }) => hello)
const app = new Elysia({ precompile: true })
.headers({
a: 'hello'
})
.get('/', 'a')
.listen(3000)

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": "1.0.2",
"version": "1.0.3",
"author": {
"name": "saltyAom",
"url": "https://github.com/SaltyAom",
Expand Down
34 changes: 14 additions & 20 deletions src/compose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,12 @@ export const composeHandler = ({
}
}): ComposedHandler => {
const isHandleFn = typeof handler === 'function'
if (!isHandleFn) handler = mapCompactResponse(handler)

if (!isHandleFn)
handler = mapResponse(handler, {
// @ts-expect-error private property
headers: app.setHeaders ?? {}
})

const hasErrorHandler =
(app.config.forceErrorEncapsulation &&
Expand Down Expand Up @@ -387,11 +392,6 @@ export const composeHandler = ({
hooks.type !== 'none' &&
(inference.body || !!validator.body)

// @ts-expect-error private
const defaultHeaders = app.setHeaders
const hasDefaultHeaders =
defaultHeaders && !!Object.keys(defaultHeaders).length

// ? defaultHeaders doesn't imply that user will use headers in handler
const hasHeaders = inference.headers || validator.headers
const hasCookie = inference.cookie || !!validator.cookie
Expand Down Expand Up @@ -556,11 +556,7 @@ export const composeHandler = ({

const hasTraceSet = traceInference.set
const hasSet =
inference.cookie ||
inference.set ||
hasTraceSet ||
hasHeaders ||
hasDefaultHeaders
inference.cookie || inference.set || hasTraceSet || hasHeaders

if (hasTrace) fnLiteral += '\nconst id = c.$$requestId\n'

Expand Down Expand Up @@ -782,7 +778,7 @@ export const composeHandler = ({
unit: hooks.transform.length
})

fnLiteral += '\nlet transformed\n'
if (hooks.transform.length) fnLiteral += '\nlet transformed\n'

for (let i = 0; i < hooks.transform.length; i++) {
const transform = hooks.transform[i]
Expand Down Expand Up @@ -1166,9 +1162,8 @@ export const composeHandler = ({
fnLiteral += encodeCookie

if (handler instanceof Response) {
fnLiteral +=
inference.set || hasDefaultHeaders
? `if(
fnLiteral += inference.set
? `if(
isNotEmpty(c.set.headers) ||
c.set.status !== 200 ||
c.set.redirect ||
Expand All @@ -1177,7 +1172,7 @@ export const composeHandler = ({
return mapResponse(${handle}.clone(), c.set, c.request)
else
return ${handle}.clone()`
: `return ${handle}.clone()`
: `return ${handle}.clone()`

fnLiteral += '\n'
} else if (hasSet)
Expand Down Expand Up @@ -1215,9 +1210,8 @@ export const composeHandler = ({
report('afterHandle')()

if (handler instanceof Response) {
fnLiteral +=
inference.set || hasDefaultHeaders
? `if(
fnLiteral += inference.set
? `if(
isNotEmpty(c.set.headers) ||
c.set.status !== 200 ||
c.set.redirect ||
Expand All @@ -1226,7 +1220,7 @@ export const composeHandler = ({
return mapResponse(${handle}.clone(), c.set, c.request)
else
return ${handle}.clone()`
: `return ${handle}.clone()`
: `return ${handle}.clone()`

fnLiteral += '\n'
} else if (hasSet)
Expand Down
30 changes: 15 additions & 15 deletions src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ export const error = <
const T = Code extends keyof InvertedStatusMap
? InvertedStatusMap[Code]
: Code,
const Status extends number = Code extends keyof StatusMap
const Status extends Code extends keyof StatusMap
? StatusMap[Code]
: Code
: Code = Code extends keyof StatusMap ? StatusMap[Code] : Code
>(
code: Code,
response?: T
Expand Down Expand Up @@ -127,7 +127,7 @@ export class ValidationError extends Error {
: customError + ''
} else if (isProduction) {
message = JSON.stringify({
type: "validation",
type: 'validation',
on: type,
message: error?.message,
found: value
Expand All @@ -140,22 +140,22 @@ export class ValidationError extends Error {
? [...validator.Errors(value)]
: [...Value.Errors(validator, value)]

let expected

try {
expected = Value.Create(schema)
} catch (error) {
expected = {
type: 'Could not create expected value',
// @ts-expect-error
message: error?.message,
error
}
let expected

try {
expected = Value.Create(schema)
} catch (error) {
expected = {
type: 'Could not create expected value',
// @ts-expect-error
message: error?.message,
error
}
}

message = JSON.stringify(
{
type: "validation",
type: 'validation',
on: type,
property: accessor,
message: error?.message,
Expand Down

0 comments on commit bdde62b

Please sign in to comment.