https://github.com/bogeychan/elysia-dev
Collection of development tools for Elysia.js
https://github.com/bogeychan/elysia-dev
eden-treaty elysia rest-client
Last synced: 8 months ago
JSON representation
Collection of development tools for Elysia.js
- Host: GitHub
- URL: https://github.com/bogeychan/elysia-dev
- Owner: bogeychan
- License: mit
- Created: 2024-07-31T23:32:50.000Z (about 1 year ago)
- Default Branch: master
- Last Pushed: 2025-01-25T08:48:35.000Z (9 months ago)
- Last Synced: 2025-01-25T08:50:54.280Z (9 months ago)
- Topics: eden-treaty, elysia, rest-client
- Language: TypeScript
- Homepage: https://npmjs.com/package/elysia-dev
- Size: 17.6 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# elysia-dev
Collection of development tools for [Elysia.js](https://elysiajs.com)
> [!CAUTION]
> This is EXPERIMENTAL software. The CLI / API may change!> [!IMPORTANT]
> Help improve this software by reporting any issues on GitHub## Supported Parsers & Writers
- Parsers
- [`typescript`](https://www.typescriptlang.org/)
- [`open-api`](https://swagger.io/specification/)- Writers
- [`typescript`](https://www.typescriptlang.org/)
- [`open-api`](https://swagger.io/specification/)
- [`treaty`](https://elysiajs.com/eden/treaty/overview.html#eden-treaty)
- [`rest`](https://marketplace.visualstudio.com/items?itemName=humao.rest-client)> [!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:
```ts
// 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
```bash
bunx elysia-dev --help
```#### Generate [Eden Treaty](https://elysiajs.com/eden/treaty/overview.html#eden-treaty) test file
```bash
bunx elysia-dev gen ./app.ts --writer=treaty --outfile=./test.test.ts
```Click to view result
```ts
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](https://marketplace.visualstudio.com/items?itemName=humao.rest-client) requests file
```bash
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](https://swagger.io/specification/) definition file
```bash
bunx elysia-dev gen ./app.ts --writer=open-api --outfile=./open-api.json
```Click to view result
```json
{
"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](https://github.com/swagger-api/swagger-ui/blob/HEAD/docs/usage/installation.md#unpkg)
```ts
new Elysia()
.get('/json', () => Bun.file('./open-api.json'))
.get('/swagger', ({ set }) => {
set.headers['content-type'] = 'text/html'
const path = '/json'
return `
SwaggerUI
window.onload = () => {
window.ui = SwaggerUIBundle({
url: '${path}',
dom_id: '#swagger-ui',
});
};`
})
```### API
```bash
bun add elysia-dev -D
```#### Generate `open-api` definition file using `typescript` parser
```typescript
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](./examples) folder.
## License
[MIT](LICENSE)