# @hoodie/client

> Client API for the Hoodie server

Latest version **10.2.0** (published 2019-11-18) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install @hoodie/client
pnpm add @hoodie/client
yarn add @hoodie/client
bun add @hoodie/client
```

## 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 | 10.2.0 |
| Published | 2019-11-18 |
| First published | 2016-04-23 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 8 |
| Unpacked size | 429.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 34 |
| Author | The Hoodie Community and other contributors \| http://hood.ie/ |
| Maintainers | hoodie |
| Keywords | hoodie, nobackend, offlinefirst |

## Links

- npm: https://www.npmjs.com/package/@hoodie/client
- Repository: https://github.com/hoodiehq/hoodie-client
- Homepage: https://github.com/hoodiehq/hoodie-client#readme
- Issues: https://github.com/hoodiehq/hoodie-client/issues
- npm.io page: https://npm.io/package/@hoodie/client

## Dependencies (8)

- [lie](https://npm.io/package/lie.md) ^3.0.1
- [nets](https://npm.io/package/nets.md) ^3.2.0
- [lodash](https://npm.io/package/lodash.md) ^4.11.1
- [@hoodie/log](https://npm.io/package/@hoodie/log.md) ^2.1.0
- [pouchdb-doc-api](https://npm.io/package/pouchdb-doc-api.md) ^1.0.0
- [@hoodie/store-client](https://npm.io/package/@hoodie/store-client.md) ^8.1.0
- [@hoodie/account-client](https://npm.io/package/@hoodie/account-client.md) ^6.0.0
- [@hoodie/connection-status](https://npm.io/package/@hoodie/connection-status.md) ^4.0.1

## Recent versions

- 10.2.0 (latest) — 2019-11-18
- 10.1.1 — 2017-08-08
- 10.1.0 — 2017-06-08
- 10.0.2 — 2017-03-18
- 10.0.1 — 2017-03-17
- 10.0.0 — 2017-03-09
- 9.0.2 — 2017-01-14
- 9.0.1 — 2017-01-10
- 9.0.0 — 2016-12-25
- 8.0.3 — 2016-12-25
- 8.0.2 — 2016-12-24
- 8.0.1 — 2016-12-22
- 8.0.0 — 2016-12-04
- 7.0.0 — 2016-12-04
- 6.0.0 — 2016-10-30
- … 14 more at https://npm.io/package/@hoodie/client/versions

## README

# hoodie-client

> Client API for the Hoodie server

[![Build Status](https://travis-ci.org/hoodiehq/hoodie-client.svg?branch=master)](https://travis-ci.org/hoodiehq/hoodie-client)
[![Coverage Status](https://coveralls.io/repos/hoodiehq/hoodie-client/badge.svg?branch=master)](https://coveralls.io/github/hoodiehq/hoodie-client?branch=master)
[![Dependency Status](https://david-dm.org/hoodiehq/hoodie-client.svg)](https://david-dm.org/hoodiehq/hoodie-client)
[![devDependency Status](https://david-dm.org/hoodiehq/hoodie-client/dev-status.svg)](https://david-dm.org/hoodiehq/hoodie-client#info=devDependencies)

`@hoodie/client` integrates Hoodie’s client core modules:

- [account-client](https://github.com/hoodiehq/hoodie-account-client)
- [store-client](https://github.com/hoodiehq/hoodie-store-client)
- [connection-status](https://github.com/hoodiehq/hoodie-connection-status)
- [log](https://github.com/hoodiehq/hoodie-log)

## Example

```js
var Hoodie = require('@hoodie/client')
var hoodie = new Hoodie({
  url: 'https://myhoodieapp.com',
  PouchDB: require('pouchdb')
})

