/spectra-flags

a simple library for feature flags.

Primary LanguageTypeScriptMIT LicenseMIT

Spectra

A simple feature flags library for your application.

Install

$ yarn add spectra-flags

Usage

// You can just import, initialize an instance.
import spectra from 'spectra-flags'

const flags = spectra({
    flags: { create_account: true, create_room: false } 
})

export default flags

In a real-word example:

import express from 'express'
import flags from './flags'

const application = express()

application.post('/create-account', (request, response) => {
    // When `create_account` flag is disabled, the code below is triggered.
    if (!flags.enabled('create_account')) {
        return response.status(500).json({ success: false, error: "Account creation is disabled." }) 
    }

    // ...
})

Actor Checkers

Spectra works well with bitbouncer, which is a library for handling bitflags.

$ yarn add bitbouncer
import spectra, { SpectraIntegrations } from 'spectra-flags'
import bitbouncer from 'bitbouncer'

const PERMISSIONS = { 
    create_account: true,
    create_room: false
}

export const bouncer = bitbouncer.from(PERMISSIONS)

export const flags = spectra({
    actors: new SpectraIntegrations.BitBouncer(bouncer)
    flags: PERMISSIONS
})

export default flags
import { User } from './models'

import express from 'express'
import flags from './flags'

const application = express()

application.post('/create-room', async (request, response) => {
    const user = await User.byId(request.user_id)

    // When `create_room` is disabled or actor does not have permission to `create_room` 
    if (!flags.enabled('create_room', { actor: { flags: user.flags } })) {
        return response.status(500).json({ success: false, error: "You're unable to create rooms." }) 
    }

    // ...
})
License

Both spectra-flags and bitbouncer licensed under MIT © @sxhk0