https://github.com/ssbc/scuttle-inject
https://github.com/ssbc/scuttle-inject
Last synced: 11 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/ssbc/scuttle-inject
- Owner: ssbc
- Created: 2018-06-18T01:50:20.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2019-01-11T20:59:22.000Z (over 7 years ago)
- Last Synced: 2025-07-30T16:24:34.657Z (11 months ago)
- Language: JavaScript
- Size: 11.7 KB
- Stars: 3
- Watchers: 8
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Scuttle-inject
Takes a particular sort of nested tree of methods and returns a version of that tree which has a scuttlebutt server injected into each method.
This pattern means you only need to inject a scuttlebot once, makes it easier to test your methods, and handles being able to inject different sorts of server for you.
## Example usage
```js
// methods.js
module.exports = {
async: {
publishPoll: function (server) {
return function (opts, cb) {
// check the opts before publishing
const cleanOpts = clean(opts)
server.publish(cleanOpts, cb)
}
},
// getPoll: (key, cb) => {}
},
pull: {
myPolls: function (server) {
return function (opts) {
const defaultQuery = { ... }
const query = Object.assign({}, defaultQuery, opts)
return server.query.read(opts)
}
}
// openPolls: (opts) => {},
// closedPolls: (opts) => {}
}
}
```
Injecting server once (somehwhere high-level):
```js
const inject = require('scuttle-inject')
cosnt methods = require('./methods')
const scuttle = inject(methods, server)
// assume you're in a context where you have a server
```
Using the scuttle helper:
```
const opts = { ... }
scuttle.async.publishPoll(opts, cb)
```
## API
`inject(server, methods, pluginDeps)`
- `server` - a `scuttlebot` server, an `ssb-client` connection to a server, or an observeable which will resolve into a server connection (such as Patchcore's `sbot.obs.connection`)
- `methods` - an Object nested at least 2 levels deep, where there must be one layer which specifies the `type` of method. Valid types are : `sync`, `async`, `pull`, `obs`
- `pluginDeps` (optional) - an Array of plugins apis the scuttlebot must have for your methods to work. e.g. `['query']` will check that `sbot.query` has methods which are accessible.