@edgefirst-dev/data

中文文档
0.0.4发布于 2025/02/18MITES Module零依赖构建来源已验证近 30 天下载 29.6万

TypeScript utilities for safely parsing and handling structured data using DTOs and parsers for FormData, URLSearchParams, and JSON objects.

原始使用文档
文档目录14 节
  1. Installation
  2. Usage
  3. Importing
  4. Data Class
  5. Example
  6. Parser Classes
  7. Parser Base Class
  8. FormParser
  9. SearchParamsParser
  10. ObjectParser
  11. Error Handling
  12. Example
  13. License
  14. Author

@edgefirst-dev/data

A TypeScript utility library for safely parsing and handling structured data from various sources such as FormData, URLSearchParams, JSON objects, and more. The Data class provides a structured way to define Data Transfer Objects (DTOs), while specialized parsers allow for type-safe access to form fields, URL search parameters, and generic objects.

Installation

bun add @edgefirst-dev/data

Usage

Importing

To use the Data class, import it from the package root:

import { Data } from "@edgefirst-dev/data";

To use the Parser base class or one of the specialized parsers (FormParser, SearchParamsParser, ObjectParser), import them from the parser module:

import {
  Parser,
  FormParser,
  SearchParamsParser,
  ObjectParser,
} from "@edgefirst-dev/data/parser";

Data Class

The Data class is designed to define a set of properties that can be parsed from a source like FormData, URLSearchParams, or a JSON object. You subclass Data to define specific DTOs, using a parser to extract and validate values.

Example

import { Data } from "@edgefirst-dev/data";
import { FormParser } from "@edgefirst-dev/data/parser";

class LoginData extends Data<FormParser> {
  get username() {
    return this.parser.string("username");
  }

  get password() {
    return this.parser.string("password");
  }
}

const formData = new FormData();
formData.append("username", "johndoe");
formData.append("password", "secret");

const loginData = new LoginData(new FormParser(formData));

console.log(loginData.username); // "johndoe"
console.log(loginData.password); // "secret"

Parser Classes

Parser Base Class

The Parser class is an abstract base class designed for parsing and validating data from structured sources. Specialized parsers extend this class to handle specific data types, ensuring type-safe access to required fields.

FormParser

FormParser is a specialized parser that wraps FormData and provides methods to access form fields in a safe and type-validated way.

Example

import { FormParser } from "@edgefirst-dev/data/parser";

const formData = new FormData();
formData.append("username", "johndoe");

const parser = new FormParser(formData);

console.log(parser.string("username")); // "johndoe"

SearchParamsParser

SearchParamsParser extracts and validates URL query parameters from a URLSearchParams object, URL, Request, or URL string.

Example

import { SearchParamsParser } from "@edgefirst-dev/data/parser";

const url = new URL("https://example.com/?search=query");
const parser = new SearchParamsParser(url);

console.log(parser.get("search")); // "query"

ObjectParser

ObjectParser safely accesses and validates values within a plain JavaScript object, ensuring type correctness for the retrieved values.

Example

import { ObjectParser } from "@edgefirst-dev/data/parser";

const data = { name: "Alice", age: 30 };
const parser = new ObjectParser(data);

console.log(parser.string("name")); // "Alice"
console.log(parser.number("age")); // 30

Error Handling

All parser classes throw specialized errors when validation fails:

  • Parser.MissingKeyError: Thrown when a required key is missing.
  • Parser.InvalidTypeError: Thrown when a value is not of the expected type.
  • Parser.InvalidInstanceOfError: Thrown when a value is not an instance of the expected class.
  • Parser.CoercionError: Thrown when a value cannot be coerced to the expected type.

And each one extends the base Parser.Error class, so you can catch all parser errors with a single instanceof condition.

Example

import { ObjectParser } from "@edgefirst-dev/data/parser";

try {
  const data = { name: "Alice", age: 30 };
  const parser = new ObjectParser(data);
  console.log(parser.string("email")); // Throws Parser.MissingKeyError
} catch (error) {
  if (error instanceof Parser.MissingKeyError) {
    // Handle missing key error
  }
  if (error instanceof Parser.Error) {
    // Handle other parser errors
  }
  throw error; // Re-throw unexpected errors
}

License

See LICENSE

Author

版本与发布记录

@edgefirst-dev/data 共发布 4 个版本,首次发布于 2024/09/27;过去 12 个月发布了 0 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.0.4latest2025/02/181.7 年前
0.0.32025/02/111.8 年前
0.0.22024/10/012 年前
0.0.12024/09/272 年前

关于 @edgefirst-dev/data 的常见问题

@edgefirst-dev/data 是什么?

TypeScript utilities for safely parsing and handling structured data using DTOs and parsers for FormData, URLSearchParams, and JSON objects.

如何安装 @edgefirst-dev/data?

在项目目录运行 npm install @edgefirst-dev/data,或使用 pnpm add / yarn add / bun add。

@edgefirst-dev/data 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/edgefirst-dev__data 是否存在。

@edgefirst-dev/data 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@edgefirst-dev/data 需要什么版本的 Node.js?

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

@edgefirst-dev/data 可以商用吗?

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

@edgefirst-dev/data 的下载量有多少?

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

@edgefirst-dev/data 还在维护吗?

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