hoodie.account.signUp({
  username: 'pat@Example.com',
  password: 'secret'
}).then(function (accountAttributes) {
  hoodie.log.info('Signed up as %s', accountAttributes.username)
}).catch(function (error) {
  hoodie.log.error(error)
})
```

## API

- [Constructor](#constructor)
- [hoodie.url](#hoodieurl)
- [hoodie.account](#hoodieaccount)
- [hoodie.store](#hoodiestore)
- [hoodie.connectionStatus](#hoodieconnectionstatus)
- [hoodie.log](#hoodielog)
- [hoodie.request](#hoodierequest)
- [hoodie.plugin](#hoodieplugin)
- [hoodie.on](#hoodieon)
- [hoodie.one](#hoodieone)
- [hoodie.off](#hoodieoff)
- [hoodie.trigger](#hoodietrigger)
- [Events](#events)

### Constructor

```js
new Hoodie(options)
```

<table>
  <thead>
    <tr>
      <th align="left">Argument</th>
      <th align="left">Type</th>
      <th align="left">Description</th>
      <th align="left">Required</th>
    </tr>
  </thead>
  <tr>
    <th align="left">options.PouchDB</th>
    <td>Constructor</td>
    <td>
      PouchDB constructor, see also <a href="https://pouchdb.com/custom.html">PouchDB custom builds</a>
    </td>
    <td>Yes</td>
  </tr>
  <tr>
    <th align="left">options.url</th>
    <td>String</td>
    <td>
      Set to hostname where Hoodie server runs, if your app runs on
      a different host
    </td>
    <td>Yes</td>
  </tr>
  <tr>
    <th align="left">options.account</th>
    <td>String</td>
    <td>
      <a href="https://github.com/hoodiehq/hoodie-account-client#constructor">account options</a>.
      <code>options.url</code> is always set to <code>hoodie.url</code> + '/account/api'
    </td>
    <td>No</td>
  </tr>
  <tr>
    <th align="left">options.store</th>
    <td>String</td>
    <td>
      <a href="https://github.com/hoodiehq/hoodie-store#constructor">store options</a>.
      <code>options.PouchDB</code> is always set to <a href="#constructor">Hoodie Client’s constructor</a>’s <code>options.PouchDB</code>.
      <code>options.dbName</code> is always set to <code>'store'</code>.
      <code>options.remote</code> is always set to <code>hoodie.url + '/store/api/user%2f' + account id</code>.
    </td>
    <td>No</td>
  </tr>
  <tr>
    <th align="left">options.task</th>
    <td>String</td>
    <td>
      <a href="https://github.com/hoodiehq/hoodie-client-task#constructor">task options</a>.
      <code>options.userId</code> is always set to <code>account id</code>.
      <code>options.remote</code> is always set to <code>hoodie.url</code> + '/task/api'
    </td>
    <td>No</td>
  </tr>
  <tr>
    <th align="left">options.connectionStatus</th>
    <td>String</td>
    <td>
      <a href="https://github.com/hoodiehq/hoodie-connection-status#constructor">connectionStatus options</a>.
      <code>options.url</code> is always set to <code>hoodie.url</code> + '/connection-status/api'.
      <code>options.method</code> is always set to <code>HEAD</code>
    </td>
    <td>No</td>
  </tr>
</table>

### hoodie.url

_Read-only_

```js
hoodie.url
```

full url to the hoodie server, e.g. `http://example.com/hoodie`

### hoodie.account

`hoodie.account` is an instance of [hoodie-account-client](https://github.com/hoodiehq/hoodie-account-client).
See [account API](https://github.com/hoodiehq/hoodie-account-client#api)

### hoodie.store

`hoodie.store` is an instance of [hoodie-store](https://github.com/hoodiehq/hoodie-store).
See [store API](https://github.com/hoodiehq/hoodie-store#api)

### hoodie.connectionStatus

`hoodie.connectionStatus` is an instance of [hoodie-connection-status](https://github.com/hoodiehq/hoodie-connection-status).
See [connectionStatus API](https://github.com/hoodiehq/hoodie-connection-status#api)

### hoodie.log

`hoodie.log` is an instance of [hoodie-log](https://github.com/hoodiehq/hoodie-log).
See [log API](https://github.com/hoodiehq/hoodie-log#api)

### hoodie.request

Sends an http request

```js
hoodie.request(url)
// or
hoodie.request(options)
```

<table>
  <thead>
    <tr>
      <th align="left">Argument</th>
      <th align="left">Type</th>
      <th align="left">Description</th>
      <th align="left">Required</th>
    </tr>
  </thead>
  <tr>
    <th align="left">url</th>
    <td>String</td>
    <td>
      Relative path or full URL. A path must start with <code>/</code> and sends a <code>GET</code>
      request to the path, prefixed by <code>hoodie.url</code>. In case a full URL is passed,
      a <code>GET</code> request to the url is sent.
    </td>
    <td>Yes</td>
  </tr>
  <tr>
    <th align="left">options.url</th>
    <td>String</td>
    <td>
      Relative path or full URL. A path must start with <code>/</code> and sends a <code>GET</code>
      request to the path, prefixed by <code>hoodie.url</code>. In case a full URL is passed,
      a <code>GET</code> request to the url is sent.
    </td>
    <td>Yes</td>
  </tr>
  <tr>
    <th align="left">options.method</th>
    <td>String</td>
    <td>
      <em>Defaults to <code>GET</code></em>. One of <code>GET</code>,
      <code>HEAD</code>, <code>POST</code>, <code>PUT</code>, <code>DELETE</code>.
    </td>
    <td>No</td>
  </tr>
  <tr>
    <th align="left">options.data</th>
    <td>Object, Array, String or Number</td>
    <td>
      For <code>PUT</code> and <code>POST</code> requests, an optional payload
      can be sent. It will be stringified before sending the request.
    </td>
    <td>No</td>
  </tr>
  <tr>
    <th align="left">options.headers</th>
    <td>Object</td>
    <td>
      Map of Headers to be sent with the request.
    </td>
    <td>No</td>
  </tr>
</table>

Examples

```js
// sends a GET request to hoodie.url + '/foo/api/bar'
hoodie.request('/foo/api/bar')
// sends a GET request to another host
hoodie.request('https://example.com/foo/bar')
// sends a PATCH request to /foo/api/bar
hoodie.request({
  method: 'PATCH',
  url: '/foo/api/bar',
  headers: {
    'x-my-header': 'my value'
  },
  data: {
    foo: 'bar'
  }
})
```

### hoodie.plugin

Initialise hoodie plugin

```js
hoodie.plugin(methods)
hoodie.plugin(plugin)
```

<table>
  <thead>
    <tr>
      <th align="left">Argument</th>
      <th align="left">Type</th>
      <th align="left">Description</th>
      <th align="left">Required</th>
    </tr>
  </thead>
  <tr>
    <th align="left">methods</th>
    <td>Object</td>
    <td>
      Method names as keys, functions as values. Methods get directly set on
      <code>hoodie</code>, e.g. <code>hoodie.plugin({foo: function () {}})</code>
      sets <code>hoodie.foo</code> to <code>function () {}</code>
    </td>
    <td>Yes</td>
  </tr>
  <tr>
    <th align="left">plugin</th>
    <td>Function</td>
    <td>
      The passed function gets called with `hoodie` as first argument, and
      can directly set new methods / properties on it.
    </td>
    <td>Yes</td>
  </tr>
</table>

Examples

```js
hoodie.plugin({
  sayHi: function () { alert('hi') }
})
hoodie.plugin(function (hoodie) {
  hoodie.sayHi = function () { alert('hi') }
})
```

### hoodie.on

Subscribe to event.

```js
hoodie.on(eventName, handler)
```

Example

```js
hoodie.on('account:signin', function (accountProperties) {
  alert('Hello there, ' + accountProperties.username)
})
```

### hoodie.one

Call function once at given event.

```js
hoodie.one(eventName, handler)
```

Example

```js
hoodie.one('mycustomevent', function (options) {
  console.log('foo is %s', options.bar)
})
hoodie.trigger('mycustomevent', { foo: 'bar' })
hoodie.trigger('mycustomevent', { foo: 'baz' })
// logs "foo is bar"
// DOES NOT log "foo is baz"
```

### hoodie.off


Removes event handler that has been added before

```js
hoodie.off(eventName, handler)
```

Example

```js
hoodie.off('connectionstatus:disconnect', showNotification)
```

### hoodie.trigger


Trigger custom events

```js
hoodie.trigger(eventName[, option1, option2, ...])
```

Example

```js
hoodie.trigger('mycustomevent', { foo: 'bar' })
```

### Events

<table>
  <tr>
    <th align="left"><code>account:*</code></th>
    <td>events, see <a href="https://github.com/hoodiehq/hoodie-account-client#events">account events</a></td>
  </tr>
  <tr>
    <th align="left"><code>store:*</code></th>
    <td>events, see <a href="https://github.com/hoodiehq/hoodie-store#events">store events</a></td>
  </tr>
  <tr>
    <th align="left"><code>connectionStatus:*</code></th>
    <td>events, see <a href="https://github.com/hoodiehq/hoodie-connection-status#events">connectionStatus events</a></td>
  </tr>
</table>

## Testing

Local setup

```
git clone https://github.com/hoodiehq/hoodie-client.git
cd hoodie-client
npm install
```

Run all tests

```
npm test
```

Run test from one file only

```
node tests/specs/id
```

## Contributing

Have a look at the Hoodie project's [contribution guidelines](https://github.com/hoodiehq/hoodie/blob/master/CONTRIBUTING.md).
If you want to hang out you can join our [Hoodie Community Chat](http://hood.ie/chat/).

## License

[Apache 2.0](http://www.apache.org/licenses/LICENSE-2.0)

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