Skip to content

Commit

Permalink
🔧 broom: fix .ws merged type
Browse files Browse the repository at this point in the history
  • Loading branch information
SaltyAom committed Mar 7, 2023
1 parent 3f73c76 commit 9f6a997
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,7 @@
# 0.3.0-rc.2 - 7 Mar 2023
Fix:
- Missing merged return type for `.ws`

# 0.3.0-rc.1 - 7 Mar 2023
Fix:
- Missing nanoid
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,7 +1,7 @@
{
"name": "elysia",
"description": "Fast, and friendly Bun web framework",
"version": "0.3.0-rc.1",
"version": "0.3.0-rc.2",
"author": {
"name": "saltyAom",
"url": "https://github.com/SaltyAom",
Expand Down
25 changes: 22 additions & 3 deletions src/index.ts
Expand Up @@ -54,7 +54,8 @@ import type {
MaybePromise,
IsNever,
MergeUnionObjects,
TypedRouteToEden
TypedRouteToEden,
TypedWSRouteToEden
} from './types'
import { type TSchema } from '@sinclair/typebox'
import { ElysiaWSContext, ElysiaWSOptions, WSTypedSchema } from './ws'
Expand Down Expand Up @@ -1256,7 +1257,7 @@ export default class Elysia<Instance extends ElysiaInstance = ElysiaInstance> {
* })
* ```
*/
ws<Path extends string = '', Schema = {}>(
ws<Path extends string = '', Schema extends TypedSchema = {}>(
/**
* Path to register websocket to
*/
Expand All @@ -1270,7 +1271,25 @@ export default class Elysia<Instance extends ElysiaInstance = ElysiaInstance> {
Instance['meta'][typeof DEFS]
>
: never
): this extends Elysia<infer Instance> ? Elysia<Instance> : ElysiaInstance {
): Elysia<{
request: Instance['request']
store: Instance['store']
schema: Instance['schema']
meta: Instance['meta'] &
Record<
typeof SCHEMA,
Record<
Path,
{
[method in 'subscribe']: TypedWSRouteToEden<
Schema,
Instance['meta'][typeof DEFS],
Path
>
}
>
>
}> {
if (!this.wsRouter)
throw new Error(
"Can't find WebSocket. Please register WebSocket plugin first by importing 'elysia/ws'"
Expand Down
22 changes: 22 additions & 0 deletions src/types.ts
Expand Up @@ -319,6 +319,28 @@ export type TypedRouteToEden<
}
: never

export type TypedWSRouteToEden<
Schema extends TypedSchema = TypedSchema,
Definitions extends TypedSchema<string> = ElysiaInstance['meta'][typeof DEFS],
Path extends string = string,
Catch = unknown
> = TypedSchemaToEden<
Schema,
Definitions
> extends infer Typed extends AnyTypedSchema
? {
body: Typed['body']
headers: Typed['headers']
query: Typed['query']
params: undefined extends Typed['params']
? Record<ExtractPath<Path>, string>
: Typed['params']
response: undefined extends Typed['response']
? Catch
: Typed['response']['200']
}
: never

export type TypedSchemaToEden<
Schema extends TypedSchema,
Definitions extends ElysiaInstance['meta'][typeof DEFS]
Expand Down

0 comments on commit 9f6a997

Please sign in to comment.