Skip to content

Commit

Permalink
🔧 fix: #830 #827 #821 #820 #819 #810
Browse files Browse the repository at this point in the history
  • Loading branch information
SaltyAom committed Sep 18, 2024
1 parent cb56831 commit 6109684
Show file tree
Hide file tree
Showing 16 changed files with 244 additions and 69 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
# 1.1.13 - 18 Sep 2024
Bug fix:
- [#830](https://github.com/elysiajs/elysia/issues/830) Incorrect type for ws.publish
- [#827](https://github.com/elysiajs/elysia/issues/827) returning a response is forcing application/json content-type
- [#821](https://github.com/elysiajs/elysia/issues/821) handle "+" in query with validation
- [#820](https://github.com/elysiajs/elysia/issues/820) params in hooks inside prefixed groups are incorrectly typed never
- [#819](https://github.com/elysiajs/elysia/issues/819) setting cookie attribute before value cause cookie attribute to not be set
- [#810](https://github.com/elysiajs/elysia/issues/810) wrong inference of response in afterResponse, includes status code

# 1.1.12 - 4 Sep 2024
Feature:
- setup provenance publish
Expand Down
14 changes: 12 additions & 2 deletions example/a.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
import { Elysia, t } from '../src'
import { req } from '../test/utils'

const plugin = new Elysia().get('/plugin', 'static ')
new Elysia()
.ws('/', {
open: (ws) => {
ws.publish('channel', 'hello')
},
response: t.String()
})
.listen(3000)

// app.handle(new Request('http://localhost'))
// .then((x) => x.headers.getSetCookie())
// .then(console.log)
2 changes: 1 addition & 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.1.12",
"version": "1.1.13",
"author": {
"name": "saltyAom",
"url": "https://github.com/SaltyAom",
Expand Down
20 changes: 10 additions & 10 deletions src/compose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -727,8 +727,8 @@ export const composeHandler = ({
let temp
if(memory === -1) temp = decodeURIComponent(url.slice(start))
else temp = decodeURIComponent(url.slice(start, memory))
if(memory === -1) temp = decodeURIComponent(url.slice(start).replace(/\\+/g, ' '))
else temp = decodeURIComponent(url.slice(start, memory).replace(/\\+/g, ' '))
const charCode = temp.charCodeAt(0)
if(charCode !== 91 && charCode !== 123)
Expand Down Expand Up @@ -756,10 +756,10 @@ export const composeHandler = ({
a${index} = []
if(memory === -1) {
a${index}.push(decodeURIComponent(url.slice(start)))
a${index}.push(decodeURIComponent(url.slice(start)).replace(/\\+/g, ' '))
break
}
else a${index}.push(decodeURIComponent(url.slice(start, memory)))
else a${index}.push(decodeURIComponent(url.slice(start, memory)).replace(/\\+/g, ' '))
memory = url.indexOf('&${key}=', memory)
if(memory === -1) break
Expand All @@ -773,8 +773,8 @@ export const composeHandler = ({
const start = memory + ${key.length + 2}
memory = url.indexOf('&', start)
if(memory === -1) a${index} = decodeURIComponent(url.slice(start))
else a${index} = decodeURIComponent(url.slice(start, memory))
if(memory === -1) a${index} = decodeURIComponent(url.slice(start).replace(/\\+/g, ' '))
else a${index} = decodeURIComponent(url.slice(start, memory).replace(/\\+/g, ' '))
if (a${index} !== undefined) {
try {
Expand All @@ -791,9 +791,9 @@ export const composeHandler = ({
const start = memory + ${key.length + 2}
memory = url.indexOf('&', start)
if(memory === -1) a${index} = decodeURIComponent(url.slice(start))
if(memory === -1) a${index} = decodeURIComponent(url.slice(start).replace(/\\+/g, ' '))
else {
a${index} = decodeURIComponent(url.slice(start, memory))
a${index} = decodeURIComponent(url.slice(start, memory).replace(/\\+/g, ' '))
${
anyOf
Expand All @@ -812,8 +812,8 @@ export const composeHandler = ({
deepMemory = url.indexOf('&', start)
let value
if(deepMemory === -1) value = decodeURIComponent(url.slice(start))
else value = decodeURIComponent(url.slice(start, deepMemory))
if(deepMemory === -1) value = decodeURIComponent(url.slice(start).replace(/\\+/g, ' '))
else value = decodeURIComponent(url.slice(start, deepMemory).replace(/\\+/g, ' '))
const vStart = value.charCodeAt(0)
const vEnd = value.charCodeAt(value.length - 1)
Expand Down
16 changes: 9 additions & 7 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export type ErrorContext<
derive: {}
resolve: {}
},
Path extends string = ''
Path extends string | undefined = undefined
> = Prettify<
{
body: Route['body']
Expand Down Expand Up @@ -102,17 +102,19 @@ export type Context<
derive: {}
resolve: {}
},
Path extends string = ''
Path extends string | undefined = undefined
> = Prettify<
{
body: Route['body']
query: undefined extends Route['query']
? Record<string, string | undefined>
: Route['query']
params: undefined extends Route['params']
? Path extends `${string}/${':' | '*'}${string}`
? ResolvePath<Path>
: never
? undefined extends Path
? Record<string, string>
: Path extends `${string}/${':' | '*'}${string}`
? ResolvePath<Path>
: never
: Route['params']
headers: undefined extends Route['headers']
? Record<string, string | undefined>
Expand Down Expand Up @@ -172,8 +174,8 @@ export type Context<
route: string
request: Request
store: Singleton['store']
response?: Route['response']
} & ({} extends Route['response']
response?: Route['response'][keyof Route['response']]
} & ({} extends Route['response']
? {
error: typeof error
}
Expand Down
10 changes: 5 additions & 5 deletions src/cookies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export class Cookie<T> implements ElysiaCookie {
) {}

get cookie() {
if (!(this.name in this.jar)) return this.initial
if (!(this.name in this.jar)) this.jar[this.name] = this.initial

return this.jar[this.name]
}
Expand All @@ -149,9 +149,7 @@ export class Cookie<T> implements ElysiaCookie {
}

set value(value: T) {
if (!(this.name in this.jar)) this.jar[this.name] = this.initial

this.jar[this.name].value = value
this.cookie.value = value
}

get expires() {
Expand All @@ -160,6 +158,8 @@ export class Cookie<T> implements ElysiaCookie {

set expires(expires) {
this.cookie.expires = expires

console.log(this.cookie)
}

get maxAge() {
Expand Down Expand Up @@ -270,7 +270,7 @@ export class Cookie<T> implements ElysiaCookie {
toString() {
return typeof this.value === 'object'
? JSON.stringify(this.value)
: this.value?.toString() ?? ''
: (this.value?.toString() ?? '')
}
}

Expand Down
20 changes: 4 additions & 16 deletions src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Cookie } from './cookies'
import { ELYSIA_RESPONSE } from './error'

import type { Context } from './context'
import { LocalHook } from './types'
import type { LocalHook } from './types'

const hasHeaderShorthand = 'toJSON' in new Headers()

Expand Down Expand Up @@ -580,11 +580,7 @@ export const mapResponse = (

default:
if (response instanceof Response)
return new Response(response.body, {
headers: {
'Content-Type': 'application/json'
}
})
return response

if (response instanceof Promise)
return response.then((x) => mapResponse(x, set)) as any
Expand Down Expand Up @@ -971,11 +967,7 @@ export const mapEarlyResponse = (

default:
if (response instanceof Response)
return new Response(response.body, {
headers: {
'Content-Type': 'application/json'
}
})
return response

if (response instanceof Promise)
return response.then((x) => mapEarlyResponse(x, set)) as any
Expand Down Expand Up @@ -1110,11 +1102,7 @@ export const mapCompactResponse = (

default:
if (response instanceof Response)
return new Response(response.body, {
headers: {
'Content-Type': 'application/json'
}
})
return response

if (response instanceof Promise)
return response.then((x) =>
Expand Down
16 changes: 7 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -934,8 +934,7 @@ export default class Elysia<
Ephemeral['derive'] &
Volatile['derive']
resolve: {}
},
BasePath
}
>
>
): this
Expand Down Expand Up @@ -1045,8 +1044,7 @@ export default class Elysia<
Ephemeral['derive'] &
Volatile['derive']
resolve: {}
},
BasePath
}
>
>
): this
Expand Down Expand Up @@ -1570,8 +1568,7 @@ export default class Elysia<
Singleton & {
derive: Ephemeral['derive'] & Volatile['derive']
resolve: Ephemeral['resolve'] & Volatile['resolve']
},
BasePath
}
>
>
): this
Expand Down Expand Up @@ -1675,8 +1672,7 @@ export default class Elysia<
Singleton & {
derive: Ephemeral['derive'] & Volatile['derive']
resolve: Ephemeral['resolve'] & Volatile['resolve']
},
BasePath
}
>
>
): this
Expand Down Expand Up @@ -4681,7 +4677,9 @@ export default class Elysia<
headers: Schema['headers']
response: {} extends Schema['response']
? unknown
: Schema['response']
: Schema['response'] extends Record<200, unknown>
? Schema['response'][200]
: unknown
}
}
>,
Expand Down
21 changes: 11 additions & 10 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ export type ElysiaConfig<
* Enable Bun static response
*
* @default true
* @since 1.1.11
*/
nativeStaticResponse?: boolean
}
Expand Down Expand Up @@ -388,7 +389,7 @@ export interface UnwrapRoute<
export interface UnwrapGroupGuardRoute<
in out Schema extends InputSchema<any>,
in out Definitions extends Record<string, unknown> = {},
Path extends string = ''
Path extends string | undefined = undefined
> {
body: UnwrapBodySchema<Schema['body'], Definitions>
headers: UnwrapSchema<
Expand Down Expand Up @@ -547,7 +548,7 @@ export type Handler<
derive: {}
resolve: {}
},
Path extends string = ''
Path extends string | undefined = undefined
> = (
context: Context<Route, Singleton, Path>
) => MaybePromise<
Expand Down Expand Up @@ -592,7 +593,7 @@ export type InlineHandler<
derive: {}
resolve: {}
},
Path extends string = '',
Path extends string | undefined = undefined,
MacroContext = {}
> =
| ((
Expand Down Expand Up @@ -644,7 +645,7 @@ export type OptionalHandler<
derive: {}
resolve: {}
},
Path extends string = ''
Path extends string | undefined = undefined
> =
Handler<Route, Singleton, Path> extends (
context: infer Context
Expand All @@ -660,15 +661,15 @@ export type AfterHandler<
derive: {}
resolve: {}
},
Path extends string = ''
Path extends string | undefined = undefined
> =
Handler<Route, Singleton, Path> extends (
context: infer Context
) => infer Returned
? (
context: Prettify<
{
response: Route['response']
response: Route['response'][keyof Route['response']]
} & Context
>
) => Returned | MaybePromise<void>
Expand All @@ -682,7 +683,7 @@ export type MapResponse<
derive: {}
resolve: {}
},
Path extends string = ''
Path extends string | undefined = undefined
> = Handler<
Omit<Route, 'response'> & {
response: MaybePromise<Response | undefined | unknown>
Expand Down Expand Up @@ -713,7 +714,7 @@ export type TransformHandler<
derive: {}
resolve: {}
},
BasePath extends string = ''
Path extends string | undefined = undefined
> = {
(
context: Prettify<
Expand All @@ -722,7 +723,7 @@ export type TransformHandler<
Omit<Singleton, 'resolve'> & {
resolve: {}
},
BasePath
Path
>
>
): MaybePromise<void>
Expand All @@ -736,7 +737,7 @@ export type BodyHandler<
derive: {}
resolve: {}
},
Path extends string = ''
Path extends string | undefined = undefined
> = (
context: Prettify<
{
Expand Down
6 changes: 5 additions & 1 deletion src/ws/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ export class ElysiaWS<
get publish() {
return (
topic: string,
data: Route['response'] = undefined,
data: {} extends Route['response']
? unknown
: Route['response'] extends Record<200, unknown>
? Route['response'][200]
: unknown = undefined,
compress?: boolean
) => {
if (this.validator?.Check(data) === false)
Expand Down
1 change: 0 additions & 1 deletion src/ws/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import type { Context } from '../context'
import type {
SingletonBase,
Handler,
VoidHandler,
ErrorHandler,
InputSchema,
RouteSchema,
Expand Down
Loading

0 comments on commit 6109684

Please sign in to comment.