// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. import { IncomingMessage } from 'node:http'; import { ClientOptions } from 'brand.dev'; import { McpOptions } from './options'; export const parseClientAuthHeaders = (req: IncomingMessage, required?: boolean): Partial => { if (req.headers.authorization) { const scheme = req.headers.authorization.split(' ')[0]!; const value = req.headers.authorization.slice(scheme.length + 1); switch (scheme) { case 'Bearer': return { apiKey: req.headers.authorization.slice('Bearer '.length) }; default: throw new Error( 'Unsupported authorization scheme. Expected the "Authorization" header to be a supported scheme (Bearer).', ); } } else if (required) { throw new Error('Missing required Authorization header; see WWW-Authenticate header for details.'); } const apiKey = Array.isArray(req.headers['x-brand-dev-api-key']) ? req.headers['x-brand-dev-api-key'][0] : req.headers['x-brand-dev-api-key']; return { apiKey }; }; export const getStainlessApiKey = (req: IncomingMessage, mcpOptions: McpOptions): string | undefined => { // Try to get the key from the x-stainless-api-key header const headerKey = Array.isArray(req.headers['x-stainless-api-key']) ? req.headers['x-stainless-api-key'][0] : req.headers['x-stainless-api-key']; if (headerKey && typeof headerKey === 'string') { return headerKey; } // Fall back to value set in the mcpOptions (e.g. from environment variable), if provided return mcpOptions.stainlessApiKey; };