leumas-universal-preset
Version:
A microservice to declare universal functions packed with reusable props via preset mappings.
42 lines (35 loc) • 1.37 kB
JavaScript
const { withPreset, mappings } = require('../index');
// Example function (simulate an API call, for instance).
const executeCell = async (
gridId,
layerName,
cellId,
environment = 'development',
ownerid = null,
ownerToken = null
) => {
console.log(`Executing cell ${layerName}.${cellId} on grid ${gridId}`);
console.log(`Environment: ${environment}, owner: ${ownerid}, token: ${ownerToken}`);
// Simulated response data
return { gridId, layerName, cellId, environment, ownerid, ownerToken };
};
// Define reusable preset values.
const presetValues = {
environment: 'production',
ownerid: 'defaultOwner',
ownerToken: 'defaultToken'
};
// Retrieve the default mapping for executeCell.
const executeCellMapping = mappings.executeCell.default;
// Wrap the executeCell function with preset values.
const executeCellProd = withPreset(executeCell, presetValues, {
mapping: executeCellMapping
});
// Test the wrapped function without overrides.
executeCellProd('grid123', 'layerA', 'cell45')
.then(data => console.log('Response:', data))
.catch(err => console.error(err));
// Test the wrapped function with an override (environment changed to 'staging').
executeCellProd('grid123', 'layerA', 'cell45', 'staging')
.then(data => console.log('Response:', data))
.catch(err => console.error(err));