ApiPublisher

raw JSON →
2.1.5 verified Fri May 01 auth: no javascript

ApiPublisher is a Node.js framework for exposing asynchronous JavaScript APIs (returning Promises) over HTTP, enabling identical async function calls on client and server. Current version 2.1.5 requires Node >=10.0.0. It ships TypeScript types. Key differentiators: works seamlessly with Connect/Express, supports ES7 async/await via Nodent, offers browser RemoteApi with caching (including localStorage), and has been in production since 2013. Unlike generic RPC libraries, it preserves Promise semantics and supports nested APIs automatically.

error Cannot find module 'apipublisher'
cause Package not installed or wrong import path.
fix
Run npm install apipublisher and ensure you are using ESM import syntax (e.g., import { ApiPublisher } from 'apipublisher').
error TypeError: api is not a function
cause Passing the whole API object to app.use() instead of api.handle.
fix
Use app.use('/path', api.handle).
error SyntaxError: await is only valid in async function
cause Using 'await' outside an async function in non-ES7 code.
fix
Wrap the code in an async function: async function main() { ... } or use Promises with .then().
error ReferenceError: require is not defined
cause Using require() in an ESM context.
fix
Use import { ... } from 'apipublisher' or rename file to .cjs.
breaking ApiPublisher v1.1.x breaking change: you must pass `api.handle` to `app.use()`, not just `api`.
fix Replace `app.use('/api', api)` with `app.use('/api', api.handle)`.
breaking ESM-only since v2.0.0; CommonJS require() will fail.
fix Use `import` syntax or upgrade to Node >=12 with --experimental-modules for older versions.
deprecated Version 2.1.5 still uses Nodent internally; Nodent is unmaintained and may cause compatibility issues with modern Node.js.
fix Consider migrating to a library that uses native async/await and standard Promises.
gotcha Browser RemoteApi automatically provisions nested APIs under the same path; ensure your API object does not have overlapping property names with built-in methods like 'resolve' or 'then'.
fix Avoid property names that conflict with Promise or Object prototypes.
gotcha ServerApi.load() accepts a URL object with options like 'agent', but the URL must be an instance of the Node URL class; plain strings are also accepted.
fix Use `new URL('http://example.com/api')` if passing an object, or simply a string URL.
npm install apipublisher
yarn add apipublisher
pnpm add apipublisher

Exposes an async API over HTTP using Express. Note: 'api.handle' must be passed to app.use(), not the API object itself.

import { ApiPublisher } from 'apipublisher';
import express from 'express';

const app = express();

const myAPI = {
  greet: async (name) => `Hello, ${name}!`,
  add: async (a, b) => a + b
};

const api = new ApiPublisher(myAPI);
app.use('/api', api.handle);

app.listen(3000, () => {
  console.log('Server listening on port 3000');
});