2020-06-03 21:44:35 +02:00
|
|
|
import { get } from "builderStore/api"
|
2020-06-03 21:35:30 +02:00
|
|
|
|
2020-05-02 16:29:10 +02:00
|
|
|
/**
|
|
|
|
* Fetches the definitions for component library components. This includes
|
|
|
|
* their props and other metadata from components.json.
|
|
|
|
* @param {string} appId - ID of the currently running app
|
|
|
|
*/
|
2020-05-18 07:40:29 +02:00
|
|
|
export const fetchComponentLibDefinitions = async appId => {
|
|
|
|
const LIB_DEFINITION_URL = `/${appId}/components/definitions`
|
2020-05-02 16:29:10 +02:00
|
|
|
try {
|
2020-06-03 21:35:30 +02:00
|
|
|
const libDefinitionResponse = await get(LIB_DEFINITION_URL)
|
2020-05-07 11:53:34 +02:00
|
|
|
return await libDefinitionResponse.json()
|
2020-05-02 16:29:10 +02:00
|
|
|
} catch (err) {
|
2020-05-07 11:53:34 +02:00
|
|
|
console.error(`Error fetching component definitions for ${appId}`, err)
|
2020-05-02 16:29:10 +02:00
|
|
|
}
|
2020-05-07 11:53:34 +02:00
|
|
|
}
|
2020-02-19 14:58:06 +01:00
|
|
|
|
2020-05-02 16:29:10 +02:00
|
|
|
/**
|
|
|
|
* Loads the JavaScript files for app component libraries and returns a map of their modules.
|
|
|
|
* @param {object} application - package definition
|
|
|
|
*/
|
|
|
|
export const fetchComponentLibModules = async application => {
|
2020-02-03 10:24:25 +01:00
|
|
|
const allLibraries = {}
|
2020-02-19 14:58:06 +01:00
|
|
|
|
2020-05-02 16:29:10 +02:00
|
|
|
for (let libraryName of application.componentLibraries) {
|
2020-05-07 11:53:34 +02:00
|
|
|
const LIBRARY_URL = `/${application._id}/componentlibrary?library=${libraryName}`
|
2020-05-02 16:29:10 +02:00
|
|
|
const libraryModule = await import(LIBRARY_URL)
|
|
|
|
allLibraries[libraryName] = libraryModule
|
2020-02-03 10:24:25 +01:00
|
|
|
}
|
2019-08-19 22:18:23 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
return allLibraries
|
2019-08-19 22:18:23 +02:00
|
|
|
}
|