Skip to content

Commit 72c09f1

Browse files
committed
Initial release
1 parent b64e1cc commit 72c09f1

File tree

11 files changed

+8954
-104
lines changed

11 files changed

+8954
-104
lines changed

.github/workflows/main.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: CI
2+
on: [push]
3+
jobs:
4+
build:
5+
name: Build, lint, and test on Node ${{ matrix.node }} and ${{ matrix.os }}
6+
7+
runs-on: ${{ matrix.os }}
8+
strategy:
9+
matrix:
10+
node: ['10.x', '12.x', '14.x']
11+
os: [ubuntu-latest, windows-latest, macOS-latest]
12+
13+
steps:
14+
- name: Checkout repo
15+
uses: actions/checkout@v2
16+
17+
- name: Use Node ${{ matrix.node }}
18+
uses: actions/setup-node@v1
19+
with:
20+
node-version: ${{ matrix.node }}
21+
22+
- name: Install deps and build (with cache)
23+
uses: bahmutov/npm-install@v1
24+
25+
- name: Lint
26+
run: yarn lint
27+
28+
- name: Test
29+
run: yarn test --ci --coverage --maxWorkers=2
30+
31+
- name: Build
32+
run: yarn build

.github/workflows/release.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: Release
2+
on: [push]
3+
jobs:
4+
release:
5+
runs-on: ubuntu-latest
6+
if: "!contains(github.event.head_commit.message, 'ci skip') && !contains(github.event.head_commit.message, 'skip ci')"
7+
steps:
8+
- uses: actions/checkout@v2
9+
10+
- name: Prepare repository
11+
run: git fetch --unshallow --tags
12+
13+
- name: Use Node.js 12.x
14+
uses: actions/setup-node@v1
15+
with:
16+
node-version: 12.x
17+
18+
- name: Cache node modules
19+
uses: actions/cache@v1
20+
with:
21+
path: node_modules
22+
key: yarn-deps-${{ hashFiles('yarn.lock') }}
23+
restore-keys: |
24+
yarn-deps-${{ hashFiles('yarn.lock') }}
25+
- name: Install
26+
run: yarn --frozen-lockfile
27+
28+
- name: Build
29+
run: yarn build
30+
31+
- name: Release
32+
env:
33+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
34+
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
35+
run: yarn run auto shipit

.github/workflows/size.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
name: size
2+
on: [pull_request]
3+
jobs:
4+
size:
5+
runs-on: ubuntu-latest
6+
env:
7+
CI_JOB_NUMBER: 1
8+
steps:
9+
- uses: actions/checkout@v1
10+
- uses: andresz1/size-limit-action@v1
11+
with:
12+
github_token: ${{ secrets.GITHUB_TOKEN }}

.gitignore

Lines changed: 2 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,104 +1,4 @@
1-
# Logs
2-
logs
31
*.log
4-
npm-debug.log*
5-
yarn-debug.log*
6-
yarn-error.log*
7-
lerna-debug.log*
8-
9-
# Diagnostic reports (https://nodejs.org/api/report.html)
10-
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
11-
12-
# Runtime data
13-
pids
14-
*.pid
15-
*.seed
16-
*.pid.lock
17-
18-
# Directory for instrumented libs generated by jscoverage/JSCover
19-
lib-cov
20-
21-
# Coverage directory used by tools like istanbul
22-
coverage
23-
*.lcov
24-
25-
# nyc test coverage
26-
.nyc_output
27-
28-
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
29-
.grunt
30-
31-
# Bower dependency directory (https://bower.io/)
32-
bower_components
33-
34-
# node-waf configuration
35-
.lock-wscript
36-
37-
# Compiled binary addons (https://nodejs.org/api/addons.html)
38-
build/Release
39-
40-
# Dependency directories
41-
node_modules/
42-
jspm_packages/
43-
44-
# TypeScript v1 declaration files
45-
typings/
46-
47-
# TypeScript cache
48-
*.tsbuildinfo
49-
50-
# Optional npm cache directory
51-
.npm
52-
53-
# Optional eslint cache
54-
.eslintcache
55-
56-
# Microbundle cache
57-
.rpt2_cache/
58-
.rts2_cache_cjs/
59-
.rts2_cache_es/
60-
.rts2_cache_umd/
61-
62-
# Optional REPL history
63-
.node_repl_history
64-
65-
# Output of 'npm pack'
66-
*.tgz
67-
68-
# Yarn Integrity file
69-
.yarn-integrity
70-
71-
# dotenv environment variables file
72-
.env
73-
.env.test
74-
75-
# parcel-bundler cache (https://parceljs.org/)
76-
.cache
77-
78-
# Next.js build output
79-
.next
80-
81-
# Nuxt.js build / generate output
82-
.nuxt
2+
.DS_Store
3+
node_modules
834
dist
84-
85-
# Gatsby files
86-
.cache/
87-
# Comment in the public line in if your project uses Gatsby and *not* Next.js
88-
# https://nextjs.org/blog/next-9-1#public-directory-support
89-
# public
90-
91-
# vuepress build output
92-
.vuepress/dist
93-
94-
# Serverless directories
95-
.serverless/
96-
97-
# FuseBox cache
98-
.fusebox/
99-
100-
# DynamoDB Local files
101-
.dynamodb/
102-
103-
# TernJS port file
104-
.tern-port

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1818
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1919
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2020
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21-
SOFTWARE.
21+
SOFTWARE.

