@elysiajs/websocket
Advanced tools
Comparing version 0.2.8 to 0.3.0-beta.0
@@ -20,8 +20,9 @@ /// <reference types="bun-types" /> | ||
export declare const websocket: (config?: Omit<WebSocketHandler, 'open' | 'message' | 'close' | 'drain'>) => (app: Elysia) => Elysia<{ | ||
store: Record<any, any> & Record<typeof import("elysia").SCHEMA, {}> & Record<typeof DEFS, {}>; | ||
store: Record<any, any>; | ||
request: { | ||
publish: (topic: string, data: string | import("bun").ArrayBufferView | ArrayBuffer, compress?: boolean | undefined) => number; | ||
publish: (topic: string, data: string | ArrayBuffer | import("bun").ArrayBufferView, compress?: boolean | undefined) => number; | ||
}; | ||
schema: {}; | ||
meta: Record<typeof import("elysia").SCHEMA, {}> & Record<typeof DEFS, {}> & Record<typeof import("elysia").EXPOSED, {}>; | ||
}>; | ||
export default websocket; |
@@ -127,3 +127,3 @@ import { Elysia, createValidationError, getSchemaValidator, DEFS } from 'elysia'; | ||
id: nanoid(), | ||
message: getSchemaValidator(options.schema?.body, this.store[DEFS]), | ||
message: getSchemaValidator(options.schema?.body, this.meta[DEFS]), | ||
transformMessage: !options.transform | ||
@@ -130,0 +130,0 @@ ? [] |
/// <reference types="bun-types" /> | ||
import type { ServerWebSocket, WebSocketHandler } from 'bun'; | ||
import type { Context, TypedSchema, HookHandler, UnwrapSchema, SCHEMA, Elysia, DEFS } from 'elysia'; | ||
import type { ExtractPath, TypedRoute, TypedSchemaToRoute, WithArray, ElysiaInstance, NoReturnHandler, AnyTypedSchema } from 'elysia/dist/types'; | ||
import type { Elysia, ElysiaInstance, Context, UnwrapSchema, TypedSchema, HookHandler, SCHEMA, DEFS } from 'elysia'; | ||
import type { ExtractPath, TypedRoute, TypedSchemaToRoute, WithArray, NoReturnHandler, AnyTypedSchema } from 'elysia/dist/types'; | ||
import type { ElysiaWS } from '.'; | ||
import type { Raikiri } from 'raikiri'; | ||
import type { TSchema } from '@sinclair/typebox'; | ||
import type { TypeCheck } from '@sinclair/typebox/compiler'; | ||
import { TSchema } from 'elysia/node_modules/@sinclair/typebox'; | ||
import type { TypeCheck } from 'elysia/node_modules/@sinclair/typebox/compiler'; | ||
export type WSTypedSchema<ModelName extends string = string> = Omit<TypedSchema<ModelName>, 'response'> & { | ||
response?: TSchema | ModelName | undefined; | ||
}; | ||
export type ElysiaWSRoute<Method extends string = string, Schema extends TypedSchema = TypedSchema, Instance extends ElysiaInstance = ElysiaInstance, Path extends string = string, CatchResponse = unknown> = Elysia<{ | ||
export type ElysiaWSRoute<Method extends string = string, Schema extends TypedSchema = {}, Instance extends ElysiaInstance = ElysiaInstance, Path extends string = string, CatchResponse = unknown> = Elysia<{ | ||
request: Instance['request']; | ||
store: Instance['store'] & { | ||
[SCHEMA]: { | ||
[path in Path]: { | ||
[method in Method]: TypedSchemaToRoute<Schema, Instance> extends infer FinalSchema extends AnyTypedSchema ? Omit<FinalSchema, 'response'> & { | ||
response: undefined extends FinalSchema['response'] ? CatchResponse : FinalSchema['response']; | ||
} : never; | ||
}; | ||
store: Instance['store']; | ||
schema: Instance['schema']; | ||
meta: Instance['meta'] & Record<typeof SCHEMA, { | ||
[path in Path]: { | ||
[method in Method]: TypedSchemaToRoute<Schema, Instance> extends infer FinalSchema extends AnyTypedSchema ? Omit<FinalSchema, 'response'> & { | ||
response: undefined extends FinalSchema['response'] ? CatchResponse : FinalSchema['response']; | ||
} : never; | ||
}; | ||
}; | ||
schema: Instance['schema']; | ||
}>; | ||
}>; | ||
export type TypedWSSchemaToRoute<Schema extends WSTypedSchema = WSTypedSchema, Instance extends ElysiaInstance = ElysiaInstance> = { | ||
body: UnwrapSchema<Schema['body'], Instance>; | ||
headers: UnwrapSchema<Schema['headers'], Instance> extends infer Result extends Record<string, any> ? Result : undefined; | ||
query: UnwrapSchema<Schema['query'], Instance> extends infer Result extends Record<string, any> ? Result : undefined; | ||
params: UnwrapSchema<Schema['params'], Instance> extends infer Result extends Record<string, any> ? Result : undefined; | ||
response: UnwrapSchema<Schema['params'], Instance> extends infer Result extends Record<string, any> ? Result : undefined; | ||
export type TypedWSSchemaToRoute<Schema extends WSTypedSchema = WSTypedSchema, Definitions extends ElysiaInstance['meta'][typeof DEFS] = {}> = { | ||
body: UnwrapSchema<Schema['body'], Definitions>; | ||
headers: UnwrapSchema<Schema['headers'], Definitions> extends infer Result extends Record<string, any> ? Result : undefined; | ||
query: UnwrapSchema<Schema['query'], Definitions> extends infer Result extends Record<string, any> ? Result : undefined; | ||
params: UnwrapSchema<Schema['params'], Definitions> extends infer Result extends Record<string, any> ? Result : undefined; | ||
response: UnwrapSchema<Schema['params'], Definitions> extends infer Result extends Record<string, any> ? Result : undefined; | ||
}; | ||
@@ -35,8 +34,8 @@ export type WSTypedSchemaToTypedSchema<Schema extends WSTypedSchema> = Omit<Schema, 'response'> & { | ||
}; | ||
export type WebSocketSchemaToRoute<Schema extends WSTypedSchema> = { | ||
body: UnwrapSchema<Schema['body']> extends Record<string, any> ? UnwrapSchema<Schema['body']> : undefined; | ||
headers: UnwrapSchema<Schema['headers']> extends Record<string, any> ? UnwrapSchema<Schema['headers']> : undefined; | ||
query: UnwrapSchema<Schema['query']> extends Record<string, any> ? UnwrapSchema<Schema['query']> : undefined; | ||
params: UnwrapSchema<Schema['params']> extends Record<string, any> ? UnwrapSchema<Schema['params']> : undefined; | ||
response: UnwrapSchema<Schema['response']> extends Record<string, any> ? UnwrapSchema<Schema['response']> : undefined; | ||
export type WebSocketSchemaToRoute<Schema extends WSTypedSchema, Definitions extends ElysiaInstance['meta'][typeof DEFS] = {}> = { | ||
body: UnwrapSchema<Schema['body'], Definitions> extends infer Result extends Record<string, any> ? Result : undefined; | ||
headers: UnwrapSchema<Schema['headers'], Definitions> extends infer Result extends Record<string, any> ? Result : undefined; | ||
query: UnwrapSchema<Schema['query'], Definitions> extends infer Result extends Record<string, any> ? Result : undefined; | ||
params: UnwrapSchema<Schema['params'], Definitions> extends infer Result extends Record<string, any> ? Result : undefined; | ||
response: UnwrapSchema<Schema['response'], Definitions> extends infer Result extends Record<string, any> ? Result : undefined; | ||
}; | ||
@@ -62,3 +61,3 @@ export type TransformMessageHandler<Message extends TSchema | string | undefined> = (message: UnwrapSchema<Message>) => void | UnwrapSchema<Message>; | ||
websocketRouter: Raikiri<any>; | ||
ws<Instance extends ElysiaInstance = this extends Elysia<infer Inner> ? Inner : ElysiaInstance, ModelName extends string = Exclude<keyof Instance['store'][typeof DEFS], number | symbol>, Schema extends WSTypedSchema<ModelName> = WSTypedSchema<ModelName>, Path extends string = string>(path: Path, options: Omit<Partial<WebSocketHandler<Context>>, 'open' | 'message' | 'close' | 'drain'> & { | ||
ws<Instance extends ElysiaInstance = this extends Elysia<infer Inner> ? Inner : ElysiaInstance, Schema extends WSTypedSchema<Exclude<keyof Instance['meta'][typeof DEFS], number | symbol>> = {}, Path extends string = string>(path: Path, options: Omit<Partial<WebSocketHandler<Context>>, 'open' | 'message' | 'close' | 'drain'> & { | ||
schema?: Schema; | ||
@@ -70,3 +69,3 @@ beforeHandle?: WithArray<HookHandler<Schema>>; | ||
open?: (ws: ElysiaWS<ElysiaWSContext<Schema, Path>, Schema, Instance>) => void | Promise<void>; | ||
message?: (ws: ElysiaWS<ElysiaWSContext<Schema, Path>, Schema, Instance>, message: UnwrapSchema<Schema['body'], Instance, string>) => any; | ||
message?: (ws: ElysiaWS<ElysiaWSContext<Schema, Path>, Schema, Instance>, message: UnwrapSchema<Schema['body'], Instance['meta'][typeof DEFS], string>) => any; | ||
close?: (ws: ElysiaWS<ElysiaWSContext<Schema, Path>, Schema, Instance>) => any; | ||
@@ -73,0 +72,0 @@ drain?: (ws: ElysiaWS<ElysiaWSContext<Schema, Path>, Schema, Instance>, code: number, reason: string) => any; |
{ | ||
"name": "@elysiajs/websocket", | ||
"version": "0.2.8", | ||
"version": "0.3.0-beta.0", | ||
"description": "Plugin for Elysia that add support for websocket", | ||
@@ -36,3 +36,4 @@ "author": { | ||
"peerDependencies": { | ||
"elysia": ">= 0.2.2" | ||
"elysia": ">= 0.3.0-beta.0", | ||
"raikiri": ">= 0.0.0-beta.6" | ||
}, | ||
@@ -42,4 +43,4 @@ "devDependencies": { | ||
"@types/node": "^18.11.7", | ||
"bun-types": "^0.5.0", | ||
"elysia": "^0.2.2", | ||
"bun-types": "^0.5.7", | ||
"elysia": "^0.3.0-beta.0", | ||
"eslint": "^8.26.0", | ||
@@ -49,5 +50,4 @@ "typescript": "^4.9.3" | ||
"dependencies": { | ||
"nanoid": "^4.0.0", | ||
"raikiri": "^0.0.0-beta.3" | ||
"nanoid": "^4.0.0" | ||
} | ||
} |
@@ -247,3 +247,3 @@ import type { Server, ServerWebSocket, WebSocketHandler } from 'bun' | ||
options.schema?.body, | ||
this.store[DEFS] | ||
this.meta[DEFS] | ||
), | ||
@@ -250,0 +250,0 @@ transformMessage: !options.transform |
118
src/types.ts
import type { ServerWebSocket, WebSocketHandler } from 'bun' | ||
import type { | ||
Elysia, | ||
ElysiaInstance, | ||
Context, | ||
UnwrapSchema, | ||
TypedSchema, | ||
HookHandler, | ||
UnwrapSchema, | ||
SCHEMA, | ||
Elysia, | ||
DEFS | ||
DEFS, | ||
EXPOSED | ||
} from 'elysia' | ||
@@ -17,6 +19,3 @@ import type { | ||
WithArray, | ||
ElysiaRoute, | ||
ElysiaInstance, | ||
NoReturnHandler, | ||
TypedRouteToEden, | ||
AnyTypedSchema | ||
@@ -28,4 +27,4 @@ } from 'elysia/dist/types' | ||
import type { Static, TSchema } from '@sinclair/typebox' | ||
import type { TypeCheck } from '@sinclair/typebox/compiler' | ||
import { TSchema, Static } from 'elysia/node_modules/@sinclair/typebox' | ||
import type { TypeCheck } from 'elysia/node_modules/@sinclair/typebox/compiler' | ||
@@ -41,3 +40,3 @@ export type WSTypedSchema<ModelName extends string = string> = Omit< | ||
Method extends string = string, | ||
Schema extends TypedSchema = TypedSchema, | ||
Schema extends TypedSchema = {}, | ||
Instance extends ElysiaInstance = ElysiaInstance, | ||
@@ -48,19 +47,22 @@ Path extends string = string, | ||
request: Instance['request'] | ||
store: Instance['store'] & { | ||
[SCHEMA]: { | ||
[path in Path]: { | ||
[method in Method]: TypedSchemaToRoute< | ||
Schema, | ||
Instance | ||
> extends infer FinalSchema extends AnyTypedSchema | ||
? Omit<FinalSchema, 'response'> & { | ||
response: undefined extends FinalSchema['response'] | ||
? CatchResponse | ||
: FinalSchema['response'] | ||
} | ||
: never | ||
store: Instance['store'] | ||
schema: Instance['schema'] | ||
meta: Instance['meta'] & | ||
Record< | ||
typeof SCHEMA, | ||
{ | ||
[path in Path]: { | ||
[method in Method]: TypedSchemaToRoute< | ||
Schema, | ||
Instance | ||
> extends infer FinalSchema extends AnyTypedSchema | ||
? Omit<FinalSchema, 'response'> & { | ||
response: undefined extends FinalSchema['response'] | ||
? CatchResponse | ||
: FinalSchema['response'] | ||
} | ||
: never | ||
} | ||
} | ||
} | ||
} | ||
schema: Instance['schema'] | ||
> | ||
}> | ||
@@ -70,8 +72,8 @@ | ||
Schema extends WSTypedSchema = WSTypedSchema, | ||
Instance extends ElysiaInstance = ElysiaInstance | ||
Definitions extends ElysiaInstance['meta'][typeof DEFS] = {} | ||
> = { | ||
body: UnwrapSchema<Schema['body'], Instance> | ||
body: UnwrapSchema<Schema['body'], Definitions> | ||
headers: UnwrapSchema< | ||
Schema['headers'], | ||
Instance | ||
Definitions | ||
> extends infer Result extends Record<string, any> | ||
@@ -82,3 +84,3 @@ ? Result | ||
Schema['query'], | ||
Instance | ||
Definitions | ||
> extends infer Result extends Record<string, any> | ||
@@ -89,3 +91,3 @@ ? Result | ||
Schema['params'], | ||
Instance | ||
Definitions | ||
> extends infer Result extends Record<string, any> | ||
@@ -96,3 +98,3 @@ ? Result | ||
Schema['params'], | ||
Instance | ||
Definitions | ||
> extends infer Result extends Record<string, any> | ||
@@ -110,17 +112,35 @@ ? Result | ||
export type WebSocketSchemaToRoute<Schema extends WSTypedSchema> = { | ||
body: UnwrapSchema<Schema['body']> extends Record<string, any> | ||
? UnwrapSchema<Schema['body']> | ||
export type WebSocketSchemaToRoute< | ||
Schema extends WSTypedSchema, | ||
Definitions extends ElysiaInstance['meta'][typeof DEFS] = {} | ||
> = { | ||
body: UnwrapSchema< | ||
Schema['body'], | ||
Definitions | ||
> extends infer Result extends Record<string, any> | ||
? Result | ||
: undefined | ||
headers: UnwrapSchema<Schema['headers']> extends Record<string, any> | ||
? UnwrapSchema<Schema['headers']> | ||
headers: UnwrapSchema< | ||
Schema['headers'], | ||
Definitions | ||
> extends infer Result extends Record<string, any> | ||
? Result | ||
: undefined | ||
query: UnwrapSchema<Schema['query']> extends Record<string, any> | ||
? UnwrapSchema<Schema['query']> | ||
query: UnwrapSchema< | ||
Schema['query'], | ||
Definitions | ||
> extends infer Result extends Record<string, any> | ||
? Result | ||
: undefined | ||
params: UnwrapSchema<Schema['params']> extends Record<string, any> | ||
? UnwrapSchema<Schema['params']> | ||
params: UnwrapSchema< | ||
Schema['params'], | ||
Definitions | ||
> extends infer Result extends Record<string, any> | ||
? Result | ||
: undefined | ||
response: UnwrapSchema<Schema['response']> extends Record<string, any> | ||
? UnwrapSchema<Schema['response']> | ||
response: UnwrapSchema< | ||
Schema['response'], | ||
Definitions | ||
> extends infer Result extends Record<string, any> | ||
? Result | ||
: undefined | ||
@@ -186,7 +206,5 @@ } | ||
: ElysiaInstance, | ||
ModelName extends string = Exclude< | ||
keyof Instance['store'][typeof DEFS], | ||
number | symbol | ||
>, | ||
Schema extends WSTypedSchema<ModelName> = WSTypedSchema<ModelName>, | ||
Schema extends WSTypedSchema< | ||
Exclude<keyof Instance['meta'][typeof DEFS], number | symbol> | ||
> = {}, | ||
Path extends string = string | ||
@@ -244,3 +262,7 @@ >( | ||
>, | ||
message: UnwrapSchema<Schema['body'], Instance, string> | ||
message: UnwrapSchema< | ||
Schema['body'], | ||
Instance['meta'][typeof DEFS], | ||
string | ||
> | ||
) => any | ||
@@ -247,0 +269,0 @@ |
33335
751
+ Addedraikiri@0.1.3(transitive)
- Removedraikiri@^0.0.0-beta.3
- Removedraikiri@0.0.0-beta.8(transitive)