Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/__tests__/x-glean.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@ import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { XGlean } from "../hooks/x-glean.js";
import { BeforeRequestContext } from "../hooks/types.js";
import { SDKOptions } from "../lib/config.js";
import { XGleanOptions } from "../hooks/x-glean-options.js";

function createMockRequest(): Request {
return new Request("https://example.com/api/test");
}

function createMockContext(options: SDKOptions = {}): BeforeRequestContext {
/**
* Creates a mock BeforeRequestContext for testing.
* Uses intersection type to support both base SDKOptions and X-Glean custom options.
*/
function createMockContext(
options: SDKOptions & XGleanOptions = {},
): BeforeRequestContext {
return {
baseURL: "https://example.com",
operationID: "test-operation",
Expand Down
29 changes: 29 additions & 0 deletions src/hooks/x-glean-options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* X-Glean custom options for configuring deprecation and experimental headers.
*
* These properties allow configuring X-Glean headers through the SDK constructor.
* Since SDKOptions in lib/config.ts is generated by Speakeasy and gets overwritten
* during regeneration, we define these as a separate type that can be used with
* intersection types.
*/

/**
* Custom SDK options for X-Glean header configuration.
* Use these options when constructing the SDK client to configure
* experimental features and deprecation testing.
*/
export interface XGleanOptions {
/**
* Exclude API endpoints that will be deprecated after this date.
* Use this to test your integration against upcoming deprecations.
* Format: YYYY-MM-DD (e.g., '2026-10-15')
*
* More information: https://developers.glean.com/deprecations/overview
*/
excludeDeprecatedAfter?: string | undefined;
/**
* When true, enables experimental API features that are not yet generally available.
* Use this to preview and test new functionality.
*/
includeExperimental?: boolean | undefined;
}
9 changes: 7 additions & 2 deletions src/hooks/x-glean.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BeforeRequestContext, BeforeRequestHook } from "./types.js";
import { XGleanOptions } from "./x-glean-options.js";

/**
* Get the first non-empty value from the provided arguments.
Expand All @@ -14,14 +15,18 @@ function getFirstValue(

export class XGlean implements BeforeRequestHook {
beforeRequest(hookCtx: BeforeRequestContext, request: Request): Request {
// Cast options to include X-Glean custom properties
// These properties may be passed by users but aren't in the generated SDKOptions type
const options = hookCtx.options as XGleanOptions;

const deprecatedValue = getFirstValue(
process.env["X_GLEAN_EXCLUDE_DEPRECATED_AFTER"],
hookCtx.options.excludeDeprecatedAfter,
options.excludeDeprecatedAfter,
);

const experimentalValue = getFirstValue(
process.env["X_GLEAN_INCLUDE_EXPERIMENTAL"],
hookCtx.options.includeExperimental === true ? "true" : undefined,
options.includeExperimental === true ? "true" : undefined,
);

if (deprecatedValue) {
Expand Down
13 changes: 0 additions & 13 deletions src/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,6 @@ export type SDKOptions = {
retryConfig?: RetryConfig;
timeoutMs?: number;
debugLogger?: Logger;
/**
* Exclude API endpoints that will be deprecated after this date.
* Use this to test your integration against upcoming deprecations.
* Format: YYYY-MM-DD (e.g., '2026-10-15')
*
* More information: https://developers.glean.com/deprecations/overview
*/
excludeDeprecatedAfter?: string | undefined;
/**
* When true, enables experimental API features that are not yet generally available.
* Use this to preview and test new functionality.
*/
includeExperimental?: boolean | undefined;
};

export function serverURLFromOptions(options: SDKOptions): URL | null {
Expand Down