first commit

This commit is contained in:
Raul Lugo
2024-12-25 20:30:57 +01:00
commit 6ef22531bb
165 changed files with 53426 additions and 0 deletions

8
dist/script/client.d.ts vendored Normal file
View File

@@ -0,0 +1,8 @@
import { ClientConfig } from "./types/config.js";
import { FileContent, ValidationResult } from "./types/validation.js";
export declare class CaballoClient {
private readonly config;
constructor(config: ClientConfig);
validateXml(content: FileContent): Promise<ValidationResult>;
validatePdf(content: FileContent): Promise<ValidationResult>;
}

63
dist/script/client.js vendored Normal file
View File

@@ -0,0 +1,63 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CaballoClient = void 0;
const errors_js_1 = require("./types/errors.js");
const constants_js_1 = require("./constants.js");
const utils_js_1 = require("./utils.js");
class CaballoClient {
config;
constructor(config) {
this.config = {
...constants_js_1.DEFAULT_CONFIG,
...config,
};
if (!this.config.baseUrl) {
throw new Error("baseUrl is required");
}
}
async validateXml(content) {
try {
const blob = (0, utils_js_1.toBlob)(content);
if (blob.size === 0) {
throw new errors_js_1.ValidationError("Empty file content");
}
const formData = new FormData();
formData.append("file", blob);
const response = await fetch(`${this.config.baseUrl}${constants_js_1.ENDPOINTS.validateXml}`, {
method: "POST",
body: formData,
signal: AbortSignal.timeout(this.config.timeout),
});
return (0, utils_js_1.handleResponse)(response);
}
catch (error) {
if (error instanceof TypeError) {
throw new errors_js_1.ValidationError("Network error or invalid URL");
}
throw error;
}
}
async validatePdf(content) {
try {
const blob = (0, utils_js_1.toBlob)(content);
if (blob.size === 0) {
throw new errors_js_1.ValidationError("Empty file content");
}
const formData = new FormData();
formData.append("file", blob);
const response = await fetch(`${this.config.baseUrl}${constants_js_1.ENDPOINTS.validatePdf}`, {
method: "POST",
body: formData,
signal: AbortSignal.timeout(this.config.timeout),
});
return (0, utils_js_1.handleResponse)(response);
}
catch (error) {
if (error instanceof TypeError) {
throw new errors_js_1.ValidationError("Network error or invalid URL");
}
throw error;
}
}
}
exports.CaballoClient = CaballoClient;

6
dist/script/constants.d.ts vendored Normal file
View File

@@ -0,0 +1,6 @@
import { ClientConfig } from "./types/config.js";
export declare const DEFAULT_CONFIG: Partial<ClientConfig>;
export declare const ENDPOINTS: {
readonly validateXml: "/api/v1/e-invoice/validate/xml";
readonly validatePdf: "/api/v1/e-invoice/validate/pdf";
};

11
dist/script/constants.js vendored Normal file
View File

@@ -0,0 +1,11 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ENDPOINTS = exports.DEFAULT_CONFIG = void 0;
exports.DEFAULT_CONFIG = {
timeout: 30000,
retries: 3,
};
exports.ENDPOINTS = {
validateXml: "/api/v1/e-invoice/validate/xml",
validatePdf: "/api/v1/e-invoice/validate/pdf",
};

4
dist/script/mod.d.ts vendored Normal file
View File

@@ -0,0 +1,4 @@
export * from "./client.js";
export * from "./types/config.js";
export * from "./types/errors.js";
export * from "./types/validation.js";

20
dist/script/mod.js vendored Normal file
View File

@@ -0,0 +1,20 @@
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
__exportStar(require("./client.js"), exports);
__exportStar(require("./types/config.js"), exports);
__exportStar(require("./types/errors.js"), exports);
__exportStar(require("./types/validation.js"), exports);

3
dist/script/package.json vendored Normal file
View File

@@ -0,0 +1,3 @@
{
"type": "commonjs"
}

5
dist/script/types/config.d.ts vendored Normal file
View File

@@ -0,0 +1,5 @@
export interface ClientConfig {
baseUrl: string;
timeout?: number;
retries?: number;
}

2
dist/script/types/config.js vendored Normal file
View File

@@ -0,0 +1,2 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });

9
dist/script/types/errors.d.ts vendored Normal file
View File

@@ -0,0 +1,9 @@
export declare class ValidationError extends Error {
statusCode?: number | undefined;
constructor(message: string, statusCode?: number | undefined);
}
export interface ApiError {
status: string;
message: string;
details?: string;
}

12
dist/script/types/errors.js vendored Normal file
View File

@@ -0,0 +1,12 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ValidationError = void 0;
class ValidationError extends Error {
statusCode;
constructor(message, statusCode) {
super(message);
this.statusCode = statusCode;
this.name = "ValidationError";
}
}
exports.ValidationError = ValidationError;

8
dist/script/types/validation.d.ts vendored Normal file
View File

@@ -0,0 +1,8 @@
/// <reference types="node" />
export interface ValidationResult {
valid: boolean;
conformanceLevel?: string;
structureErrors?: string[];
pdfErrors?: string[];
}
export type FileContent = Uint8Array | Blob | File;

2
dist/script/types/validation.js vendored Normal file
View File

@@ -0,0 +1,2 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });

5
dist/script/utils.d.ts vendored Normal file
View File

@@ -0,0 +1,5 @@
/// <reference types="node" />
/// <reference types="node" />
import { FileContent, ValidationResult } from "./types/validation.js";
export declare function toBlob(content: FileContent): Blob;
export declare function handleResponse(response: Response): Promise<ValidationResult>;

22
dist/script/utils.js vendored Normal file
View File

@@ -0,0 +1,22 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.handleResponse = exports.toBlob = void 0;
const errors_js_1 = require("./types/errors.js");
function toBlob(content) {
if (content instanceof Blob)
return content;
return new Blob([content]);
}
exports.toBlob = toBlob;
async function handleResponse(response) {
if (!response.ok) {
const errorData = await response.json().catch(() => ({
message: response.statusText,
status: `${response.status}`,
}));
throw new errors_js_1.ValidationError(errorData.message || "Unknown error", response.status);
}
const data = await response.json();
return data;
}
exports.handleResponse = handleResponse;