shred
raw JSON → 1.1.0-alpha-10 verified Sat Apr 25 auth: no javascript
Shred is a resource-oriented HTTP client for JavaScript/Node.js that allows declarative definition of API wrappers with support for URL templates, response compression, authorization, and streaming. The current version is 1.1.0-alpha-10 (alpha quality). It is a complete reboot from 1.0.x, not backwards compatible. Key differentiator: declarative resource definition using CoffeeScript/JavaScript objects, focusing on HTTP resource abstraction rather than low-level requests.
Common errors
error TypeError: Cannot read properties of undefined (reading 'data') ↓
cause Forgetting to await the response object before accessing .data.
fix
const { data } = await request();
error Error: Unexpected token o in JSON at position 1 ↓
cause Non-JSON response parsed as JSON because content-type indicated JSON but body is not valid JSON.
fix
Check server response or set headers appropriately.
Warnings
breaking Shred 1.0.x is a complete reboot and not backwards compatible with earlier versions (0.x). ↓
fix Update code to use new resource-based declarative API; see README for migration guide.
deprecated Shred 1.x is alpha quality; API may change before stable release. ↓
fix Pin to specific version and test thoroughly when upgrading.
gotcha Response 'data' is a promise that resolves when streaming finishes; must be awaited. ↓
fix Use await on response.data to get parsed JSON or string body.
gotcha URL templates use colon syntax (e.g., '/:id') not '{id}' pattern in some versions; verify README. ↓
fix Use library's documented template syntax.
Install
npm install shred yarn add shred pnpm add shred Imports
- resource wrong
const shred = require('shred');correctimport { resource } from 'shred' - invoke
import { invoke } from 'shred' - ShredResponse
import type { ShredResponse } from 'shred'
Quickstart
const { resource } = require('shred');
const github = resource('https://api.github.com/', {
repos: (resource) => resource('repos/{owner}/{repo}/', {
issues: (resource) => resource('issues', {
list: { method: 'get', headers: { accept: 'application/vnd.github.v3.raw+json' }, expect: 200 }
})
})
});
async function main() {
try {
const { data } = await github.repos({ owner: 'user', repo: 'repo' }).issues.list();
console.log(data);
} catch (error) {
console.error(error);
}
}
main();