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

rps-engine-client-js

v5.0.0

Published

RPS Engine client for JavaScript/TypeScript

Downloads

429

Readme

rps-engine-client-js

npm version

Regdata RPS Engine Client library (JS/TS)

TypeScript support | NodeJS/Browser support | Request builder and validator | Easy to use

Playground | 📚 Documentation | 📦 NPM | GitHub

🚨 Since version 5.0.0 the API has changed significantly. The previous version is available in the v4 branch.

Documentation

The documentation for API is available in RPS Community. Here is a more detailed description of the library.

Usage

First of all, you need to install the library:

npm i rps-engine-client-js

RPS Agent

Then you're able to setup RPS Agent to communicate with the RPS Engine and RPS Identity Server.

import { RPSAgent } from 'rps-engine-client-js'

const rpsAgent = new RPSAgent({
  engineUrl: 'https://engine.rpsprod.ch',
  identityUrl: 'https://identity.rpsprod.ch',

  // you can define the client id and secret here or later
  identity: {
    clientId: 'c6cbde13-542d-4849-a69e-3962ed09bc10',
    clientSecret: '37571534bf6d40878fa77cb7b354b3274e6c047bd6404468b0fa2345cb7ebe61',
  },
})

// to check identity url and client, you can try to get token
const token = await rpsAgent.getToken()

If you successfully get the token, you can start to use the RPS Agent to communicate with the RPS Engine.

const output = await rpsAgent.transform({
  processingContexts: [
    // ...
  ],
  rightsContexts: [
    // ...
  ],
  requests: [
    // ...
  ],
})

transform input is a complicated structure, to define it correctly, you can use the RPSCraft class.

More methods and options are available in the API documentation below.

RPSCraft

import { RPSCraft } from 'rps-engine-client-js'

const rpsCraft = new RPSCraft()
rpsCraft.addRequest({
  instances: [
    {
      value: 'Jonny',
      className: 'User',
      propertyName: 'FirstName',
    },
  ],
  rightsContext: {
    evidences: [
      {
        name: 'Role',
        value: 'Admin',
      },
    ],
  },
  processingContext: {
    evidences: [
      {
        name: 'Action',
        value: 'Protect',
      },
    ],
  },
})
rpsCraft.addRequest({
  // ...
})

const input = rpsCraft.build()

const output = await rpsAgent.transform(input)

Request validation

You can validate the request json before sending it to the RPS Engine.

import { validateTransformJson } from 'rps-engine-client-js'

const result = validateTransformJson(json)
validateTransformJson(input: any): IRpsJsonValidationResult

input (any): The JSON input to validate and transform. Returns:

An IRpsJsonValidationResult object containing the validation result. The structure of the IRpsJsonValidationResult object is as follows:

  • schemaResult (ValidatorResult | null): The result of the schema validation. It's null if the validation fails.
  • errors (Array): An array of error objects. Each error object has the following properties:
    • message (string): The error message.
    • prettyPath (string): A prettified string representation of the path to the property that caused the error in the input data.
    • path (Array): The path to the property that caused the error in the input data.
  • warns (Array): An array of warning objects. Each warning object has the same structure as the error objects.

More methods and options are available in the API documentation below.

TypeScript

The library is written in TypeScript and has type definitions. You can use it in your TypeScript project without any additional configuration.

API

RPSAgent API

RPSAgent constructor accepts an object with the following properties:

  • identityUrl (string): Identity server url.
  • engineUrl (string): Engine server url.
  • identity (object): Identity server options.
    • clientId (string): Client id.
    • clientSecret (string): Client secret.
    • authPath (string): Path to the authentication endpoint. Default value is 'connect/token'.
  • engine (object): Engine server options.
    • returnOriginalTransformResponse (boolean): If true, the response will contain the original request. Default value is false.
const rpsAgent = new RPSAgent({
  engineUrl: 'https://engine.rpsprod.ch',
  identityUrl: 'https://identity.rpsprod.ch',
  identity: {
    clientId: 'c6cbde13-542d-4849-a69e-3962ed09bc10',
    clientSecret: '37571534bf6d40878fa77cb7b354b3274e6c047bd6404468b0fa2345cb7ebe61',
  },
})

Methods:

  • get isIdentitySet(): boolean: Check if the identity url, client id, and client secret are set.

  • get isEngineSet: Check if the engine url is set.

  • get isAuthenticated: Check if the token is set.

  • setClient(data: { clientId: string, clientSecret: string }): void: Set the client id and secret after the RPSAgent object is created.

  • trySetClient(data: { clientId: string, clientSecret: string }): Promise<void>: Try to set the client id and secret. It immediately tries to get the token. Throws an error if the token is not received.

  • getToken(): Promise<{token: string, tokenType: string}>: Get the token from the identity server. Automatically authenticates if the token is not set.

  • setEngineUrl(url: string): void: Set the engine url after the RPSAgent object is created.

  • setIdentityUrl(url: string): void: Set the identity url after the RPSAgent object is created.

  • resetAuth(): void: Reset the token.

  • resetClient(): void: Reset the client id and secret.

  • transform(requestData: ITransformInput): Promise<ITransformOutput>: Send a request to the RPS Engine to transform the data.

  • transformAndReturnOriginal(requestData: ITransformInput): Promise<ITransformOutputWithOriginal>: Send a request to the RPS Engine to transform the data and return the processed response RPSAgent.processTransformOutput.

  • static RPSAgent.processTransformOutput(transformOutput: ITransformOutput, requestData: ITransformInput): ITransformOutputWithOriginal: Process the transform output and return the processed response with the original values near the processed values.

RPSCraft API

RPSCraft constructor doesn't accept any arguments.

After creating an RPSCraft object, you can use the following methods:

  • addRequest(request: IRequest): RPSCraft: Add a request to the RPSCraft object and return the RPSCraft object for chaining.
  • build(): ITransformInput: Build the transform input object from the added requests. This input object can be used in the transform method of the RPSAgent object.