Skip to content

Repository files navigation

@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";classLoginDataextendsData<FormParser>{getusername(){returnthis.parser.string("username");}getpassword(){returnthis.parser.string("password");}}constformData=newFormData();formData.append("username","johndoe");formData.append("password","secret");constloginData=newLoginData(newFormParser(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";constformData=newFormData();formData.append("username","johndoe");constparser=newFormParser(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";consturl=newURL("https://example.com/?search=query");constparser=newSearchParamsParser(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";constdata={name: "Alice",age: 30};constparser=newObjectParser(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{constdata={name: "Alice",age: 30};constparser=newObjectParser(data);console.log(parser.string("email"));// Throws Parser.MissingKeyError}catch(error){if(errorinstanceofParser.MissingKeyError){// Handle missing key error}if(errorinstanceofParser.Error){// Handle other parser errors}throwerror;// Re-throw unexpected errors}

License

See LICENSE

Author

About

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

Topics

Resources

Contributing

Stars

24 stars

Watchers

0 watching

Forks

Releases

Sponsor this project

Packages

Used by

Contributors

Languages

Generated from edgefirst-dev/package