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

@warrantdev/warrant-js

v3.4.0

Published

JavaScript Library to Use Warrant in Client Applications

Downloads

58

Readme

@warrantdev/warrant-js

Use Warrant as an ES module.

npm Slack

Installation

Use npm to install the Warrant module:

npm install @warrantdev/warrant-js

Usage

Import the Warrant client and pass a valid Config object to the constructor to get started:

import Warrant from "@warrantdev/warrant-js";

// A valid session token is required to initialize the Client
const warrant = new Warrant({
  clientKey: "client_test_f5dsKVeYnVSLHGje44zAygqgqXiLJBICbFzCiAg1E=",
  sessionToken: "sess_test_f9asdfASD90mkj2jXZIaeoqbIUAIjsJAHSAnsndW=",
});

check

This function returns a Promise that resolves with true if the user for the current session token has the specified warrant and false otherwise.

import Warrant from "@warrantdev/warrant-js";

// A valid session token is required to initialize the Client
const warrant = new Warrant({
  clientKey: "client_test_f5dsKVeYnVSLHGje44zAygqgqXiLJBICbFzCiAg1E=",
  sessionToken: "sess_test_f9asdfASD90mkj2jXZIaeoqbIUAIjsJAHSAnsndW=",
});

//
// Example Scenario:
// An e-commerce website where Store Owners can edit their own Store's info
//
warrant.check({ object: myReport, relation: "editor" }).then((isAuthorized) => {
  if (isAuthorized) {
    // Carry out logic to allow user to edit a Store
  }
});

Or using async/await:

import Warrant from "@warrantdev/warrant-js";

// A valid session token is required to initialize the Client
const warrant = new Warrant({
  clientKey: "client_test_f5dsKVeYnVSLHGje44zAygqgqXiLJBICbFzCiAg1E=",
  sessionToken: "sess_test_f9asdfASD90mkj2jXZIaeoqbIUAIjsJAHSAnsndW=",
});

//
// Example Scenario:
// An e-commerce website where Store Owners can edit their own Store's info
//
const isAuthorized = await warrant.check({
  object: myReport,
  relation: "editor",
});
if (isAuthorized) {
  // Carry out logic to allow user to edit a Store
}

checkMany

This function returns a Promise that resolves with true if the user for the current session token has allOf or anyOf (depending on the passed in op) the specified warrants and false otherwise.

CheckOp.AnyOf specifies that the access check request will be authorized if any of the warrants are matched and will not be authorized otherwise.

CheckOp.AllOf specifies that the access check request will be authorized if all of the warrants are matched and will not be authorized otherwise.

import Warrant, { CheckOp } from "@warrantdev/warrant-js";

// A valid session token is required to initialize the Client
const warrant = new Warrant({
  clientKey: "client_test_f5dsKVeYnVSLHGje44zAygqgqXiLJBICbFzCiAg1E=",
  sessionToken: "sess_test_f9asdfASD90mkj2jXZIaeoqbIUAIjsJAHSAnsndW=",
});

warrant
  .checkMany({
    op: CheckOp.AllOf,
    warrants: [
      {
        object: tenantA,
        relation: "member",
      },
      {
        object: reportA,
        relation: "editor",
      },
    ],
  })
  .then((isAuthorized) => {
    if (isAuthorized) {
      // Carry out logic if user is member of tenantA AND editor of reportA
    }
  });

Or using async/await:

import Warrant from "@warrantdev/warrant-js";

// A valid session token is required to initialize the Client
const warrant = new Warrant({
  clientKey: "client_test_f5dsKVeYnVSLHGje44zAygqgqXiLJBICbFzCiAg1E=",
  sessionToken: "sess_test_f9asdfASD90mkj2jXZIaeoqbIUAIjsJAHSAnsndW=",
});

const isAuthorized = await warrant.checkMany({
  op: CheckOp.AllOf,
  warrants: [
    {
      object: tenantA,
      relation: "member",
    },
    {
      object: reportA,
      relation: "editor",
    },
  ],
});
if (isAuthorized) {
  // Carry out logic if user is member of tenantA AND editor of reportA
}

hasPermission

This function returns a Promise that resolves with true if the user for the current session token has the specified permission and false otherwise.

import Warrant from "@warrantdev/warrant-js";

// A valid session token is required to initialize the Client
const warrant = new Warrant({
  clientKey: "client_test_f5dsKVeYnVSLHGje44zAygqgqXiLJBICbFzCiAg1E=",
  sessionToken: "sess_test_f9asdfASD90mkj2jXZIaeoqbIUAIjsJAHSAnsndW=",
});

warrant.hasPermission({ permissionId: "view-items" }).then((canViewItems) => {
  if (canViewItems) {
    // Carry out logic if user has permission view-items
  }
});

Or using async/await:

import Warrant from "@warrantdev/warrant-js";

// A valid session token is required to initialize the Client
const warrant = new Warrant({
  clientKey: "client_test_f5dsKVeYnVSLHGje44zAygqgqXiLJBICbFzCiAg1E=",
  sessionToken: "sess_test_f9asdfASD90mkj2jXZIaeoqbIUAIjsJAHSAnsndW=",
});

const canViewItems = await warrant.hasPermission({
  permissionId: "view-items",
});
if (canViewItems) {
  // Carry out logic if user has permission view-items
}

hasFeature

This function returns a Promise that resolves with true if the user for the current session token has the specified feature and false otherwise.

import Warrant from "@warrantdev/warrant-js";

// A valid session token is required to initialize the Client
const warrant = new Warrant({
  clientKey: "client_test_f5dsKVeYnVSLHGje44zAygqgqXiLJBICbFzCiAg1E=",
  sessionToken: "sess_test_f9asdfASD90mkj2jXZIaeoqbIUAIjsJAHSAnsndW=",
});

warrant.hasFeature({ featureId: "save-items" }).then((canSaveItems) => {
  if (canSaveItems) {
    // Carry out logic if user has feature save-items
  }
});

Or using async/await:

import Warrant from "@warrantdev/warrant-js";

// A valid session token is required to initialize the Client
const warrant = new Warrant({
  clientKey: "client_test_f5dsKVeYnVSLHGje44zAygqgqXiLJBICbFzCiAg1E=",
  sessionToken: "sess_test_f9asdfASD90mkj2jXZIaeoqbIUAIjsJAHSAnsndW=",
});

const canSaveItems = await warrant.hasFeature({ featureId: "save-items" });
if (canSaveItems) {
  // Carry out logic if user has feature save-items
}

We’ve used a random Client Key in these code examples. Replace it with your actual publishable Client Key to test this code through your own Warrant account.

For more information on how to use the Warrant API, please refer to the Warrant API reference.

TypeScript support

This package includes TypeScript declarations for Warrant.

Note that we may release new minor and patch versions of @warrantdev/warrant-js with small but backwards-incompatible fixes to the type declarations. These changes will not affect Warrant itself.

Warrant Documentation