{"_id":"@elfennol/env-to-js","_rev":"3-d996a516310ad471e9631bae8e44efa3","name":"@elfennol/env-to-js","dist-tags":{"latest":"1.0.2"},"versions":{"1.0.0":{"name":"@elfennol/env-to-js","version":"1.0.0","keywords":["symfony","env","runtime-config","typescript","browser"],"license":"MIT","_id":"@elfennol/env-to-js@1.0.0","maintainers":[{"name":"elfennol","email":"vincent.c.hevea@gmail.com"}],"homepage":"https://github.com/elfennol/env-to-js#readme","bugs":{"url":"https://github.com/elfennol/env-to-js/issues"},"dist":{"shasum":"d9e3f6ac5e0218e3317996383f8cb056a0289722","tarball":"https://registry.npmjs.org/@elfennol/env-to-js/-/env-to-js-1.0.0.tgz","fileCount":12,"integrity":"sha512-x6XwmOdlAY6LaygyWVYxZTGwzUdlANQsPZ3LA40PbdfpZT8mMm8Jue0rkhgqgqqQJEZojGEwOEXjnAMCLS82ww==","signatures":[{"sig":"MEUCIQCXIcfGtS/hn/bSDK2CHEJ8v7rC0Eyi94v0kApUVezMMwIgO/ZNTL8VxKnnQ7417cyDFKpYmkbVdk3SU2rEfXtwotA=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":7624},"main":"dist/index.js","type":"module","types":"dist/index.d.ts","exports":{".":{"types":"./dist/index.d.ts","import":"./dist/index.js"}},"gitHead":"6e3ba13efbd25ccd6a828066bed55ad72ec49bfb","scripts":{"qa":"npm run lint && npm run typecheck && npm run test","lint":"eslint src tests","test":"jest","build":"tsc","typecheck":"tsc --noEmit","test:coverage":"jest --coverage","prepublishOnly":"npm run build"},"_npmUser":{"name":"elfennol","email":"vincent.c.hevea@gmail.com"},"overrides":{"js-yaml":"^4.2.0"},"repository":{"url":"git+https://github.com/elfennol/env-to-js.git","type":"git"},"_npmVersion":"11.16.0","description":"Read runtime config injected as JSON script tags, with full TypeScript typing","directories":{},"_nodeVersion":"26.2.0","allowScripts":{"unrs-resolver@1.12.2":true},"_hasShrinkwrap":false,"devDependencies":{"jest":"^30.4.2","eslint":"^10.5.0","ts-jest":"^29.4.11","@eslint/js":"^10.0.1","typescript":"^5.5.0","@types/jest":"^30.0.0","typescript-eslint":"^8.61.0","jest-environment-jsdom":"^30.4.1"},"_npmOperationalInternal":{"tmp":"tmp/env-to-js_1.0.0_1781632892048_0.6566626533001305","host":"s3://npm-registry-packages-npm-production"}},"1.0.1":{"name":"@elfennol/env-to-js","version":"1.0.1","keywords":["symfony","env","runtime-config","typescript","browser"],"license":"MIT","_id":"@elfennol/env-to-js@1.0.1","maintainers":[{"name":"elfennol","email":"vincent.c.hevea@gmail.com"}],"homepage":"https://github.com/elfennol/env-to-js#readme","bugs":{"url":"https://github.com/elfennol/env-to-js/issues"},"dist":{"shasum":"3bc2bbae5316629959491a66fd3d33b4dd0fde45","tarball":"https://registry.npmjs.org/@elfennol/env-to-js/-/env-to-js-1.0.1.tgz","fileCount":7,"integrity":"sha512-l7y+W+VxYfnjykYeD/m15mMtRg2TuJcyUo5XVjZSent/OcCaget2ewHW86cZJ7EgsyvJqqw0wtcVXHKzrOhejQ==","signatures":[{"sig":"MEUCIDopBgAgLuoCjZds/23UWpqowbrN40p2Mguj/oniWss6AiEAuRg15rTcOEHJ6+Qtm8izz9yK6sSVmxjmqirKKCQHEKk=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":9206},"main":"./dist/index.cjs","type":"module","types":"./dist/index.d.ts","module":"./dist/index.js","exports":{".":{"import":{"types":"./dist/index.d.ts","default":"./dist/index.js"},"require":{"types":"./dist/index.d.cts","default":"./dist/index.cjs"}}},"gitHead":"a99ec6e85f7b48cbbc6b4442475f510ac863c30f","scripts":{"qa":"npm run lint && npm run typecheck && npm run test","lint":"eslint src tests","test":"jest","build":"tsup","typecheck":"tsc --noEmit","test:coverage":"jest --coverage","prepublishOnly":"npm run build"},"_npmUser":{"name":"elfennol","email":"vincent.c.hevea@gmail.com"},"overrides":{"esbuild":"^0.28.1","js-yaml":"^4.2.0"},"repository":{"url":"git+https://github.com/elfennol/env-to-js.git","type":"git"},"_npmVersion":"11.16.0","description":"Read runtime config injected as JSON script tags, with full TypeScript typing","directories":{},"_nodeVersion":"26.2.0","allowScripts":{"esbuild@0.28.1":true,"unrs-resolver@1.12.2":true},"_hasShrinkwrap":false,"devDependencies":{"jest":"^30.4.2","tsup":"^8.5.1","eslint":"^10.5.0","ts-jest":"^29.4.11","@eslint/js":"^10.0.1","typescript":"^5.5.0","@types/jest":"^30.0.0","typescript-eslint":"^8.61.0","jest-environment-jsdom":"^30.4.1"},"_npmOperationalInternal":{"tmp":"tmp/env-to-js_1.0.1_1781805278155_0.8802423904220726","host":"s3://npm-registry-packages-npm-production"}},"1.0.2":{"name":"@elfennol/env-to-js","version":"1.0.2","description":"Read runtime config injected as JSON script tags, with full TypeScript typing","keywords":["symfony","env","runtime-config","typescript","browser"],"license":"MIT","repository":{"type":"git","url":"git+https://github.com/elfennol/env-to-js.git"},"type":"module","main":"./dist/index.cjs","module":"./dist/index.js","types":"./dist/index.d.ts","exports":{".":{"import":{"types":"./dist/index.d.ts","default":"./dist/index.js"},"require":{"types":"./dist/index.d.cts","default":"./dist/index.cjs"}}},"scripts":{"build":"tsup","prepublishOnly":"npm run build","test":"jest","test:coverage":"jest --coverage","typecheck":"tsc --noEmit","lint":"eslint src tests","qa":"npm run lint && npm run typecheck && npm run test"},"overrides":{"js-yaml":"^4.2.0","esbuild":"^0.28.1"},"devDependencies":{"@eslint/js":"^10.0.1","@types/jest":"^30.0.0","eslint":"^10.5.0","jest":"^30.4.2","jest-environment-jsdom":"^30.4.1","ts-jest":"^29.4.11","tsup":"^8.5.1","typescript":"^5.5.0","typescript-eslint":"^8.61.0"},"allowScripts":{"unrs-resolver@1.12.2":true,"esbuild@0.28.1":true},"gitHead":"2ad705f8c02eab6fbb44fe47ce9e17393946be57","_id":"@elfennol/env-to-js@1.0.2","bugs":{"url":"https://github.com/elfennol/env-to-js/issues"},"homepage":"https://github.com/elfennol/env-to-js#readme","_nodeVersion":"26.2.0","_npmVersion":"11.16.0","dist":{"integrity":"sha512-CyInsURMmx2esRds0Q1sY2xLTUI8FVi9lytjKFPjOVB7TgkTqNsOaUy+VK1Fz8Jdj66bjdB9yufBhqunWpRZRQ==","shasum":"c8d2d2edbd1b364581e62ce25c59fc6671ed956c","tarball":"https://registry.npmjs.org/@elfennol/env-to-js/-/env-to-js-1.0.2.tgz","fileCount":7,"unpackedSize":9204,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEUCIA1RrB5w/dYDJ9vaAwlwykEwKGN02gYPacCmj6Xufq8TAiEAlihC/swTnp98QNjrdylMGxDqheuCeDQQ0K/0pLa2zn0="}]},"_npmUser":{"name":"elfennol","email":"vincent.c.hevea@gmail.com"},"directories":{},"maintainers":[{"name":"elfennol","email":"vincent.c.hevea@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/env-to-js_1.0.2_1781842572035_0.2823578074284312"},"_hasShrinkwrap":false}},"time":{"created":"2026-06-16T18:01:31.835Z","modified":"2026-06-19T04:16:12.267Z","1.0.0":"2026-06-16T18:01:32.185Z","1.0.1":"2026-06-18T17:54:38.313Z","1.0.2":"2026-06-19T04:16:12.182Z"},"bugs":{"url":"https://github.com/elfennol/env-to-js/issues"},"license":"MIT","homepage":"https://github.com/elfennol/env-to-js#readme","keywords":["symfony","env","runtime-config","typescript","browser"],"repository":{"type":"git","url":"git+https://github.com/elfennol/env-to-js.git"},"description":"Read runtime config injected as JSON script tags, with full TypeScript typing","maintainers":[{"name":"elfennol","email":"vincent.c.hevea@gmail.com"}],"readme":"# @elfennol/env-to-js\n\nRead runtime config injected as `<script type=\"application/json\">` tags, with full TypeScript typing.\n\nDesigned to work with [`elfennol/env-to-js-symfony-bundle`](https://github.com/elfennol/env-to-js-symfony-bundle), but usable with any backend that injects JSON script tags.\n\n## Installation\n\n```bash\nnpm install @elfennol/env-to-js\n```\n\n## Usage\n\n```ts\nimport { createConfigReader } from '@elfennol/env-to-js';\n\ntype AppConfig = {\n  apiBaseUrl: string;\n  cdnBaseUrl: string;\n};\n\nconst getAppConfig = createConfigReader<AppConfig>('#rc-app');\n\ngetAppConfig('apiBaseUrl');  // string\ngetAppConfig('cdnBaseUrl');  // string\n```\n\nThe selector passed to `createConfigReader` must match the `id` attribute of the injected `<script>` tag:\n\n```html\n<script type=\"application/json\" id=\"rc-app\">{\"apiBaseUrl\":\"https://api.example.com\"}</script>\n```\n\n## API\n\n### `createConfigReader<T>(selector)`\n\nReturns a typed getter function for the config block identified by `selector`.\n\n- Parses the JSON on the first key access, then caches the result for the lifetime of the reader instance.\n- Multiple readers are fully independent — each has its own cache.\n\n```ts\nconst getAppConfig = createConfigReader<AppConfig>('#rc-app');\nconst getMapConfig = createConfigReader<MapConfig>('#rc-map');\n```\n\n### `EnvToJsError`\n\nThrown when the element is not found or the JSON is invalid. The message is actionable:\n\n```\nEnvToJsError: element \"#rc-app\" not found. Make sure a <script type=\"application/json\"> tag matching this selector is present in your HTML.\nEnvToJsError: failed to parse JSON in element \"#rc-app\": Unexpected token\n```\n\nThese are configuration errors — they indicate a missing or malformed script tag, not a runtime condition to recover from. Let them bubble up so they are visible immediately.\n\nFor advanced use cases (centralised logging, error monitoring), `instanceof EnvToJsError` lets you distinguish them from other errors:\n\n```ts\nimport { createConfigReader, EnvToJsError } from '@elfennol/env-to-js';\n\nwindow.addEventListener('error', (event) => {\n  if (event.error instanceof EnvToJsError) {\n    reportToMonitoring(event.error.message);\n  }\n});\n```\n\n## With the Symfony bundle\n\nOnce [`elfennol/env-to-js-symfony-bundle`](https://github.com/elfennol/env-to-js-symfony-bundle) is installed and `env_to_js_scripts()` is added to your base template, the bundle injects a `<script>` tag per provider on each request:\n\n```html\n<script type=\"application/json\" id=\"rc-app\">{\"apiBaseUrl\":\"https://api.example.com\"}</script>\n```\n\nRead it in TypeScript:\n\n```ts\nimport { createConfigReader } from '@elfennol/env-to-js';\n\nconst getAppConfig = createConfigReader<{ apiBaseUrl: string }>('#rc-app');\n\nfetch(getAppConfig('apiBaseUrl') + '/users');\n```\n\n## Requirements\n\n- TypeScript >= 5.0\n- Zero dependencies\n- Compatible with any bundler (Vite, Webpack, esbuild, Rollup) and vanilla JS\n\n## Development\n\n```bash\nnpm run qa            # lint + typecheck + test\nnpm run lint          # ESLint only\nnpm run typecheck     # TypeScript type check only\nnpm test              # jest only\n```\n\n## License\n\nMIT\n","readmeFilename":"README.md"}