# rocker

> This is a javascript client used to communicate with a Rocker server

Latest version **1.1.6** (published 2013-12-09) · 0 weekly downloads

## Install

```sh
npm install rocker
pnpm add rocker
yarn add rocker
bun add rocker
```

## 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.6 |
| Published | 2013-12-09 |
| First published | 2013-04-21 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=0.4.11 |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Victor Jonsson |
| Maintainers | vic |
| Keywords | API, REST, restful |

## Links

- npm: https://www.npmjs.com/package/rocker
- Repository: git@github.com:victorjonsson/rocker.js
- Homepage: https://github.com/victorjonsson/rocker.js
- npm.io page: https://npm.io/package/rocker

## Alternatives

- [launchdarkly-js-client-sdk](https://npm.io/package/launchdarkly-js-client-sdk.md) — 2.5M weekly downloads
- [@elastic/elasticsearch](https://npm.io/package/@elastic/elasticsearch.md) — 2.1M weekly downloads
- [@c8y/client](https://npm.io/package/@c8y/client.md) — 15.3K weekly downloads
- [@signaldb/maverickjs](https://npm.io/package/@signaldb/maverickjs.md) — 1.7K weekly downloads
- [@bbc/http-transport-cache](https://npm.io/package/@bbc/http-transport-cache.md) — 1.2K weekly downloads

## Recent versions

- 1.1.6 (latest) — 2013-12-09
- 1.1.5 — 2013-05-19
- 1.1.4 — 2013-05-05
- 1.1.3 — 2013-05-05
- 1.1.0 — 2013-05-02
- 1.0.0 — 2013-04-21

## README

rocker.js
=========

This is a javascript client used to communicate with a [Rocker server](https://github.com/victorjonsson/PHP-Rocker).
This script **works both in a browser and as a nodejs module**. *(The Rocker server is a restful backend service (LAMP), [here you can read more about PHP-Rocker](https://github.com/victorjonsson/PHP-Rocker))*

#### Example

```js

var Rocker = require('rocker'),
    server = new Rocker('https://api.website.com/');

var userMeta = {
    birth : '1980-12-04',
    gender : 'male',
    country: 'Finland'
};
server.createUser('john@gmail.com', 'John', 'password', userMeta, function(status, user) {
    if( status == 409 ) {
        console.log('E-mail taken by another user');
    } else {
        console.log(user);
    }
});

```

### Methods

- **setSecret( s )** — In case using encrypted authentication (more info below)
- **setUser( email, password )** — Set authentication credentials
- **me( callback )** — Get user data belonging to authenticated user
- **createUser( email, nick, pass, meta, callback )** — Create a new user
- **saveFile( content, name, contentType, callback, base64Decode, imageVersions)** — Save a file related to authenticated user (more info below)
- **fileUpload( inputElement, callback, beforeUploadCallback, imageVersions )** — Browser only (more info below)
- **removeFile( name, callback )**
- **request( obj )** — Request an API operation. Example

```js
var server = new Rocker('https://api.website.com/');
server.setUser('admin@gmail.com', 'thepassword');
server.request({
    path : 'email/send',
    method : 'POST',
    data : {
        to : 'anna@facebook.com',
        subject : 'Hi there!',
        body : 'I like your smile :)'
    },
    auth : true, // send authorization header
    onComplete : function(status, json, http) {
        console.log(status); // the http status code
        console.log(json); // json data returned by server
        console.log(http); // XHR object in the browser, http response object in nodejs
    }
});

```

Here you can [read more about which operations that is available out-of-the-box](https://github.com/victorjonsson/PHP-Rocker/wiki/API-Reference)

### RC4 encrypted authentication

If you're using RC4 encrypted authentication you will have to give the shared secret to
your client before making any requests that requires authentication. Example:

```js
var server = new Rocker('https://api.website.com/');
server.setSecret('AodXqLN.3Amejksao!elM');
server.setUser('admin@gmail.com', 'thepassword');
server.me(function(user) {
  console.log('My name is ' + user.nick );
});
```

### Storing files

Browser example (requires support for FileReader):

```html
<html>
<head></head>
<body>
  <input type="file" id="file-upload" />
  <script src="js/rocker.js"></script>
  <script>
    var server = new Rocker('https://api.website.com/');
    server.setUser('admin@website.com', 'password...');
    server.fileUpload(

        // input of type file
        document.getElementByd('file-upload'),

        // Callback when operation finished
        function(status, obj) {
            if( status == 'success' ) {
                // all went well....
                console.log('File available at '+obj.location);
            }
            else if( status == 'browser-error' ) {
                // error in browser
                alert(obj); // alert error message
            }
            else if( status == 'server-error' ) {
                // something went wrong on backend
                console.log(obj);
            }
        },

        // Callback called before sending file to server
        function(file) {
            // file variable being an object representing the file
            // return false to prevent file from being sent to server
        },

        // Object declaring image versions that should be generated
        // in case the file is an image
        {
            thumb : '80x80',
            small : '160x0',
            medium : '468x0'
        }
    );
  </script>
</body>
</html>
```

Nodejs example:

```js

var fs = require('fs'),
    mime = require('mime'),
    Rocker = require('rocker'),
    server = new Rocker('https://api.website.com/');

// Give client user credentials
server.setUser('admin@website.com', 'password....');

// Load image into base64 encoded string (only binary files needs to be base64 encoded)
var imgBase64 = new Buffer(fs.readFileSync('my-image.jpg', 'binary'), 'binary').toString('base64'),
    mimeType = mime.lookup('my-image.jpg');

// Declare callback
var onFileSent = function(status, response) {
    if( status == 201 ) {
        // All is fine :)
        console.log('Image located at '+ response.location);
    } else {
        console.log(status);
        console.log(response);
    }
};

// Send file to server
server.saveFile(
    imgBase64,
    'file/my-image.jpg', // you can user what ever file name you want for your file
    mimeType,
    onFileSent, // the callback
    true, // Boolean telling the server if file content should be base64 decoded
    {thumb : '100x100'} // object with image versions (generated in case saved file is an image)
});

```

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