0.0.40 • Published 5 years ago

@spine/config v0.0.40

Weekly downloads
2
License
MIT
Repository
gitlab
Last release
5 years ago

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>
0.0.40

5 years ago

0.0.39

5 years ago

0.0.38

6 years ago

0.0.36

6 years ago

0.0.35

6 years ago

0.0.34

6 years ago

0.0.33

6 years ago

0.0.32

6 years ago

0.0.31

6 years ago

0.0.30

6 years ago

0.0.29

6 years ago

0.0.28

6 years ago

0.0.27

6 years ago

0.0.26

6 years ago

0.0.25

6 years ago

0.0.24

6 years ago

0.0.23

6 years ago

0.0.22

6 years ago

0.0.21

6 years ago

0.0.20

6 years ago

0.0.19

6 years ago

0.0.18

6 years ago

0.0.17

6 years ago