diff --git a/example/a.ts b/example/a.ts index 0f4498de..61d46a3d 100644 --- a/example/a.ts +++ b/example/a.ts @@ -1,25 +1,19 @@ -import { Elysia, t } from '../src' +// index.ts +import Elysia from '../src' const app = new Elysia() - .post( - '/', - ({ body }) => { - return body - }, - { - body: t.Uint8Array({ - maxByteLength: 1 - }) - } - ) + .onError((err) => { + if (err.code === 'NOT_FOUND') return + + process.stdout.write(`Error: ${JSON.stringify(err, null, 2)}\n`) + }) + .get('/testing-status', ({ status }) => { + return status(403, 'This is a test error from status') + }) + .resolve(({ status }) => { + return status(403, 'This is a test error from resolve') + }) + .get('/testing-resolve', () => 'Hello World!') .listen(3000) -const response = await fetch( - new Request('http://localhost:3000', { - method: 'POST', - body: new TextEncoder().encode('可愛くてごめん'), - headers: { 'content-type': 'application/octet-stream' } - }) -) - -console.log(response.headers.toJSON()) +console.log(app.routes[1].compile().toString()) diff --git a/example/uint8array.ts b/example/uint8array.ts new file mode 100644 index 00000000..ea66c64d --- /dev/null +++ b/example/uint8array.ts @@ -0,0 +1,19 @@ +import { Elysia, t } from '../src' + +new Elysia() + .post('/', ({ body }) => body, { + body: t.Uint8Array() + }) + .listen(3000) + +const response = await fetch('http://localhost:3000', { + method: 'POST', + body: new Uint8Array([ + 229, 143, 175, 230, 132, 155, 227, 129, 143, 227, 129, 166, 227, 129, + 148, 227, 130, 129, 227, 130, 147 + ]), + headers: { 'content-type': 'application/octet-stream' } +}) + +console.log(response.status) +console.log(await response.text()) diff --git a/src/compose.ts b/src/compose.ts index 2303eecb..877249ff 100644 --- a/src/compose.ts +++ b/src/compose.ts @@ -1164,7 +1164,9 @@ export const composeHandler = ({ `case '${key}':` + `let bo${key}=parser['${key}'](c,contentType)\n` + `if(bo${key} instanceof Promise)bo${key}=await bo${key}\n` + - `if(bo${key} instanceof ElysiaCustomStatusResponse)throw result\n` + + `if(bo${key} instanceof ElysiaCustomStatusResponse){` + + mapResponse(`bo${key}`) + + `}` + `if(bo${key}!==undefined)c.body=bo${key}\n` + `break` + '\n' @@ -1200,8 +1202,9 @@ export const composeHandler = ({ if (transform.subType === 'mapDerive') fnLiteral += - `if(transformed instanceof ElysiaCustomStatusResponse)throw transformed\n` + - `else{` + + `if(transformed instanceof ElysiaCustomStatusResponse){` + + mapResponse('transformed') + + `}else{` + `transformed.request=c.request\n` + `transformed.store=c.store\n` + `transformed.qi=c.qi\n` + @@ -1214,8 +1217,9 @@ export const composeHandler = ({ '}' else fnLiteral += - `if(transformed instanceof ElysiaCustomStatusResponse)throw transformed\n` + - `else Object.assign(c,transformed)\n` + `if(transformed instanceof ElysiaCustomStatusResponse){` + + mapResponse('transformed') + + `}else Object.assign(c,transformed)\n` endUnit() } @@ -1626,9 +1630,9 @@ export const composeHandler = ({ if (beforeHandle.subType === 'mapResolve') fnLiteral += - `if(resolved instanceof ElysiaCustomStatusResponse)` + - `throw resolved\n` + - `else{` + + `if(resolved instanceof ElysiaCustomStatusResponse){` + + mapResponse('resolved') + + `}else{` + `resolved.request=c.request\n` + `resolved.store=c.store\n` + `resolved.qi=c.qi\n` + @@ -1641,7 +1645,9 @@ export const composeHandler = ({ `}` else fnLiteral += - `if(resolved instanceof ElysiaCustomStatusResponse)throw resolved\n` + + `if(resolved instanceof ElysiaCustomStatusResponse){` + + mapResponse('resolved') + + `}` + `else Object.assign(c, resolved)\n` } else if (!returning) { fnLiteral += isAsync(beforeHandle) diff --git a/test/lifecycle/derive.test.ts b/test/lifecycle/derive.test.ts index ea3fbf58..de8f2ff2 100644 --- a/test/lifecycle/derive.test.ts +++ b/test/lifecycle/derive.test.ts @@ -326,4 +326,19 @@ describe('derive', () => { // const res2 = await root.handle(req('/root')).then((t) => t.text()) // expect(res2).toBe('hi') // }) + + it('handle return derive without throw', async () => { + let isOnErrorCalled = false + + const app = new Elysia() + .onError(() => { + isOnErrorCalled = true + }) + .derive(({ status }) => status(418)) + .get('/', () => '') + + await app.handle(req('/')) + + expect(isOnErrorCalled).toBe(false) + }) }) diff --git a/test/lifecycle/resolve.test.ts b/test/lifecycle/resolve.test.ts index e032dbcb..ff7c151f 100644 --- a/test/lifecycle/resolve.test.ts +++ b/test/lifecycle/resolve.test.ts @@ -216,16 +216,18 @@ describe('resolve', () => { }) .get('/', () => '') - const res = await (new Elysia({aot: true})).use(route).handle(req('/')) + const res = await new Elysia({ aot: true }).use(route).handle(req('/')) expect(await res.status).toEqual(418) expect(await res.text()).toEqual("I'm a teapot") - const res2 = await (new Elysia({aot: false})).use(route).handle(req('/')) + const res2 = await new Elysia({ aot: false }) + .use(route) + .handle(req('/')) expect(await res2.status).toEqual(418) expect(await res2.text()).toEqual("I'm a teapot") - }) + /** These work but there's no support for type it('work inline', async () => { const app = new Elysia().get('/', ({ hi }) => hi(), { resolve: () => ({ @@ -360,4 +362,20 @@ describe('resolve', () => { const res2 = await root.handle(req('/root')).then((t) => t.text()) expect(res2).toBe('hi') }) + */ + + it('handle return resolve without throw', async () => { + let isOnErrorCalled = false + + const app = new Elysia() + .onError(() => { + isOnErrorCalled = true + }) + .resolve(({ status }) => status(418)) + .get('/', () => '') + + await app.handle(req('/')) + + expect(isOnErrorCalled).toBe(false) + }) })