This project demonstrates how to use the jamespot-user-api package in a Node.js environment.
The jamespot-user-api requires a window object polyfill for Node.js!
See IMPORTANT-NODEJS-SETUP.md for details.
TL;DR: Add this before importing the package:
(global as any).window = {};The jamespot-user-api package provides a typed API client for interacting with Jamespot.Pro backends. While it's primarily designed for browser usage, this sample shows how to adapt it for Node.js using a custom WindowNode adapter.
jUserApiClientSample/
├── src/
│ ├── WindowNode.ts # Node.js adapter for the API (adapted from source)
│ ├── example.ts # Comprehensive example with multiple API calls
│ └── simple-example.ts # Simple login and list groups example
├── package.json
├── tsconfig.json
├── .env.example # Environment variables template
└── README.md
npm installCopy .env.example to .env and update with your Jamespot credentials:
cp .env.example .envEdit .env:
JAMESPOT_URL=https://your-jamespot-instance.com
JAMESPOT_EMAIL=your-email
JAMESPOT_PASSWORD=your-password
DEBUG=falsenpx ts-node src/example.tsThis will:
- Login to Jamespot
- Get current user info
- List groups
- Search for groups
- Get group categories
- Search for users
npx ts-node src/simple-example.tsnpm run buildnode dist/example.jsThe jamespot-user-api package is designed for browsers and expects a window object. For Node.js, you must polyfill it before importing the package:
// This MUST be done before any imports from jamespot-user-api
(global as any).window = {};Without this, you'll get: ReferenceError: window is not defined
The WindowNode class implements the WindowInterface required by the API:
import { WindowNode } from './WindowNode';
const windowNode = new WindowNode('https://your-jamespot-instance.com');Key features:
- Manages cookies automatically for authentication
- Uses
node-fetchfor HTTP requests - Provides debug logging when
DEBUG=true
import { JamespotUserApi, Network } from 'jamespot-user-api';
const network = new Network(windowNode);
const api = new JamespotUserApi(network);IMPORTANT: Always polyfill the window object before importing jamespot-user-api:
// Polyfill window object (required for Node.js)
(global as any).window = {};
import { JamespotUserApi, Network } from 'jamespot-user-api';
import { WindowNode } from './WindowNode';
// Login (use signIn method)
const loginResult = await api.user.signIn('username', 'password');
// List groups
const groups = await api.group.list({
type: 'spot',
limit: 10,
});
// Search users
const users = await api.user.autocomplete('john');The JamespotUserApi provides access to many modules:
api.user- User operations (login, profile, search)api.group- Group/community operationsapi.article- Article/content operationsapi.file- File operationsapi.calendar- Calendar and eventsapi.meeting- Meeting managementapi.taxonomy- Taxonomy/tagsapi.search- Search functionality- And many more...
Enable debug mode to see HTTP requests and cookie management:
DEBUG=true npx ts-node src/example.tsOr pass the --debug flag:
npx ts-node src/example.ts --debugThis project is fully typed using TypeScript. The jamespot-user-api package includes TypeScript definitions, providing:
- Auto-completion in your IDE
- Type checking at compile time
- Better documentation through types
Most API calls return an object with this structure:
{
error: number, // 0 for success, non-zero for errors
errorMsg?: string, // Error message if error !== 0
result: T, // The actual result data
}Always check error === 0 before accessing result:
const response = await api.user.getCurrent();
if (response.error === 0) {
console.log('User:', response.result);
} else {
console.error('Error:', response.errorMsg);
}// Login (use signIn method)
const loginResult = await api.user.signIn('username', 'password');
// Get current user (use the uri from login result)
const user = await api.user.get(loginResult.result.uri);// List all groups
const groups = await api.group.list({
type: 'spot',
limit: 50
});
// Search groups
const searchResults = await api.group.list({
type: 'spot',
query: 'project',
limit: 10
});
// Get specific group
const group = await api.group.getSpot('123');
// Get group members
const members = await api.group.getObjectListJamespotSpotMembers('123');// Autocomplete/search users
const users = await api.user.autocomplete('john');
// Get user by ID
const user = await api.user.getUser('123');- The WindowNode adapter manages authentication cookies automatically
- All API calls are asynchronous (use
awaitor.then()) - Environment variables can be overridden by modifying the code directly
- The session persists as long as the API instance is alive
jamespot-user-api- The main API clientnode-fetch- HTTP client for Node.jstypescript- TypeScript compilerts-node- Run TypeScript directly
- IMPORTANT-NODEJS-SETUP.md - Critical window polyfill requirement
- QUICKSTART.md - Quick start guide
- TROUBLESHOOTING.md - Common issues and solutions
ISC