# toys

> The hapi utility toy chest

Latest version **2.3.1** (published 2020-01-31) · MIT license · 0 weekly downloads

## Install

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

## 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 | 2.3.1 |
| Published | 2020-01-31 |
| First published | 2017-02-06 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 21.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 32 |
| Author | Devin Ivy |
| Maintainers | devinivy |
| Keywords | hapi, utility, performance, convenience, tools |

## Links

- npm: https://www.npmjs.com/package/toys
- Repository: https://github.com/hapipal/toys
- Homepage: https://github.com/hapipal/toys#readme
- Issues: https://github.com/hapipal/toys/issues
- npm.io page: https://npm.io/package/toys

## Dependencies (1)

- [@hapi/hoek](https://npm.io/package/@hapi/hoek.md) 8.x.x

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 2.3.1 (latest) — 2020-01-31
- 2.3.0 — 2019-12-09
- 2.2.3 — 2019-11-05
- 2.2.2 — 2019-04-23
- 2.2.1 — 2018-08-07
- 2.2.0 — 2018-04-23
- 2.1.1 — 2018-01-06
- 2.1.0 — 2018-01-06
- 2.0.0 — 2017-12-31
- 1.0.1 — 2017-12-31
- 1.0.0 — 2017-03-08
- 0.0.1 — 2017-02-06

## README

# toys

The hapi utility toy chest

[![Build Status](https://travis-ci.org/hapipal/toys.svg?branch=master)](https://travis-ci.org/hapipal/toys) [![Coverage Status](https://coveralls.io/repos/hapipal/toys/badge.svg?branch=master&service=github)](https://coveralls.io/github/hapipal/toys?branch=master)

Lead Maintainer - [Devin Ivy](https://github.com/devinivy)

## Usage
> See also the [API Reference](API.md)

Toys is a collection of utilities made to reduce common boilerplate in **hapi v17+** projects, aid usage of events and streams in `async` functions (e.g. handlers and server methods), and provide versions of widely-used utilities from [Hoek](https://github.com/hapijs/hoek) optimized to perform well in hot code paths such as route handlers.

Below is an example featuring [`Toys.auth.strategy()`](API.md#toysauthstrategyserver-name-authenticate), [`Toys.reacher()`](API.md#toysreacherchain-options), and [`Toys.withRouteDefaults()`](API.md#toyswithroutedefaultsdefaults).  The [API Reference](API.md) is also filled with examples.

```js
const Hapi = require('@hapi/hapi');
const Boom = require('@hapi/boom');
const Toys = require('toys');

(async () => {

    const server = Hapi.server();

    // Make a one-off auth strategy for testing
    Toys.auth.strategy(server, 'name-from-param', (request, h) => {

        // Yes, perhaps not the most secure
        const { username } = request.params;

        if (!username) {
            throw Boom.unauthorized(null, 'Custom');
        }

        return h.authenticated({ credentials: { user: { name: username } } });
    });

    // Make function to efficiently index into a request to grab an authed user's name
    const grabAuthedUsername = Toys.reacher('auth.credentials.user.name');

    // Default all route methods to "get", unless otherwise specified
    const defaultToGet = Toys.withRouteDefaults({ method: 'get' });

    server.route(
        defaultToGet([
            {
                method: 'post',
                path: '/',
                handler: (request) => {

                    return { posted: true };
                }
            },
            {   // Look ma, my method is defaulting to "get"!
                path: '/as/{username}',
                options: {
                    auth: 'name-from-param', // Here's our simple auth strategy
                    handler: (request) => {

                        // grabAuthedUsername() is designed to be quick
                        const username = grabAuthedUsername(request);

                        return { username };
                    }
                }
            }
        ])
    );

    await server.start();

    console.log(`Now, go forth and ${server.info.uri}/as/your-name`);
})();
```

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