Skip to content
This repository has been archived by the owner on May 1, 2020. It is now read-only.

Commit

Permalink
Merge pull request #8 from Pavel910/master
Browse files Browse the repository at this point in the history
Add support for custom method invocation on a template file.
  • Loading branch information
eahefnawy authored Oct 21, 2019
2 parents 02bd3ce + 57119e0 commit 190adf2
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
18 changes: 17 additions & 1 deletion serverless.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,26 @@ const {
createGraph,
executeGraph,
syncState,
getOutputs
getOutputs,
createCustomMethodHandler
} = require('./utils')

class Template extends Component {
constructor(id, context) {
const defaultFunction = super(id, context)

return new Proxy(defaultFunction, {
get: (obj, prop) => {
if (obj.hasOwnProperty(prop)) {
return obj[prop]
}

// Return a function that will invoke the custom method on requested components
return createCustomMethodHandler(obj, prop)
}
})
}

async default(inputs = {}) {
this.context.status('Deploying')

Expand Down
50 changes: 49 additions & 1 deletion utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,53 @@ const syncState = async (allComponents, instance) => {
await instance.save()
}

const createCustomMethodHandler = (instance, method) => {
return async ({ component, template, ...inputs }) => {
let components = Array.isArray(component) ? component : [component]
components = components.filter(Boolean)

if (!components.length) {
throw Error(`"component" input is required to run custom methods`)
}

instance.context.debug(
`Attempting to run method "${method}" on template aliases: ${components.join(', ')}`
)
// Load template components
const templateComponents = await getAllComponents(template)

// Get only the requested components ("component" input)
const componentsToRun = Object.keys(templateComponents)
.filter((alias) => components.includes(alias))
.reduce((acc, item) => {
acc.push({ alias: item, path: templateComponents[item].path })
return acc
}, [])

// Verify all the components have the requested method implemented
instance.context.debug(`Verifying presence of method "${method}" in requested components...`)
for (let i = 0; i < componentsToRun.length; i++) {
const item = componentsToRun[i]
const cmp = await instance.load(item.path, item.alias)
if (typeof cmp[method] !== 'function') {
throw Error(`method "${method}" not found in "${item.path}"`)
}
// Store the loaded component so we don't have to load it again
componentsToRun[i].component = cmp
}

// Run custom method and return output
const outputs = {}
for (let i = 0; i < componentsToRun.length; i++) {
const cmp = componentsToRun[i]
instance.context.debug(`Running method "${method}" on "${cmp.path}"...`)
outputs[cmp.alias] = await cmp.component[method](inputs)
}

return outputs
}
}

module.exports = {
getTemplate,
resolveTemplate,
Expand All @@ -350,5 +397,6 @@ module.exports = {
createGraph,
executeGraph,
syncState,
getOutputs
getOutputs,
createCustomMethodHandler
}

0 comments on commit 190adf2

Please sign in to comment.