@mapvx/website-component-helpers
v0.1.1
Published
This package contains a helper function to prepare the initial data to pass to the website component.
Readme
Website Component Helper
This package contains a helper function to prepare the initial data to pass to the website component.
Usage
prepareInitialData(apiKey: string): Promise<string>
This function fetches and prepares the initial data required by the website component. It makes parallel requests to fetch available places and institutions data from the Mapvx API.
Parameters
apiKey(string): Your Mapvx API key for authentication
Returns
Promise<string>: A JSON string containing the initial data with the following structure:{ availablePlaces: unknown[] | null, institutions: unknown[] | null }
Example
import { prepareInitialData } from '@mapvx/website-component-helper'
async function initializeComponent() {
try {
const initialData = await prepareInitialData('your-api-key-here')
// Pass the initialData to your website component
console.log('Initial data prepared:', initialData)
} catch (error) {
console.error('Failed to prepare initial data:', error)
}
}Error Handling
- If any API request fails, the function will log the error and return
nullfor the failed data - The function uses
Promise.all()to fetch both data sources in parallel for better performance - Network errors and API errors are logged to the console
