Skip to content

Commit 20e28bd

Browse files
committed
📘 doc: document change
1 parent d0e1f84 commit 20e28bd

File tree

14 files changed

+158
-144
lines changed

14 files changed

+158
-144
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
# 1.4.7
2+
Feature:
3+
- [#236](https://github.com/elysiajs/eden/issues/236) reconcile default headers in type-level
4+
- [#206](https://github.com/elysiajs/eden/issues/206), [#245](https://github.com/elysiajs/eden/pull/245) add `parseDate` option
5+
26
Bug fix:
37
- [#247](https://github.com/elysiajs/eden/pull/247), [#246](https://github.com/elysiajs/eden/issues/246) handle network error
48
- [#244](https://github.com/elysiajs/eden/issues/244) don't skip index path name
59
- [#241](https://github.com/elysiajs/eden/issues/241) prevent freeze from thenable
6-
- [#236](https://github.com/elysiajs/eden/issues/236) reconcile default headers in type-level
710
- [#232](https://github.com/elysiajs/eden/issues/232) accept query array
811

912
# 1.4.6 - 23 Dec 2025

example/a.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ const app = new Elysia()
1414
})
1515
.listen(3000)
1616

17-
const api = treaty(app)
17+
const api = treaty(app, {
18+
parseDate: false
19+
})
1820

1921
const { data, error } = await api.get({
2022
query: {
21-
minDate: new Date(),
22-
maxDate: new Date()
23+
minDate: '2025-01-01',
24+
maxDate: '2025-01-01'
2325
}
2426
})
2527

src/fetch/index.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import type { Elysia } from 'elysia'
22

33
import { EdenFetchError } from '../errors'
44
import type { EdenFetch } from './types'
5-
import { parseStringifiedValue } from '../utils/parsingUtils'
6-
import { ThrowHttpErrors } from '../types'
5+
import { parseStringifiedValue } from '../utils/parse'
6+
import type { ThrowHttpError } from '../types'
77

88
export type { EdenFetch } from './types'
99

@@ -33,22 +33,23 @@ const parseResponse = async (response: Response) => {
3333

3434
const shouldThrow = (
3535
error: EdenFetchError<number, unknown>,
36-
throwHttpErrors?: ThrowHttpErrors
36+
throwHttpError?: ThrowHttpError
3737
): boolean => {
38-
if (typeof throwHttpErrors === 'function') return throwHttpErrors(error)
39-
return throwHttpErrors === true
38+
if (typeof throwHttpError === 'function') return throwHttpError(error)
39+
40+
return throwHttpError === true
4041
}
4142

4243
const handleResponse = async (
4344
response: Response,
4445
retry: () => any,
45-
throwHttpErrors?: ThrowHttpErrors
46+
throwHttpError?: ThrowHttpError
4647
) => {
4748
const data = await parseResponse(response)
4849

4950
if (response.status >= 300 || response.status < 200) {
5051
const error = new EdenFetchError(response.status, data)
51-
if (shouldThrow(error, throwHttpErrors)) throw error
52+
if (shouldThrow(error, throwHttpError)) throw error
5253
return {
5354
data: null,
5455
status: response.status,
@@ -72,15 +73,15 @@ export const edenFetch = <App extends Elysia<any, any, any, any, any, any, any>>
7273
config?: EdenFetch.Config
7374
): EdenFetch.Create<App> =>
7475
// @ts-ignore
75-
(endpoint: string, { query, params, body, throwHttpErrors: requestThrowHttpErrors, ...options } = {}) => {
76+
(endpoint: string, { query, params, body, throwHttpError: requestThrowHttpError, ...options } = {}) => {
7677
if (params)
7778
Object.entries(params).forEach(([key, value]) => {
7879
endpoint = endpoint.replace(`:${key}`, value as string)
7980
})
8081

8182
const fetch = config?.fetcher || globalThis.fetch
82-
// Per-request throwHttpErrors overrides config
83-
const resolvedThrowHttpErrors = requestThrowHttpErrors ?? config?.throwHttpErrors
83+
// Per-request throwHttpError overrides config
84+
const resolvedThrowHttpError = requestThrowHttpError ?? config?.throwHttpError
8485

8586
const nonNullishQuery = query
8687
? Object.fromEntries(
@@ -118,11 +119,11 @@ export const edenFetch = <App extends Elysia<any, any, any, any, any, any, any>>
118119

119120
const execute = () =>
120121
fetch(requestUrl, init)
121-
.then((response) => handleResponse(response, execute, resolvedThrowHttpErrors))
122+
.then((response) => handleResponse(response, execute, resolvedThrowHttpError))
122123
.catch((err) => {
123124
if (err instanceof EdenFetchError) throw err
124125
const error = new EdenFetchError(503, err)
125-
if (shouldThrow(error, resolvedThrowHttpErrors)) throw error
126+
if (shouldThrow(error, resolvedThrowHttpError)) throw error
126127
return {
127128
data: null,
128129
error,

src/fetch/types.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type {
66
IsNever,
77
Prettify,
88
TreatyToPath,
9-
ThrowHttpErrors
9+
ThrowHttpError
1010
} from '../types'
1111

1212
export namespace EdenFetch {
@@ -22,7 +22,7 @@ export namespace EdenFetch {
2222

2323
export interface Config extends RequestInit {
2424
fetcher?: typeof globalThis.fetch
25-
throwHttpErrors?: ThrowHttpErrors
25+
throwHttpError?: ThrowHttpError
2626
}
2727

2828
export type Fn<Schema extends Record<string, any>> = <
@@ -63,7 +63,7 @@ export namespace EdenFetch {
6363
(IsUnknown<Route['body']> extends false
6464
? { body: Route['body'] }
6565
: { body?: unknown }) & {
66-
throwHttpErrors?: ThrowHttpErrors
66+
throwHttpError?: ThrowHttpError
6767
}
6868
) => Promise<
6969
Prettify<

src/treaty/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { Elysia, InputSchema } from 'elysia'
22
import { EdenFetchError } from '../errors'
33
import { composePath } from './utils'
44
import type { EdenTreaty } from './types'
5-
import { parseMessageEvent, parseStringifiedValue } from '../utils/parsingUtils'
5+
import { parseMessageEvent, parseStringifiedValue } from '../utils/parse'
66

77
export type { EdenTreaty } from './types'
88

src/treaty2/index.ts

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import { EdenWS } from './ws'
99
import {
1010
parseStringifiedDate,
1111
parseStringifiedValue
12-
} from '../utils/parsingUtils'
13-
import { ThrowHttpErrors } from '../types'
12+
} from '../utils/parse'
13+
import type { ThrowHttpError } from '../types'
1414

1515
const method = [
1616
'get',
@@ -26,10 +26,10 @@ const method = [
2626

2727
const shouldThrow = (
2828
error: EdenFetchError<number, unknown>,
29-
throwHttpErrors?: ThrowHttpErrors
29+
throwHttpError?: ThrowHttpError
3030
): boolean => {
31-
if (typeof throwHttpErrors === 'function') return throwHttpErrors(error)
32-
return throwHttpErrors === true
31+
if (typeof throwHttpError === 'function') return throwHttpError(error)
32+
return throwHttpError === true
3333
}
3434

3535
const locals = ['localhost', '127.0.0.1', '0.0.0.0']
@@ -126,7 +126,7 @@ const processHeaders = async (
126126

127127
function parseSSEBlock(
128128
block: string,
129-
options?: { parseDates?: boolean }
129+
options?: { parseDate?: boolean }
130130
): Record<string, unknown> | null {
131131
const lines = block.split('\n')
132132
const result: Record<string, unknown> = {}
@@ -155,7 +155,7 @@ function* extractEvents(
155155
bufferRef: {
156156
value: string
157157
},
158-
options?: { parseDates?: boolean }
158+
options?: { parseDate?: boolean }
159159
): Generator<Record<string, unknown>> {
160160
let eventEnd: number
161161
while ((eventEnd = bufferRef.value.indexOf('\n\n')) !== -1) {
@@ -171,7 +171,7 @@ function* extractEvents(
171171

172172
export async function* streamResponse(
173173
response: Response,
174-
options?: { parseDates?: boolean }
174+
options?: { parseDate?: boolean }
175175
) {
176176
const body = response.body
177177

@@ -333,13 +333,13 @@ const createProxy = (
333333
? body.fetch
334334
: options?.fetch
335335

336-
// Per-request throwHttpErrors overrides config
337-
const requestThrowHttpErrors =
336+
// Per-request throwHttpError overrides config
337+
const requestThrowHttpError =
338338
isGetOrHead && typeof body === 'object'
339-
? body.throwHttpErrors
340-
: options?.throwHttpErrors
341-
const resolvedThrowHttpErrors =
342-
requestThrowHttpErrors ?? config.throwHttpErrors
339+
? body.throwHttpError
340+
: options?.throwHttpError
341+
const resolvedThrowHttpError =
342+
requestThrowHttpError ?? config.throwHttpError
343343

344344
fetchInit = {
345345
...fetchInit,
@@ -532,7 +532,7 @@ const createProxy = (
532532
) ?? fetcher!(url, fetchInit))
533533
} catch (err) {
534534
const error = new EdenFetchError(503, err)
535-
if (shouldThrow(error, resolvedThrowHttpErrors))
535+
if (shouldThrow(error, resolvedThrowHttpError))
536536
throw error
537537
return {
538538
data: null,
@@ -582,7 +582,7 @@ const createProxy = (
582582
) {
583583
case 'text/event-stream':
584584
data = streamResponse(response, {
585-
parseDates: config.parseDates
585+
parseDate: config.parseDate
586586
})
587587
break
588588

@@ -591,7 +591,7 @@ const createProxy = (
591591
if (typeof v !== 'string') return v
592592

593593
const date = parseStringifiedDate(v, {
594-
parseDates: config.parseDates
594+
parseDate: config.parseDate
595595
})
596596
if (date) return date
597597

@@ -617,14 +617,14 @@ const createProxy = (
617617
default:
618618
data = await response.text().then((text) =>
619619
parseStringifiedValue(text, {
620-
parseDates: config.parseDates
620+
parseDate: config.parseDate
621621
})
622622
)
623623
}
624624

625625
if (response.status >= 300 || response.status < 200) {
626626
error = new EdenFetchError(response.status, data)
627-
if (shouldThrow(error, resolvedThrowHttpErrors))
627+
if (shouldThrow(error, resolvedThrowHttpError))
628628
throw error
629629
data = null
630630
}

src/treaty2/types.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type {
77
MaybeEmptyObject,
88
Not,
99
Prettify,
10-
ThrowHttpErrors
10+
ThrowHttpError
1111
} from '../types'
1212

1313
// type Files = File | FileList
@@ -75,7 +75,7 @@ type RelaxFileArrays<T> =
7575
? {
7676
[K in keyof T]: MaybeArrayFile<T[K]>
7777
}
78-
: T
78+
: T
7979

8080
type SerializeQueryParams<T> =
8181
T extends Record<string, any>
@@ -91,7 +91,7 @@ type SerializeQueryParams<T> =
9191
export namespace Treaty {
9292
export interface TreatyParam {
9393
fetch?: RequestInit
94-
throwHttpErrors?: ThrowHttpErrors
94+
throwHttpError?: ThrowHttpError
9595
}
9696

9797
export type Create<
@@ -246,8 +246,16 @@ export namespace Treaty {
246246
>
247247
onResponse?: MaybeArray<(response: Response) => MaybePromise<unknown>>
248248
keepDomain?: boolean
249-
parseDates?: boolean
250-
throwHttpErrors?: ThrowHttpErrors
249+
/**
250+
* @default true
251+
*
252+
* parse stringifed Date to new Date
253+
*/
254+
parseDate?: boolean
255+
/**
256+
*
257+
*/
258+
throwHttpError?: ThrowHttpError
251259
}
252260

253261
// type UnwrapAwaited<T extends Record<number, unknown>> = {

src/treaty2/ws.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { InputSchema } from 'elysia'
22
import type { Treaty } from './types'
3-
import { parseMessageEvent } from '../utils/parsingUtils'
3+
import { parseMessageEvent } from '../utils/parse'
44

55
export class EdenWS<in out Schema extends InputSchema<any> = {}> {
66
ws: WebSocket

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { EdenFetchError } from './errors'
22

3-
export type ThrowHttpErrors = boolean | ((error: EdenFetchError<number, unknown>) => boolean)
3+
export type ThrowHttpError = boolean | ((error: EdenFetchError<number, unknown>) => boolean)
44

55
// https://stackoverflow.com/a/39495173
66
type Range<F extends number, T extends number> = Exclude<
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ export const isNumericString = (message: string) =>
1010

1111
export const parseStringifiedDate = (
1212
value: any,
13-
options?: { parseDates?: boolean }
13+
options?: { parseDate?: boolean }
1414
) => {
1515
if (typeof value !== 'string') return null
16-
if (options?.parseDates === false) return null
16+
if (options?.parseDate === false) return null
1717

1818
// Remove quote from stringified date
1919
const temp = value.replace(/"/g, '')
@@ -40,7 +40,7 @@ export const isStringifiedObject = (value: string) => {
4040

4141
export const parseStringifiedObject = (
4242
data: string,
43-
options?: { parseDates?: boolean }
43+
options?: { parseDate?: boolean }
4444
) =>
4545
JSON.parse(data, (_, value) => {
4646
const date = parseStringifiedDate(value, options)
@@ -54,14 +54,14 @@ export const parseStringifiedObject = (
5454

5555
export const parseStringifiedValue = (
5656
value: string,
57-
options?: { parseDates?: boolean }
57+
options?: { parseDate?: boolean }
5858
) => {
5959
if (!value) return value
6060
if (isNumericString(value)) return +value
6161
if (value === 'true') return true
6262
if (value === 'false') return false
6363

64-
if (options?.parseDates !== false) {
64+
if (options?.parseDate !== false) {
6565
const date = parseStringifiedDate(value, options)
6666
if (date) return date
6767
}
@@ -77,7 +77,7 @@ export const parseStringifiedValue = (
7777

7878
export const parseMessageEvent = (
7979
event: MessageEvent,
80-
options?: { parseDates?: boolean }
80+
options?: { parseDate?: boolean }
8181
) => {
8282
const messageString = event.data.toString()
8383

0 commit comments

Comments
 (0)