From e1fd091cc32cc931d5366f508f5bef3ef1d6323f Mon Sep 17 00:00:00 2001 From: Robert Craigie Date: Mon, 20 Jan 2025 11:42:34 +0000 Subject: [PATCH] feat(streaming): add `.request_id` getter --- src/lib/BetaMessageStream.ts | 6 ++++++ src/lib/MessageStream.ts | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/src/lib/BetaMessageStream.ts b/src/lib/BetaMessageStream.ts index 7c8408fe..06fbc46a 100644 --- a/src/lib/BetaMessageStream.ts +++ b/src/lib/BetaMessageStream.ts @@ -55,6 +55,7 @@ export class BetaMessageStream implements AsyncIterable #errored = false; #aborted = false; #catchingPromiseCreated = false; + #request_id: string | null | undefined; constructor() { this.#connectedPromise = new Promise((resolve, reject) => { @@ -75,6 +76,10 @@ export class BetaMessageStream implements AsyncIterable this.#endPromise.catch(() => {}); } + get request_id(): string | null | undefined { + return this.#request_id; + } + /** * Returns the `MessageStream` data, the raw `Response` instance and the ID of the request, * returned vie the `request-id` header which is useful for debugging requests and resporting @@ -178,6 +183,7 @@ export class BetaMessageStream implements AsyncIterable protected _connected(response: Response | null) { if (this.ended) return; + this.#request_id = response?.headers.get('request-id'); this.#resolveConnectedPromise(response); this._emit('connect'); } diff --git a/src/lib/MessageStream.ts b/src/lib/MessageStream.ts index bace2cd9..65e84b29 100644 --- a/src/lib/MessageStream.ts +++ b/src/lib/MessageStream.ts @@ -55,6 +55,7 @@ export class MessageStream implements AsyncIterable { #errored = false; #aborted = false; #catchingPromiseCreated = false; + #request_id: string | null | undefined; constructor() { this.#connectedPromise = new Promise((resolve, reject) => { @@ -75,6 +76,10 @@ export class MessageStream implements AsyncIterable { this.#endPromise.catch(() => {}); } + get request_id(): string | null | undefined { + return this.#request_id; + } + /** * Returns the `MessageStream` data, the raw `Response` instance and the ID of the request, * returned vie the `request-id` header which is useful for debugging requests and resporting @@ -178,6 +183,7 @@ export class MessageStream implements AsyncIterable { protected _connected(response: Response | null) { if (this.ended) return; + this.#request_id = response?.headers.get('request-id'); this.#resolveConnectedPromise(response); this._emit('connect'); }