Skip to content

Commit

Permalink
🎉 feat(treaty2): headers, response
Browse files Browse the repository at this point in the history
  • Loading branch information
SaltyAom committed Mar 5, 2024
1 parent cbe0fa8 commit e51186d
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 63 deletions.
28 changes: 6 additions & 22 deletions example/a.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,15 @@
import { Elysia, t } from 'elysia'
import { treaty } from '../src'
import { treaty, edenTreaty } from '../src'

const app = new Elysia()
.get('/headers', ({ headers }) => headers, {
headers: t.Object({
username: t.String(),
alias: t.Literal('Kristen')
})
})
.ws('/ws', {
open({ data, send }) {
console.log(data)
},
message({ data, send }) {
}
.get('/hello', ({ headers }) => {
return headers
})
.listen(3000)

const api = treaty<typeof app>('localhost:3000')

api.ws.subscribe({ query: { username: 'Aly', room: '123' } })

// const { data, error } = await api.headers.get({
// headers: {
// username: 'a',
// alias: 'Kristen'
// }
// })
const a = await api.hello.get()

// console.log(data)
a.data
a.error
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@elysiajs/eden",
"version": "1.0.0-rc.0",
"version": "1.0.0-rc.1",
"description": "Fully type-safe Elysia client",
"author": {
"name": "saltyAom",
Expand Down
2 changes: 1 addition & 1 deletion src/treaty2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ const createProxy = (
headers,
onRequest,
onResponse,
...conf
fetch: conf
} = config

const isGetOrHead =
Expand Down
40 changes: 27 additions & 13 deletions src/treaty2/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,20 @@ export namespace Treaty {
: K extends `:${string}`
? never
: K]: K extends 'subscribe' // ? Websocket route
? undefined extends Route['subscribe']['query']
? (params?: {
query?: Record<string, string>
}) => EdenWS<Route['subscribe']>
: (params: {
query: Route['subscribe']['query']
}) => EdenWS<Route['subscribe']>
? (undefined extends Route['subscribe']['headers']
? { headers?: Record<string, unknown> }
: {
headers: Route['subscribe']['headers']
}) &
(undefined extends Route['subscribe']['query']
? { query?: Record<string, unknown> }
: {
query: Route['subscribe']['query']
}) extends infer Param
? {} extends Param
? (options?: Param) => EdenWS<Route['subscribe']>
: (options?: Param) => EdenWS<Route['subscribe']>
: never
: Route[K] extends {
body: infer Body
headers: infer Headers
Expand Down Expand Up @@ -97,7 +104,8 @@ export namespace Treaty {
: Prettify<Sign<Route[K]>>
}

export interface Config extends Omit<RequestInit, 'headers'> {
export interface Config {
fetch?: Omit<RequestInit, 'headers' | 'method'>
fetcher?: typeof fetch
headers?:
| RequestInit['headers']
Expand All @@ -116,27 +124,33 @@ export namespace Treaty {
onResponse?: MaybeArray<(response: Response) => MaybePromise<unknown>>
}

type TreatyResponse<Response extends Record<number, unknown>> =
type TreatyResponse<Res extends Record<number, unknown>> =
| {
data: Response[200]
data: Res[200]
error: null
response: Response
status: number
headers: FetchRequestInit['headers']
}
| {
data: null
error: Response extends { 200: unknown }
error: Res extends { 200: unknown }
? // @ts-expect-error
{
[Status in keyof Response as Status extends 200
[Status in keyof Res as Status extends 200
? never
: Status]: {
status: Status
value: Response[Status]
value: Res[Status]
}
}[Exclude<keyof Response, 200>]
: {
status: unknown
value: unknown
}
response: Response
status: number
headers: FetchRequestInit['headers']
}

export interface OnMessage<Data = unknown> extends MessageEvent {
Expand Down
Loading

0 comments on commit e51186d

Please sign in to comment.