Registry / server / rest-contracts-express-server

rest-contracts-express-server

JSON →
library1.1.3jsnpmunverified

A TypeScript library that automatically generates strongly-typed Express route handlers from REST API contracts defined via the rest-contracts package. Version 1.1.3 is the current stable release; no recent updates or defined release cadence. It eliminates manual type mapping by allowing developers to define contracts (method, path, parameters, return type) and then call an implement() method on RestContractsExpressServer, which enforces type safety at compile time and generates Express handlers. Key differentiators: minimal footprint, no code generation step, pure TypeScript compile-time enforcement, and seamless integration with both client and server sides using the same contract definitions. Requires peer dependency rest-contracts@^1.0.8 and Node >=6.9.

npm install rest-contracts-express-server
INSTALL
IMPORT
SIG · REST-CONTRACTS-EXP
R
rest-contracts-express-server
serverjavascriptv1.1.3
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.

RestContractsExpressServer
import { RestContractsExpressServer } from 'rest-contracts-express-server'
const RestContractsExpressServer = require('rest-contracts-express-server')
ESM import is preferred; CommonJS require works but the library ships TypeScript types, so use named import.
RestContractsExpressServer
import RestContractsExpressServer from 'rest-contracts-express-server'
const RestContractsExpressServer = require('rest-contracts-express-server').RestContractsExpressServer
Default import is also valid since the package exports a default export; avoid mixing named and default.
RestContractsExpressServer
const { RestContractsExpressServer } = require('rest-contracts-express-server')
const RestContractsExpressServer = require('rest-contracts-express-server')
CommonJS destructured require works; plain require returns the module object. Use destructuring.

Demonstrates defining a REST contract with rest-contracts and implementing it with rest-contracts-express-server, showing automatic type safety for path parameters and return value.

import * as express from 'express'; import { RestContractsExpressServer } from 'rest-contracts-express-server'; import { API } from 'rest-contracts'; // Define a contract const GetUser = API.Get .Path('/users/:id/') .PathParameters<{ id: string }>() .Returns<{ name: string; email: string }>(); // Create Express app and router const app = express(); const router = express.Router(); // Create server instance const server = new RestContractsExpressServer(router); // Implement the contract server.implement(GetUser, (params) => { // params is typed as { id: string } return { name: 'John Doe', email: 'john@example.com' }; }); app.use('/api', router); app.listen(3000);
Debug
Known issues
deprecatedThe package rest-contracts-express-server is deprecated in favor of rest-contracts-express-server v2? (Not specified, but the GitHub repo recommends using rest-contracts with other server packages).
fix
Consider migrating to a more actively maintained alternative like rest-contracts-lambda or implementing your own server adapter.
affects: 1.x
gotchaRequires Express.Router(), not Express application instance directly. Passing app instead of router will cause runtime errors.
fix
Always create an express.Router() and pass it to the constructor, then mount the router on the app.
affects: >=1.0.0
gotchaMust use the same router instance for all implement() calls; otherwise routes may not register correctly.
fix
Pass the same router object to all RestContractsExpressServer instances or reuse the same server instance.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: Cannot read properties of undefined (reading 'path')
Passing an undefined or null contract to implement()
fix
Ensure the contract is properly defined and imported before calling implement(contract, handler).
TypeScript error: Argument of type '...' is not assignable to parameter of type '...'
Mismatched types between contract and handler – e.g., handler returns wrong shape
fix
Check that the handler's return type matches the Returns<> type in the contract.
Error: Route not found
The router is not mounted on the Express app or the base path is wrong
fix
Ensure app.use('/basepath', router) is called after all implement() calls.
Upgrade
Version history
1.1.3latest on npm
Audit
Dependencies
rest-contractsrequiredPeer dependency required for defining API contracts; the express server consumes contracts exported by rest-contracts.
Agent activity
39 hits · last 30 days
node
32
OpenAI (training)
1
Resources
rest-contracts-express-server — npm install rest-contracts-express-server · libregistry