Skip to content

Commit 1282872

Browse files
committed
fix: mark fetch init properties required in typings
This makes it easier for people to pass custom headers without having to use optional chaining/guard for undefined values which will never actually be undefined, as we know we are passing certain values to it.
1 parent 20869db commit 1282872

File tree

3 files changed

+33
-15
lines changed

3 files changed

+33
-15
lines changed

src/EventSource.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import type {
66
EventListenerOptions,
77
EventListenerOrEventListenerObject,
88
EventSourceEventMap,
9+
EventSourceFetchInit,
910
EventSourceInit,
1011
FetchLike,
11-
FetchLikeInit,
1212
FetchLikeResponse,
1313
} from './types.js'
1414

@@ -447,16 +447,15 @@ export class EventSource extends EventTarget {
447447
* @returns The request options
448448
* @internal
449449
*/
450-
#getRequestOptions(): FetchLikeInit {
450+
#getRequestOptions(): EventSourceFetchInit {
451451
const lastEvent = this.#lastEventId ? {'Last-Event-ID': this.#lastEventId} : undefined
452-
const headers = {Accept: 'text/event-stream', ...lastEvent}
453452

454-
const init: FetchLikeInit = {
453+
const init: EventSourceFetchInit = {
455454
// [spec] Let `corsAttributeState` be `Anonymous`…
456455
// [spec] …will have their mode set to "cors"…
457456
mode: 'cors',
458457
redirect: 'follow',
459-
headers,
458+
headers: {Accept: 'text/event-stream', ...lastEvent},
460459
cache: 'no-store',
461460
signal: this.#controller?.signal,
462461
}

src/types.ts

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,49 @@ import type {ErrorEvent} from './errors.js'
66
*
77
* @public
88
*/
9-
export type FetchLike = (url: string | URL, init?: FetchLikeInit) => Promise<FetchLikeResponse>
9+
export type FetchLike = (
10+
url: string | URL,
11+
init: EventSourceFetchInit,
12+
) => Promise<FetchLikeResponse>
1013

1114
/**
12-
* Stripped down version of `RequestInit`, only defining the parts we care about.
15+
* Subset of `RequestInit` used for `fetch()` calls made by the `EventSource` class.
16+
* As we know that we will be passing certain values, we can be more specific and have
17+
* users not have to do optional chaining and similar for things that will always be there.
1318
*
1419
* @public
1520
*/
16-
export interface FetchLikeInit {
21+
export interface EventSourceFetchInit {
1722
/** An AbortSignal to set request's signal. Typed as `any` because of polyfill inconsistencies. */
1823
// eslint-disable-next-line @typescript-eslint/no-explicit-any
19-
signal?: {aborted: boolean} | any
24+
signal: {aborted: boolean} | any
2025

2126
/** A Headers object, an object literal, or an array of two-item arrays to set request's headers. */
22-
headers?: Record<string, string>
27+
headers: {
28+
[key: string]: string
29+
Accept: 'text/event-stream'
30+
}
2331

2432
/** A string to indicate whether the request will use CORS, or will be restricted to same-origin URLs. Sets request's mode. */
25-
mode?: 'cors' | 'no-cors' | 'same-origin'
33+
mode: 'cors' | 'no-cors' | 'same-origin'
2634

2735
/** A string indicating whether credentials will be sent with the request always, never, or only when sent to a same-origin URL. Sets request's credentials. */
2836
credentials?: 'include' | 'omit' | 'same-origin'
2937

3038
/** Controls how the request is cached. */
31-
cache?: 'no-store'
39+
cache: 'no-store'
3240

3341
/** A string indicating whether request follows redirects, results in an error upon encountering a redirect, or returns the redirect (in an opaque fashion). Sets request's redirect. */
34-
redirect?: 'error' | 'follow' | 'manual'
42+
redirect: 'error' | 'follow' | 'manual'
3543
}
3644

45+
/**
46+
* @public
47+
* @deprecated Use `EventSourceFetchInit` instead.
48+
* This type is only here for backwards compatibility and will be removed in a future version.
49+
*/
50+
export type FetchLikeInit = EventSourceFetchInit
51+
3752
/**
3853
* Stripped down version of `ReadableStreamDefaultReader`, only defining the parts we care about.
3954
*

test/tests.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import {EventSource as OurEventSource, type FetchLike, type FetchLikeInit} from '../src/index.js'
1+
import {
2+
EventSource as OurEventSource,
3+
type EventSourceFetchInit,
4+
type FetchLike,
5+
} from '../src/index.js'
26
import {unicodeLines} from './fixtures.js'
37
import {deferClose, expect, getCallCounter} from './helpers.js'
48
import type {TestRunner} from './waffletest/index.js'
@@ -67,7 +71,7 @@ export function registerTests(options: {
6771
})
6872

6973
test('passes `no-store` to `fetch`, avoiding cache', async () => {
70-
let passedInit: FetchLikeInit | undefined
74+
let passedInit: EventSourceFetchInit | undefined
7175

7276
const onMessage = getCallCounter({name: 'onMessage'})
7377
const es = new OurEventSource(new URL(`${baseUrl}:${port}/debug`), {

0 commit comments

Comments
 (0)