Skip to content

Add server URL normalization for flexible serverURL input#109

Merged
chris-freeman-glean merged 4 commits intomainfrom
cfreeman/server-url-normalization
Mar 4, 2026
Merged

Add server URL normalization for flexible serverURL input#109
chris-freeman-glean merged 4 commits intomainfrom
cfreeman/server-url-normalization

Conversation

@chris-freeman-glean
Copy link
Contributor

Summary

Adds server URL normalization so users can pass schemeless URLs (e.g., "mycompany-be.glean.com") as serverURL when constructing the SDK client. This is part of promoting serverURL as the primary configuration option, replacing instance in all documentation and examples.

TypeScript-specific implementation

The TypeScript SDK required a two-layer approach due to how Speakeasy generates the TypeScript target:

  1. SDKInit hook (src/hooks/server-url-normalizer.ts): Normalizes URLs that already have a scheme (strips trailing slashes, preserves http/https). This is consistent with the hook-based approach used in Go, Python, and Java SDKs.

  2. Custom Code edit to config.ts (src/lib/config.ts): The Speakeasy-generated serverURLFromOptions() calls new URL() before the SDKInit hook runs. Unlike Go/Python/Java where the hook receives a raw string, the TypeScript hook receives a parsed URL object — so schemeless URLs throw TypeError: Invalid URL before the hook can normalize them. This edit adds normalization directly in serverURLFromOptions(), before new URL() is called. Speakeasy's Custom Code 3-way merge preserves this edit across regenerations.

Normalization rules

  • No scheme → prepend https://
  • http:// preserved (for localhost/dev)
  • https:// preserved
  • Trailing slashes stripped

User-facing API

// All of these now work identically:
const glean = new Glean({ serverURL: "mycompany-be.glean.com" });
const glean = new Glean({ serverURL: "https://mycompany-be.glean.com" });
const glean = new Glean({ serverURL: "https://mycompany-be.glean.com/" });

Backwards compatibility

The instance parameter continues to work unchanged. No breaking changes.

Test plan

  • 15 unit tests pass (8 normalizeServerURL, 2 hook, 5 serverURLFromOptions)
  • Full integration test suite passes (120 pass, 61 skipped, 0 failed)

Add SDKInit hook that normalizes server URLs: strips trailing slashes
and preserves http/https schemes. Consistent with Go/Python/Java SDKs.
The Speakeasy-generated serverURLFromOptions() calls new URL() which
throws on schemeless input like "mycompany-be.glean.com". This edit
adds normalization (prepend https://, strip trailing slashes) before
the URL constructor, matching the behavior of Go/Python/Java SDKs.

This is a Custom Code edit to a generated file — Speakeasy's 3-way
merge will preserve it across regenerations.
@chris-freeman-glean chris-freeman-glean marked this pull request as ready for review March 3, 2026 22:38
@chris-freeman-glean chris-freeman-glean requested a review from a team as a code owner March 3, 2026 22:38
…t edits

- Update README preamble and Experimental Features examples to use
  serverURL instead of instance
- Update indexing.test-d.ts to use serverURL/GLEAN_SERVER_URL
- Enable persistentEdits in gen.yaml to preserve the config.ts
  schemeless URL normalization across Speakeasy regenerations
@chris-freeman-glean chris-freeman-glean merged commit 908c78f into main Mar 4, 2026
3 checks passed
@chris-freeman-glean chris-freeman-glean deleted the cfreeman/server-url-normalization branch March 4, 2026 17:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant