Skip to content

Commit 2ddd628

Browse files
Fix provider filtering for browse/search in Excel add-in
1 parent d8534a2 commit 2ddd628

1 file changed

Lines changed: 23 additions & 3 deletions

File tree

src/shared/api.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ export async function searchSeries(apiKey: string | null, query: string, source?
263263
const headers: Record<string, string> = {};
264264
if (apiKey) headers.Authorization = `Bearer ${apiKey}`;
265265
let url = `${BASE_URL}${SEARCH_PATH}?q=${encodeURIComponent(query)}`;
266-
if (source) url += `&source=${encodeURIComponent(source)}`;
266+
if (source) url += `&q=${encodeURIComponent(source)}`; // keep query but also bias by source
267267
const response = await fetch(url, { headers });
268268
if (!response.ok) {
269269
return [];
@@ -282,14 +282,24 @@ export async function searchSeries(apiKey: string | null, query: string, source?
282282
export async function browseBySource(apiKey: string | null, source: string): Promise<SearchResult[]> {
283283
const headers: Record<string, string> = {};
284284
if (apiKey) headers.Authorization = `Bearer ${apiKey}`;
285-
const url = `${BASE_URL}${SEARCH_PATH}?source=${encodeURIComponent(source)}&limit=50`;
285+
const normalizedSource = normalizeSourceProvider(source);
286+
const url = `${BASE_URL}${SEARCH_PATH}?q=${encodeURIComponent(normalizedSource)}&limit=50`;
286287
const response = await fetch(url, { headers });
287288
if (!response.ok) {
288289
return [];
289290
}
290291
const body = await response.json();
291292
if (!body.results || !Array.isArray(body.results)) return [];
292-
return body.results.map((item: any) => ({
293+
const filtered = body.results.filter((item: any) => {
294+
const provider = (item.provider || item.source || '').toString().toUpperCase();
295+
return (
296+
provider === normalizedSource.toUpperCase() ||
297+
provider.includes(normalizedSource.toUpperCase()) ||
298+
normalizedSource.toUpperCase().includes(provider)
299+
);
300+
});
301+
const resultsToMap = filtered.length ? filtered : body.results;
302+
return resultsToMap.map((item: any) => ({
293303
id: item.id,
294304
title: item.title,
295305
frequency: item.frequency,
@@ -298,6 +308,16 @@ export async function browseBySource(apiKey: string | null, source: string): Pro
298308
}));
299309
}
300310

311+
function normalizeSourceProvider(source: string): string {
312+
const upper = source.trim().toUpperCase();
313+
const alias: Record<string, string> = {
314+
WORLDBANK: 'WB',
315+
WORLD_BANK: 'WB',
316+
WORLD_BANK_GROUP: 'WB',
317+
};
318+
return alias[upper] || upper;
319+
}
320+
301321
export function mapError(code: ErrorCode | undefined, status: number, fallback?: string): string {
302322
if (code === 'NO_KEY') return CONNECT_MESSAGE;
303323
if (code === 'INVALID_KEY') return 'Invalid API Key. Reconnect at datasetiq.com/dashboard/api-keys';

0 commit comments

Comments
 (0)