Skip to content

Commit

Permalink
🎉 feat(treaty2): promise error
Browse files Browse the repository at this point in the history
  • Loading branch information
SaltyAom committed Mar 8, 2024
1 parent e51186d commit bfa8e01
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 37 deletions.
Binary file modified bun.lockb
Binary file not shown.
14 changes: 10 additions & 4 deletions example/a.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import { Elysia, t } from 'elysia'
import { treaty, edenTreaty } from '../src'
import { treaty } from '../src'

const app = new Elysia()
.get('/hello', ({ headers }) => {
return headers
.get('/hello', ({ headers, error }) => {
if (Math.random()) return error(418, 'a')
if (Math.random()) return error(420, 'b')

return "A"
})
.listen(3000)

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

type Res = typeof app._routes.hello.get.response
type B = Exclude<keyof Res, 200> extends number ? true : false

const a = await api.hello.get()

a.data
a.error
a.error
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@elysiajs/eden",
"version": "1.0.0-rc.1",
"version": "1.0.0-rc.2",
"description": "Fully type-safe Elysia client",
"author": {
"name": "saltyAom",
Expand Down Expand Up @@ -48,7 +48,7 @@
"scripts": {
"dev": "bun run --watch example/index.ts",
"test": "bun test && bun test:types",
"test:types": "tsc --project tsconfig.test.json",
"test:types": "tsc --project tsconfig.test.json",
"build": "rimraf dist && tsup",
"release": "npm run build && npm run test && npm publish --access public"
},
Expand All @@ -59,7 +59,7 @@
"@elysiajs/cors": "0.7.0",
"@types/bun": "^1.0.3",
"@types/node": "^18.15.5",
"elysia": "1.0.0-rc.0",
"elysia": "1.0.0-rc.10",
"esbuild": "^0.19.3",
"eslint": "^8.26.0",
"expect-type": "^0.17.3",
Expand Down
6 changes: 6 additions & 0 deletions src/treaty2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,12 @@ const createProxy = (
if (data === 'true') return true
if (data === 'false') return false

if(!data) return data

const date = new Date(data)
if (!Number.isNaN(date.getTime()))
return date

return data
})
}
Expand Down
27 changes: 15 additions & 12 deletions src/treaty2/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,13 @@ export namespace Treaty {
onResponse?: MaybeArray<(response: Response) => MaybePromise<unknown>>
}

type TreatyResponse<Res extends Record<number, unknown>> =
type UnwrapAwaited<T extends Record<number, unknown>> = {
[K in keyof T]: Awaited<T[K]>
}

type TreatyResponse<
Res extends Record<number, unknown>,
> =
| {
data: Res[200]
error: null
Expand All @@ -134,20 +140,17 @@ export namespace Treaty {
}
| {
data: null
error: Res extends { 200: unknown }
? // @ts-expect-error
{
[Status in keyof Res as Status extends 200
? never
: Status]: {
status: Status
value: Res[Status]
}
}[Exclude<keyof Response, 200>]
: {
error: Exclude<keyof Res, 200> extends never
? {
status: unknown
value: unknown
}
: {
[Status in keyof Res]: {
status: Status
value: Res[Status]
}
}[Exclude<keyof Res, 200>]
response: Response
status: number
headers: FetchRequestInit['headers']
Expand Down
11 changes: 11 additions & 0 deletions test/treaty2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ const app = new Elysia()
})
}
)
.post('/date', ({ body: { date } }) => date, {
body: t.Object({
date: t.Date()
})
})

const client = treaty(app)

Expand Down Expand Up @@ -371,4 +376,10 @@ describe('Treaty2', () => {
}
})
})

it('send date', async () => {
const { data, error } = await client.date.post({ date: new Date() })

expect(data).toBeInstanceOf(Date)
})
})
Loading

0 comments on commit bfa8e01

Please sign in to comment.