@360works/fmpromise
Version:
A modern JS toolkit for FileMaker Web Viewers, including a dev server and type generation.
47 lines (46 loc) • 1.8 kB
JavaScript
import { validateConfig } from './validation';
function renderPermanentError(errors) {
const errorDiv = document.createElement('div');
errorDiv.innerHTML = `
<h3>Module Configuration Error</h3>
<p>This module is not configured correctly. Please contact your developer.</p>
<ul>${errors.map(e => `<li>${e}</li>`).join('')}</ul>
`;
Object.assign(errorDiv.style, {
padding: '1em',
border: '2px solid red',
backgroundColor: '#ffeeee',
margin: '1em',
});
document.body.innerHTML = '';
document.body.appendChild(errorDiv);
}
/**
* The primary configuration gatekeeper for an fmPromise module.
* It validates pre-injected configuration and provides a UI for developers in dev mode.
* @param schema The configuration schema for the module.
* @returns A promise that resolves with the valid, typed settings object, or never resolves if configuration is needed.
*/
export async function useConfig(schema) {
const config = window.FMPROMISE_CONFIG;
const isDevMode = window.location.protocol !== 'data:';
const validationResult = validateConfig(config, schema);
if (validationResult.isValid) {
// Happy Path: Config is valid, resolve immediately.
return Promise.resolve(config);
}
// Unhappy Path: Config is invalid.
if (!isDevMode) {
// Production: Show a permanent error and halt.
renderPermanentError(validationResult.errors);
return new Promise(() => {
}); // A promise that never resolves
}
else {
// Development: Lazily load the UI and show the config prompt.
const { showConfigPrompt } = await import('./ui');
showConfigPrompt(schema);
return new Promise(() => {
}); // A promise that never resolves
}
}