@bvhoach2393/nest-sum
v1.1.8
Published
A NestJS library for safe number summation with MAX_VALUE validation
Maintainers
Readme
@bvhoach2393/nest-sum
A NestJS library for safe number summation with MAX_VALUE validation.
Features
- Validates input numbers against a configurable MAX_VALUE
- Integrates with NestJS ConfigService
- Provides clear error messages for validation failures
- Full TypeScript support
Installation
npm install @bvhoach2393/nest-sumUsage
1. Import the module
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { NestSumModule } from '@bvhoach2393/nest-sum';
@Module({
imports: [
ConfigModule.forRoot({
isGlobal: true,
}),
NestSumModule,
],
})
export class AppModule {}2. Use the service
import { Injectable } from '@nestjs/common';
import { NestSumService } from '@bvhoach2393/nest-sum';
@Injectable()
export class MyService {
constructor(private readonly nestSumService: NestSumService) {}
calculateSum(a: number, b: number): number {
return this.nestSumService.sum(a, b);
}
}3. Environment configuration
Set the MAX_VALUE in your environment variables:
MAX_VALUE=1000API
sum(a: number, b: number): number
Calculates the sum of two numbers with validation.
Parameters:
a: First numberb: Second number
Returns:
- The sum of
aandb
Throws:
- Error if
MAX_VALUEis not configured - Error if either
aorbexceedsMAX_VALUE
Example
// With MAX_VALUE=100
nestSumService.sum(30, 40); // Returns 70
nestSumService.sum(150, 20); // Throws error: exceeds MAX_VALUETesting
Running Unit Tests
To run the unit tests for this library:
# Run tests for the nest-sum library
npm test -- libs/nest-sum
# Run tests with coverage
npm run test:cov -- libs/nest-sum
# Run tests in watch mode
npm run test:watch -- libs/nest-sumTest Coverage
The library includes comprehensive unit tests covering:
- ✅ Service initialization
- ✅ Valid sum calculation within MAX_VALUE
- ✅ Error handling when MAX_VALUE is not configured
- ✅ Validation when parameters exceed MAX_VALUE
- ✅ Console logging for error cases
Development
Local Development
- Clone the repository
- Install dependencies:
npm install - Set up environment:
cp .env.example .env - Build the library:
npm run build:lib - Run tests:
npm test -- libs/nest-sum
Building
# Build the library
npm run build:lib
# Build all projects
npm run buildPublishing to NPM Registry
Prerequisites
- Create an NPM account at npmjs.com
- Login to NPM:
npm login - Verify your account:
npm whoami
Publishing Steps
# 1. Navigate to the library directory
cd libs/nest-sum
# 2. Update version in package.json if needed
npm version patch # or minor/major
# 3. Build the library
cd ../..
npm run build:lib
# 4. Navigate back to library directory
cd libs/nest-sum
# 5. Publish to NPM
npm publish --access public
# For scoped packages, use:
npm publish --access publicVersion Management
# Patch version (1.0.0 -> 1.0.1)
npm version patch
# Minor version (1.0.0 -> 1.1.0)
npm version minor
# Major version (1.0.0 -> 2.0.0)
npm version major
Publication Checklist
- [ ] All tests pass:
npm test -- libs/nest-sum - [ ] Build successful:
npm run build:lib - [ ] Version updated in
package.json - [ ] README.md is up to date
- [ ] Dependencies are correctly specified as peer dependencies
- [ ] NPM account has appropriate permissions
Peer Dependencies
@nestjs/common: ^10.0.0 || ^11.0.0@nestjs/config: ^3.0.0 || ^4.0.0@nestjs/core: ^10.0.0 || ^11.0.0reflect-metadata: ^0.1.13 || ^0.2.0rxjs: ^7.0.0
License
MIT
