vies-checker
Version:
Modern European VIES VAT number validator with full TypeScript support
97 lines • 4.06 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ViesError = void 0;
exports.checkVAT = checkVAT;
exports.isValid = isValid;
const request_js_1 = __importDefault(require("./helpers/request.js"));
const errors_js_1 = require("./errors.js");
Object.defineProperty(exports, "ViesError", { enumerable: true, get: function () { return errors_js_1.ViesError; } });
const validators_js_1 = require("./validators.js");
/**
* Comprehensive VAT validation with full response data
*
* @param country - Two-letter EU country code (e.g., 'LU', 'DE', 'FR')
* @param vatNumber - VAT number to validate (spaces and separators will be removed)
* @param options - Optional request configuration (timeout, retries)
* @returns Full validation result with company details
* @throws {ViesError} When VIES service returns an error state
*
* @example
* ```typescript
* // Basic usage
* const result = await checkVAT('LU', '26375245');
* console.log(result.name); // "AMAZON EUROPE CORE S.A R.L."
* console.log(result.address); // Full address
* console.log(result.isValid); // true
*
* // With timeout
* const result = await checkVAT('LU', '26375245', { timeout: 5000 });
*
* // With retries
* const result = await checkVAT('LU', '26375245', { retries: 2 });
* ```
*/
function checkVAT(country, vatNumber, options) {
return __awaiter(this, void 0, void 0, function* () {
// Validate and normalize inputs
const validated = (0, validators_js_1.validateInput)(country, vatNumber);
// Make API request
const response = yield (0, request_js_1.default)(validated.country, validated.vatNumber, options);
// Transform to user-friendly result
return {
isValid: response.isValid,
requestDate: new Date(response.requestDate),
country: validated.country,
vatNumber: response.vatNumber || validated.vatNumber,
name: response.name,
address: response.address,
approximateMatch: response.viesApproximate,
};
});
}
/**
* Simple boolean VAT validation check
*
* @deprecated Consider using checkVAT() for richer response data including company name and address
* @param country - Two-letter EU country code
* @param number - VAT number to validate
* @returns true if valid, false if invalid or on network errors
* @throws {ViesError} When VIES service returns an error state (rate limits, service unavailable, etc.)
*
* @example
* ```typescript
* const valid = await isValid('LU', '26375245');
* console.log(valid); // true
* ```
*/
function isValid(country, number) {
return __awaiter(this, void 0, void 0, function* () {
if (!country || !number)
return null;
try {
const result = yield checkVAT(country, number);
return result.isValid;
}
catch (error) {
if (error instanceof errors_js_1.ViesError) {
throw error; // Propagate VIES errors
}
return false; // Network errors return false for backward compatibility
}
});
}
// Default export for backward compatibility
exports.default = isValid;
//# sourceMappingURL=index.js.map