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

@autoscout24/toguru-client

v2.1.6

Published

> A `TypeScript` client for integrating Toguru into a `Node.js` environment

Downloads

72

Readme

Toguru Client for Node.js

A TypeScript client for integrating Toguru into a Node.js environment

Actions Status npm

Installation

You can install the library with your package manager of choice

$ yarn add toguru-client
# or
$ npm install toguru-client

The client is fully written in TypeScript and comes with types out of the box.

Usage

Base Client

The library provides a base Toguru client that is independent of the framework or environment used, and can be used for AWS lambdas, Kafka streams, generic scripts, etc where toggling functionality might be needed. It allows computing toggle activations based on an activation context. For web application contexts, we recommend using the more specialized Express bridge, see below.

import { toguruClient, TogglingApiByActivationContext } from '@autoscout24/toguru-client'

const client: TogglingApiByActivationContext = toguruClient({
    endpoint: 'https://example.com/togglestate',
    refreshIntervalMs: 60 * 1000, // 1 minute
})

Once we have instantiated the client, it will poll the Toguru backend automatically and fetch all toggle information. The client needs an ActivationContext (user identifier, possible forced toggles, etc) which we can provide

const activationContext: ActivationContext = {
    uuid: 'ad89f957-d646-1111-1111-a02c9aa7faba',
    forcedToggles: {
        fooToggle: true,
    },
}

We can then use this context to finally compute toggle activations

client(activationContext).isToggleEnabled({ id: 'test-toggle', default: false }) // based on toguru data, fallback to `false`
client(activationContext).isToggleEnabled({ id: 'fooToggle', default: false }) // `true`
client(activationContext).togglesForService('service') // `Toggles` based on toguru data

Express Bridge

When working with a web application, it makes sense to compute toggle information based on the client's Request. The library provides an Express bridge that takes a base client and uses it to compute toggle activations based on a Request

Request-based client

We can create a client that will compute the activation context based on a Request.

import { client, toguruExpressBridge } from '@autoscout24/toguru-client'


const client = client({...}) // instantiate the base toguru client

const toguruExpressClient = toguruExpressBridge.client({
    client,
    extractors: {
        uuid: toguruExpressBridge.requestExtractors.cookieValue('user-id') // will attempt to pull the user uuid from the `user-id` cookie
    }
})

The extractors key allows customizing the behaviour, and will use certain defaults if not specified. When they are not specified, they will use defaults. After the express client is instantiated, it can be used to determine toggle activations

app.get('/some-route', (req, res) => {
      const testToggleIsOn = client(req).isToggleEnabled({ id: 'test-toggle', default: false })
      // ...
    }
))

Middleware

The library also provides an Express middleware that will augment then Request object with an additional toguru attribute. The instantiation options are the same as the express client above, and can be as follows

const toguruClientMiddleware = toguruExpressBridge.middleware(...) // same as `toguruExpressBridge.client`
app.get('/some-route', toguruClientMiddleware, (req, res) => {
      const testToggleIsOn = req.toguru.isToggleEnabled({ id: 'test-toggle', default: false })
      // ...
    }
))

Testing

In general, we recommend parametrizing your main application to take a toguru client interface, and instantiating the client itself at the very edge of your application (typically index.ts). This allows you to use different clients during tests that you can fully control without the need to introduce any mocking tools.

Express

Following the pattern align above, a typical Express application will look like

const application = (
    toguruMiddleware: Express.RequestHandler
) => {
  const app = express()
  app.get('/toguru-test', toguruMiddleware,...)
})

During tests, we can pass the express.stubToguruMiddleware or express.stubClient, depending on what the app is using, that easily allows you to instantiate the middleware/client with a given set of toggles, falling back to the toggle defaults for the rest.

stubToguruMiddleware([{ id: 'some-toggle', enabled: false }])