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

@linode/api-v4

v0.130.0

Published

JavaScript wrapper around the Linode APIv4

Downloads

2,146

Readme

Linode JavaScript SDK

JavaScript client for the Linode APIv4

Installation

$ npm install @linode/api-v4

or with yarn:

$ yarn add @linode/api-v4

or with a CDN:

<script src="https://unpkg.com/@linode/api-v4/lib/index.global.js"></script>

Usage

Most APIv4 endpoints require authentication, either with an OAuth Token or Personal Access Token (PAT). Please see the Linode API docs for instructions on obtaining a token.

Once you have your token, authenticating involves adding an Authorization header to each request. We provide a helper setToken function for this purpose:

import { setToken } from '@linode/api-v4';

setToken('my-access-token');

If you would like more fine-grained control, this library is built on top of the Axios HTTP Client. We expose our base Axios instance, which you can use to attach interceptors:

import { baseRequest } from '@linode/api-v4/lib/request';

/**
 * intercepts every request with the following config
 * see https://github.com/axios/axios#interceptors for more documentation.
 */
baseRequest.interceptors.request.use((config) => {
  const myToken = '1234';

  return {
    ...config,
    headers: {
      ...config.headers,
      Authorization: `Bearer ${myToken}`,
    },
  };
});
/** index.js */

import './request';
import { getAccount } from '@linode/api-v4/lib/account';

getAccount()
  .then((response) => {
    document.getElementById('root').innerHTML = `<div>${response.email}</div>`;
  })
  .catch((e) => {
    console.error(e);
  });

Or using Node.js:

const { setToken, getProfile } = require('@linode/api-v4');

setToken('access-token');

getProfile().then((response) => {
  return response.username;
});

Other examples:

Tree Shaking

All methods are exposed from the SDK root, but we also support tree shaking:

import { getLinodes } from '@linode/api-v4'; // This is fine
import { getLinodes } from '@linode/api-v4/lib/linodes'; // This works too

Important note about imports

If you are using interceptors on the base request, you should keep your import paths consistent (import either from root or from /lib) or else the interceptors will not work.

import { baseRequest } from '@linode/api-v4';
import { getLinodes } from '@linode/api-v4/lib/linodes';

baseRequest.interceptors.use(customInterceptor);
getLinodes(); // customInterceptor not called!

// To fix, change the first import to:
// import { baseRequest } from '@linode/api-v4/lib/request';
//
// (or, import getLinodes from '@linode/api-v4')

Pagination and Filtering

APIv4 supports pagination and filtering and sorting. Paginated endpoints include the current page, total number of pages, and total number of results in the response:

import { getLinodes } from '@linode/api-v4/lib/linodes';

getLinodes().then((response) => {
  console.log(response);
});

/*
  {
    page: 1,
    pages: 1,
    results: 9,
    data: [...]
  }
*/

Where appropriate, SDK functions allow you to pass pagination and filter parameters to the API:

// Return page 2 of Linodes, with a page size of 100:
getLinodes({ page: 2, page_size: 100 });

// Return all public Linode Images:
getImages({}, { is_public: true });

The API docs provide a list of which response fields are filterable, as well as examples of more complex filtering and sorting operations.

Types

This library comes with TypeScript definitions so no need to write your own or find them elsewhere online. Just import the functions as normal and they should play nicely with TypeScript!

Most types can be imported from their corresponding pathname. For instance:

import { Linode } from '@linode/api-v4/lib/linodes';

More general types (such as the error shape that comes back from the Linode APIv4) can be found in the /types directory:

import { APIError } from '@linode/api-v4/lib/types';

You can also import from the root if preferred:

import { APIError, Linode } from '@linode/api-v4';

Contributing

This SDK aims to have a 1-to-1 relationship with the endpoints exposed from the Linode APIv4, but endpoints are being added all the time, so it's entirely possible that the SDK is incomplete. If you see an endpoint in the API docs that doesn't exist in this package, don't hesitate to open a PR and add the function and typings that consume the endpoint. If you don't feel comfortable opening a PR, feel free to open a ticket.

We'll do our best to publicize what work needs to be done in the GitHub issues and mark tickets as a good first issue. That way, it will be more apparent where the SDK needs work.

When in doubt, look at the code that already exists and mimic that.