README.md

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,68 @@
1-
# config-runtime
1+
# config-runtime
2+
3+
## Motivation
4+
5+
The handoff from the server to client of config values is not simple. Typings are hard, global variables are used and usually not cleaned up and there's no distinction of server and client only configs.
6+
7+
This library is heavily inspired from [NextJS Runtime Config](https://nextjs.org/docs/api-reference/next.config.js/runtime-configuration).
8+
9+
## Usage
10+
11+
```ts
12+
import { CreateRuntimeConfig, RuntimeConfig } from 'config-runtime'
13+
14+
interface ClientConfig {
15+
foo: string
16+
}
17+
18+
interface ServerConfig {
19+
bar: number
20+
}
21+
22+
const myConfig: RuntimeConfig<ClientConfig, ServerConfig> = {
23+
client: {
24+
foo: 'hello',
25+
},
26+
server: {
27+
bar: 5,
28+
},
29+
}
30+
31+
const Config = new CreateRuntimeConfig<ClientConfig, ServerConfig>(
32+
// Useful for different kinds of config
33+
'__MY_CONFIG_NAME_TOTALLY_OPTIONAL__'
34+
)
35+
36+
// Server entrypoint (only once)
37+
Config.setConfig(
38+
myConfig
39+
)
40+
41+
// Handoff between server and client (html payload)
42+
`<script>${Config.serializedClientConfig}</script>`
43+
44+
// On the server
45+
config.client // { foo: 'hello' }
46+
config.server // { bar: 5 }
47+
48+
// On the client
49+
config.client // { foo: 'hello' }
50+
config.server // Error
51+
```
52+
53+
## Features
54+
55+
1. Allows for server only config
56+
2. An app could have as many configs as it wants (They are cached by the configName parameter in `new CreateRuntimeConfig(configName)`)
57+
3. Caching enabled (Saves to an in memory variable, never to the global scope, exception being on the payload sent from the server, which get's deleted on the first getCall made)
58+
59+
## Flow
60+
61+
1. Create a config with this module, with a unique key and export it
62+
2. Import that config in your server side entrypoint and call setConfig at the very top, just once
63+
3. In the server to client handoff payload send the serialized public runtime config `serializedClientConfig`, this allows it to be available in Config.client
64+
4. Call `Config.client` anywhere and call `Config.server` only on the server, doing something else will throw errors
65+
66+
## Gotchas
67+
68+
It doesn't protect against possible runtime errors due to something relying on a server value in the client so be careful.

package.json

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
{
2+
"name": "config-runtime",
3+
"version": "0.1.0",
4+
"author": "Flávio Carvalho",
5+
"license": "MIT",
6+
"module": "dist/config-runtime.esm.js",
7+
"main": "dist/index.js",
8+
"typings": "dist/index.d.ts",
9+
"files": [
10+
"dist",
11+
"src"
12+
],
13+
"engines": {
14+
"node": ">=10"
15+
},
16+
"scripts": {
17+
"start": "tsdx watch",
18+
"build": "tsdx build",
19+
"test": "tsdx test",
20+
"lint": "tsdx lint",
21+
"prepare": "tsdx build",
22+
"size": "size-limit",
23+
"analyze": "size-limit --why"
24+
},
25+
"peerDependencies": {},
26+
"husky": {
27+
"hooks": {
28+
"pre-commit": "tsdx lint"
29+
}
30+
},
31+
"size-limit": [
32+
{
33+
"path": "dist/config-runtime.cjs.production.min.js",
34+
"limit": "10 KB"
35+
},
36+
{
37+
"path": "dist/config-runtime.esm.js",
38+
"limit": "10 KB"
39+
}
40+
],
41+
"devDependencies": {
42+
"@size-limit/preset-small-lib": "^4.12.0",
43+
"husky": "^6.0.0",
44+
"size-limit": "^4.12.0",
45+
"tsdx": "^0.14.1",
46+
"tslib": "^2.3.0",
47+
"typescript": "^4.3.2"
48+
},
49+
"dependencies": {
50+
"serialize-javascript": "^5.0.1"
51+
},
52+
"jest": {
53+
"coverageThreshold": {
54+
"global": {
55+
"branches": 90,
56+
"functions": 85,
57+
"lines": 95,
58+
"statements": 95
59+
}
60+
}
61+
}
62+
63+
}

0 commit comments

Comments
 (0)