Resources
A Resource class wraps one input object and returns the public API shape from
toArray().
import { Resource } from "@amrachraf6690/resourcesjs";
class UserResource extends Resource<User> { toArray() { return { id: this.resource.id, name: this.resource.name, email: this.resource.email, }; }}make()
Section titled “make()”Use make() to serialize one resource:
const payload = UserResource.make(user);Output:
{ "data": { "id": 1, "name": "John", "email": "john@example.com" }}Null Resources
Section titled “Null Resources”Passing null is explicit and serializes to data: null.
UserResource.make(null);{ "data": null}Serialization Rules
Section titled “Serialization Rules”toArray()should be synchronous.- Source data is not mutated.
- Plain objects and arrays are recursively resolved.
- Nested Resource envelopes are unwrapped to their inner
data. - Non-plain serializable values such as
Dateare preserved. - Cyclic objects and arrays throw a
TypeError.