-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from openobserve/feature/dynemic-data
Feature/dynemic data
- Loading branch information
Showing
8 changed files
with
106 additions
and
177 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
interface ImportMetaEnv { | ||
readonly PUBLIC_API_BASE_URL: string; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
interface Props { | ||
endpoint: string; | ||
query?: Record<string, string>; | ||
wrappedByKey?: string; | ||
wrappedByList?: boolean; | ||
token?: string; // Optional authorization token | ||
} | ||
|
||
/** | ||
* Fetches data from the Strapi API | ||
* @param endpoint - The endpoint to fetch from | ||
* @param query - The query parameters to add to the URL | ||
* @param wrappedByKey - The key to unwrap the response from | ||
* @param wrappedByList - If the response is a list, unwrap it | ||
* @returns | ||
*/ | ||
export default async function fetchApi<T>({ | ||
endpoint, | ||
query, | ||
wrappedByKey, | ||
wrappedByList, | ||
}: Props): Promise<T> { | ||
// Construct the base URL | ||
const baseUrl = import.meta.env.PUBLIC_API_BASE_URL; | ||
let url = new URL(`${baseUrl}/${endpoint}`); | ||
|
||
// Append query parameters if provided | ||
if (query) { | ||
Object.entries(query).forEach(([key, value]) => { | ||
url.searchParams.append(key, value); | ||
}); | ||
} | ||
|
||
|
||
const headers: HeadersInit = { | ||
"Content-Type": "application/json", | ||
}; | ||
|
||
// Use token from function parameter OR environment variable | ||
const authToken = import.meta.env.PUBLIC_TOKEN; | ||
if (authToken) { | ||
headers["Authorization"] = `Bearer ${authToken}`; | ||
} | ||
|
||
try { | ||
const res = await fetch(url.toString(), { | ||
method: "GET", | ||
headers, | ||
}); | ||
|
||
if (!res.ok) { | ||
throw new Error(`Error fetching data: ${res.status} ${res.statusText}`); | ||
} | ||
|
||
let data = await res.json(); | ||
|
||
if (wrappedByKey) { | ||
data = data[wrappedByKey]; | ||
} | ||
|
||
if (wrappedByList) { | ||
data = data[0]; | ||
} | ||
|
||
return data as T; | ||
} catch (error) { | ||
console.error("Fetch API Error:", error); | ||
throw error; | ||
} | ||
} |