Skip to content

Commit

Permalink
Merge pull request #13 from elysiajs/next
Browse files Browse the repository at this point in the history
feat: 0.2 | The Blessing
  • Loading branch information
SaltyAom committed Jan 27, 2023
2 parents 5946f99 + cb89dcc commit 3e6df36
Show file tree
Hide file tree
Showing 23 changed files with 1,327 additions and 317 deletions.
46 changes: 46 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,49 @@
# 0.2.0-rc.1 - 24 Jan 2023
Improvement:
- Map OpenAPI's schema detail on response
- Fix Type instantiation is excessively deep and possibly infinite
- Improve TypeScript inference time by removing recursive type in generic
- Inferred body is never instead of unknown

# 0.2.0-rc.0 - 23 Jan 2023
Feature:
- Add support for reference model via `.setModel`
- Add support for OpenAPI's `definitions` field

# 0.2.0-beta.2 - 22 Jan 2023
Feature:
- Add support for custom openapi field using `schema.detail`
- Add support for custom code for `response`

Improvement:
- Unioned status type for response
- Optimize TypeScript inference performance

# 0.2.0-beta.1 - 22 Jan 2023
Breaking Change:
- `onParse` now accepts `(context: PreContext, contentType: string)` instead of `(request: Request, contentType: string)`
- To migrate, add `.request` to context to access `Request`

Feature:
- `onRequest` and `onParse` now can access `PreContext`
- Support `application/x-www-form-urlencoded` by default

Improvement:
- body parser now parse `content-type` with extra attribute eg. `application/json;charset=utf-8`

# 0.2.0-beta.0 - 17 Jan 2023
Feature:
- Support for Async / lazy-load plugin

Improvement:
- Decode URI parameter path parameter
- Handle union type correctly

# 0.1.3 - 12 Jan 2023
Improvement:
- Validate `Response` object
- Union type inference on response

# 0.1.2 - 31 Dec 2022
Bug fix:
- onRequest doesn't run in `group` and `guard`
Expand Down
Binary file modified bun.lockb
Binary file not shown.
37 changes: 37 additions & 0 deletions example/a.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Elysia, SCHEMA, t } from '../src'

const app = new Elysia()
.get('/', () => 'Elysia')
.post('/', () => 'Elysia')
.get('/id/:id', () => 1)
.post('/mirror', ({ body }) => body, {
schema: {
query: t.Object({
n: t.String()
}),
body: t.Object(
{
username: t.String(),
password: t.String()
},
{
description: 'An expected body'
}
),
detail: {
summary: 'Sign in the user',
tags: ['authentication']
}
}
})
.get('/sign-in', ({ body }) => 'ok')
.get('/products/nendoroid/skadi', ({ body }) => 1)
.get('/id2/:id', ({ params }) => 1, {
schema: {
detail: {
summary: 'a'
}
}
})

type App = typeof app['store'][typeof SCHEMA]['/mirror']['POST']['body']
7 changes: 5 additions & 2 deletions example/body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Elysia, t } from '../src'

const app = new Elysia()
// Add custom body parser
.onParse(async (request, contentType) => {
.onParse(async ({ request }, contentType) => {
switch (contentType) {
case 'application/Elysia':
return request.text()
Expand All @@ -25,7 +25,10 @@ const app = new Elysia()
body: t.Object({
id: t.Number(),
username: t.String()
})
}),
detail: {
summary: 'A'
}
}
})
.post('/mirror', ({ body }) => body)
Expand Down
19 changes: 19 additions & 0 deletions example/lazy-module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Elysia, SCHEMA } from '../src'

const plugin = (app: Elysia) => app.get('/plugin', () => 'Plugin')
const asyncPlugin = async (app: Elysia) => app.get('/async', () => 'A')

const app = new Elysia()
.decorate('a', () => 'hello')
.use(plugin)
.use(asyncPlugin)
.use(import('./lazy'))
.use((app) => app.get('/inline', () => 'inline'))
.get('/', ({ a }) => a())
.listen(3000)

await app.modules

console.log(
`🦊 Elysia is running at ${app.server?.hostname}:${app.server?.port}`
)
5 changes: 5 additions & 0 deletions example/lazy/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Elysia from "../../src";

export const lazy = (app: Elysia) => app.get('/lazy', () => 'Hi')

export default lazy
34 changes: 24 additions & 10 deletions example/schema.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
import { Elysia, t } from '../src'
import { Elysia, t, SCHEMA, DEFS } from '../src'

const app = new Elysia()
// Strictly validate response
.get('/', () => 'hi', {
schema: {
.setModel({
a: t.Object({
response: t.String()
}
}),
b: t.Object({
response: t.Number()
})
})
// Strictly validate response
.get('/', () => 'hi')
// Strictly validate body and response
.post('/', ({ body }) => body.id, {
.post('/', ({ body, query }) => body.id, {
schema: {
body: t.Object({
id: t.Number(),
username: t.String(),
profile: t.Object({
name: t.String()
})
}),
response: t.Number()
})
}
})
// Strictly validate query, params, and body
Expand All @@ -28,7 +31,13 @@ const app = new Elysia()
}),
params: t.Object({
id: t.String()
})
}),
response: {
200: t.String(),
300: t.Object({
error: t.String()
})
}
}
})
.guard(
Expand All @@ -53,7 +62,7 @@ const app = new Elysia()
schema: {
params: t.Object({
id: t.Number()
}),
})
},
transform: ({ params }) => {
params.id = +params.id
Expand All @@ -62,3 +71,8 @@ const app = new Elysia()
)
)
.listen(8080)

type A = typeof app['store'][typeof SCHEMA]['/']['POST']['query']
type B = typeof app['store'][typeof DEFS]

// const a = app.getModel('b')
17 changes: 9 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "elysia",
"description": "Fast, and friendly Bun web framework",
"version": "0.1.2",
"version": "0.2.0",
"author": {
"name": "saltyAom",
"url": "https://github.com/SaltyAom",
Expand Down Expand Up @@ -35,15 +35,16 @@
"release": "npm run build && npm run test && npm publish"
},
"dependencies": {
"@sinclair/typebox": "0.25.10"
"@sinclair/typebox": "0.25.21",
"openapi-types": "^12.1.0"
},
"devDependencies": {
"@types/node": "^18.11.10",
"@typescript-eslint/eslint-plugin": "^5.45.0",
"@typescript-eslint/parser": "^5.45.0",
"bun-types": "^0.3.0",
"eslint": "^8.29.0",
"@types/node": "^18.11.18",
"@typescript-eslint/eslint-plugin": "^5.48.2",
"@typescript-eslint/parser": "^5.48.2",
"bun-types": "^0.5.0",
"eslint": "^8.32.0",
"rimraf": "^3.0.2",
"typescript": "^4.9.3"
"typescript": "^4.9.4"
}
}
8 changes: 4 additions & 4 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export interface Context<
}

// Use to mimic request before mapping route
export type PreContext<Store extends Elysia['store'] = Elysia['store']> = Omit<
Context<{}, Store>,
'query' | 'params' | 'body'
>
export type PreContext<
Route extends TypedRoute = TypedRoute,
Store extends Elysia['store'] = Elysia['store']
> = Omit<Context<Route, Store>, 'query' | 'params' | 'body'>
Loading

0 comments on commit 3e6df36

Please sign in to comment.