@batterii/encode-object

中文文档
2.1.1发布于 2020/02/26MITTypeScriptCommonJS近 30 天下载 1.6万

Object encoding and decoding utils

原始使用文档

@batterii/encode-object

This module contains utility functions for encoding and decoding JSON-serializable objects from base64url, allowing them to be safely sent as HTTP headers and URI components.

There are many modules like this already, but at Batterii we wrote yet another one and ended up wanting to open-source some things that depend on it. So we open-sourced this as well. Feel free to use but there's really nothing special about it.

Note that while encoded objects are not typically human-readable, that does not mean they are encrypted or signed in any way. Reading the data from them is as simple as reversing the encoding process, and tampering with them is as simple as repeating it with different data.

To produce signed tokens, which prevent tampering, use tokengrip instead.

Usage

import { decodeObject, encodeObject } from '@batterii/encode-object';

const str = encodeObject({ foo: 'bar' } );

console.log(str);
// eyJmb28iOiJiYXIifQ

const obj = decodeObject(str);

console.log(obj);
// { foo: 'bar' }

In addition to plain objects and arrays, top-level JSON primitives are also supported:

console.log(decodeObject(encodeObject('some string')));
// some string

console.log(decodeObject(encodeObject(true)));
// true

console.log(decodeObject(encodeObject(false)));
// false

console.log(decodeObject(encodeObject(null)));
// null

Error Handling

This package uses Nani to define the following error classes:

  • EncodeObjectError: This is the base class for all other errors in the package, for namespacing purposes.

  • InvalidJsonError: Will be thrown if encoded text contaning invalid JSON is sent to decodeObject.

版本与发布记录

@batterii/encode-object 共发布 4 个版本,首次发布于 2020/01/17;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.1.1latest2020/02/266 年前
2.1.02020/02/226 年前
2.0.02020/02/106 年前
1.0.02020/01/176 年前

关于 @batterii/encode-object 的常见问题

@batterii/encode-object 是什么?

Object encoding and decoding utils

如何安装 @batterii/encode-object?

在项目目录运行 npm install @batterii/encode-object,或使用 pnpm add / yarn add / bun add。

@batterii/encode-object 支持 TypeScript 吗?

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

@batterii/encode-object 支持 ESM 还是 CommonJS?

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

@batterii/encode-object 需要什么版本的 Node.js?

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

@batterii/encode-object 可以商用吗?

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

@batterii/encode-object 的下载量有多少?

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

@batterii/encode-object 还在维护吗?

最新版本 2.1.1 发布于 2020/02/26(6 年前);至今共发布 4 个版本,其中过去 12 个月发布了 0 个;首次发布于 2020/01/17。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。