Skip to content

Commit aef7618

Browse files
committed
🎉 release: 1.2.1
1 parent dbd3142 commit aef7618

File tree

8 files changed

+171
-44
lines changed

8 files changed

+171
-44
lines changed

CHANGELOG.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,49 @@
1+
# 1.2.1 - 23 Dec 2024
2+
Bug fix:
3+
- conditional import for fs
4+
- object macro parameter maybe array
5+
- optional return for macro
6+
7+
# 1.2.0 - 23 Dec 2024
8+
Feature:
9+
- Commitment to Universal Runtime Support
10+
- Node Adapter
11+
- Web Standard Adapter
12+
- Bun Adapter
13+
- Universal Utilities
14+
- Name parser
15+
- Add resolve support to Macro
16+
- Improve WebSocket
17+
- Support ping, pong and latest Bun feature
18+
- Match type declaration with Bun
19+
- Support for return, yield
20+
- Match Context type
21+
- Performance Improvement
22+
- Entire rewrite
23+
- bind over getter return
24+
- Infer 422 validation
25+
- Compilation minification
26+
- Validation Stuff
27+
- t.MaybeArray
28+
- Typebox Module & Nested model
29+
- Inline module
30+
31+
Improvement:
32+
- Memory Usage
33+
- [Internal] Register loosePath in compilation process to reduce memory usage and reduce registration time from O(2n) to O(n)
34+
- Try to accept and coerce different version of Elysia plugin
35+
- Event Listener now infers path parameter automatically based on scope
36+
- Add ‘scoped’ to bulk `as` for casting type to ‘scoped’ similar to ‘plugin’
37+
38+
Change:
39+
- Update `cookie` to 1.0.1
40+
- Update TypeBox to 0.33
41+
- `content-length` now accept number
42+
43+
Breaking Change:
44+
- [Internal] Remove router internal property static.http.staticHandlers
45+
- [Internal] Router history compile now link with history composed
46+
147
# 1.1.27 - 23 Dec 2024
248
Bug fix:
349
- [#963](https://github.com/elysiajs/elysia/pull/963) array parser on query string when AOT is off

example/a.ts

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,35 @@
11
import { Elysia, t } from '../src'
2+
import { req } from '../test/utils'
23

3-
export const auth = new Elysia().macro({
4-
isAuth(isAuth: boolean) {
5-
return {
6-
resolve() {
7-
return {
8-
user: 'saltyaom'
4+
export const userService = new Elysia({ name: 'user/service' })
5+
.macro({
6+
isSignIn(enabled: boolean) {
7+
if (!enabled) return
8+
9+
return {
10+
beforeHandle({ error, cookie: { token }, store: { session } }) {
11+
if (!token.value)
12+
return error(401, {
13+
success: false,
14+
message: 'Unauthorized'
15+
})
16+
17+
const username = session[token.value as unknown as number]
18+
19+
if (!username)
20+
return error(401, {
21+
success: false,
22+
message: 'Unauthorized'
23+
})
924
}
1025
}
1126
}
12-
},
13-
role(role: 'admin' | 'user') {
14-
return {}
15-
}
16-
})
27+
})
28+
.get('/', () => 'a', {
29+
isSignIn: false
30+
})
1731

18-
new Elysia().ws('/ws', {
19-
ping: (message) => message
20-
})
32+
userService
33+
.handle(req('/'))
34+
.then((x) => x.text())
35+
.then(console.log)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "elysia",
33
"description": "Ergonomic Framework for Human",
4-
"version": "1.2.0",
4+
"version": "1.2.1",
55
"author": {
66
"name": "saltyAom",
77
"url": "https://github.com/SaltyAom",

src/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3722,7 +3722,13 @@ export default class Elysia<
37223722
Volatile
37233723
>
37243724

3725-
macro<const NewMacro extends HookMacroFn>(
3725+
macro<
3726+
const NewMacro extends HookMacroFn<
3727+
Metadata['schema'],
3728+
Singleton,
3729+
Definitions['error']
3730+
>
3731+
>(
37263732
macro: NewMacro
37273733
): Elysia<
37283734
BasePath,

src/types.ts

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,36 +1290,26 @@ export type HookMacroFn<
12901290
> = Record<
12911291
keyof any,
12921292
(...a: any) => {
1293-
parse?(fn: MaybeArray<BodyHandler<TypedRoute, Singleton>>): unknown
1294-
transform?(fn: MaybeArray<VoidHandler<TypedRoute, Singleton>>): unknown
1295-
// derive?(fn: DeriveHandler<Singleton>): unknown
1296-
beforeHandle?(
1297-
fn: MaybeArray<OptionalHandler<TypedRoute, Singleton>>
1298-
): unknown
1299-
afterHandle?(
1300-
fn: MaybeArray<AfterHandler<TypedRoute, Singleton>>
1301-
): unknown
1302-
error?(
1303-
fn: MaybeArray<ErrorHandler<Errors, TypedRoute, Singleton>>
1304-
): unknown
1305-
mapResponse?(
1306-
fn: MaybeArray<MapResponse<TypedRoute, Singleton>>
1307-
): unknown
1308-
afterResponse?(
1309-
fn: MaybeArray<AfterResponseHandler<TypedRoute, Singleton>>
1310-
): unknown
1293+
parse?: MaybeArray<BodyHandler<TypedRoute, Singleton>>
1294+
transform?: MaybeArray<VoidHandler<TypedRoute, Singleton>>
1295+
beforeHandle?: MaybeArray<OptionalHandler<TypedRoute, Singleton>>
1296+
afterHandle?: MaybeArray<AfterHandler<TypedRoute, Singleton>>
1297+
error?: MaybeArray<ErrorHandler<Errors, TypedRoute, Singleton>>
1298+
mapResponse?: MaybeArray<MapResponse<TypedRoute, Singleton>>
1299+
afterResponse?: MaybeArray<AfterResponseHandler<TypedRoute, Singleton>>
13111300
resolve?: MaybeArray<ResolveHandler<TypedRoute, Singleton>>
1312-
}
1301+
} | void
13131302
>
13141303

1315-
export type MacroToProperty<in out T extends BaseMacroFn | HookMacroFn> =
1316-
Prettify<{
1317-
[K in keyof T]: T[K] extends Function
1318-
? T[K] extends (a: infer Params) => any
1319-
? Params | undefined
1320-
: T[K]
1304+
export type MacroToProperty<
1305+
in out T extends BaseMacroFn | HookMacroFn<any, any, any>
1306+
> = Prettify<{
1307+
[K in keyof T]: T[K] extends Function
1308+
? T[K] extends (a: infer Params) => any
1309+
? Params | undefined
13211310
: T[K]
1322-
}>
1311+
: T[K]
1312+
}>
13231313

13241314
interface MacroOptions {
13251315
insert?: 'before' | 'after'

src/universal/file.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
/* eslint-disable sonarjs/no-duplicate-string */
2-
import { createReadStream, statSync, type Stats } from 'fs'
2+
import { type Stats } from 'fs'
3+
import { createReadStream, statSync } from './fs'
34

45
import { isBun } from './utils'
5-
import { BunFile } from 'bun'
6+
import type { BunFile } from 'bun'
67

78
export const mime = {
89
aac: 'audio/aac',

src/universal/fs.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import type * as Fs from 'fs'
2+
import { env } from './env'
3+
4+
const fs: typeof Fs =
5+
typeof process !== 'undefined' && env.NODE_ENV ? require('fs') : {}
6+
7+
const noop = () => {}
8+
9+
export const createReadStream: (typeof Fs)['createReadStream'] =
10+
fs.createReadStream ?? noop
11+
export const statSync: (typeof Fs)['statSync'] = fs.statSync ?? noop
12+
13+
export default fs

test/types/index.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2163,3 +2163,59 @@ type a = keyof {}
21632163
}
21642164
)
21652165
}
2166+
2167+
// Macro
2168+
{
2169+
const userService = new Elysia({ name: 'user/service' })
2170+
.state({
2171+
user: {} as Record<string, string>,
2172+
session: {} as Record<number, string>
2173+
})
2174+
.model({
2175+
signIn: t.Object({
2176+
username: t.String({ minLength: 1 }),
2177+
password: t.String({ minLength: 8 })
2178+
}),
2179+
session: t.Cookie(
2180+
{
2181+
token: t.Number()
2182+
},
2183+
{
2184+
secrets: 'seia'
2185+
}
2186+
),
2187+
optionalSession: t.Optional(t.Ref('session'))
2188+
})
2189+
.macro({
2190+
isSignIn(enabled: boolean) {
2191+
if (!enabled) return {}
2192+
2193+
return {
2194+
beforeHandle({
2195+
error,
2196+
cookie: { token },
2197+
store: { session }
2198+
}) {
2199+
if (!token.value)
2200+
return error(401, {
2201+
success: false,
2202+
message: 'Unauthorized'
2203+
})
2204+
2205+
expectTypeOf<typeof session>().toEqualTypeOf<
2206+
Record<number, string>
2207+
>()
2208+
2209+
const username =
2210+
session[token.value as unknown as number]
2211+
2212+
if (!username)
2213+
return error(401, {
2214+
success: false,
2215+
message: 'Unauthorized'
2216+
})
2217+
}
2218+
}
2219+
}
2220+
})
2221+
}

0 commit comments

Comments
 (0)