{"id":17781,"library":"little-api","title":"Little API","description":"`little-api` is a JavaScript library designed for creating simple JSON-over-HTTP/WS RPC servers and clients with minimal configuration. It acts as a lightweight wrapper around common web technologies like Express (for HTTP servers), XHRs (for HTTP clients), and WebSockets. The current stable version is 2.0.1. It provides a straightforward way to define server-side methods (both standard and WebSocket-specific) that are then exposed to a client-side API. A key differentiator is its emphasis on simplicity and low boilerplate, allowing developers to quickly set up RPC communication without diving into complex protocols. It strictly uses JSON as the transport format, meaning all arguments and return values must be JSON-serializable. Client-side usage relies on browser globals such as `XMLHttpRequest`, `WebSocket`, `btoa`, and `Promise`. The library does not specify a strict release cadence but provides a functional abstraction over underlying network primitives.","status":"active","version":"2.0.1","language":"javascript","source_language":"en","source_url":null,"tags":["javascript","json","rest","api","server","client","rpc","http","express"],"install":[{"cmd":"npm install little-api","lang":"bash","label":"npm"},{"cmd":"yarn add little-api","lang":"bash","label":"yarn"},{"cmd":"pnpm add little-api","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Used internally for HTTP server functionality.","package":"express","optional":false}],"imports":[{"note":"The examples provided use CommonJS `require`. For ESM, you might need a transpiler or a different import path if the package doesn't expose an ESM entry point for its server module.","wrong":"import { createServer } from 'little-api/server';","symbol":"createServer","correct":"const createServer = require('little-api/server');"},{"note":"The examples provided use CommonJS `require`. For ESM, you might need a transpiler or a different import path if the package doesn't expose an ESM entry point for its client module.","wrong":"import { createClient } from 'little-api/client';","symbol":"createClient","correct":"const createClient = require('little-api/client');"}],"quickstart":{"code":"const createServer = require(\"little-api/server\");\nconst createClient = require(\"little-api/client\");\n\nconst PORT = 8080;\n\n// --- Server Setup ---\nconst server = createServer({\n  methods: {\n    uppercase(...words) {\n      return words.map((word) => word.toUpperCase());\n    },\n    add(a, b) {\n      return a + b;\n    }\n  },\n});\n\nserver.listen(PORT, () => {\n  console.log(`Server is listening on port ${PORT}`);\n\n  // --- Client Usage (after server starts) ---\n  const api = createClient({\n    url: `http://localhost:${PORT}`,\n    methods: [\"uppercase\", \"add\"],\n  });\n\n  api.uppercase(\"hello\", \"world\").then((results) => {\n    console.log(\"Uppercase results:\", results); // Expected: [\"HELLO\", \"WORLD\"]\n  }).catch(console.error);\n\n  api.add(5, 3).then((result) => {\n    console.log(\"Addition result:\", result); // Expected: 8\n  }).catch(console.error);\n\n  // Example of synchronous call (use with caution in browser)\n  try {\n    const syncResults = api.uppercase.sync(\"sync\", \"test\");\n    console.log(\"Synchronous uppercase results:\", syncResults); // Expected: [\"SYNC\", \"TEST\"]\n  } catch (e) {\n    console.warn(\"Synchronous XHR failed, likely not in browser or sync XHR disabled:\", e.message);\n  }\n\n  // To gracefully shut down the server after a short period for this example\n  setTimeout(() => {\n    server.close(() => {\n      console.log('Server closed.');\n    });\n  }, 2000);\n});\n","lang":"javascript","description":"This quickstart demonstrates setting up a `little-api` HTTP server with a few methods and then consuming those methods from a `little-api` client, including both asynchronous and synchronous calls."},"warnings":[{"fix":"Ensure all data passed to and from API methods adheres to the JSON specification (strings, numbers, booleans, null, arrays, plain objects).","message":"All data exchanged between client and server (arguments, return values) must be JSON-serializable. Complex types like functions, Dates, BigInts, or objects with circular references will not serialize correctly.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Use `little-api/client` primarily in browser environments. If attempting to use in Node.js, ensure appropriate polyfills are available or consider a different RPC client solution.","message":"The client-side library (`little-api/client`) relies on browser global objects (`XMLHttpRequest`, `WebSocket`, `btoa`, `Promise`). It is not intended for Node.js client-side usage without polyfills for these globals.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Prefer the default asynchronous (Promise-based) client methods. Avoid `.sync` calls unless absolutely necessary for specific legacy contexts, and be aware of the performance implications.","message":"The `.sync` property on client methods enables synchronous XHRs. Synchronous XHRs are largely deprecated in modern web development due to their blocking nature, which can freeze the browser UI and lead to a poor user experience.","severity":"deprecated","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-23T00:00:00.000Z","next_check":"2026-07-22T00:00:00.000Z","problems":[{"fix":"Refactor your data structures to avoid circular references. If you need to pass complex objects, ensure they are simplified or transformed into JSON-serializable forms.","cause":"An argument passed from the client to the server, or a return value from the server to the client, contains a circular reference and cannot be serialized to JSON.","error":"TypeError: Converting circular structure to JSON"},{"fix":"Ensure the client-side code runs in a browser environment or provide a suitable polyfill for `XMLHttpRequest` if running in a non-browser environment.","cause":"The `little-api` client is being used in an environment (e.g., Node.js) where `XMLHttpRequest` is not a global object.","error":"ReferenceError: XMLHttpRequest is not defined"},{"fix":"Change the port number for the `little-api` server or terminate the process currently using the desired port.","cause":"The port specified for the `little-api` server is already being used by another process.","error":"Error: listen EADDRINUSE: address already in use :::8080"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}