MCPcopy Create free account
hub / github.com/bytebase/dbhub / fetchSource

Function fetchSource

frontend/src/api/sources.ts:38–71  ·  view source on GitHub ↗
(sourceId: string)

Source from the content-addressed store, hash-verified

36}
37
38export async function fetchSource(sourceId: string): Promise<DataSource> {
39 try {
40 // Validate sourceId to prevent path traversal attacks
41 if (!sourceId || sourceId.trim() === '') {
42 throw new ApiError('Source ID cannot be empty', 400);
43 }
44 if (sourceId.includes('/') || sourceId.includes('..')) {
45 throw new ApiError('Invalid source ID format', 400);
46 }
47
48 const response = await fetch(`${API_BASE}/sources/${encodeURIComponent(sourceId)}`);
49
50 if (!response.ok) {
51 const errorMessage = await parseJsonResponse<{ error: string }>(response)
52 .then((data) => data.error)
53 .catch(() => response.statusText);
54
55 throw new ApiError(
56 response.status === 404 ? `Source not found: ${sourceId}` : `Failed to fetch source: ${errorMessage}`,
57 response.status,
58 response.statusText
59 );
60 }
61
62 return response.json();
63 } catch (err) {
64 // Ensure all errors are ApiError instances
65 if (err instanceof ApiError) {
66 throw err;
67 }
68 // Network errors, timeout, etc.
69 throw new ApiError(err instanceof Error ? err.message : 'Network error', 0);
70 }
71}

Callers 2

ToolDetailViewFunction · 0.90
SourceDetailViewFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected