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

@getjoystick/joystick-js

v0.1.3

Published

Javascript SDK for Joystick

Downloads

203

Readme

Javascript Client for Joystick Remote Config

Test code Latest Stable Version Total Downloads License

Streamline your app or game development process with Joystick's Javascript/Typescript JSON remote config platform. Easily manage and update configurations without codebase clutter or lengthy deployment processes. Our modern platform offers advanced workflow management, automated version control, and seamless integration with any Javascript or Typescript project. Plus, our native multi-environment support with permissions and review workflow ensures smooth operations your entire team will love.

Experience the benefits of simplified configuration management with Joystick. Try our @getjoystick/joystick-js library today to see how easy it is to my your app or game dynamic and fundamentally upgrade your team's workflow.

Installation

Requires NodeJS 16 and later. Install via npm:

npm install @getjoystick/joystick-js

Usage

Using Joystick to get remote configurations in your Javascript or Typescript project is a breeze.

// Import the package.
import { Joystick } from "@getjoystick/joystick-js";

// Initialize a client with a Joystick API Key
const joystickClient = new Joystick({
  apiKey: process.env.JOYSTICK_API_KEY,
});

(async () => {
  // Request a single configuration
  const contentId1 = await joystickClient.getContent("content-id1");

  // Request a single configuration (typescript)
  const contentId1Typed = await joystickClient.getContent<TypeForContentId1>(
    "content-id1"
  );

  // Request multiple configurations at the same time
  const configurations = await joystickClient.getContents([
    "content-id1",
    "content-id2",
  ]);
  console.log(configurations);

  // {
  //     "content-id1": {...},
  //     "content-id2": {...}
  // }
})();

Specifying Additional Parameters

When creating the Joystick object, you can specify additional parameters which will be used by all API calls to the Joystick API. These additional parameters are used for AB Testing (userId), segmentation (params), and backward-compatible version delivery (semVer).

For more details see API documentation.

// Initializing a client with options
const joystickClient = new Joystick({
  apiKey: process.env.JOYSTICK_API_KEY,
  semVer: "0.0.1",
  userId: "user-id-1",
  params: {
    param1: "value1",
    param2: "value2",
  },
  options: {
    cacheExpirationSeconds: 600, // default 600 (10 mins)
    serialized: false, // default false
  },
});

Additional Request Options

fullResponse

In most, you will just want the contents of your configuration. In advanced use cases where you want to process the AB testing or segmentation information, you can specify the fullResponse option to the client methods. The client will return you raw API response.

joystickClient
  .getContent("content-id1", { fullResponse: true })
  .then((getContentResponse) => console.log(getContentResponse));

// OR

joystickClient
  .getContents(["content-id1", "content-id2"], { fullResponse: true })
  .then((getContentsResponse) => console.log(getContentsResponse));

serialized

You can get the contents of your configuration serialized. When set as true, we will pass query parameter responseType=serialized to Joystick API.

joystickClient
  .getContent("content-id1", { serialized: true })
  .then((getContentResponse) => console.log(getContentResponse));

// OR

joystickClient
  .getContents(["content-id1", "content-id2"], { serialized: true })
  .then((getContentsResponse) => console.log(getContentsResponse));

This option for a serialized response can be set globally for every API call by setting setSerialized(true) when initializing the client:

const joystickClient = new Joystick({
  apiKey: process.env.JOYSTICK_API_KEY,
  options: {
    serialized: true,
  },
});

// OR

joystickClient.setSerialized(true);

refresh

To ignore the existing cache when requesting a config – pass this option as true.

joystickClient
  .getContent("content-id1", { refresh: true })
  .then((getContentResponse) => console.log(getContentResponse));

// OR

joystickClient
  .getContents(["content-id1", "content-id2"], { refresh: true })
  .then((getContentsResponse) => console.log(getContentsResponse));

Error handling

The client can raise different types of exceptions with the base class of JoystickError.

try {
  await joystickClient.getContents(["content-id1", "content-id2"]);
} catch (e) {
  if (e instanceof ApiHttpError) {
    // Handle HTTP error (i.e. timeout, or invalid HTTP code)
  } else if (e instanceof MultipleContentsApiException) {
    // Handle API exception (i.e. content is not found, or some of the keys can't be retrieved)
  }
}

Caching

By default, the client uses InMemoryCache, based on Map, which means the cache will be erased after application restart.

You can specify your own cache implementation by implementing the interface SdkCache.

See examples/typescript/node-cache or examples/typescript/redis-cache for more details.

Clear the cache

If you want to clear the cache:

joystickClient.clearCache().then(() => console.log("Cache cleared!"));

HTTP Client

You can provide a custom HTTP client, which may be useful for specifying a custom proxy or collecting detailed metrics about HTTP requests.

Change your HTTP client implementation by implementing the interface HttpClient.

See src/internals/client/axios-client for more details.

Testing

To run unit tests, just run:

npm test

Credits

License

The MIT. Please see License File for more information.