# serverless-wrapper-plugin

> Serverless Wrapper Plugin - Wrap all your functions from one place

Latest version **1.2.0** (published 2018-07-11) · ISC license · 0 weekly downloads

## Install

```sh
npm install serverless-wrapper-plugin
pnpm add serverless-wrapper-plugin
yarn add serverless-wrapper-plugin
bun add serverless-wrapper-plugin
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.0 |
| Published | 2018-07-11 |
| First published | 2018-07-11 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 15.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Konrad Markus |
| Maintainers | blackevil245 |
| Keywords | serverless, plugin |

## Links

- npm: https://www.npmjs.com/package/serverless-wrapper-plugin
- npm.io page: https://npm.io/package/serverless-wrapper-plugin

## Dependencies (3)

- [dot](https://npm.io/package/dot.md) ^1.1.1
- [bluebird](https://npm.io/package/bluebird.md) ^3.4.6
- [fs-extra](https://npm.io/package/fs-extra.md) ^1.0.0

## Recent versions

- 1.2.0 (latest) — 2018-07-11

## README

Serverless Wrapper Plugin
------------------------------------------------------------------------
[![serverless](http://public.serverless.com/badges/v3.svg)](http://www.serverless.com)

This goal of this plugin is to provide an easy way to wrap your serverless functions with a wrapper function, without
having to edit all the functions themselves.

One use case for this is for example, if you want to override ```console.warn``` to prepend a custom string, to make these warning easier to trace in the logs. You could add this to the begining of every handler in your project, or you could write one wrapper function to perform this override and use the ```serverless-wrapper-plugin``` to automatically wrap all your handlers.

Another use case might be, you want to write a process to periodically make requests to your functions to keep them hot. You hadn't planned for this when you wrote all your handlers, so what input do you send to avoid causing either errors or unwanted side-effects? With the ```serverless-wrapper-plugin``` you could write a wrapper to intercept the input event, and if it is the dummy "wake up" event, then ignore it and return. If it isn't the dummy event then simply pass it through to the handler as normal.

**UPDATE 1.1.0**
- Support function specific wrapper function, using `wrapper.path` option, set using relative wrapper path to the root of the project in the `s-function.json` file.
- Support skipping wrapper if function-specific `wrapper.path` is set to `false`


## Setup
> Not compatible with Serverless 1.0.0 and above
> Support only Node Lambdas

> NOTE: If you are using the ```serverless-webpack-plugin```, this plugin must be before the webpack plugin in the list.

### Wrapper Function
Firstly you need to write the wrapper function that you want to apply.
This wrapper function should have the following form:

```{js}
function myWrapper(handler, event, context) {
    // Do whatever you like here..
    // ...

    // Call the original handler
    return handler(event, context);
}

module.exports = myWrapper;
```

The way that this is used is that the handler is transformed to be something like this:

```{js}
const _handler = require('...path to original handler...');
const _wrapper = require('...path to myWrapper...');

module.exports.handler = function (event, context) {
    return _wrapper(_handler, event, context);
}
```

### Plugin Installation
* Install the plugin in the root of your Serverless Project:
```{bash}
npm install serverless-wrapper-plugin --save-dev
```

* Add the plugin to the `plugins` array in your Serverless Project's `s-project.json`, as below.

> NOTE: If you are using the ```serverless-webpack-plugin```, this plugin must be before the webpack plugin in the list.

```{json}
"plugins": [
    "serverless-wrapper-plugin"
]
```

* In the `custom` property of either your `s-project.json` or `s-function.json` add a default wrapper property. The path is relative to the project root. This is a fallback if your s-function does not contain a `wrapper.path` property.

```{js}
{
    ...
    "custom": {
        "wrapper": {
            "path": "path/relative/to/project-root"
        }
    }
    ...
}
```

* To set a custom wrapper for specific function, add the wrapper function's relative path to `s-function.json`. If you do not want your function to be wrapped by any wrapper function, event the default one, set the `wrapper` propertry to **false**
```{js}
  ...
  "name": "your-function",
  "runtime": "nodejs4.3",
  "custom": {
    "wrapper" {
      "path": "wrapper.js",
    }
  }
  "handler": "handler.handler",
  ...
  ...
  "name": "your-function",
  "runtime": "nodejs4.3",
  "custom": {
    "wrapper": false
  }
  "handler": "handler.handler",
  ...
```

## Development

A brief note about development of the plugin itself. Part of the function of this plugin is to generate code. To do this it uses the template engine [doT.js](http://olado.github.io/doT/index.html).

There is a template in ```lib/wrapped-handler.jst``` this is pre-compiled for runtime efficiency and saved as ```lib/wrapped-handler.js```.

To re-compiled this template, you can use the convenience script in ```bin/compile-template.js```

---
_Source: https://npm.io/package/serverless-wrapper-plugin · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
