Skip to content

The open-source agent harness - the runtime layer that turns an LLM into a working agent.

1.4 KB
Raw Blame
// This file was auto-generated by Fern from our API Definition.

import * as core from "../core/index.js";

const TOKEN_PARAM = "token" as const;

export class BearerAuthProvider implements core.AuthProvider {
    private readonly options: BearerAuthProvider.Options;

    constructor(options: BearerAuthProvider.Options) {
        this.options = options;
    }

    public static canCreate(options: Partial<BearerAuthProvider.Options>): boolean {
        return options?.[TOKEN_PARAM] != null;
    }

    public async getAuthRequest({
        endpointMetadata,
    }: {
        endpointMetadata?: core.EndpointMetadata;
    } = {}): Promise<core.AuthRequest> {
        const token = await core.Supplier.get(this.options[TOKEN_PARAM]);
        if (token == null) {
            return { headers: {} };
        }

        return {
            headers: { Authorization: `Bearer ${token}` },
        };
    }
}

export namespace BearerAuthProvider {
    export const AUTH_SCHEME = "BearerAuth" as const;
    export const AUTH_CONFIG_ERROR_MESSAGE: string =
        `Please provide '${TOKEN_PARAM}' when initializing the client` as const;
    export type Options = AuthOptions;
    export type AuthOptions = { [TOKEN_PARAM]?: core.Supplier<core.BearerToken> };

    export function createInstance(options: Options): core.AuthProvider {
        return new BearerAuthProvider(options);
    }
}