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} 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);
|
|
|
|
}
|
|
|
|
};
|
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) {
|
|
|
|
const LIBRARY_URL = `/${application._id}/componentlibrary?library=${libraryName}`;
|
|
|
|
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
|
|
|
}
|
2019-08-27 08:32:56 +02:00
|
|
|
|
2020-05-02 16:29:10 +02: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 06:02:33 +02:00
|
|
|
|
2020-05-02 16:29:10 +02:00
|
|
|
// return allLibraries
|
|
|
|
// }
|
2019-09-22 06:02:33 +02:00
|
|
|
|
2020-05-02 16:29:10 +02:00
|
|
|
// export const loadLib = async (appId, lib, allLibs) => {
|
|
|
|
// allLibs[lib] = await import(makeLibraryUrl(appId, lib))
|
|
|
|
// return allLibs
|
|
|
|
// }
|
2019-08-27 08:32:56 +02:00
|
|
|
|
2020-05-02 16:29:10 +02:00
|
|
|
// export const makeLibraryUrl = (appId, lib) =>
|
|
|
|
// `/_builder/${appId}/componentlibrary?lib=${encodeURI(lib)}`
|
2020-02-19 14:58:06 +01:00
|
|
|
|
2020-05-02 16:29:10 +02:00
|
|
|
// export const libsFromPages = pages =>
|
|
|
|
// pipe(pages, [values, map(p => p.componentLibraries), flatten, uniq])
|
2020-03-11 17:42:19 +01:00
|
|
|
|
2020-05-02 16:29:10 +02:00
|
|
|
// 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)
|
2020-03-11 17:42:19 +01:00
|
|
|
|
2020-05-02 16:29:10 +02:00
|
|
|
// let newPath = path
|
2020-03-11 17:42:19 +01:00
|
|
|
|
2020-05-02 16:29:10 +02:00
|
|
|
// if (!newPath.startsWith("./") && !newPath.startsWith("/")) {
|
|
|
|
// newPath = `/node_modules/${path}`
|
|
|
|
// }
|
2020-03-11 17:42:19 +01:00
|
|
|
|
2020-05-02 16:29:10 +02:00
|
|
|
// return {
|
|
|
|
// importPath: `/lib${newPath}/${file}`,
|
|
|
|
// libName: path,
|
|
|
|
// }
|
|
|
|
// }
|
2020-03-11 17:42:19 +01:00
|
|
|
|
2020-05-02 16:29:10 +02:00
|
|
|
// return pipe([appPackage.pages[pageName]], [libsFromPages, map(resolve)])
|
|
|
|
// }
|