src/auth/BearerAuthProvider.ts
Rama main · réplica de lectura del repositorio original.
// This file was auto-generated by Fern from our API Definition.
import * as core from "../core/index.js";
import * as errors from "../errors/index.js";
const TOKEN_PARAM = "apiKey" as const;
const ENV_TOKEN = "AGENTMAIL_API_KEY" 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 || process.env?.[ENV_TOKEN] != null;
}
public async getAuthRequest({
endpointMetadata,
}: {
endpointMetadata?: core.EndpointMetadata;
} = {}): Promise<core.AuthRequest> {
const apiKey = (await core.Supplier.get(this.options[TOKEN_PARAM])) ?? process.env?.[ENV_TOKEN];
if (apiKey == null) {
throw new errors.AgentMailError({
message: BearerAuthProvider.AUTH_CONFIG_ERROR_MESSAGE,
});
}
return {
headers: { Authorization: `Bearer ${apiKey}` },
};
}
}
export namespace BearerAuthProvider {
export const AUTH_SCHEME = "Bearer" as const;
export const AUTH_CONFIG_ERROR_MESSAGE: string =
`Please provide '${TOKEN_PARAM}' when initializing the client, or set the '${ENV_TOKEN}' environment variable` as const;
export type Options = AuthOptions;
export type AuthOptions = { [TOKEN_PARAM]?: core.Supplier<core.BearerToken> | undefined };
export function createInstance(options: Options): core.AuthProvider {
return new BearerAuthProvider(options);
}
}