Registry / storage / rest-resource

rest-resource

JSON →
library0.11.0jsnpmunverified

Simplifies REST API consumption by modeling endpoints as classes with caching, relationship resolution, validation, and attribute normalization. Current version 0.11.0. Release cadence is irregular with last update in 2021. Differentiators include automatic caching with configurable TTL, cross-resource attribute resolution (e.g., await comment.resolveAttribute('post.author.city')), and built-in validation/normalization hooks.

npm install rest-resource
INSTALL
IMPORT
SIG · REST-RESOURCE
R
rest-resource
storagejavascriptv0.11.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Resource
import Resource from 'rest-resource'
const Resource = require('rest-resource')
Default export; ESM import works in Node >=12. CommonJS require may break if module is not transpiled.
Resource
import Resource from 'rest-resource'; class UserResource extends Resource { ... }
import { Resource } from 'rest-resource'
Resource is the default export, not a named export. Named import returns undefined.
types
import type { ResourceType } from 'rest-resource'
TypeScript types are shipped but may not be complete for all customization points.

Shows defining a Resource subclass, fetching detail/list, modifying attributes, and saving.

import Resource from 'rest-resource' class UserResource extends Resource { static endpoint = '/users' greet() { console.log('I am %s', this.attributes.name) } } const main = async () => { const arthur = await UserResource.detail(123) // GET /users/123 arthur.greet() // => I am Arthur arthur.set('name', 'King Arthur') await arthur.save() // PATCH { name: 'King Arthur' } to /users/123 const users = await UserResource.list() // GET /users users.resources.forEach(u => u.greet()) } main()
Debug
Known issues
gotchaStatic endpoint must be defined on the class, not instance.
fix
Use static getter or static property: static endpoint = '/users'
affects: >=0.0.0
gotchaCacheMaxAge defaults to 0 (no cache). For caching, set static get cacheMaxAge() { return 60; }
fix
Define cacheMaxAge static property in your Resource subclass.
affects: >=0.0.0
gotchaThe resource class must be instantiated via static methods (detail, list, etc.). new Resource() will not work.
fix
Always use Resource.detail(id) or Resource.list() to fetch instances.
affects: >=0.0.0
Errors
Common errors & fixes
TypeError: Cannot read property 'endpoint' of undefined
Forgot to define static endpoint on the Resource subclass.
fix
Add static endpoint = '/your-path' to the class.
import { Resource } from 'rest-resource' results in undefined
Resource is the default export, not a named export.
fix
Use import Resource from 'rest-resource'
Resource.detail is not a function
Instance created with new Resource() instead of static method.
fix
Use await Resource.detail(id) instead of new Resource().
Upgrade
Version history
0.11.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
16 hits · last 30 days
node
14
Resources
rest-resource — npm install rest-resource · libregistry