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/esm/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>;
}

59
dist/esm/client.js vendored Normal file
View File

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

6
dist/esm/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";
};

8
dist/esm/constants.js vendored Normal file
View File

@@ -0,0 +1,8 @@
export const DEFAULT_CONFIG = {
timeout: 30000,
retries: 3,
};
export const ENDPOINTS = {
validateXml: "/api/v1/e-invoice/validate/xml",
validatePdf: "/api/v1/e-invoice/validate/pdf",
};

4
dist/esm/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";

4
dist/esm/mod.js 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";

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

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

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

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

1
dist/esm/types/config.js vendored Normal file
View File

@@ -0,0 +1 @@
export {};

9
dist/esm/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;
}

8
dist/esm/types/errors.js vendored Normal file
View File

@@ -0,0 +1,8 @@
export class ValidationError extends Error {
statusCode;
constructor(message, statusCode) {
super(message);
this.statusCode = statusCode;
this.name = "ValidationError";
}
}

8
dist/esm/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;

1
dist/esm/types/validation.js vendored Normal file
View File

@@ -0,0 +1 @@
export {};

5
dist/esm/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>;

17
dist/esm/utils.js vendored Normal file
View File

@@ -0,0 +1,17 @@
import { ValidationError } from "./types/errors.js";
export function toBlob(content) {
if (content instanceof Blob)
return content;
return new Blob([content]);
}
export async function handleResponse(response) {
if (!response.ok) {
const errorData = await response.json().catch(() => ({
message: response.statusText,
status: `${response.status}`,
}));
throw new ValidationError(errorData.message || "Unknown error", response.status);
}
const data = await response.json();
return data;
}