Authorization

You’ll need to fetch a userToken and provide this when initializing the Kana JS Client instance in order for calls to be successfully made. We have a handy guide which goes into further detail on how to obtain and provide this.

Authorization (Frontend)

Errors

We return errors within the error field of a request. You can use the following to assign the relevant fields to a data or error variable and check against them to understand if problems occurred or if data was returned:

const { data, error } = await userClient.canUseFeature("test-feature-id");

You can then decide to handle these however you feel is necessary.

Handling

You can provide the onError argument with an async function when initializing the client in order to globally take actions when errors occur. The following example logs all errors upon one occurring:

const userClient = new KanaUserClient({
  userToken,

  onError: async (err) => {
    console.log(err); // or push errors to a central logger
  },
});

If using Typescript, errors will be typed and you can check against this type. The following error types are possible:

  • EmptyArgumentError

  • FeatureNotFoundError

  • AuthenticationError

  • NetworkError

  • Error

Retries

By default, we will retry sending all calls up to 2 times when a NetworkError occurs with no backoff or delay. You can override this behaviour with the retry argument:

const userClient = new KanaUserClient({
  userToken,

  retry: (error: Error, retryNumber: number) => {
    return error instanceof NetworkError && retryNumber < 3;
  },
});

Methods

canUseFeature(featureId, delta?)

Understand if a User is able to use a particular Feature.

The returned access boolean is true when the user has access (for Binary features), has enough of a feature remaining to be used (for Consumable features), or has overage enabled (for Consumable features).

Arguments

FieldTypeText
featureIdString!The id of the feature. This maps to the id field as set on the Feature object.
deltaIntThe amount of the consumable feature which the user will be using and you want to check access against. Defaults to 1.

Returns

A Promise that resolves to a CanUseFeatureData object.

Example

JS
// Initializing the client
import { KanaUserClient } from "@usekana/client-kana-js";

const userToken = "<INSERT_USER_TOKEN_HERE>";
const kanaClient = new KanaUserClient({ userToken });

// Using the method
const { data, error } = await kanaClient.canUseFeature("messages", 1);
console.log(data);
console.log(error);

getSubscribedFeatures()

Lists the Features which a User has access to.

Returns

A Promise that resolves to an array of Feature objects with only the following fields present:

  • id

  • name

  • type

  • unitLabel

  • unitLabelPlural

  • metadata

Example

// Initializing the client

import { KanaUserClient } from "@usekana/client-kana-js";

const userToken = "<INSERT_USER_TOKEN_HERE>";

const kanaClient = new KanaUserClient({ userToken });

// Using the method

const { data, error } = await kanaClient.getSubscribedFeatures();

console.log(data);

console.log(error);

getSubscribedPackages()

Lists the plans which a user is subscribed to.

Returns

A Promise that resolves to an array of Package objects with only the following fields present:

  • id

  • name

  • isAddon

  • metadata

Example

// Initializing the client

import { KanaUserClient } from "@usekana/client-kana-js";

const userToken = "<INSERT_USER_TOKEN_HERE>";
const kanaClient = new KanaUserClient({ userToken });

// Using the method
const { data, error } = await kanaClient.getSubscribedPackages();
console.log(data);
console.log(error);

resetCache()

Resets a user’s data which has been stored in the cache.

By default, the SDK uses the cache to store a user’s subscribed packages and features upon initially calling any method. This cached data will then be returned when calling any method subsequently. You will need to call resetCache() in order to refresh this data.

Returns

A Promise that resolves to an empty object if the cache has been successfully reset.

Examples

// Initializing the client

import { KanaUserClient } from "@usekana/client-kana-js";

const userToken = "<INSERT_USER_TOKEN_HERE>";
const kanaClient = new KanaUserClient({ userToken });

// Using the method
await kanaClient.resetCache();