Registry / serialization / fortune-json-api

fortune-json-api

JSON →
library2.3.1jsnpmunverified

This package functions as a robust JSON API serializer specifically designed for Fortune.js. It meticulously implements all features outlined in the JSON API specification (jsonapi.org) and adheres closely to its recommendations. The current stable version is 2.3.1. While there isn't a fixed release schedule, updates are typically driven by dependency upgrades, bug fixes, and occasional feature additions, as observed in recent minor bumps. Its core differentiators include deep integration with the Fortune.js data abstraction layer, ensuring consistent and compliant JSON API data formatting, and extensive configurability through an `options` object. This allows developers to fine-tune aspects like URL prefixes, field and type inflection, pagination limits, and buffer encoding, making it an effective tool for building hypermedia-driven APIs on top of Fortune.js without manual serialization overhead.

serializationweb-framework
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

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

The primary export is the default serializer function. This package is primarily CommonJS-first, so direct ESM `import` might require Node.js's ESM compatibility layers or bundler configuration. The `require` syntax is shown in all documentation examples.

import jsonApiSerializer from 'fortune-json-api'

This is the idiomatic CommonJS import shown in the official documentation and examples. The serializer is a default export.

const jsonApiSerializer = require('fortune-json-api')

This example sets up a basic Fortune.js server with the JSON API serializer, creating an HTTP listener that serves a 'users' and 'posts' resource type over a `/api` prefix.

const http = require('http'); const fortune = require('fortune'); const fortuneHTTP = require('fortune-http'); const jsonApiSerializer = require('fortune-json-api'); // Define your Fortune.js record types const store = fortune({ user: { email: String, name: String, posts: [Array('post'), 'author'] }, post: { title: String, content: String, author: ['user', 'posts'] } }); const listener = fortuneHTTP(store, { serializers: [ // The `options` object here is optional. [ jsonApiSerializer, { prefix: '/api', jsonSpaces: 2 } ] ] }); const server = http.createServer((request, response) => listener(request, response) .catch(error => { console.error('API Error:', error); response.writeHead(500, { 'Content-Type': 'application/vnd.api+json' }); response.end(JSON.stringify({ errors: [{ status: '500', title: 'Internal Server Error', detail: error.message }] })); }) ); const port = process.env.PORT ?? 8080; server.listen(port, () => console.log(`JSON:API server listening on http://localhost:${port}/api`)); // Example: How to interact (e.g., in another file or via curl) /* curl -X POST -H "Content-Type: application/vnd.api+json" -d '{ "data": { "type": "users", "attributes": { "email": "test@example.com", "name": "Test User" } } }' http://localhost:8080/api/users curl http://localhost:8080/api/users */
Debug
Known footguns
breakingPrior to Fortune.js v1.0.0-rc.13, the JSON API serializer was bundled directly within the `fortune` package. This changed to an external module, `fortune-json-api`, requiring explicit installation and import for any projects migrating from very old `fortune` versions (pre-1.0.0-rc.13) to newer ones.
gotchaThis library is tightly coupled with `fortune` and `fortune-http`. Mismatched major versions of these peer dependencies can lead to unexpected behavior or runtime errors. Always check the `fortune-json-api`'s `package.json` for compatible peer dependency ranges.
gotchaThe `prefix` option, if set to a path starting with `/`, will rewrite URLs relative to that prefix. For example, a prefix of `/api` will make requests for `/api/users/1` map to the `users` resource with ID `1`. Misunderstanding this behavior can lead to incorrect routing or URL generation.
gotchaThis package is primarily designed for CommonJS (`require`) environments, and all official examples use this syntax. While it might work with ESM via bundlers or Node.js's compatibility layers, explicit ESM support (e.g., named exports, `.mjs` files) is not documented, which can lead to import errors in pure ESM projects.
Upgrade
Version history

Breaking-change detection hasn't run for this library yet.

Audit
Security & dependencies

CVE tracking and dependency tree are planned for a later release.

Agent activity
9 hits · last 30 days
gptbot
4
ahrefsbot
4
script
1
Resources