# @hapipal/toys 中文文档

规范地址：https://npmcn.com/package/%40hapipal/toys/

版本：4\.0\.0
npm 元数据更新时间：2023-02-27T04:57:36.831Z
中文译文更新时间：2026-11-17T09:09:19.834Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40hapipal/toys
原文对照：https://npmcn.com/package/%40hapipal/toys/?lang=original
项目主页：https://github.com/hapipal/toys#readme
源码仓库：https://github.com/hapipal/toys

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：19.2 kB，5 个文件
- 模块格式：CommonJS
- TypeScript 类型：包内自带
- Node.js 要求：\>=16
- 运行依赖：1 个
- peer 依赖：@hapi/hapi \>=20 \<22
- 首次发布：2020-10-20T04:34:42.152Z
- 最新版本发布：2023-02-27T04:57:36.691Z
- 近 30 天下载量：36139（2026-10-17 至 2026-11-15，npm 官方下载统计）
- 已发布版本数：5（近 12 个月 0 个）

## 中文简介

霍比玩具胸口

## 安装

```sh
npm install @hapipal/toys
```

也可以使用：`pnpm add @hapipal/toys`、`yarn add @hapipal/toys`、`bun add @hapipal/toys`

## README

# 玩具

霍比玩具胸口

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

主维护者 - [变形常春藤](https://github.com/devinivy)

## 安装

```sh
npm install @hapipal/toys
```

## 使用量

> 另见 [API 参考](https://npmcn.com/package/%40hapipal/toys/API.md)
> 
> 玩具的用法为:hopi v20+和nodejs v16+(_ưμ㼯a_) (中文(简体) ).

玩具是用来减少普通锅炉板 **霍皮 v20+ (英语)** 项目.

以下是一个实例 [`Toys.auth.strategy()`](https://npmcn.com/package/%40hapipal/toys/API.md#toysauthstrategyserver-name-authenticate) 财务报告和审定财务报表 [`Toys.withRouteDefaults()`](https://npmcn.com/package/%40hapipal/toys/API.md#toyswithroutedefaultsdefaults)编辑 [API 参考](https://npmcn.com/package/%40hapipal/toys/API.md) 也充满实例.

```js
const Hapi = require('@hapi/hapi');
const Boom = require('@hapi/boom');
const Toys = require('@hapipal/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 } } });
    });

    // 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) => {

                        const username = request.auth.credentials?.user?.name;

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

    await server.start();

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

## 常见问题

### @hapipal/toys 是什么？

霍比玩具胸口

### 如何安装 @hapipal/toys？

在项目目录运行 npm install @hapipal/toys，或使用 pnpm add / yarn add / bun add。

### @hapipal/toys 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @hapipal/toys 支持 ESM 还是 CommonJS？

以 CommonJS 形式发布，可以直接 require\(\)；在 ES Module 项目中也可以用 import 引入（Node\.js 会把 module\.exports 作为默认导出）。

### @hapipal/toys 需要什么版本的 Node\.js？

package\.json 的 engines 字段要求 Node\.js \>=16。版本不符时 npm 默认只给出警告，开启 engine-strict 后会拒绝安装。

### @hapipal/toys 可以商用吗？

@hapipal/toys 使用 MIT 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### @hapipal/toys 的下载量有多少？

根据 npm 官方下载统计，@hapipal/toys 在 2026/10/17 – 2026/11/15 这 30 天内被下载 36,139 次，日均约 1,205 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @hapipal/toys 还在维护吗？

最新版本 4\.0\.0 发布于 2023/02/27（3 年前）；至今共发布 5 个版本，其中过去 12 个月发布了 0 个；首次发布于 2020/10/20。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

