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-node

v6.3.0

Published

Warrant API Wrapper for Node.js clients

Downloads

1,912

Readme

Warrant Node.js Library

Use Warrant in server-side Node.js projects.

npm Slack

Installation

Use npm to install the Warrant module:

npm install @warrantdev/warrant-node

Usage

Import the Warrant client and pass your API key to the constructor to get started:

const Warrant = require("@warrantdev/warrant-node");
const warrantClient = new Warrant.WarrantClient({
  apiKey: "api_test_f5dsKVeYnVSLHGje44zAygqgqXiLJBICbFzCiAg1E=",
});

Or using ES modules:

import { WarrantClient } from "@warrantdev/warrant-node";
const warrantClient = new WarrantClient({
  apiKey: "api_test_f5dsKVeYnVSLHGje44zAygqgqXiLJBICbFzCiAg1E=",
});

This method creates a user in Warrant with the provided userId. Provide an optional email to make it easier to identify users in the Warrant dashboard.

const Warrant = require("@warrantdev/warrant-node");
const warrantClient = new Warrant.WarrantClient({
  apiKey: "api_test_f5dsKVeYnVSLHGje44zAygqgqXiLJBICbFzCiAg1E=",
});

// Creates a user with user.id as the userId
warrantClient.User
  .create({ userId: user.id, email: user.email })
  .then((newUser) => console.log(newUser))
  .catch((error) => console.log(error));

Or using ES modules and async/await:

import { WarrantClient } from "@warrantdev/warrant-node";
const warrantClient = new WarrantClient({
  apiKey: "api_test_f5dsKVeYnVSLHGje44zAygqgqXiLJBICbFzCiAg1E=",
});

// Creates a user with user.id as the userId and
// assigns the new user the "store_owner" role
const newUser = await warrantClient.User.create({
  userId: user.id,
  email: user.email,
});

Configuring the API Endpoint


The API endpoint the SDK makes requests to is configurable via the endpoint attribute when initializing the client:

import { WarrantClient } from "@warrantdev/warrant-node";

// Set api and authorize endpoints to http://localhost:8000
const warrantClient = new WarrantClient({
  apiKey: "api_test_f5dsKVeYnVSLHGje44zAygqgqXiLJBICbFzCiAg1E=",
  endpoint: "http://localhost:8000",
});

Authorization

All access checks are performed based on an object, relation and subject. You can pass your own defined objects to the check methods by implementing the WarrantObject interface.

interface WarrantObject {
    getObjectType(): string;
    getObjectId(): string;
}

check(Check)

This method returns a Promise that resolves with true if the subject has the specified relation to the object and false otherwise.

const Warrant = require("@warrantdev/warrant-node");

const warrantClient = new Warrant.WarrantClient({
  apiKey: "api_test_f5dsKVeYnVSLHGje44zAygqgqXiLJBICbFzCiAg1E=",
});

// Store class implements WarrantObject
class Store {
  private id: number;

  public getObjectType(): string {
    return "store";
  }

  public getObjectId(): string {
    return this.id.toString();
  }
}

//
// Example Scenario:
// An e-commerce website where Store Owners can edit store info
//
const myStore = new Store('my-store');
warrantClient.Authorization
  .check({
    object: myStore,
    relation: "edit",
    subject: {
      objectType: "user",
      objectId: user.id,
    },
  })
  .then((isAuthorized) => {
    if (isAuthorized) {
      // Carry out logic to allow user to edit a Store
    }
  })
  .catch((error) => console.log(error));

Or using ES modules and async/await:

import { WarrantClient } from "@warrantdev/warrant-node";

const warrantClient = new WarrantClient({
  apiKey: "api_test_f5dsKVeYnVSLHGje44zAygqgqXiLJBICbFzCiAg1E=",
});

//
// Example Scenario:
// An e-commerce website where Store Owners can edit store info
//
const myStore = new Store('my-store');
if (
  await warrantClient.Authorization.check({
    object: myStore,
    relation: "edit",
    subject: {
      objectType: "user",
      objectId: user.id,
    },
  })
) {
  // Carry out logic to allow user to edit a Store
}

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

For more information on how to use the Warrant API and usage examples for all methods, please refer to the Warrant API reference.

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

TypeScript support

This package includes TypeScript declarations for Warrant.

Warrant Documentation