Skip to content

Export calculation engine as standalone npm package#37

Merged
kcrt merged 5 commits into
masterfrom
copilot/add-npm-install-calculation-feature
Feb 6, 2026
Merged

Export calculation engine as standalone npm package#37
kcrt merged 5 commits into
masterfrom
copilot/add-npm-install-calculation-feature

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Feb 6, 2026

The Next.js application's calculation engine can now be consumed as an npm package by external applications.

Changes

Library Export (lib/index.ts)

  • Exports formula access (getFormula, formulaData, evaluateFormulaOutputs)
  • Exports medical utilities (BSA_DuBois, GetZScore, erf, etc.)
  • Exports all TypeScript types (Formula, FormulaData, FormulaInput/Output)

Build Pipeline

  • Added tsconfig.lib.json targeting CommonJS for Node.js compatibility
  • Integrated tsc-alias to resolve @/* path mappings at build time
  • Build script: npm run build:lib

Package Configuration

  • Removed private: true, added main and types fields
  • files whitelist: dist/, src/formulas/
  • prepublishOnly hook ensures build before publish

Type Safety

  • Fixed CalculationFormula and HtmlFormula to include base Formula fields (name, info, metadata, locales)

Usage

import { getFormula, evaluateFormulaOutputs } from 'med';

const formula = getFormula('bmi_adult');
const results = evaluateFormulaOutputs(formula, {
  height: 170,
  weight: 70
});
// { BMI: 24.2, who_diag: "normal", jasso_diag: "普通体重" }

Core dependencies: expr-eval, zod. Next.js/React dependencies included but not required for calculation functionality.

Original prompt

The current code is working perfectly fine. It functions flawlessly as a Next.js application, and the API features are also operational.
However, I also thought it would be more convenient if other applications could utilize this calculation feature as well.
Make calculation feature available for use by installing it via npm from another application.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 4 commits February 6, 2026 04:25
Co-authored-by: kcrt <505011+kcrt@users.noreply.github.com>
- Created lib/index.ts as main export file for the package
- Added TypeScript build configuration (tsconfig.lib.json)
- Updated package.json with exports, main, types, and build:lib script
- Split dependencies: core deps (expr-eval, zod) vs optional Next.js deps
- Added comprehensive library tests to verify exports work correctly
- Created LIB_README.md with usage examples and API documentation
- Updated main README.md to include npm package usage instructions
- Added /dist to .gitignore to avoid committing build artifacts
- Fixed CalculationFormula and HtmlFormula types to align with Formula schema

Co-authored-by: kcrt <505011+kcrt@users.noreply.github.com>
- Removed jsx compiler option from tsconfig.lib.json (not needed for library)
- Removed .js extensions from TypeScript imports in lib/index.ts (let tsc-alias handle it)
- Updated package description to clarify dual use as library and web app
- All tests pass (except pre-existing ShareButton test failure unrelated to changes)

Co-authored-by: kcrt <505011+kcrt@users.noreply.github.com>
- Created PACKAGE_PUBLISHING.md with step-by-step instructions
- Includes pre-publishing checklist
- Covers publishing steps, testing, and troubleshooting
- Documents what gets published and how users will consume the package

Co-authored-by: kcrt <505011+kcrt@users.noreply.github.com>
Copilot AI changed the title [WIP] Add calculation feature for npm installation Export calculation engine as standalone npm package Feb 6, 2026
Copilot AI requested a review from kcrt February 6, 2026 04:37
@kcrt kcrt marked this pull request as ready for review February 6, 2026 05:04
@kcrt kcrt merged commit c308743 into master Feb 6, 2026
0 of 2 checks passed
@kcrt kcrt deleted the copilot/add-npm-install-calculation-feature branch February 6, 2026 05:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants