# sweetp-base

> Base module to write services for Sweetp server to automate your development workflow.

Latest version **1.0.0** (published 2015-03-01) · 0 weekly downloads

## Install

```sh
npm install sweetp-base
pnpm add sweetp-base
yarn add sweetp-base
bun add sweetp-base
```

## 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.0.0 |
| Published | 2015-03-01 |
| First published | 2013-04-27 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Node | ~0.10.17 |
| Dependencies | 3 |
| Known vulnerabilities | 0 (+7 in 2 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Stefan Gojan |
| Maintainers | hoschi |
| Keywords | sweetp, utility |

## Links

- npm: https://www.npmjs.com/package/sweetp-base
- Repository: https://github.com/sweetp/base-nodejs
- Issues: https://github.com/sweetp/base-nodejs/issues
- npm.io page: https://npm.io/package/sweetp-base

## Dependencies (3)

- [debug](https://npm.io/package/debug.md) ~2.1.0
- [jayson](https://npm.io/package/jayson.md) ~1.1.2
- [lodash](https://npm.io/package/lodash.md) ~1.0.1

## Alternatives

- [lodash.assign](https://npm.io/package/lodash.assign.md) — 2.3M weekly downloads
- [lodash.chunk](https://npm.io/package/lodash.chunk.md) — 1.8M weekly downloads
- [react-native-ios-utilities](https://npm.io/package/react-native-ios-utilities.md) — 138.5K weekly downloads
- [@technically/lodash](https://npm.io/package/@technically/lodash.md) — 50.9K weekly downloads
- [@fluid-topics/ft-icon](https://npm.io/package/@fluid-topics/ft-icon.md) — 20.6K weekly downloads

## Recent versions

- 1.0.0 (latest) — 2015-03-01
- 0.6.3 — 2013-06-09
- 0.6.2 — 2013-04-27
- 0.6.1 — 2013-04-27
- 0.6.0 — 2013-04-27

## README

# Base package for Sweetp services developed with node.js

[![NPM version](https://badge.fury.io/js/sweetp-base.png)](http://badge.fury.io/js/sweetp-base)

[Sweetp main site](http://sweet-productivity.com/).

Features:

* simple DSL to define service methods easily
* asynchronous callback API like node.js packages
* wire protocol abstracted for you, again, so you can write new services easily
* call other services

# Examples

## Service creation


    var service, methods, client, sweetp, log;

    log = require('sweetp-base/lib/log')('YOUR SERVICE NAME:internal:');
    sweetp = require('sweetp-base');

    // service methods with sweetp meta data
    service = {

        // add methods as properties of the service object, the key is used as target
        yourMethod:{
            // target:"/yourService/your/own/target/string/", /* optional! */
            options: {
                // description which params you need from sweetp
                params: {
                    // use URL to call other services
                    url: sweetp.PARAMETER_TYPES.url,
                    // define own parameters
                    myOwnParam: sweetp.PARAMETER_TYPES.one,
                    // or fetch config
                    config: sweetp.PARAMETER_TYPES.projectConfig
                },
                // add simple descriptions so everyone knows what this method does
                description: {
                    summary:"Get user and password for 'key'.",
                    example:"more fancy text here, you can use HTML here",
                    returns:"a string with text"
                }
            },
            // assign a function to the "fn" property, this gets executed when the service method gets called
            fn:function(params, callback) {
                // create unicorns here or any other fancy stuff which makes you more productive

                log.debug("Search for unicorns.");
                log.info("I create beautiful unicorns.");
                log.warn("uh oh, no unicorns??");
                log.error("No unicorns!!!! :(");

                return callback(null, "This is your response and can be a String or JSON.");
            }
        },

    };

    // create service methods and start sweetp service (client)
    methods = sweetp.createMethods(service, '/yourService/');
    client = sweetp.start("YOUR SERVICE NAME", methods);

Put this in a file like `/somedir/foo.js`. Add this to your Sweetp `services.json` file:

    {
        "id":"example-node-service",
        "exec":[
            "node",
            "foo.js"
        ],
        "dir":"/somedir/"
    }

Now (re-)start Sweetp server and you should be able to call your example service with `http://localhost:7777/services/yourService/yourMethod` :-)


## Call other service

Calling an other service is easy. Fetch the url of the Sweetp server your
service is running with parameter type `sweetp.PARAMETER_TYPES.url`. Add
parameters for the service call as simple map and put in the other parameters:

    var callback = function (err, result) {
        console.log('result from service call', result);
    };
    var params = {
        title:'This is my title',
        message:'This is my message'
    };
    sweetp.callService(url, "noproject", "ui/dialog/password", params, false, callback);

This would call the service "ui/dialog/password" of project "noproject", which
runs in the same Sweetp server instance as your service. As result you going to
get the answer of the called service.

# Parameter types

See [JVM version](https://github.com/sweetp/base-groovy/blob/master/src/main/groovy/org/hoschi/sweetp/services/base/ServiceParameter.groovy) for more details.

    sweetp.PARAMETER_TYPES.url = "url";
    sweetp.PARAMETER_TYPES.one = "one";
    sweetp.PARAMETER_TYPES.projectConfig = "projectConfig";
    sweetp.PARAMETER_TYPES.list = "list";
    sweetp.PARAMETER_TYPES.request = "request";

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