Javascript SDK for Joystick
![License](https://img.shields.io/npm/l/@getjoystick/joystick-js)
This is the library that simplifies the way how you can communicate with Joystick API.
Requirements
NodeJS 16 and later
Installation
You can install the package
via npm:
npm install @getjoystick/joystick-js
Usage
To use the client, import the package.
import { Joystick } from "@getjoystick/joystick-js";
Simple usage looks like this:
const client = new Joystick({
apiKey: process.env.JOYSTICK_API_KEY,
});
client
.getContents(["content-id1", "content-id2"])
.then((getContentsResponse) => {
console.log(getContentsResponse["content-id1"].myProperty1);
console.log(getContentsResponse["content-id2"].myProperty2);
});
Requesting Content by single Content Id
client
.getContent("content-id1")
.then((getContentResponse) => console.log(getContentResponse.myProperty1));
Specifying additional parameters:
When creating the Joystick
object, you can specify additional parameters which will be used
by all API calls from the client, for more details see
API documentation:
const client = 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: true
}
});
Options
fullResponse
In most of the cases you will be not interested in the full response from the API, but if you're you can specify
fullResponse
option to the client methods. The client will return you raw API response:
client
.getContent("content-id1", { fullResponse: true })
.then((getContentResponse) => console.log(getContentResponse));
client
.getContents(["content-id1", "content-id2"], { fullResponse: true })
.then((getContentsResponse) => console.log(getContentsResponse));
serialized
When true
, we will pass query parameter responseType=serialized
to Joystick API.
client
.getContent("content-id1", { serialized: true })
.then((getContentResponse) => console.log(getContentResponse));
client
.getContents(["content-id1", "content-id2"], { serialized: true })
.then((getContentsResponse) => console.log(getContentsResponse));
refresh
If you want to ignore existing cache and request the new config – pass this option as true
.
client
.getContent("content-id1", { refresh: true })
.then((getContentResponse) => console.log(getContentResponse));
client
.getContents(["content-id1", "content-id2"], { refresh: true })
.then((getContentsResponse) => console.log(getContentsResponse));
This option can be set for every API call from the client by setting setSerialized(true)
:
const client = new Joystick({
apiKey: process.env.JOYSTICK_API_KEY,
options: {
serialized: true
}
});
client.setSerialized(true);
Caching
By default, the client uses InMemoryCache, based on Map,
which means the cache will be erased after application restart.
You can specify your 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 – run:
client.clearCache().then(() => console.log("Cache cleared!"));
HTTP Client
If you want to provide custom HTTP client, which may be useful for use-cases like specifying custom proxy,
collecting detailed metrics about HTTP requests,
You can 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
Security
If you discover any security related issues, please email letsgo@getjoystick.com
instead of using the issue tracker.
Credits
License
The MIT. Please see License File for more information.