feat: Add onRequest callback to JS SDK (#4919)

Add onRequest callback
This commit is contained in:
William FH
2025-06-02 13:43:16 -07:00
committed by GitHub
parent 2563301f39
commit e2b14a9499
3 changed files with 20 additions and 5 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@langchain/langgraph-sdk",
"version": "0.0.79",
"version": "0.0.80",
"description": "Client library for interacting with the LangGraph API",
"type": "module",
"packageManager": "yarn@1.22.19",
+17 -3
View File
@@ -86,12 +86,18 @@ function getRunMetadataFromResponse(
};
}
export type RequestHook = (
url: URL,
init: RequestInit,
) => Promise<RequestInit> | RequestInit;
export interface ClientConfig {
apiUrl?: string;
apiKey?: string;
callerOptions?: AsyncCallerParams;
timeoutMs?: number;
defaultHeaders?: Record<string, string | null | undefined>;
onRequest?: RequestHook;
}
class BaseClient {
@@ -103,6 +109,8 @@ class BaseClient {
protected defaultHeaders: Record<string, string | null | undefined>;
protected onRequest?: RequestHook;
constructor(config?: ClientConfig) {
const callerOptions = {
maxRetries: 4,
@@ -136,6 +144,7 @@ class BaseClient {
// Regex to remove trailing slash, if present
this.apiUrl = config?.apiUrl?.replace(/\/$/, "") || defaultApiUrl;
this.defaultHeaders = config?.defaultHeaders || {};
this.onRequest = config?.onRequest;
const apiKey = getApiKey(config?.apiKey);
if (apiKey) {
this.defaultHeaders["X-Api-Key"] = apiKey;
@@ -230,9 +239,14 @@ class BaseClient {
withResponse?: boolean;
},
): Promise<T | [T, Response]> {
const response = await this.asyncCaller.fetch(
...this.prepareFetchOptions(path, options),
);
const [url, init] = this.prepareFetchOptions(path, options);
let finalInit = init;
if (this.onRequest) {
finalInit = await this.onRequest(url, init);
}
const response = await this.asyncCaller.fetch(url, finalInit);
const body = (() => {
if (response.status === 202 || response.status === 204) {
+2 -1
View File
@@ -1,4 +1,5 @@
export { Client } from "./client.js";
export { Client, getApiKey } from "./client.js";
export type { ClientConfig, RequestHook } from "./client.js";
export type {
AssistantBase,