Skip to content
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
16 changes: 12 additions & 4 deletions src/treaty2/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ type ReplaceGeneratorWithAsyncGenerator<
: RecordType[K]
} & {}

type Enumerate<N extends number, Acc extends number[] = []> =
Acc['length'] extends N ? Acc[number] : Enumerate<N, [...Acc, Acc['length']]>;

type IntegerRange<F extends number, T extends number> = Exclude<Enumerate<T>, Enumerate<F>>;

type SuccessCodeRange = IntegerRange<200, 300>;
type IsSuccessCode<S extends number> = S extends SuccessCodeRange ? true : false;

type MaybeArray<T> = T | T[]
type MaybePromise<T> = T | Promise<T>

Expand Down Expand Up @@ -159,19 +167,19 @@ export namespace Treaty {

export type TreatyResponse<Res extends Record<number, unknown>> =
| {
data: Res[200] extends {
data: Res[Extract<keyof Res, SuccessCodeRange>] extends {
[ELYSIA_FORM_DATA]: infer Data
}
? Data
: Res[200]
: Res[Extract<keyof Res, SuccessCodeRange>]
error: null
response: Response
status: number
headers: RequestInit['headers']
}
| {
data: null
error: Exclude<keyof Res, 200> extends never
error: Exclude<keyof Res, SuccessCodeRange> extends never
? {
status: unknown
value: unknown
Expand All @@ -185,7 +193,7 @@ export namespace Treaty {
? Data
: Res[Status]
}
}[Exclude<keyof Res, 200>]
}[Exclude<keyof Res, SuccessCodeRange>]
response: Response
status: number
headers: RequestInit['headers']
Expand Down
67 changes: 66 additions & 1 deletion test/types/treaty2.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Elysia, file, form, t } from 'elysia'
import { Elysia, file, form, status, t } from 'elysia'
import { treaty } from '../../src'
import { expectTypeOf } from 'expect-type'

Expand Down Expand Up @@ -1064,3 +1064,68 @@ type ValidationError = {

edenClient({ id: '1' }).get()
}

{
const app = new Elysia().get("/", () => status(199, "yay"))

type App = typeof app

const edenClient = treaty<App>('http://localhost:3000')

const { data, error } = await edenClient.get()

expectTypeOf(data).toEqualTypeOf<unknown>()
expectTypeOf(error).toEqualTypeOf<{ status: 199; value: "yay"; } | null>()
}

{
const app = new Elysia().get("/", () => status(200, "yay"))

type App = typeof app

const edenClient = treaty<App>('http://localhost:3000')

const { data, error } = await edenClient.get()

expectTypeOf(data).toEqualTypeOf<"yay" | null>()
expectTypeOf(error).toEqualTypeOf<{ status: unknown; value: unknown; } | null>()
}

{
const app = new Elysia().get("/", () => status(201, "yay"))

type App = typeof app

const edenClient = treaty<App>('http://localhost:3000')

const { data, error } = await edenClient.get()

expectTypeOf(data).toEqualTypeOf<"yay" | null>()
expectTypeOf(error).toEqualTypeOf<{ status: unknown; value: unknown; } | null>()
}

{
const app = new Elysia().get("/", () => status(299, "yay"))

type App = typeof app

const edenClient = treaty<App>('http://localhost:3000')

const { data, error } = await edenClient.get()

expectTypeOf(data).toEqualTypeOf<"yay" | null>()
expectTypeOf(error).toEqualTypeOf<{ status: unknown; value: unknown; } | null>()
}

{
const app = new Elysia().get("/", () => status(300, "yay"))

type App = typeof app

const edenClient = treaty<App>('http://localhost:3000')

const { data, error } = await edenClient.get()

expectTypeOf(data).toEqualTypeOf<unknown>()
expectTypeOf(error).toEqualTypeOf<{ status: 300; value: "yay"; } | null>()
}