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

abax-node-sdk

v3.4.1

Published

SDK for Abax API

Downloads

23

Readme

ABAX API client built using Typescript for Node.js. This SDK is used by multiple integrations with the ABAX API, but it is still in early development.

Features

  • Built with Typescript
  • Uses undici (Faster requests 🚀)
  • Pure ESM

The endpoints currently supported are:

  • GET v1/vehicles
  • GET v1/trips
  • GET v1/trips/odometerReadings
  • GET v1/trips/expense
  • GET v2/equipment
  • GET v2/equipment/{id}
  • GET v2/equipment/usage-log

Installation

pnpm add abax-node-sdk

Usage

import { AbaxClient } from 'abax-node-sdk';

const client = new AbaxClient({
  apiKey: 'xxxxx',
});

client.getVehicles().then(vehicles => {
  console.log(vehicles);
});

Authentication

If you don't know how to authenticate with the Abax API, you should consult their documentation. This SDK comes packed with AbaxAuth which helps you solve the authentication part.

When using Authorization Code Flow

The first step is to create an instance of AbaxAuth and pass in your clientId and clientSecret. You can then use the getAuthorizationUrl method to get the URL you need to redirect the user to. After the user has authorized your application, they will be redirected to the redirectUri you specified when creating the AbaxAuth instance. The redirectUri will contain a code query parameter which you can use to get an access token.

import { AbaxAuth } from 'abax-node-sdk';

const auth = new AbaxAuth({
  clientId: 'xxxxx',
  clientSecret: 'xxxxx',
  redirectUri: 'https://example.com/auth/callback',
});

const authorizationUrl = auth.getAuthorizationUrl();

After the user has authorized your application, they will be redirected to the redirectUri you specified when creating the AbaxAuth instance. The redirectUri will contain a code query parameter which you can use to get an access token.

import { AbaxAuth } from 'abax-node-sdk';

const auth = new AbaxAuth({
  clientId: 'xxxxx',
  clientSecret: 'xxxxx',
  redirectUri: 'https://example.com/auth/callback',
});

/**
 * Typically you get this code from the query parameters in the redirect URI.
 * e.g. https://example.com/auth/callback?code=xxxxx
 */
const authorizationCode = 'xxxxx';

/**
 * The function below will request the credentials from the Abax Identity API,
 * and store them in memory (it also returns the credentials, if you need them, eg. for storing in database).
 */
await auth.getCredentialsFromCode(authorizationCode);

/**
 * You can pass auth.getAccessToken() to the AbaxClient constructor.
 * This will automatically refresh the access token when it expires.
 */
const client = new AbaxClient({
  apiKey: () => auth.getAccessToken(),
});

When using Client Credentials Flow

import { AbaxAuth } from 'abax-node-sdk';

const auth = new AbaxAuth({
  clientId: 'xxxxx',
  clientSecret: 'xxxxx',
});

await auth.getCredentialsFromClientCredentials();

/**
 * You can pass auth.getAccessToken() to the AbaxClient constructor.
 * This will automatically refresh the access token when it expires.
 */
const client = new AbaxClient({
  apiKey: () => auth.getAccessToken(),
});

Saving and loading credentials

If you want to save the credentials to a database, you can use the getCredentials method on AbaxAuth. This will return the credentials as an object, which you can then store in your database. Alternatively does both getCredentialsFromCode and getCredentialsFromClientCredentials return the credentials, so you can also store them from there.

import { AbaxAuth } from 'abax-node-sdk';

const auth = new AbaxAuth({
  clientId: 'xxxxx',
  clientSecret: 'xxxxx',
});

const credentials = await auth.getCredentialsFromCode(authorizationCode);

// Store credentials in database

When you want to load the credentials from the database, you can use the setCredentials method on AbaxAuth. This will set the credentials on the

import { AbaxAuth } from 'abax-node-sdk';

const auth = new AbaxAuth({
  clientId: 'xxxxx',
  clientSecret: 'xxxxx',
});

// Load credentials from database
auth.setCredentials(credentialsFromDatabase);

/**
 * You can pass auth.getAccessToken() to the AbaxClient constructor.
 * This will automatically refresh the access token when it expires.
 */
const client = new AbaxClient({
  apiKey: () => auth.getAccessToken(),
});

Setting scopes and automatic refreshing

You can set the scopes you want to request when authenticating with the Abax Identity API. You can do this by passing the scopes option to AbaxAuth.

import { AbaxAuth } from 'abax-node-sdk';

const auth = new AbaxAuth({
  clientId: 'xxxxx',
  clientSecret: 'xxxxx',
  scopes: ['open_api', 'open_api.equipment'],
});

Note: Automatic refreshing of the access token is only supported when using the offline_access scope. As far as we know, this does not give access for longer than 30 days, so you will have to re-authenticate after 30 days.

See the scopes available in the Abax Identity API documentation on scopes.

Contributing

We love contributions! Please read our Contributing Guide to learn how to contribute to this project.