-
-
Notifications
You must be signed in to change notification settings - Fork 229
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🎉 (entity selector) sort by external indicators
- Loading branch information
1 parent
c4170b8
commit 28a40e3
Showing
8 changed files
with
402 additions
and
71 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,44 @@ | ||
import { OwidVariableDataMetadataDimensions } from "@ourworldindata/types" | ||
|
||
export const getVariableDataRoute = ( | ||
dataApiUrl: string, | ||
variableId: number | ||
): string => { | ||
if (dataApiUrl.includes("v1/indicators/")) { | ||
// fetching from Data API, e.g. https://api.ourworldindata.org/v1/indicators/123.data.json | ||
return `${dataApiUrl}${variableId}.data.json` | ||
} else { | ||
throw new Error(`dataApiUrl format not supported: ${dataApiUrl}`) | ||
} | ||
} | ||
|
||
export const getVariableMetadataRoute = ( | ||
dataApiUrl: string, | ||
variableId: number | ||
): string => { | ||
if (dataApiUrl.includes("v1/indicators/")) { | ||
// fetching from Data API, e.g. https://api.ourworldindata.org/v1/indicators/123.metadata.json | ||
return `${dataApiUrl}${variableId}.metadata.json` | ||
} else { | ||
throw new Error(`dataApiUrl format not supported: ${dataApiUrl}`) | ||
} | ||
} | ||
|
||
export async function loadVariableDataAndMetadata( | ||
variableId: number, | ||
dataApiUrl: string | ||
): Promise<OwidVariableDataMetadataDimensions> { | ||
const dataPromise = fetch(getVariableDataRoute(dataApiUrl, variableId)) | ||
const metadataPromise = fetch( | ||
getVariableMetadataRoute(dataApiUrl, variableId) | ||
) | ||
const [dataResponse, metadataResponse] = await Promise.all([ | ||
dataPromise, | ||
metadataPromise, | ||
]) | ||
if (!dataResponse.ok) throw new Error(dataResponse.statusText) | ||
if (!metadataResponse.ok) throw new Error(metadataResponse.statusText) | ||
const data = await dataResponse.json() | ||
const metadata = await metadataResponse.json() | ||
return { data, metadata } | ||
} |
Oops, something went wrong.