@lyra/export
Version:
Export Lyra documents and assets
52 lines (40 loc) • 1.44 kB
JavaScript
const defaults = require('lodash/defaults');
const clientMethods = ['getUrl', 'config'];
const booleanFlags = ['assets', 'raw', 'compress', 'drafts'];
const exportDefaults = {
compress: true,
drafts: true,
assets: true,
raw: false
};
function validateOptions(opts) {
const options = defaults({}, opts, exportDefaults);
if (typeof options.dataset !== 'string' || options.dataset.length < 1) {
throw new Error(`options.dataset must be a valid dataset name`);
}
if (options.onProgress && typeof options.onProgress !== 'function') {
throw new Error(`options.onProgress must be a function`);
}
if (!options.client) {
throw new Error('`options.client` must be set to an instance of @lyra/client');
}
const missing = clientMethods.find(key => typeof options.client[key] !== 'function');
if (missing) {
throw new Error(`\`options.client\` is not a valid @lyra/client instance - no "${missing}" method found`);
}
const clientConfig = options.client.config();
if (!clientConfig.token) {
throw new Error('Client is not instantiated with a `token`');
}
booleanFlags.forEach(flag => {
if (typeof options[flag] !== 'boolean') {
throw new Error(`Flag ${flag} must be a boolean (true/false)`);
}
});
if (!options.outputPath) {
throw new Error('outputPath must be specified (- for stdout)');
}
return options;
}
module.exports = validateOptions;
;