# @spine/config

> A configuration utility valid for both client and server

Latest version **0.0.40** (published 2019-01-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install @spine/config
pnpm add @spine/config
yarn add @spine/config
bun add @spine/config
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.40 |
| Published | 2019-01-05 |
| First published | 2018-07-06 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=6 |
| Dependencies | 1 |
| Unpacked size | 56.5 KB |
| Known vulnerabilities | 0 (+4 in 1 direct dependencies) |
| Install scripts | no |
| Author | Ezequiel S. Pereira |
| Maintainers | ezsper |
| Keywords | erect, config, client, browser, server |

## Links

- npm: https://www.npmjs.com/package/@spine/config
- Repository: https://gitlab.com/ezsper.com/spine/config
- npm.io page: https://npm.io/package/@spine/config

## Dependencies (1)

- [serialize-javascript](https://npm.io/package/serialize-javascript.md) ^1.4.0

## Alternatives

- [jsforce](https://npm.io/package/jsforce.md) — 851.2K weekly downloads
- [react-native-qrcode-svg](https://npm.io/package/react-native-qrcode-svg.md) — 693.5K weekly downloads
- [@salesforce/plugin-data](https://npm.io/package/@salesforce/plugin-data.md) — 394.9K weekly downloads
- [@backstage/plugin-search-common](https://npm.io/package/@backstage/plugin-search-common.md) — 308.5K weekly downloads
- [@chain-registry/types](https://npm.io/package/@chain-registry/types.md) — 38.4K weekly downloads

## Recent versions

- 0.0.40 (latest) — 2019-01-05
- 0.0.39 — 2018-11-21
- 0.0.38 — 2018-10-23
- 0.0.36 — 2018-10-22
- 0.0.35 — 2018-10-20
- 0.0.34 — 2018-10-04
- 0.0.33 — 2018-10-04
- 0.0.32 — 2018-10-04
- 0.0.31 — 2018-10-04
- 0.0.30 — 2018-10-04
- 0.0.29 — 2018-10-04
- 0.0.28 — 2018-10-04
- 0.0.27 — 2018-10-04
- 0.0.26 — 2018-10-03
- 0.0.25 — 2018-10-03
- … 8 more at https://npm.io/package/@spine/config/versions

## README

# Spine Config Utility

A configuration utility valid for both client and server

## Key aspects

* Exports config safely on browser
* Typed config structure
* Namespaces

## How to use it

1. Create a `config` directory in your project's root path

2. Create `config/clientConfigFilter.ts` and define which config should be safely exported to client

  ```
  import { ConfigSource } from '.';
  
  export const clientConfigFilter: ConfigSource['clientConfigFilter'] = {
    htmlPage: {
      welcomeMessage: true,
    },
  }
  ```

3. Create your `config/index.ts` to be imported in both client and server

  ```
  import {
    createConfig,
    ClientConfigFilter,
  } from '@spine/config';

  export interface MyProjectConfigSource {
    clientConfigFilter: ClientConfigFilter<this>;
    port: number;
    host: string;
    htmlPage: {
      welcomeMessage: string;
    };
  }

  export interface ConfigSource extends MyProjectConfigSource {}
  export const config = createConfig<ConfigSource>('MyProject', '__CONFIG__');
  export default config;

  const isBrowser = typeof window !== 'undefined';

  if (isBrowser) {
    config.hydrate();
  } else {
    require('./load');
  }
  ```

4. Create `config/load.ts` and load your config

  ```
  import * as envVars from '@spine/config/envVars';
  import { config } from '.';
  import { clientConfigFilter } from './clientConfigFilter';

  config.load({
    clientConfigFilter,
    port: envVars.number('PORT', 1337),
    host: envVars.number('HOST', '0.0.0.0'),
    htmlPage: {
      welcomeMessage: 'Welcome',
    },
  });
  ```

5. Start using your config inside your project

  ```
  import config from '../config';
  console.log(config.htmlPage.welcomeMessage);
  console.log(config('htmlPage.welcomeMessage'));
  ```

### Export config from your server to your client

  ```
  import { renderClientConfig } from '@spine/config/server';
  import { config } from './config';
  console.log(renderClientConfig(config, config.clientConfigFilter, 'your nonce here'));
  ```
  outputs

  ```
  <script nonce="your nonce here">
  window.__CONFIG__={
      htmlPage: {
        welcomeMessage: "Hello World"
      },
  }
  </script>
  ```

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