1
0
Fork 0
mirror of synced 2024-10-01 09:38:55 +13:00
budibase/packages/builder/src/builderStore/loadComponentLibraries.js

74 lines
2.4 KiB
JavaScript
Raw Normal View History

/**
* Fetches the definitions for component library components. This includes
* their props and other metadata from components.json.
* @param {string} clientId - ID of the current client
* @param {string} appId - ID of the currently running app
*/
export const fetchComponentLibDefinitions = async (clientId, appId) => {
const LIB_DEFINITION_URL = `/${clientId}/${appId}/components/definitions`;
try {
const libDefinitionResponse = await fetch(LIB_DEFINITION_URL);
return await libDefinitionResponse.json();
} catch (err) {
console.error(`Error fetching component definitions for ${appId}`, err);
}
};
/**
* 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 => {
const allLibraries = {}
for (let libraryName of application.componentLibraries) {
const LIBRARY_URL = `/${application._id}/componentlibrary?library=${libraryName}`;
const libraryModule = await import(LIBRARY_URL)
allLibraries[libraryName] = libraryModule
}
2019-08-20 08:18:23 +12:00
return allLibraries
2019-08-20 08:18:23 +12:00
}
2019-08-27 18:32:56 +12:00
// export const loadLibUrls = (appId, appPackage) => {
// const allLibraries = []
// for (let lib of libsFromPages(appPackage.pages)) {
// const libUrl = makeLibraryUrl(appId, lib)
// allLibraries.push({ libName: lib, importPath: libUrl })
// }
2019-09-22 16:02:33 +12:00
// return allLibraries
// }
2019-09-22 16:02:33 +12:00
// export const loadLib = async (appId, lib, allLibs) => {
// allLibs[lib] = await import(makeLibraryUrl(appId, lib))
// return allLibs
// }
2019-08-27 18:32:56 +12:00
// export const makeLibraryUrl = (appId, lib) =>
// `/_builder/${appId}/componentlibrary?lib=${encodeURI(lib)}`
// export const libsFromPages = pages =>
// pipe(pages, [values, map(p => p.componentLibraries), flatten, uniq])
// export const libUrlsForPreview = (appPackage, pageName) => {
// const resolve = path => {
// let file = appPackage.components.libraryPaths[path]
// if (file.startsWith("./")) file = file.substring(2)
// if (file.startsWith("/")) file = file.substring(1)
// let newPath = path
// if (!newPath.startsWith("./") && !newPath.startsWith("/")) {
// newPath = `/node_modules/${path}`
// }
// return {
// importPath: `/lib${newPath}/${file}`,
// libName: path,
// }
// }
// return pipe([appPackage.pages[pageName]], [libsFromPages, map(resolve)])
// }