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

@forgerock/crest-js

v2.1.11

Published

Lightweight Library for Communicating With ForgeRock CREST APIs.

Downloads

3,118

Readme

Prerequisites

ForgeRock CREST.js requires the fetch and Promise APIs to be available in the global environment.

Supported Versions

ForgeRock CREST.js supports CREST versions 2.0 and 2.1.

import { CRESTv2, CRESTv2_1 } from "@forgerock/crest-js";

Installation

via Yarn

yarn add @forgerock/crest-js

via NPM

npm install @forgerock/crest-js

Usage

Basic

import { CRESTv2 } from "@forgerock/crest-js";

const resource = new CRESTv2("http://www.domain.com/crest/api");

resource.get("id").then(json => {
  // Success! `json` is an Object
}, error => {
  if (error instanceof CRESTError) {
    // CREST error
  } else if (error instanceof RequestError) {
    // Request could not be completed e.g. network failure
  } else if (error instanceof ParseError) {
    // Response couldn't be parsed as JSON
  }
});

All the functions upon a CREST resource return Promises.

Promises allow for easily building on top of the core functionality, for example, with common handlers that deal with rejections consistently.

Supported Methods

import { CRESTv2_1 } from "@forgerock/crest-js";

const resource = new CRESTv2_1("http://www.domain.com/crest/api");
const body = { data: "value" };

// #action
resource.action("action");
resource.action("action", { body }); // Action with body

// #create
resource.create(body); // Server provided ID
resource.create(body, { id: "id" }); // Client provided ID

// #delete
resource.delete("id");

// #get
resource.get("id");

// #queryFilter
resource.queryFilter(); // Only supports `_queryFilter=true`

// #update
resource.update("id", body);

Pagination is currently only supported via the additional query strings option queryString.

See the API Documentation for all possible options.

Options

queryString

For adding additional query strings to the any request.

import { CRESTv2 } from "@forgerock/crest-js";

const resource = new CRESTv2("http://www.domain.com/crest/api");

resource.get("id", {
  queryString: {
      query: "value"
  }
})
// => http://www.domain.com/crest/api/id?query=value

Query strings applied by ForgeRock CREST.js cannot be overridden.

import { CRESTv2 } from "@forgerock/crest-js";

const resource = new CRESTv2("http://www.domain.com/crest/api");

resource.action("action1", {
  queryString: {
      _action: "action2"
  }
})
// => http://www.domain.com/crest/api?_action=action1

Middleware

One or many middleware can be applied to an CREST resource.

Each middleware is a function that takes a single Promise parameter, and returns a Promise to pass to the next middleware.

The first middleware in the chain is guaranteed to receive a Promise which is either resolved to a parsed JSON payload, or rejected with one of the defined error types.

import { CRESTv2_1 } from "@forgerock/crest-js";

const customMiddleware = (promise) => {
  return promise.then((json) => {
    // Success! `json` is an Object
    const jsonToReturn = {
      ...json,
      myAttribute: true
    };

    // Return value will be passed the next middleware
    return json;
  }, (error) => {
    // Capture, rethrow or modify errors
    throw new CustomError(error.message);
  });
};

const resource = new CRESTv2_1("http://www.domain.com/crest/api", {
  middleware: [customMiddleware]
});

Development

Project Structure

dist/ # Output of "yarn run build".
config/ # Tooling configuration files.
docs/ # API documentation And output of "yarn run docs".
src/ # Source code.
├── .babelrc # Babel configuration for source when executing "yarn run test" or "yarn run test:coverage".
└── ...

Building

Builds production CommonJS, ES and UMD versions into /dist.

yarn run build

Docs

Generates API JSDoc into /docs.

yarn run docs

Testing

Runs tests against the source.

yarn run test

The yarn command will pass arguments to the underlying command. For examples, use yarn run test --verbose --watch for continuous testing with extra output.

Test Coverage

Generates test coverage report.

yarn run test:coverage

Contributing

Contribute to ForgeRock CREST.js by opening a Pull Request.

Further Reading