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

octane-node

v2.3.0

Published

Node bindings for the Octane API

Downloads

301

Readme

Octane Node.js Library

Version GitHub Actions status

Octane

The Octane Node.js library provides programmatic access to the Octane API for server-side JavaScript apps.


Getting started

First, install octane-node and add it to your package.json dependencies:

npm install octane-node --save

We use the fetch API to handle making all of our requests. If your environment doesn't have fetch available, you'll need to polyfill it using something like node-fetch:

npm install node-fetch --save

Next, obtain an API key from within the Octane portal, and set it in your environment:

export OCTANE_API_KEY="<insert_octane_api_key_here>"

Then, from within your application, import the module (and optionally the fetch polyfill):

import fetch from 'node-fetch';
import Octane from 'octane-node';
const octane = new Octane(process.env.OCTANE_API_KEY, {
  // If your fetch polyfill isn't on the global scope,
  // provide it to the SDK explicitly.
  fetchApi: fetch,
});

CommonJS support

The octane-node library is compatible with CommonJS-style imports, but unfortunately, node-fetch is not by default. If you need to use CommonJS and do not have fetch available on the global scope, octane-node allows passing in any fetch library directly. For example, you can provide a copy of node-fetch v2, which supports CommonJS-style imports:

const fetch = require('node-fetch');

const octane = require('octane-node')(process.env.OCTANE_API_KEY, {
  fetchApi: fetch,
});

For more details on compatibility between node-fetch and CommonJS, check out this section in their docs.

Configuring the SDK

The SDK accepts an optional configuration object. You can use it to point the SDK at a different host or to enable experimental features.

import Octane from 'octane-node';

// This is the full config object and its default values.
const config = {
  // 'host', 'port', and 'protocol' modify the connection to the API, although
  // it isn't likely that you'll need to set these.
  host: 'api.cloud.getoctane.io',
  port: 443,
  protocol: 'https',
  // You can override the global `fetch` or directly provide access to a
  // fetch API using `fetchApi`. The global fetch is used by default.
  fetchApi: fetch,
  // Our SDK supports middleware for intercepting requests before they go
  // out and responses before they come back.
  middlewares: [
    {
      pre: ({ fetch, url, init }) => {
        logRequest(url, init);
        return Promise.resolve({ url, init });
      },
      post: ({ fetch, url, init, response }) => {
        logResponse(response.clone());
        return Promise.resolve(response);
      },
    },
  ],
};
const octane = new Octane(process.env.OCTANE_API_KEY, config);

Example apps

The following demo applications found in the examples/ directory display how to use the Octane Node.js library in real-world settings:

Making API calls

The Octane class provides programmatic access to the Octane API.

Note: Throughout several of the examples, we will reference a predefined sample function, crash, which can be used to handle API errors:

const crash = (errorResponse) => {
  return errorResponse.json().then((data) => {
    console.error(`Code:    ${data['code']}`);
    console.error(`Status:  ${data['status']}`);
    console.error(`Message: ${data['message']}`);
  });
};

Customers API

The customers namespace on an Octane class instance provides the ability to make calls to the Octane Customers API.

Example: Creating a new customer

const name = 'r2d2';
const displayName = 'Artoo-Detoo';

const customer = {
  name,
  displayName,
};

octane.customers.create(customer).catch(crash);

Example: Subscribe a customer to a price plan

const customerName = 'r2d2';

const subscription = {
  pricePlanName: 'droidplan',
};

octane.customers.createSubscription(customerName, subscription).catch(crash);

Meters API

The meters namespace on an Octane class instance provides the ability to make calls to the Octane Meters API.

Example: Creating a new meter

const meterName = 'droidrepairs';

const meter = {
  name: meterName,
  meterType: 'COUNTER',
  isIncremental: true,
};

octane.meters.create(meter).catch(crash);

Price Plans API

The pricePlans namespace on an Octane class instance provides the ability to make calls to the Octane Price Plans API.

Example: Creating a new price plan

const pricePlanName = 'droidplan';
const pricePlanRate = 10000; // $100.00
const meterName = 'droidrepairs';

const pricePlan = {
  name: pricePlanName,
  period: 'month',
  meteredComponents: [
    {
      meterName: meterName,
      priceScheme: {
        prices: [
          {
            price: pricePlanRate,
          },
        ],
        schemeType: 'FLAT',
      },
    },
  ],
};

octane.pricePlans.create(pricePlan).catch(crash);

Measurements API

The measurements namespace on an Octane class instance provides the ability to make calls to the Octane Measurements API.

Example: Sending a measurement

const meterName = 'droidrepairs';
const customerName = 'r2d2';

const measurement = {
  meterName,
  customerName,
  value: 1,
};

octane.measurements.create(measurement).catch(crash);

TypeScript support

The library itself is written first in TypeScript, and thus, TypeScript is fully supported (v3.6 and later).

The octane-node/types submodule contains all of the type defintions used for various method arguments and return values when making API requests, etc.

Types example

Here is a full example of using types while creating a customer:

import fetch from 'node-fetch';
import Octane from 'octane-node';
import { CreateCustomerArgs } from 'octane-node/types';

const octane = new Octane(process.env.OCTANE_API_KEY!);

const customerName = 'r2d2';

const customer: CreateCustomerArgs = {
  name: customerName,
  measurementMappings: [
    {
      label: 'customer_name',
      valueRegex: customerName,
    },
  ],
};

octane.customers.create(customer).catch((errorResponse: Response) => {
  console.error(errorResponse.status);
  process.exit(1);
});

Development

In the root of this repo, download required dependencies from npm:

npm install

To regenerate files in src/codegen/ from latest Octane OpenAPI spec, run the following:

npm run-script codegen

These files should then be checked into git:

git add src/codegen/

To compile TypeScript to JavaScript, creating the build/ directory, run the following:

npm run-script build

Contributing

Contributions are welcome!

Prior to submitting a pull request, please check the list of open issues. If there is not an existing issue related to your changes, please open a new issue to first discuss your thoughts with the project maintainers.