Skip to content

Commit

Permalink
Subscribe method proposed implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinTail committed Jan 2, 2025
1 parent 78b3ba0 commit c663d7e
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions example/example.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,28 @@ export class ExpressZodAPIClient {
),
);
}

public subscribe<
K extends Request & `get ${string}`,
>(request: K, params: Input[K]) {
const path = request.split(" ")[1] as Path;
const source = new EventSource(new URL(`${path}?${new URLSearchParams(params)}`, "https://example.com"));
type Res<T extends string = string> = Extract<PositiveResponse[K], {event: T}>;
const connection = {
source,
on: <E extends Res["event"]>(
event: E,
handler: (data: Res<E>["data"]) => void | Promise<void>
) => {
source.addEventListener(event, (msg) => {
const data = JSON.parse((msg as MessageEvent).data);
handler(data);
});
return connection;
}
}
return connection;
}
}

// Usage example:
Expand Down

0 comments on commit c663d7e

Please sign in to comment.