Skip to content

Commit

Permalink
chore: remove deprecated extraOptions
Browse files Browse the repository at this point in the history
Signed-off-by: Lukas Reining <[email protected]>
  • Loading branch information
lukas-reining committed Dec 15, 2024
1 parent 6e35837 commit 5ceb643
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 38 deletions.
26 changes: 9 additions & 17 deletions src/eventsource.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { http, HttpResponse as MswHttpResponse } from 'msw';
import { server } from '../mocks/node';
import { CustomEventSource as EventSource, CustomEvent } from './eventsource';
import { CustomEvent, CustomEventSource as EventSource } from './eventsource';
import DoneCallback = jest.DoneCallback;

describe('EventSource', () => {
Expand Down Expand Up @@ -52,15 +52,10 @@ describe('EventSource', () => {
return globalThis.fetch(input, init);
}) as typeof fetch;

const ev = new EventSource(
'http://localhost/sse',
{
disableRetry: true,
},
{
fetchInput: fetchFn,
},
);
const ev = new EventSource('http://localhost/sse', {
disableRetry: true,
fetch: fetchFn,
});

ev.onopen = (event) => {
expect(event).toBeInstanceOf(Event);
Expand All @@ -76,13 +71,10 @@ describe('EventSource', () => {
return globalThis.fetch(input, init);
}) as typeof fetch;

const ev = new EventSource(
'http://localhost/sse',
{
disableRetry: true,
fetch: fetchFn,
},
);
const ev = new EventSource('http://localhost/sse', {
disableRetry: true,
fetch: fetchFn,
});

ev.onopen = (event) => {
expect(event).toBeInstanceOf(Event);
Expand Down
27 changes: 6 additions & 21 deletions src/eventsource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,10 @@ export type EventSourceOptions = {
fetch?: typeof fetch;
} & Omit<RequestInit, 'cache' | 'credentials' | 'signal'>;

/**
* @deprecated
*/
export type EventSourceExtraOptions = {
/**
* @deprecated Use {@link EventSourceOptions#fetch} instead
*/
fetchInput?: typeof fetch;
export type CustomEvent = Event & {
response?: Response;
};

export type CustomEvent = Event & {
response?: Response;
};

export class CustomEventSource extends EventTarget implements EventSource {
// https://html.spec.whatwg.org/multipage/server-sent-events.html#dom-eventsource-url
public url: string;
Expand All @@ -67,7 +57,6 @@ export class CustomEventSource extends EventTarget implements EventSource {
| null = null;

public readonly options: EventSourceInit & EventSourceOptions;
private readonly extraOptions?: EventSourceExtraOptions;
private abortController?: AbortController;
private timeoutId: ReturnType<typeof setTimeout> | undefined = undefined;
private retry: number;
Expand All @@ -78,14 +67,9 @@ export class CustomEventSource extends EventTarget implements EventSource {
constructor(
url: string | URL,
initDict?: EventSourceInit & EventSourceOptions,
/**
* @deprecated Use the related options in initDict
*/
extraOptions?: EventSourceExtraOptions,
) {
super();
this.options = initDict ?? {};
this.extraOptions = extraOptions;
this.url = url instanceof URL ? url.toString() : url;
this.retry = initDict?.retry ?? 5000;

Expand Down Expand Up @@ -137,8 +121,6 @@ export class CustomEventSource extends EventTarget implements EventSource {

const response = this.options.fetch
? await this.options.fetch(this.url, fetchOptions)
: this.extraOptions?.fetchInput
? await this.extraOptions.fetchInput(this.url, fetchOptions)
: await globalThis.fetch(this.url, fetchOptions);

// https://html.spec.whatwg.org/multipage/server-sent-events.html#dom-eventsource (Step 15)
Expand All @@ -157,7 +139,10 @@ export class CustomEventSource extends EventTarget implements EventSource {
response,
);
} else if (!response?.body) {
return this.failConnection(`Request failed with empty response body'`, response);
return this.failConnection(
`Request failed with empty response body'`,
response,
);
}

this.announceConnection(response);
Expand Down

0 comments on commit 5ceb643

Please sign in to comment.