npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

odoo-xmlrpc-ts

v1.0.4

Published

Type-safe Odoo XML-RPC client for Node.js written in TypeScript

Downloads

302

Readme

odoo-xmlrpc-ts

A type-safe Odoo XML-RPC client for Node.js written in TypeScript. This package provides a robust interface to interact with Odoo's external API through XML-RPC.

Features

  • ✨ Full TypeScript support with type definitions
  • 🔄 Promise-based API
  • 🔐 Automatic authentication handling
  • 🛡️ Comprehensive error handling
  • 🎯 Support for all major Odoo operations
  • 📝 Built-in TypeScript interfaces for Odoo models
  • 🔍 Type-safe domain builders
  • 📦 Zero external dependencies except xmlrpc

Prerequisites

  • Node.js >= 16
  • pnpm >= 8
  • Odoo instance with XML-RPC enabled
  • API access enabled in your Odoo instance

Installation

pnpm add odoo-xmlrpc-ts

Using npm:

npm install odoo-xmlrpc-ts

Using yarn:

yarn add odoo-xmlrpc-ts

Usage

Basic Example

import { OdooClient } from 'odoo-xmlrpc-ts';

// Define your model interfaces
interface Partner {
  id: number;
  name: string;
  email?: string;
  is_company: boolean;
}

async function example() {
  // Initialize client
  const client = new OdooClient({
    url: 'https://your-odoo-instance.com',
    db: 'your-database',
    username: 'admin',
    password: 'admin',
  });

  try {
    // Search and read partners
    const partners = await client.searchRead<Partner>('res.partner', [['is_company', '=', true]], {
      fields: ['name', 'email'],
      limit: 10,
    });

    console.log('Partners:', partners);
  } catch (error) {
    if (error instanceof OdooError) {
      console.error('Odoo Error:', error.message);
    }
  }
}

Advanced Usage

import { OdooClient, OdooBaseModel } from 'odoo-xmlrpc-ts';

// Extend the base model interface
interface CustomPartner extends OdooBaseModel {
  name: string;
  email: string;
  phone?: string;
  is_company: boolean;
  child_ids: number[];
}

async function advancedExample() {
  const client = new OdooClient({
    url: 'https://your-odoo-instance.com',
    db: 'your-database',
    username: 'admin',
    password: 'admin',
  });

  // Create a new partner
  const partnerId = await client.create<Partial<CustomPartner>>('res.partner', {
    name: 'Test Company',
    is_company: true,
    email: '[email protected]',
  });

  // Read the created partner
  const [partner] = await client.read<CustomPartner>('res.partner', [partnerId]);

  // Update the partner
  await client.write<Partial<CustomPartner>>('res.partner', [partnerId], {
    phone: '+1234567890',
  });

  // Delete the partner
  await client.unlink('res.partner', [partnerId]);
}

API Reference

Constructor

const client = new OdooClient({
  url: string;    // Odoo instance URL
  db: string;     // Database name
  username: string;
  password: string;
});

Methods

async version(): Promise<OdooVersion>

Get Odoo server version information.

async authenticate(): Promise<number>

Authenticate with the Odoo server. Called automatically when needed.

async search(model: string, domain: OdooDomain, options?: SearchOptions): Promise<number[]>

Search for record IDs.

interface SearchOptions {
  offset?: number;
  limit?: number;
  order?: string;
}

async searchRead<T>(model: string, domain: OdooDomain, options?: SearchReadOptions): Promise<T[]>

Search and read records in one call.

interface SearchReadOptions extends SearchOptions {
  fields?: string[];
}

async read<T>(model: string, ids: number[], fields?: string[]): Promise<T[]>

Read specific records by ID.

async create<T>(model: string, values: T): Promise<number>

Create a new record.

async write<T>(model: string, ids: number[], values: T): Promise<boolean>

Update existing records.

async unlink(model: string, ids: number[]): Promise<boolean>

Delete records.

async fieldsGet(model: string, attributes?: string[]): Promise<OdooFieldsMap>

Get field information for a model.

Error Handling

The client includes built-in error classes:

  • OdooError: Base error class for all Odoo-related errors
  • OdooAuthenticationError: Authentication-specific errors
try {
  await client.authenticate();
} catch (error) {
  if (error instanceof OdooAuthenticationError) {
    console.error('Authentication failed:', error.message);
  }
}

Development

# Install dependencies
pnpm install

# Build
pnpm run build

# Run tests
pnpm test

# Run tests with coverage
pnpm test:coverage

# Lint
pnpm run lint

# Format code
pnpm run format

# Type check
pnpm run type-check

Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'feat: add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Common Issues

CORS Issues

If you're using this client in a browser environment, you might encounter CORS issues. This client is intended for Node.js usage. For browser environments, consider using Odoo's JSON-RPC interface instead.

Authentication Issues

Make sure your Odoo instance has XML-RPC enabled and your user has the necessary access rights. For Odoo.sh or Odoo Online instances, you might need to whitelist your IP address.

License

MIT © Dilip Ray Ch