Collection of development tools for Elysia.js
-
Parsers
-
Writers
Tip
You can freely combine parsers and writers, such as using an Open-API parser with a TypeScript writer.
Structure your code like this:
// app.ts
import { Elysia, t } from 'elysia'
// make sure to export the main instance (variable name doesn't matter)
export const app = new Elysia()
.model(
'user',
t.Object({
name: t.String(),
age: t.Number()
})
)
.get('/', () => 'yay')
.post('/', () => '', { body: 'user' })
// below routes are excluded from generation due to `export` above
app.get('/excluded', () => 'excluded')
if (process.env.NODE_ENV !== 'test') {
// we don't need to call `listen` within `bun test`
app.listen(8080)
}
bunx elysia-dev --help
Generate Eden Treaty test file
bunx elysia-dev gen ./app.ts --writer=treaty --outfile=./test.test.ts
Click to view result
import { describe, it, expect } from 'bun:test'
import { treaty } from '@elysiajs/eden'
import { app } from './app'
await app.modules
const api = treaty(app)
describe('Elysia', () => {
it('GET - / - Response: { 200: string; }"', async () => {
const { data, error } = await api.index.get()
expect(error).toBeNull()
expect(data).toBeTypeOf('string')
})
it('POST - /user - Request: { name: string; age: number; } - Response: { 200: string; }"', async () => {
const { data, error } = await api.user.post({
name: 'Bogeychan',
age: 42
})
expect(error).toBeNull()
expect(data).toBeTypeOf('string')
})
})
Generate REST Client requests file
bunx elysia-dev gen ./app.ts --writer=rest --outfile=./request.http
Click to view result
@protocol = http
@hostname = localhost
@port = 8080
@origin = {{protocol}}://{{hostname}}:{{port}}
###
# index
GET {{origin}}/ HTTP/1.1
###
# user - { name: string; age: number; }
POST {{origin}}/user HTTP/1.1
Content-Type: application/json
{
"name": "Bogeychan",
"age": 42
}
###
Generate OpenAPI definition file
bunx elysia-dev gen ./app.ts --writer=open-api --outfile=./open-api.json
Click to view result
{
"openapi": "3.1.0",
"info": {
"title": "Elysia Documentation",
"description": "Development documentation",
"version": "0.0.0"
},
"paths": {
"/": {
"post": {
"responses": {
"200": {
"description": "200",
"content": {
"text/plain": {
"schema": {
"type": "string"
}
}
}
}
},
"requestBody": {
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"age": {
"type": "number"
}
}
}
}
},
"required": true
}
},
"get": {
"responses": {
"200": {
"description": "200",
"content": {
"text/plain": {
"schema": {
"type": "string"
}
}
}
}
}
}
}
}
}
Click to see how to integrate the generated definition into Elysia
Based on Swagger UI docs
new Elysia()
.get('/json', () => Bun.file('./open-api.json'))
.get('/swagger', ({ set }) => {
set.headers['content-type'] = 'text/html'
const path = '/json'
return `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="description" content="SwaggerUI" />
<title>SwaggerUI</title>
<link rel="stylesheet" href="https://unpkg.com/[email protected]/swagger-ui.css" />
</head>
<body>
<div id="swagger-ui"></div>
<script src="https://unpkg.com/[email protected]/swagger-ui-bundle.js" crossorigin></script>
<script>
window.onload = () => {
window.ui = SwaggerUIBundle({
url: '${path}',
dom_id: '#swagger-ui',
});
};
</script>
</body>
</html>`
})
bun add elysia-dev -D
import { gen } from 'elysia-dev'
await gen({
entrypoint: './app.ts',
parse: {
$type: 'typescript'
},
outFile: './open-api.json',
write: {
$type: 'open-api'
},
logging: {
level: 'silent' // disable logging
}
})
Checkout the examples folder.