Skip to content

Commit ecdc430

Browse files
committed
Refactor baseUrl handling in LangflowClient constructor and add tests for missing baseUrl scenarios
If baseUrl is provided as an property on the opts, but is it undefined or null, this is likely not the intention. Previously the baseUrl would get set to the DATASTAX_LANGFLOW_BASE_URL and the instantiation would fail due to a missing langflowId. Now it will throw an more useful error, unless langflowId and apiKey are present.
1 parent 8cfb007 commit ecdc430

3 files changed

Lines changed: 83 additions & 1 deletion

File tree

‎CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Ongoing [☰](https://github.com/datastax/langflow-client-ts/compare/v0.2.1...main)
44

5+
### Changed
6+
7+
- When you explicitly try to set the `baseUrl` for a client, but it is undefined, null or empty, it now throws an error with a better explanation.
8+
59
## 0.3.0 (May 30, 2025) [☰](https://github.com/datastax/langflow-client-ts/compare/v0.2.1...v0.3.0)
610

711
### Added

‎src/index.ts‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export class LangflowClient {
3434
logs: Logs;
3535

3636
constructor(opts: LangflowClientOptions) {
37-
this.baseUrl = opts.baseUrl ?? DATASTAX_LANGFLOW_BASE_URL;
37+
this.baseUrl = this.#resolveBaseUrl(opts);
3838
this.basePath = "/api";
3939
this.langflowId = opts.langflowId;
4040
this.apiKey = opts.apiKey;
@@ -63,6 +63,35 @@ export class LangflowClient {
6363
this.logs = new Logs(this);
6464
}
6565

66+
#resolveBaseUrl(opts: LangflowClientOptions) {
67+
// If baseUrl is provided as an property on the opts, but is it undefined or
68+
// null, this is likely not the intention.
69+
// Previously the baseUrl would get set to the DATASTAX_LANGFLOW_BASE_URL
70+
// and the instantiation would fail due to a missing langflowId. Now it will
71+
// throw an more useful error, unless langflowId and apiKey are present.
72+
if (Object.hasOwn(opts, "baseUrl")) {
73+
const isBaseUrlEffectivelyEmpty =
74+
typeof opts.baseUrl === "undefined" ||
75+
opts.baseUrl === null ||
76+
(typeof opts.baseUrl === "string" && opts.baseUrl.trim() === "");
77+
78+
if (isBaseUrlEffectivelyEmpty) {
79+
// BaseUrl is explicitly provided but is empty/undefined/null.
80+
// If langflowId and apiKey are also provided, assume DataStax
81+
// connection and use the default DataStax URL.
82+
if (opts.langflowId && opts.apiKey) {
83+
return DATASTAX_LANGFLOW_BASE_URL;
84+
}
85+
// Otherwise, this is an invalid configuration.
86+
throw new TypeError(
87+
`You are trying to set baseUrl, but the value is '${String(opts.baseUrl)}'.`
88+
);
89+
}
90+
// If opts.baseUrl is explicitly set and not effectively empty, it will be used by the return statement below.
91+
}
92+
return opts.baseUrl ?? DATASTAX_LANGFLOW_BASE_URL;
93+
}
94+
6695
#isDataStax(): boolean {
6796
return this.baseUrl === DATASTAX_LANGFLOW_BASE_URL;
6897
}

‎src/test/index.test.ts‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,4 +542,53 @@ describe("LangflowClient", () => {
542542
});
543543
});
544544
});
545+
546+
describe("with a missing baseUrl", () => {
547+
const apiKey = "my-api-key";
548+
const langflowId = "my-langflow-id";
549+
550+
[undefined, ""].forEach((baseUrl) => {
551+
it(`throws an error baseUrl is set to '${String(baseUrl)}'`, () => {
552+
try {
553+
new LangflowClient({ baseUrl });
554+
} catch (error) {
555+
assert.ok(error instanceof TypeError);
556+
assert.match(
557+
error.message,
558+
/You are trying to set baseUrl, but the value is/
559+
);
560+
}
561+
});
562+
563+
it(`throws an error when baseUrl is set to '${String(baseUrl)}' and only langflowId is provided`, () => {
564+
try {
565+
new LangflowClient({ baseUrl, langflowId });
566+
} catch (error) {
567+
assert.ok(error instanceof TypeError);
568+
assert.match(
569+
error.message,
570+
/You are trying to set baseUrl, but the value is/
571+
);
572+
}
573+
});
574+
575+
it(`throws an error when baseUrl is set to '${String(baseUrl)}' and only apiKey is provided`, () => {
576+
try {
577+
new LangflowClient({ baseUrl, apiKey });
578+
} catch (error) {
579+
assert.ok(error instanceof TypeError);
580+
assert.match(
581+
error.message,
582+
/You are trying to set baseUrl, but the value is/
583+
);
584+
}
585+
});
586+
587+
it(`doesn't throw when baseUrl is set to '${String(baseUrl)}' and langflowId and apiKey are provided`, () => {
588+
assert.doesNotThrow(() => {
589+
new LangflowClient({ baseUrl, apiKey, langflowId });
590+
});
591+
});
592+
});
593+
});
545594
});

0 commit comments

Comments
 (0)