Macro
Reusable route options.
Imagine we have an authentication check like this:
import { Elysia, t } from 'elysia'
beforeHandle({ cookie: { session } }) {
if(!session.value) throw 'Unauthorized'
If we have multiple routes that require authentication, we have to repeat the same options over and over again.
Instead, we can use a macro to reuse route options:
import { Elysia, t } from 'elysia'
beforeHandle({ cookie: { session }, status }) {
if(!session.value) return status(401)
auth will then inline both cookie, and beforeHandle to the route.
Simply put, Macro is a reusable route option, similar to function but as a route option with type soundness.
Assignment
Let’s define a macro to check if a body is a Fibonacci number:
function isFibonacci(n: number) {
while(b < n) [a, b] = [b, a + b]
return b === n || n === 0