Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
@edgefirst-dev/data
Advanced tools
TypeScript utilities for safely parsing and handling structured data using DTOs and parsers for FormData, URLSearchParams, and JSON objects.
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.
bun add @edgefirst-dev/data
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";
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.
import { Data } from "@edgefirst-dev/data";
import { FormParser } from "@edgefirst-dev/data/parser";
class LoginData extends Data<FormParser> {
get username() {
return this.parser.getString("username");
}
get password() {
return this.parser.getString("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"
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
is a specialized parser that wraps FormData
and provides methods to access form fields in a safe and type-validated way.
import { FormParser } from "@edgefirst-dev/data/parser";
const formData = new FormData();
formData.append("username", "johndoe");
const parser = new FormParser(formData);
console.log(parser.getString("username")); // "johndoe"
SearchParamsParser
extracts and validates URL query parameters from a URLSearchParams
object, URL
, Request
, or URL string.
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
safely accesses and validates values within a plain JavaScript object, ensuring type correctness for the retrieved values.
import { ObjectParser } from "@edgefirst-dev/data/parser";
const data = { name: "Alice", age: 30 };
const parser = new ObjectParser(data);
console.log(parser.getString("name")); // "Alice"
console.log(parser.getNumber("age")); // 30
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.import { ObjectParser } from "@edgefirst-dev/data/parser";
try {
const data = { name: "Alice", age: 30 };
const parser = new ObjectParser(data);
console.log(parser.getString("email")); // Throws Parser.MissingKeyError
} catch (error) {
if (error instanceof Parser.MissingKeyError) {
console.error(error.message); // Key "email" does not exist
}
throw error; // Re-throw unexpected errors
}
See LICENSE
FAQs
TypeScript utilities for safely parsing and handling structured data using DTOs and parsers for FormData, URLSearchParams, and JSON objects.
The npm package @edgefirst-dev/data receives a total of 4,990 weekly downloads. As such, @edgefirst-dev/data popularity was classified as popular.
We found that @edgefirst-dev/data demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.