Registry / devops / json-rest-api

json-rest-api

JSON →
library1.0.24jsnpmunverified

A REST API plugin for hooked-api that provides JSON:API-compliant endpoints with minimal configuration. Version 1.0.24 is stable. Key differentiators: full JSON:API specification support, relationship management (belongsTo, hasMany, polymorphic), advanced querying (filter, sort, paginate, sparse fieldsets), file uploads with storage adapters, schema-based validation, and framework-agnostic design via connectors. Suitable for building production-grade REST APIs quickly.

npm install json-rest-api
INSTALL
IMPORT
SIG · JSON-REST-API
J
json-rest-api
devopsjavascriptv1.0.24
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.

RestApiPlugin
import { RestApiPlugin } from 'json-rest-api';
import RestApiPlugin from 'json-rest-api';
Named export, not default. Always use named import.
RestApiKnexPlugin
import { RestApiKnexPlugin } from 'json-rest-api';
Required for database integration with knex.
ExpressPlugin
import { ExpressPlugin } from 'json-rest-api';
import { ExpressPlugin } from 'json-rest-api/express';
All plugins are exported from the main package entry point.
Api
import { Api } from 'hooked-api';
import { Api } from 'json-rest-api';
Api class is from 'hooked-api', not from 'json-rest-api'.
all exports (CJS)
const { RestApiPlugin, RestApiKnexPlugin, ExpressPlugin } = require('json-rest-api');
const jsonRestApi = require('json-rest-api');
Use destructuring to access individual exports.

Sets up a JSON:API server with SQLite, defines an 'authors' resource, and starts Express listener.

import { RestApiPlugin, RestApiKnexPlugin, ExpressPlugin } from 'json-rest-api'; import { Api } from 'hooked-api'; import knexLib from 'knex'; import express from 'express'; const knex = knexLib({ client: 'better-sqlite3', connection: { filename: ':memory:' } }); const api = new Api({ name: 'book-catalog-api', logLevel: 'trace' }); await api.use(RestApiPlugin); await api.use(RestApiKnexPlugin, { knex }); await api.use(ExpressPlugin, { mountPath: '/api' }); await api.addResource('authors', { schema: { name: { type: 'string', required: true, max: 100, search: true }, surname: { type: 'string', required: true, max: 100, search: true }, }, }); await api.resources.authors.createKnexTable(); const app = express(); app.use('/api', api.router); app.listen(3000, () => { console.log('Express server started on port 3000. API available at http://localhost:3000/api'); });
Debug
Known issues
gotchaMust install 'knex' (and a database driver like 'better-sqlite3') separately, not included as dependency.
fix
Run 'npm install knex better-sqlite3' in addition to 'json-rest-api'.
affects: >=1.0.0
breakingSocket.io peer dependency changed from v4 to v4.8.1 in version 1.0.20; mismatch causes connection errors.
fix
Update socket.io to ^4.8.1 if using real-time features.
affects: >=1.0.20
deprecatedThe 'RestApiSocketIoPlugin' has been renamed to 'SocketIoPlugin' as of version 1.0.18.
fix
Use 'import { SocketIoPlugin } from 'json-rest-api';' and update references.
affects: >=1.0.18
gotchaUsing Express? You must install 'express' as a separate dependency and use 'ExpressPlugin' exported from the main package.
fix
Run 'npm install express' and import 'ExpressPlugin' from 'json-rest-api'.
affects: >=1.0.0
gotchaJSON:API compliance requires correct MIME type 'application/vnd.api+json' for requests and responses; otherwise validation fails.
fix
Set Content-Type header to 'application/vnd.api+json' in client requests.
affects: >=1.0.0
gotchaKnex table creation via 'createKnexTable()' is required before accessing resources; missing step leads to 'Table not found' errors.
fix
Call 'await api.resources.authors.createKnexTable();' after adding the resource.
affects: >=1.0.0
Errors
Common errors & fixes
Cannot find module 'hooked-api'
Missing peer dependency.
fix
Run 'npm install hooked-api'.
TypeError: api.use is not a function
Api not imported from 'hooked-api' or incorrectly constructed.
fix
Ensure correct import: 'import { Api } from 'hooked-api';' and create instance with 'new Api(...)'.
Error: listen EADDRINUSE :::3000
Port already in use by another process.
fix
Kill the process using port 3000 or change the port in app.listen.
Uncaught TypeError: knexLib is not a function
Incorrect import of knex for ESM modules.
fix
Use 'import knexLib from 'knex';' (default import) instead of 'const { knex } = require('knex');'.
Upgrade
Version history
1.0.24latest on npm
Audit
Dependencies
hooked-apirequiredCore framework dependency; json-rest-api is a plugin for hooked-api
knexoptionalRequired for database operations via RestApiKnexPlugin
expressoptionalRequired for Express connector
socket.iooptionalOptional peer dependency for real-time features
joseoptionalOptional peer dependency for JWT authentication
Agent activity
12 hits · last 30 days
node
10
Resources
json-rest-api — npm install json-rest-api · libregistry