diff --git a/CHANGELOG.md b/CHANGELOG.md index c5c94bac..8dfde1a5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +# 0.7.14 - 26 Sep 2023 +Bug fix: +- Make `t.Files` parameter optional +- model remap now using `TSchema` instead of literal type for creating type abstraction + # 0.7.13 - 25 Sep 2023 Improvement: - Using listener instead of microtick to handle `trace.set` diff --git a/example/a.ts b/example/a.ts index 34f2d2dc..1a651dbf 100644 --- a/example/a.ts +++ b/example/a.ts @@ -16,6 +16,8 @@ console.log(a.routes.map((x) => x.path)) const app = new Elysia() .use(a) + .model('a', t.String()) + .model((x) => x) .get('/', async () => { await delay() diff --git a/example/b.ts b/example/b.ts deleted file mode 100644 index 8c5fd44e..00000000 --- a/example/b.ts +++ /dev/null @@ -1,31 +0,0 @@ -import Elysia from '../src' - -const app = new Elysia() - .get('/', ({ set }) => { - set.status = "I'm a teapot" - - return Bun.file('example/teapot.webp') - }) - .trace(async ({ beforeHandle }) => { - try { - console.log('Start BeforeHandle') - const { end } = await beforeHandle - - const a = await end - } catch { - console.log("A") - } - }) - .get('/trace', () => 'a', { - beforeHandle: [ - function setup() {}, - function error() { - throw new Error('A') - }, - function resume() {} - ], - afterHandle() { - console.log('After') - } - }) - .listen(3000) diff --git a/example/c.ts b/example/c.ts deleted file mode 100644 index 3c259a03..00000000 --- a/example/c.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { Elysia, t } from '../src' - -const app = new Elysia() - .post('/0.6', ({ body }) => body, { - body: t.Union([ - t.Undefined(), - t.Object({ - name: t.String(), - job: t.String(), - trait: t.Optional(t.String()) - }) - ]) - }) - .listen(3000) diff --git a/example/combined-plugin.ts b/example/combined-plugin.ts deleted file mode 100644 index 66ec6ac7..00000000 --- a/example/combined-plugin.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { Elysia, t } from '../src' - -const counter = (app: Elysia) => app.state('nested-counter', 1) - -new Elysia() - .decorate('getDate', () => new Date()) - .state('counter', 1) - .use(counter) - .get('/:id', ({ params, getDate, store }) => getDate()) diff --git a/example/cookie.ts b/example/cookie.ts index e560519c..de06b3e0 100644 --- a/example/cookie.ts +++ b/example/cookie.ts @@ -1,17 +1,22 @@ -import { Elysia, getSchemaValidator, t } from '../src' +import { Elysia, t } from '../src' -const app = new Elysia() - // .get( - // '/council', - // ({ cookie: { council } }) => - // (council.value = [ - // { - // name: 'Rin', - // affilation: 'Administration' - // } - // ]) - // ) - // .get('/create', ({ cookie: { name } }) => (name.value = 'Himari')) +const app = new Elysia({ + cookie: { + secrets: 'Fischl von Luftschloss Narfidort', + sign: ['name'] + } +}) + .get( + '/council', + ({ cookie: { council } }) => + (council.value = [ + { + name: 'Rin', + affilation: 'Administration' + } + ]) + ) + .get('/create', ({ cookie: { name } }) => (name.value = 'Himari')) .get( '/update', ({ cookie: { name } }) => { @@ -24,20 +29,9 @@ const app = new Elysia() return name.value }, { - cookie: t.Cookie( - { - name: t.Optional(t.String()) - }, - { - secrets: 'Fischl von Luftschloss Narfidort', - sign: ['name'] - } - ) + cookie: t.Cookie({ + name: t.Optional(t.String()) + }) } ) - // .get('/remove', ({ cookie }) => { - // for (const self of Object.values(cookie)) self.remove() - - // return 'Deleted' - // }) .listen(3000) diff --git a/example/custom-response.ts b/example/custom-response.ts index e228da51..38f3ead9 100644 --- a/example/custom-response.ts +++ b/example/custom-response.ts @@ -1,16 +1,11 @@ import { Elysia } from '../src' -// This plugin will format JSON before return response -const prettyJson = (app: Elysia) => - // Custom - app.onAfterHandle((response, context) => { - if (response instanceof Object) - try { - return JSON.stringify(response, null, 4) - } catch (error) { - return - } - }) +const prettyJson = new Elysia().onAfterHandle(({ response }) => { + if (response instanceof Object) + try { + return JSON.stringify(response, null, 4) + } catch {} +}) new Elysia() .use(prettyJson) diff --git a/example/expose.ts b/example/expose.ts deleted file mode 100644 index e3197e26..00000000 --- a/example/expose.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { Elysia } from '../src' - -const app = new Elysia() - .get('/', () => 'Hello Elysia') - // .fn({ - // mirror: async (value: T) => { - // if (value === 'false') throw new Error("Value can't be false") - - // return value - // } - // }) - .listen(8080) diff --git a/example/headers.ts b/example/headers.ts index 87dc3964..79d3b68b 100644 --- a/example/headers.ts +++ b/example/headers.ts @@ -5,6 +5,6 @@ import cookie from '../src/index' new Elysia() .get('/', ({ set }) => { set.headers['x-powered-by'] = 'Elysia' - set.status = 400 + set.status = 'Bad Request' }) .listen(8080) diff --git a/example/html.ts b/example/html.ts deleted file mode 100644 index 2603cbde..00000000 --- a/example/html.ts +++ /dev/null @@ -1,38 +0,0 @@ -import { Elysia } from '../src' - -// ? Download from @elysia/html -const html = (app: Elysia) => - app - .decorate( - 'html', - (value: string) => - new Response(value, { - headers: { - 'content-type': 'text/html' - } - }) - ) - .onAfterHandle((context, response) => { - if ( - typeof response === 'string' && - response.trimStart().startsWith(' ` - - - - Hello World - - -

