npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

serverless-template-function-calls

v0.1.4

Published

serverless framework plugin for using javascript functions in templates

Downloads

6

Readme

Serverless Plugin Template Function Calls

This package is a plugin for the serverless framework. It provides an extension to the serverless template files to include function calls to javascript node modules.

Installation

Install the package with npm via npm install serverless-template-function-calls and add serverless-template-function-calls to the plugins list. For Yaml that would look like this:

plugins:
   - serverless-template-function-calls

Usage

Functions are loaded from in the config specified module files and support nested functions in objects. Long function names are resolved via the dot notation. Short function names are available as aliases via the name behind the last dot (long name: module.nested.func1, short name: func1) (be aware that long function names will replace short ones in case of identical names).

There are two ways functions are called:

Inline Replacement:

In the template string values will be replaced if a string has the format [function_prefix][function_name]("[arg1]", "[arg2]", ...) by the function return value if supplied with the arguments.

Example: prop: +module.func1("hello ")

Object Replacement:

In the template if the child key of an object has the format [function_prefix][function_name](...) the parent will be replaced by the function return value if supplied with the child value as the argument.

Example:

prop:
	+module.func1(...): "hello "

Example

Modules

single_function.js

module.exports = () => "hello";

multi_function.js

module.exports.func0 = (a) => paresInt(a) * 2,
module.exports.some_namespace = {
   func1: (x, y) => x + " and " + y,
   nested_namespace: {
      func2: (a, b) => ({ a, b }),
      func4: ({a1, a2}) => a1 + a2
   }
};

Input Template (in YAML)

custom:
   ref_prop: 1000
   some_prop: +single_function()
   other_prop: '+multi_function.some_namespace.nested_namespace.func2("valA", "valB")'
   nested_prop:
      - something
      - +multi_function.func0("${self:custom.ref_prop}")
   object_prop:
	   +multi_function.some_namespace.nested_namespace.func4(...):
		   a1: 5
		   a2: 10

which is equivalent to (with short function names)

custom:
   ref_prop: 1000
   some_prop: +single_function()
   other_prop: '+func2("valA", "valB")'
   nested_prop:
      - something
      - +func0("${self:custom.ref_prop}")
   object_prop:
	   +func4(...):
		   a1: 5
		   a2: 10

Output Template (in YAML)

custom:
   ref_prop: 1000
   some_prop: "hello"
   other_prop:
      a: "valA"
      b: "valB"
   nested_prop:
      - "something"
      - 2000
   object_prop: 15

Also note that strings are trimmed for the interpretation as a function call string.

Configuration

The configuration is read from the serverless template in the custom.templateFunctionCalls object. Like this:

custom:
   templateFunctionCalls:
      some_config_property: some_config_value
      ...

The possible properties and values follow now.

Properties

modules (default value: [ ], expected type: string array)

These are the module files from which the functions are read. The paths have to be specified relative to the template file or absolute and need to be conforment to the require node syntax (.js can therefore be omitted).

functionPrefix (default value: "+", expected type: string)

The prefix that needs to be before any function name in order to be recognized. Can be any string (including "") .

roots (default value: ["resources", "functions", "custom", "layers", "service", "provider", "plugins", "outputs"], expected type: string array)

These are the template root properties to which the replacement of function call strings are applied.

Example Config

In this example config, functions would be recognized without having any prefix. The functions are loaded from module1.js and the module file in the dir2 directory. And any function string call on properties other from resources are ignored. Therefore the template:

custom:
   templateFunctionCalls:
         functionPrefix: ""
         modules: ["module1.js", "dir2/module"]
         roots: ["resources"]

   some_prop: module1.hello()
   other_prop: module.test()

resources:
   Resources:
      resource1: ${self:custom.some_prop}
      resource2: module.test()

assuming module1.hello() = 5 and module.test() = "some string", the template would result in:

custom:
   templateFunctionCalls:
         functionPrefix: ""
         modules: ["module1.js", "dir2/module"]
         roots: ["resources"]

   some_prop: module1.hello()
   other_prop: module.test()

resources:
   Resources:
      resource1: 5
      resource2: "some string"