🚀 Big News:Socket Has Acquired Secure Annex.Learn More
Socket
Book a DemoSign in
Socket

elysia-dev

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

elysia-dev

Development tools for Elysia.js

latest
Source
npmnpm
Version
0.2.0
Version published
Weekly downloads
13
225%
Maintainers
1
Weekly downloads
 
Created
Source

elysia-dev

Collection of development tools for Elysia.js

[!CAUTION] This is EXPERIMENTAL software. The CLI / API may change!

[!IMPORTANT] Help improve this software by reporting any issues on GitHub

Supported Parsers & Writers

[!TIP] You can freely combine parsers and writers, such as using an Open-API parser with a TypeScript writer.

Usage

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)
}

CLI

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/swagger-ui-dist@5.11.0/swagger-ui.css" />
</head>
<body>
<div id="swagger-ui"></div>
<script src="https://unpkg.com/swagger-ui-dist@5.11.0/swagger-ui-bundle.js" crossorigin></script>
<script>
  window.onload = () => {
    window.ui = SwaggerUIBundle({
      url: '${path}',
      dom_id: '#swagger-ui',
    });
  };
</script>
</body>
</html>`
	})

API

bun add elysia-dev -D

Generate open-api definition file using typescript parser

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.

License

MIT

Keywords

elysia

FAQs

Package last updated on 18 Feb 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts