# tote-lib

> `tote-lib` is a helper library for [Tote](https://github.com/alizahid/tote).

Latest version **0.0.11** (published 2019-06-22) · MIT license · 0 weekly downloads

## Install

```sh
npm install tote-lib
pnpm add tote-lib
yarn add tote-lib
bun add tote-lib
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.11 |
| Published | 2019-06-22 |
| First published | 2019-06-20 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 18.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | alizahid |

## Links

- npm: https://www.npmjs.com/package/tote-lib
- npm.io page: https://npm.io/package/tote-lib

## Dependencies (2)

- [@hapi/joi](https://npm.io/package/@hapi/joi.md) ^15.0.3
- [lodash.get](https://npm.io/package/lodash.get.md) ^4.4.2

## Recent versions

- 0.0.11 (latest) — 2019-06-22
- 0.0.10 — 2019-06-21
- 0.0.9 — 2019-06-21
- 0.0.8 — 2019-06-21
- 0.0.7 — 2019-06-20
- 0.0.6 — 2019-06-20
- 0.0.5 — 2019-06-20
- 0.0.4 — 2019-06-20
- 0.0.3 — 2019-06-20
- 0.0.2 — 2019-06-20
- 0.0.1 — 2019-06-20

## README

# `tote-lib`

`tote-lib` is a helper library for [Tote](https://github.com/alizahid/tote).

## API

`tote-lib` exports four items.

### `Func`

`Func` is the class you use to setup your function. It includes several helper methods.

#### Example

```javascript
// import dependencies
import { Func, joi } from 'tote-lib'

// create your function
class Hello extends Func {
  // define your schema
  schema = {
    // query parameters
    query: joi.object({
      number: joi.number()
    })
  }

  // define your function here
  invoke() {
    // get query params
    const {
      query: { number = 5 }
    } = this.request

    // return object
    return {
      hello: 'world',
      result: number * 2
    }
  }
}

// bootstrap and export
export default Func.bootstrap(Hello)
```

#### Options

| Name             | Type       | Description                                                                         |
| ---------------- | ---------- | ----------------------------------------------------------------------------------- |
| `context`        | `Object`   | Azure Function execution context                                                    |
| `request`        | `Object`   | Request object. Contains `body`, `headers`, and `query` objects with any parameters |
| `status<number>` | `Function` | Set the HTTP status code                                                            |
| `send<object>`   | `Function` | Set the JSON body                                                                   |

##### Usage

**`request`**

Get input parameters.

```javascript
// get parameters
const {
  body: { foo },
  query: { name }
} = this.request

// use them
```

**`status`**

```javascript
this.status(404)
```

**`send`**

You can just return the object from your function. But you can also use `send`.

```javascript
this.send({
  foo: 'bar'
})
```

### `Exception`

`Exception` extends `Error` and lets you throw sensible errors from your functions.

#### Parameters

| Name      | Type     | Description          | Default         |
| --------- | -------- | -------------------- | --------------- |
| `message` | `string` | Error message or key | `unknown_error` |
| `status`  | `number` | HTTP status code     | `500`           |

#### Usage

```javascript
throw new Exception('Email required', 400)
```

### `invoke`

`invoke` is a helper for invoking functions when writing your tests.

#### Parameters

| Name      | Type     | Description                                                                  |
| --------- | -------- | ---------------------------------------------------------------------------- |
| `name`    | `string` | Function name, should correspond to a directory                              |
| `request` | `Object` | Optional. Request object, may contain `body`, `headers`, and `query` objects |

#### Usage

```javascript
invoke('hello', {
  headers: {
    authorization: 'Bearer ...'
  },
  query: {
    name: 'Ali'
  }
})
```

### `joi`

Look at [Joi docs](https://github.com/hapijs/joi).

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