Skip to content
Merged
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
12 changes: 10 additions & 2 deletions packages/spockify-ide-client/src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,21 @@ export function normalizeBaseUrl(baseUrl?: string): string {
return normalizeProductBaseUrl(baseUrl);
}

/** Strip a leading "Bearer" prefix (case-insensitive) and trim. */
function normalizeApiKey(raw: string | undefined | null): string {
return String(raw || '')
.replace(/^Bearer\s+/i, '')
.trim();
}

export class SpockifyHttpClient {
readonly baseUrl: string;
private readonly apiKey: string;
private readonly fetchImpl: typeof globalThis.fetch;

constructor(options: SpockifyClientOptions = {}) {
this.baseUrl = normalizeBaseUrl(options.baseUrl);
this.apiKey = (options.apiKey || '').trim();
this.apiKey = normalizeApiKey(options.apiKey);
this.fetchImpl = options.fetch ?? globalThis.fetch.bind(globalThis);
}

Expand All @@ -110,9 +117,10 @@ export class SpockifyHttpClient {
return `${this.baseUrl}/v1`;
}

authHeaders(init?: HeadersInit): Headers {
authHeaders(init?: HeadersInit): Headers {
const headers = new Headers(init);
if (this.apiKey) {
// Always a single "Bearer " prefix; never "Bearer Bearer sk-…"
headers.set('Authorization', `Bearer ${this.apiKey}`);
}
if (!headers.has('X-Spockify-Client')) {
Expand Down
Loading