# shortstop-handlers

> Common protocol handlers for use with the shortstop node module.

Latest version **1.1.1** (published 2022-02-14) · 0 weekly downloads

## Install

```sh
npm install shortstop-handlers
pnpm add shortstop-handlers
yarn add shortstop-handlers
bun add shortstop-handlers
```

## 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.1.1 |
| Published | 2022-02-14 |
| First published | 2014-02-07 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 19.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 15 |
| Author | Erik Toth |
| Maintainers | jasisk, grawk, totherik, krakenjs-user |
| Keywords | shortstop, node, json |

## Links

- npm: https://www.npmjs.com/package/shortstop-handlers
- Repository: https://github.com/krakenjs/shortstop-handlers
- Homepage: https://github.com/krakenjs/shortstop-handlers#readme
- Issues: https://github.com/krakenjs/shortstop-handlers/issues
- npm.io page: https://npm.io/package/shortstop-handlers

## Dependencies (3)

- [glob](https://npm.io/package/glob.md) ^7.0.5
- [caller](https://npm.io/package/caller.md) ^1.0.1
- [core-util-is](https://npm.io/package/core-util-is.md) ^1.0.1

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 1.1.1 (latest) — 2022-02-14
- 1.0.1 — 2016-06-23
- 1.0.0 — 2014-05-15
- 0.1.0 — 2014-02-07

## README

# shortstop-handlers

[![Build Status](https://travis-ci.org/krakenjs/shortstop-handlers.svg?branch=master)](https://travis-ci.org/krakenjs/shortstop-handlers)

A common set of handlers for use with [shortstop](https://github.com/paypal/shortstop).

NOTE: As of v1.0 `shortstop-handlers` works best with `shortstop` >=1.0. This is
due to the fact that as of shortstop v1.0 async handlers are now supported and
have subsequently been added to this module.


```javascript
var shortstop = require('shortstop'),
    handlers = require('shortstop-handlers');


var resolver, json;

resolver = shortstop.create();
resolver.use('path',   handlers.path(__dirname));
resolver.use('file',   handlers.file(__dirname));
resolver.use('base64', handlers.base64());
resolver.use('env',    handlers.env());
resolver.use('require', handlers.require(__dirname));
resolver.use('exec',   handlers.exec(__dirname));

resolver.resolve(require('./myfile'), function (err, data) {
    // data
});
```

## API
### handlers.path([basedir])

* `basedir` (*String*, optional) - The base path used for resolving relative path values. Defaults to `caller` dirname.

Creates a handler that can be given to shortstop to resolve file paths.

```javascript
var foo = {
    "mydir": "path:./lib/dir"
};

var resolver = shortstop.create();
resolver.use('path', handlers.path());
resolver.resolve(foo, function (err, data) {
  data.mydir; // `/path/to/my/project/lib/dir`
});
```



### handlers.file([basedir], [options])

* `basedir` (*String*, optional) - The base path used for resolving relative path values. Defaults to `caller` dirname.
* `options` (*Object*, optional) - Options object provided to fs.readFile.

Creates a handler which resolves the provided value to the basedir and returns the contents of the file as a Buffer.

```javascript
var foo = {
    "cert": "file:./cert.pem"
};

var resolver = shortstop.create();
resolver.use('file', handlers.file());
resolver.resolve(foo, function (err, data) {
    foo.cert; // <Buffer 48 65 6c 6c 6f 2c 20 77 6f72 6c 64 21>
});
```


### handlers.base64()

Creates a handler which will return a buffer containing the content of the base64-encoded string.

```javascript
var foo = {
    "key": "base64:SGVsbG8sIHdvcmxkIQ=="
};

var resolver = shortstop.create();
resolver.use('base64', handlers.base64());
resolver.resolve(foo, function (err, data) {
    data.key; // <Buffer 48 65 6c 6c 6f 2c 20 77 6f72 6c 64 21>
    data.key.toString('utf8'); // Hello, world!
});
```

### handlers.env()

Creates a handler which will resolve the provided value as an environment variable, optionally casting the value using the provided filter. Supported filters are '|d', '|b', and '|!b' which will cast to Number and Boolean types respectively.

```javascript
process.env.HOST = 'localhost';
process.env.PORT = '8000';
process.env.ENABLED = 'true';
process.env.FALSY = 'false'; // or '', or '0'

var foo = {
    "bar": "env:HOST",
    "baz": "env:PORT|d",
    "bam": "env:ENABLED|b",
    "bag": "env:FALSY|b"
    "bat": "env:FALSY|!b"
};

var resolver = shortstop.create();
resolver.use('env', handlers.env());
resolver.resolve(foo, function (err, data) {
    data.bar; // 'localhost'
    data.baz; // 8000
    data.bam; // true
    data.bag; // false
    data.bat; // true
});
```


### handlers.require([basedir])

* `basedir` (*String*, optional) - The base path used for resolving relative path values. Defaults to `caller` dirname.

Creates a handler which resolves and loads, and returns the specified module.

```javascript
var foo = {
    "path": "require:path",
    "minimist": "require:minimist",
    "mymodule": "require:./mymodule"
    "json": "require:../config/myjson"
};

var resolver = shortstop.create();
resolver.use('require', handlers.require());
resolver.resolve(foo, function (err, data) {
    data.path; // Node core `path` module
    data.minimist; // `minimist` module as loaded from node_modules
    data.mymodule; // module as loaded from `./mymodule.js`
    data.json; // JS object as loaded from `../config/myjson.json`
});
```


### handlers.exec([basedir])

* `basedir` (*String*, optional) - The base path used for resolving relative path values. Defaults to `caller` dirname.

Creates a handler which resolves and loads the specified module, executing the method (if specified) or the module itself, using the return value as the resulting value. The value should have the format `{module}(#{method})?`. If no function is able to be found this handler will throw with an error.
```javascript
var foo = {
    "item1": "exec:./mymodule#create"
    "item2": "exec:./myothermodule"
};

var resolver = shortstop.create();
resolver.use('exec', handlers.exec(__dirname));
resolver.resolve(foo, function (err, data) {
    data.item1; // the result of calling mymodule.create()
    data.item2; // the result of calling myothermodule()
});
```



### handlers.glob([basedir|options])

* `basedir` (*String* or *Object*, optional) - The base path use for resolving or a `glob` options object per https://github.com/isaacs/node-glob#options

Creates a handler which match files using the patterns the shell uses.
```javascript
var foo = {
    "files": "glob:**/*.js"
};

var resolver = shortstop.create();
resolver.use('glob', handlers.glob(__dirname));
resolver.resolve(foo, function (err, data) {
    data.files[0]; // '/my/dirname/foo/index.js';
    data.files[1]; // '/my/dirname/index.js';
});
```

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