![License](https://img.shields.io/npm/l/@getjoystick/joystick-js)
Use Joystick to manage and use JSON remote configs easily with any Javascript/Typescript projects. Get configurations out of your code base. Update your configurations instantly without a long build, deploy process.
Joystick is a modern remote config platform built for to manage configurations effortless in one place, behind one API. We are natively multi-environment, have automated version control with advanced work-flow & permissions management, and much more. Your entire team will love us.
The @getjoystick/joystick-js library simplifies how your Javascript/Typescript project can communicate with the Joystick API to get remote configs over a REST API.
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 { Joystick } from "@getjoystick/joystick-js";
const joystickClient = new Joystick({
apiKey: process.env.JOYSTICK_API_KEY,
});
(async () => {
const contentId1 = await joystickClient.getContent("content-id1");
const contentId1Typed = await joystickClient.getContent<TypeForContentId1>(
"content-id1"
);
const configurations = await joystickClient.getContents([
"content-id1",
"content-id2",
]);
console.log(configurations);
})();
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.
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,
serialized: 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));
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));
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,
},
});
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));
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) {
} else if (e instanceof MultipleContentsApiException) {
}
}
### Caching
By default, the client uses [InMemoryCache](./src/internals/cache/in-memory-cache.ts), 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](./src/internals/cache/sdk-cache.ts).
See [`examples/typescript/node-cache`](./examples/typescript/src/node-cache) or [`examples/typescript/redis-cache`](./examples/typescript/src/redis-cache) for more details.
#### Clear the cache
If you want to clear the cache:
```ts
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.