0.0.40 • Published 6 years ago
@spine/config v0.0.40
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
Create a
config
directory in your project's root pathCreate
config/clientConfigFilter.ts
and define which config should be safely exported to clientimport { ConfigSource } from '.'; export const clientConfigFilter: ConfigSource['clientConfigFilter'] = { htmlPage: { welcomeMessage: true, }, }
Create your
config/index.ts
to be imported in both client and serverimport { 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'); }
Create
config/load.ts
and load your configimport * 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', }, });
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
6 years ago
0.0.39
7 years ago
0.0.38
7 years ago
0.0.36
7 years ago
0.0.35
7 years ago
0.0.34
7 years ago
0.0.33
7 years ago
0.0.32
7 years ago
0.0.31
7 years ago
0.0.30
7 years ago
0.0.29
7 years ago
0.0.28
7 years ago
0.0.27
7 years ago
0.0.26
7 years ago
0.0.25
7 years ago
0.0.24
7 years ago
0.0.23
7 years ago
0.0.22
7 years ago
0.0.21
7 years ago
0.0.20
7 years ago
0.0.19
7 years ago
0.0.18
7 years ago
0.0.17
7 years ago