Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supports supplying custom sec-websocket-protocol in EdenWS subscribe #148

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/treaty/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ export class EdenWS<Schema extends InputSchema<any> = InputSchema> {
ws: WebSocket
url: string

constructor(url: string) {
this.ws = new WebSocket(url)
constructor(url: string, protocols?: string[]) {
this.ws = new WebSocket(url, protocols)
this.url = url
}

Expand Down Expand Up @@ -172,7 +172,7 @@ const createProxy = (
transform?: EdenTreaty.Transform
headers?: Record<string, string>
query?: Record<string, string | number>
}
},
] = [{}, {}]
) {
let bodyObj: any =
Expand Down Expand Up @@ -223,7 +223,8 @@ const createProxy = (
url.replace(
/^([^]+):\/\//,
url.startsWith('https://') ? 'wss://' : 'ws://'
)
),
options as unknown as string[],
)

const execute = async <T extends EdenTreaty.ExecuteOptions>(
Expand Down
4 changes: 2 additions & 2 deletions src/treaty/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ export namespace EdenTreaty {
undefined extends Route['query']
? (params?: {
$query?: Record<string, string>
}) => EdenWS<Route>
: (params: { $query: Route['query'] }) => EdenWS<Route['subscribe']>
}, protocols?: string[]) => EdenWS<Route>
: (params: { $query: Route['query'] }, protocols?: string[]) => EdenWS<Route['subscribe']>
: // ? HTTP route
((
params: Prettify<
Expand Down
2 changes: 1 addition & 1 deletion src/treaty2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ const createProxy = (
path +
q

return new EdenWS(url)
return new EdenWS(url, options)
}

return (async () => {
Expand Down
4 changes: 2 additions & 2 deletions src/treaty2/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ export namespace Treaty {
query: Route['subscribe']['query']
}) extends infer Param
? {} extends Param
? (options?: Param) => EdenWS<Route['subscribe']>
: (options?: Param) => EdenWS<Route['subscribe']>
? (options?: Param, protocols?: string[]) => EdenWS<Route['subscribe']>
: (options?: Param, protocols?: string[]) => EdenWS<Route['subscribe']>
: never
: Route[K] extends {
body: infer Body
Expand Down
4 changes: 2 additions & 2 deletions src/treaty2/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { parseMessageEvent } from '../utils/parsingUtils'
export class EdenWS<in out Schema extends InputSchema<any> = {}> {
ws: WebSocket

constructor(public url: string) {
this.ws = new WebSocket(url)
constructor(public url: string, protocols?: string[]) {
this.ws = new WebSocket(url, protocols)
}

send(data: Schema['body'] | Schema['body'][]) {
Expand Down
20 changes: 20 additions & 0 deletions test/treaty.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ const app = new Elysia()
.post('/string', ({ body }) => body, {
body: t.String()
})
.ws('/custom-protocol', {
headers: t.Object({
'sec-websocket-protocol': t.Literal('customprotocol'),
}),
open: async (ws) => {
ws.send('success');
ws.close();
},
})
.listen(8082)

const client = edenTreaty<typeof app>('http://localhost:8082')
Expand Down Expand Up @@ -270,4 +279,15 @@ describe('Eden Treaty', () => {

expect(data).toEqual({ body: null })
})


it('sends the correct custom protocols', async (done) => {
await new Promise<void>(res => {
const socket = client['custom-protocol'].subscribe(undefined, ['customprotocol']);

socket.subscribe(() => res());
})

done()
});
})
21 changes: 21 additions & 0 deletions test/treaty2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,15 @@ const app = new Elysia()
ws.close()
}
})
.ws('/custom-protocol', {
headers: t.Object({
'sec-websocket-protocol': t.Literal('customprotocol'),
}),
open: async (ws) => {
ws.send('success');
ws.close();
},
})
.get('/stream', function* stream() {
yield 'a'
yield 'b'
Expand Down Expand Up @@ -640,4 +649,16 @@ describe('Treaty2 - Using endpoint URL', () => {
done()
})
})

it('sends the correct custom protocols', async (done) => {
const client = treaty<typeof app>('http://localhost:8080')

await new Promise<void>(res => {
const socket = client['custom-protocol'].subscribe(undefined, ['customprotocol']);

socket.subscribe(() => res());
})

done()
});
})