Skip to content

Commit

Permalink
🎉 feat: clean up webstandard handler, add universal server type
Browse files Browse the repository at this point in the history
  • Loading branch information
SaltyAom committed Nov 17, 2024
1 parent a5dcbab commit d438a67
Show file tree
Hide file tree
Showing 13 changed files with 536 additions and 178 deletions.
50 changes: 31 additions & 19 deletions example/a.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
import { Elysia, t } from '../src'
import { Elysia, redirect, t } from '../src'
import { mapResponse } from '../src/adapter/web-standard/handler'

const a = new Elysia()
.model({
a: t.Object({
a: t.Ref('a')
}),
})
.model((model) => ({
...model,
b: t.Object({
a: model.a,
b: t.Ref('b')
})
}))
.get('/', ({ body }) => 'a', {
body: 'b'
})
.listen(3000)
const response = mapResponse(redirect('https://cunny.school', 302), {
status: "I'm a teapot",
headers: {
Name: 'Sorasaki Hina'
},
redirect: 'https://cunny.school',
cookie: {}
})

a._routes.index.get.response[422].
console.log(response.headers.toJSON())

// const a = new Elysia()
// .model({
// a: t.Object({
// a: t.Ref('a')
// }),
// })
// .model((model) => ({
// ...model,
// b: t.Object({
// a: model.a,
// b: t.Ref('b')
// })
// }))
// .get('/', ({ body }) => 'a', {
// body: 'b'
// })
// .listen(3000)

// a._routes.index.get.response[422].
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "elysia",
"description": "Ergonomic Framework for Human",
"version": "1.2.0-exp.37",
"version": "1.2.0-exp.40",
"author": {
"name": "saltyAom",
"url": "https://github.com/SaltyAom",
Expand Down Expand Up @@ -113,6 +113,11 @@
"import": "./dist/universal/index.mjs",
"require": "./dist/cjs/universal/index.js"
},
"./universal/server": {
"types": "./dist/universal/server.d.ts",
"import": "./dist/universal/server.mjs",
"require": "./dist/cjs/universal/server.js"
},
"./universal/file": {
"types": "./dist/universal/file.d.ts",
"import": "./dist/universal/file.mjs",
Expand Down
39 changes: 14 additions & 25 deletions src/adapter/web-standard/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,6 @@ export async function* streamResponse(response: Response) {
export const handleSet = (set: Context['set']) => {
if (typeof set.status === 'string') set.status = StatusMap[set.status]

if (set.redirect) {
set.headers.Location = set.redirect
if (!set.status || set.status < 300 || set.status >= 400)
set.status = 302
}

if (set.cookie && isNotEmpty(set.cookie)) {
const cookie = serializeCookie(set.cookie)

Expand All @@ -217,7 +211,7 @@ export const handleSet = (set: Context['set']) => {
}
}

const mergeResponseWithSetHeaders = (
export const mergeResponseWithSetHeaders = (
response: Response,
set: Context['set']
) => {
Expand Down Expand Up @@ -247,12 +241,7 @@ export const mapResponse = (
set: Context['set'],
abortSignal?: AbortSignal
): Response => {
if (
isNotEmpty(set.headers) ||
set.status !== 200 ||
set.redirect ||
set.cookie
) {
if (isNotEmpty(set.headers) || set.status !== 200 || set.cookie) {
handleSet(set)

switch (response?.constructor?.name) {
Expand Down Expand Up @@ -418,8 +407,14 @@ export const mapResponse = (
}

// Stream response defers a 'set' API, assume that it may include 'set'
// @ts-expect-error
if (typeof response?.next === 'function')
if (
// @ts-expect-error
typeof response?.next === 'function' ||
response instanceof ReadableStream ||
(response instanceof Response &&
(response as Response).headers.get('transfer-encoding') ===
'chunked')
)
// @ts-expect-error
return handleStream(response as any, set, abortSignal)

Expand All @@ -433,12 +428,7 @@ export const mapEarlyResponse = (
): Response | undefined => {
if (response === undefined || response === null) return

if (
isNotEmpty(set.headers) ||
set.status !== 200 ||
set.redirect ||
set.cookie
) {
if (isNotEmpty(set.headers) || set.status !== 200 || set.cookie) {
handleSet(set)

switch (response?.constructor?.name) {
Expand Down Expand Up @@ -510,10 +500,9 @@ export const mapEarlyResponse = (

case 'Promise':
// @ts-ignore
return (response as Promise<unknown>).then((x) => {
const r = mapEarlyResponse(x, set)
if (r !== undefined) return r
})
return (response as Promise<unknown>).then((x) =>
mapEarlyResponse(x, set)
)

case 'Error':
return errorToResponse(response as Error, set)
Expand Down
6 changes: 2 additions & 4 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,6 @@ export type Context<
/**
* @deprecated Use inline redirect instead
*
* Will be removed in 1.2.0
*
* @example Migration example
* ```ts
* new Elysia()
Expand Down Expand Up @@ -179,10 +177,10 @@ export type Context<
: Route['response'][keyof Route['response']]
} & ({} extends Route['response']
? {
[k in 'error' | 'status']: typeof error
error: typeof error
}
: {
[k in 'error' | 'status']: <
error: <
const Code extends
| keyof Route['response']
| InvertedStatusMap[Extract<
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Serve, Server } from 'bun'
import type { Serve, Server } from './universal/server'

import { Memoirist } from 'memoirist'
import type { TObject, Static, TSchema, TModule, TRef } from '@sinclair/typebox'
Expand Down
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,8 @@ export type Handler<
: Route['response'][keyof Route['response']]
>

export type IsAny<T> = 0 extends 1 & T ? true : false

export type Replace<Original, Target, With> =
IsAny<Target> extends true
? Original
Expand All @@ -573,8 +575,6 @@ export type Replace<Original, Target, With> =
? With
: Original

type IsAny<T> = 0 extends 1 & T ? true : false

export type CoExist<Original, Target, With> =
IsAny<Target> extends true
? Original
Expand Down
9 changes: 9 additions & 0 deletions src/universal/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
export { file } from './file'
export type {
ErrorLike,
GenericServeOptions,
Serve,
ServeOptions,
Server,
ServerWebSocketSendStatus,
SocketAddress
} from './server'
Loading

0 comments on commit d438a67

Please sign in to comment.