# @softwareimaging/backstage

> Backstage is a headless environment & feature flag library that isn't tied to any specific service or format. It's a React based library that focuses on being easy to use and simple to integrate with any (and multiple) feature providers.

Latest version **3.2.0** (published 2024-01-26) · MIT license · 0 weekly downloads

## Install

```sh
npm install @softwareimaging/backstage
pnpm add @softwareimaging/backstage
yarn add @softwareimaging/backstage
bun add @softwareimaging/backstage
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.2.0 |
| Published | 2024-01-26 |
| First published | 2022-02-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 8 |
| Unpacked size | 37 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Maintainers | james-si |

## Links

- npm: https://www.npmjs.com/package/@softwareimaging/backstage
- npm.io page: https://npm.io/package/@softwareimaging/backstage

## Dependencies (8)

- [uuid](https://npm.io/package/uuid.md) ^8.3.2
- [lodash](https://npm.io/package/lodash.md) ^4.17.21
- [zustand](https://npm.io/package/zustand.md) ^4.3.9
- [lodash.merge](https://npm.io/package/lodash.merge.md) ^4.6.2
- [suspend-react](https://npm.io/package/suspend-react.md) ^0.0.8
- [deep-object-diff](https://npm.io/package/deep-object-diff.md) ^1.1.7
- [promise-status-async](https://npm.io/package/promise-status-async.md) ^1.2.10
- [use-sync-external-store](https://npm.io/package/use-sync-external-store.md) ^1.2.0

## Recent versions

- 3.2.0 (latest) — 2024-01-26
- 3.1.0 — 2023-09-28
- 3.0.0 — 2023-07-07
- 2.0.0 — 2022-10-13
- 1.9.0 — 2022-08-03
- 1.8.0 — 2022-08-03
- 1.7.0 — 2022-06-16
- 1.6.0 — 2022-03-06
- 1.5.0 — 2022-03-06
- 1.4.0 — 2022-03-06
- 1.3.0 — 2022-03-05
- 1.2.0 — 2022-02-21
- 1.1.0 — 2022-02-18
- 1.0.0 — 2022-02-17

## README

# Backstage

Backstage is a headless environment & feature flag library that isn't tied to any specific service or format. It's a React based library that focuses on being easy to use and simple to integrate with any (and multiple) feature providers.

The current available providers are:

- [`@softwareimaging/backstage-local`]() - A provider for a local JSON file as configuration.
- [`@softwareimaging/backstage-azure-config`]() - A provider to use the [Azure App Configuration](https://docs.microsoft.com/en-us/azure/azure-app-configuration/) flag backend.

- `[TODO]` - `@softwareimaging/backstage-cra` - [`create-react-app` environment variables.](https://create-react-app.dev/docs/adding-custom-environment-variables/)
- `[TODO]` - `@softwareimaging/backstage-http` - A provider for using a simple, single endpoint with optional headers.

---

## Usage

Install the main library first.

```
npm i @softwareimaging/backstage --save
```

And then any providers you want to use, e.g.

```
npm i @softwareimaging/backstage-local @softwareimaging/backstage-azure-config --save
```

> See the [@softwareimaging/backstage-local]() documentation on how to set up a local environment file.

Adopt and configure the provider as close to the top of component tree as possible.

```js
// backstage-provider.jsx
import { Backstage } from '@softwareimaging/backstage'
import LocalProvider from '@softwareimaging/backstage-local'
import config from './backstage.local'

const BackstageProvider = ({ children }) => {
  const providers = [LocalProvider(1, { config })]

  return <Backstage providers={providers}>{children}</Backstage>
}
```

Backstage uses suspense to manage it's loading state. Provide a suspense boundary and a fallback state. See the React documentation for more information.

```js
import { Suspense } from 'react'
import { BackstageProvider } from './backstage-provider'

const App = () => {
  return (
    <Suspense fallback="Loading...">
      <BackstageProvider>
        <RestOfApplication />
      </BackstageProvider>
    </Suspense>
  )
}
```

To use it in your components, use the `useVariable` and `useFlag` hooks.

```js
import { useVariable, useFlag } from '@softwareimaging/backstage'
const Component = () => {
  const name = useVariable('name') // string
  const showBanner = useFlag('banner') // boolean

  return <div>{showBanner && <Banner>Hello {name}!</Banner>}</div>
}
```

```ts
const Component = () => {
  const name = useVariable('name') // string

  return (
    <div>
      <Flag name="banner">
        <Banner>Hello {name}!</Banner>
      </Flag>
    </div>
  )
}
```

## Use with TypeScript

It's recommend for TypeScript projects to use an interface to define the known options for your configuration for all of the type-safe-goodness that comes with TypeScript.

Firstly, define your config interface.

```typescript
// backstage-config.ts
export interface MyConfig extends BackstageConfig {
  variables: {
    apiKey: string
    name: string
  }
  flags: {
    banner: boolean
    promotion: boolean
  }
}

export type Variables = keyof MyConfig['variables']
export type Flags = keyof MyConfig['flags']
```

It's also worth defining an extra export of your possible variable & flag names. This will make typing your hook calls have less boilerplate.

While that may look a bit weird, it is simply just a type represents the keys in the object. In the example above `Variables` is equal to `'apiKey' | 'name'` and `Flags` is equal to `'banner' | 'promotion'`.

To use this in your components, add the type to your hook calls.

```typescript
import { useFlag } from '@softwareimaging/backstage'
import { Flags } from './backstage-config'
const Text = () => {
  const promotion = useFlag<Flags>('promotion') // <- This key is typechecked.
  return promotion ? <p>Discount: £10!</p> : <p>Normal pricing: £100</p>
}
```

## Provider Development

If you want to implement a new provider, please use the example and types below as a starting point.

```ts
// Please note the nested function, this allows for custom
// priorities at run time. Feel free to use additional arguments
// for configuration.
const provider: BackstageProvider = (priority: number = 0) => () => {
  return {
    priority,
    name: 'Example Provider',
    get: async () => {
      return {
        variables: {
          name: 'Website name',
          colour: '#29008a'
        },
        flags: {
          banner: true
        }
      }
    }
  }
}
```

---
_Source: https://npm.io/package/@softwareimaging/backstage · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
