Introduction β
ts-vat is a powerful TypeScript library designed to handle EU VAT calculations and validations with precision and ease. It provides a comprehensive solution for businesses dealing with European Union Value Added Tax regulations, including special cases, reduced rates, and post-Brexit UK rules.
Features β
πͺπΊ Complete EU VAT Coverage β
- Support for all EU member states
- Post-Brexit UK VAT rules
- Special territories and exceptions
- Reduced rates and parking rates
- Reverse charge mechanism
β Comprehensive Validation β
- VAT number validation against official VIES service
- Postal code validation for all EU countries
- Country code validation
- Detailed error messages and handling
π― Special Cases β
- Support for special territories (e.g., Canary Islands, Mount Athos)
- Handling of B2B and B2C transactions
- Cross-border transaction rules
- Reverse charge mechanism
π‘οΈ Type Safety β
- Full TypeScript support
- Comprehensive type definitions
- Compile-time checks
- IDE autocompletion
π§ Flexible Configuration β
- Customizable validation rules
- Configurable timeouts
- Error handling options
- Business country settings
Browser Compatibility β
ts-vat is designed to work in both Node.js / Bun and modern browser environments. Here's what you need to know:
Modern Browsers β
- Chrome 61+
- Firefox 60+
- Safari 11+
- Edge 79+
Key Considerations β
- The library uses the
fetch
API for VIES service communication - XML parsing is handled via the
@xmldom/xmldom
package - All modern ES6+ features are transpiled for broader compatibility
- The library is tree-shakeable for optimal bundle size
CORS Considerations β
When using the library in a browser environment, you may need to handle CORS for VIES service calls. We recommend:
- Using a CORS proxy in development
- Setting up proper backend proxying in production
- Implementing request caching for better performance
Performance Optimization β
ts-vat is optimized for:
- Tree Shaking: Only import what you need
- Caching: Implement response caching for VAT validations
- Lazy Loading: Import validation features on demand
- Bundle Size: Core package is under 10KB minified and gzipped
Use Cases β
ts-vat is perfect for:
- E-commerce platforms
- SaaS businesses
- Digital service providers
- Accounting software
- Invoice generation systems
- Tax compliance tools
Real-World Example β
Here's a quick example of calculating VAT for a product sold to a customer in Germany:
import { VatCalculator } from 'ts-vat'
const calculator = new VatCalculator({
businessCountryCode: 'DE',
validateVatNumbers: true,
})
// Calculate VAT for a β¬100 product sold to a business customer in Germany
const result = calculator.calculate(
100,
'DE',
'10115', // Berlin postal code
true, // is business customer
)
console.log(result)
// Output:
// {
// netPrice: 100,
// grossPrice: 119,
// vatAmount: 19,
// vatRate: 0.19,
// countryCode: 'DE',
// isCompany: true,
// details: {
// ruleApplied: 'standard',
// reverseCharge: true,
// vatNumberUsed: undefined,
// postalCodeUsed: '10115'
// }
// }
Why ts-vat? β
- Accuracy: Built with precision in mind, following official EU VAT rules and regulations
- Type Safety: Full TypeScript support ensures compile-time error catching
- Validation: Built-in validation for VAT numbers, postal codes, and country codes
- Maintenance: Regular updates to keep up with VAT rate changes and regulation updates
- Performance: Optimized for high-performance applications with minimal overhead
Ready to get started? Check out our Installation Guide or dive into the Usage Documentation.
Contributing β
Please see CONTRIBUTING for details.
Stargazers β
Community β
For help, discussion about best practices, or any other conversation that would benefit from being searchable:
For casual chit-chat with others using this package:
Join the Stacks Discord Server
Postcardware β
βSoftware that is free, but hopes for a postcard.β We love receiving postcards from around the world showing where Stacks is being used! We showcase them on our website too.
Our address: Stacks.js, 12665 Village Ln #2306, Playa Vista, CA 90094, United States π
Credits β
- Rinvex for the original PHP library
- Chris Breuer for the TypeScript conversion
Sponsors β
We would like to extend our thanks to the following sponsors for funding Stacks development. If you are interested in becoming a sponsor, please reach out to us.
License β
The MIT License (MIT). Please see LICENSE for more information.
Made with π