Hello World

- - ` - ) - .listen(8080) diff --git a/example/lazy-module.ts b/example/lazy-module.ts index c609f062..99e19fae 100644 --- a/example/lazy-module.ts +++ b/example/lazy-module.ts @@ -4,11 +4,11 @@ const plugin = (app: Elysia) => app.get('/plugin', () => 'Plugin') const asyncPlugin = async (app: Elysia) => app.get('/async', () => 'A') const app = new Elysia() - // .decorate('a', () => 'hello') + .decorate('a', () => 'hello') .use(plugin) .use(import('./lazy')) .use((app) => app.get('/inline', ({ store: { a } }) => 'inline')) - // .get('/', ({ a }) => a()) + .get('/', ({ a }) => a()) .listen(3000) await app.modules diff --git a/example/router.ts b/example/router.ts index 5f718c73..f895e4bc 100644 --- a/example/router.ts +++ b/example/router.ts @@ -1,21 +1,14 @@ import { Elysia } from '../src' -const prefix = - (prefix: Prefix) => - (app: Elysia) => - app.group(`${prefix}/api`, (app) => - app - .state('b', 'b') - .get('/2', () => 2) - .guard({}, (app) => app.get('/do', () => 'something')) - .group('/v2', (app) => - app.guard({}, (app) => app.get('/ok', () => 1)) - ) - ) +const prefix = (prefix: Prefix) => + new Elysia({ prefix }) + .state('b', 'b') + .get('/2', () => 2) + .guard({}, (app) => app.get('/do', () => 'something')) + .group('/v2', (app) => app.guard({}, (app) => app.get('/ok', () => 1))) const a = new Elysia() .get('/a', () => 'A') .guard({}, (app) => app.get('/guard', () => 'a')) .use(prefix('prefixed')) .listen(8080) - diff --git a/example/turing-complete.ts b/example/turing-complete.ts deleted file mode 100644 index 07cc45ef..00000000 --- a/example/turing-complete.ts +++ /dev/null @@ -1,75 +0,0 @@ -import { Elysia } from '../src' - -type isDynamicNumber = number extends A ? true : false - -type Plus< - Number extends number, - Incrementor extends number, - iterator extends any[] = [] -> = isDynamicNumber extends true - ? 0 - : Number extends iterator['length'] - ? CreateArray extends infer Total extends any[] - ? [...Total, ...iterator]['length'] - : never - : Plus - -type CreateArray< - Number extends number, - iterator extends any[] = [] -> = isDynamicNumber extends true - ? [] - : Number extends iterator['length'] - ? iterator - : CreateArray - -type GetCount< - App extends Elysia<{ - path: '' - error: {} - request: {} - store: { - count: any - } - schema: {} - meta: { - schema: {} - defs: {} - exposed: {} - } - }> -> = App['store']['count'] - -const plus = - (count: Count) => - ( - app: Elysia<{ - path: '' - error: {} - request: {} - store: { - count: Current - } - schema: {} - meta: { - schema: {} - defs: {} - exposed: {} - } - }> - ) => - app.state('count', app.store.count as Plus) - -const app = new Elysia() - .state('count', 0 as const) - .use((app) => app.use(plus<5, GetCount>(5))) - .use((app) => app.use(plus<2, GetCount>(2))) - .use((app) => app.use(plus<1, GetCount>(1))) - .use((app) => - // @ts-ignore: This comparison appears to be unintentional because the types '8' and '0' have no overlap.ts(2367) - app.if((app.store.count === 0) as false, (app) => - app.use(plus<100, GetCount>(100)) - ) - ) - -app.store.count diff --git a/package.json b/package.json index 23fb4bc2..d7ad0fb5 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "elysia", "description": "Ergonomic Framework for Human", - "version": "0.7.13", + "version": "0.7.14", "author": { "name": "saltyAom", "url": "https://github.com/SaltyAom", diff --git a/src/custom-types.ts b/src/custom-types.ts index a9d455c6..3ff5af9e 100644 --- a/src/custom-types.ts +++ b/src/custom-types.ts @@ -216,7 +216,7 @@ export const ElysiaType = { }) .Encode((value) => JSON.stringify(value)) as any as TObject, File: TypeSystem.Type('File', validateFile), - Files: (options: ElysiaTypeOptions.Files) => + Files: (options: ElysiaTypeOptions.Files = {}) => Type.Transform(Type.Union([Files(options)])) .Decode((value) => { if (Array.isArray(value)) return value diff --git a/src/index.ts b/src/index.ts index e531a18a..e539fd0b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2821,13 +2821,17 @@ export default class Elysia< Scoped > - model>( - mapper: (decorators: Definitions['type']) => NewType + model>( + mapper: (decorators: { + [type in keyof Definitions['type']]: ReturnType< + typeof t.Unsafe + > + }) => NewType ): Elysia< BasePath, Decorators, { - type: NewType + type: { [x in keyof NewType]: Static } error: Definitions['error'] }, ParentSchema,