2026-01-13 00:30:39 +01:00
|
|
|
/**
|
|
|
|
|
* Binect API client
|
2026-01-14 16:50:57 +01:00
|
|
|
*
|
2026-01-15 14:41:44 +01:00
|
|
|
* This module wraps the @binect/js library to provide a simplified API
|
|
|
|
|
* for the Chrome extension. It delegates all API integration to the
|
|
|
|
|
* upstream library.
|
2026-01-13 00:30:39 +01:00
|
|
|
*/
|
|
|
|
|
|
2026-01-15 14:41:44 +01:00
|
|
|
import {
|
|
|
|
|
BinectClient,
|
|
|
|
|
BinectApiError,
|
|
|
|
|
BinectAuthError,
|
|
|
|
|
type Document as BinectDocument,
|
|
|
|
|
type DocumentUploadOptions,
|
|
|
|
|
EnvelopeType,
|
|
|
|
|
FrankingType,
|
|
|
|
|
} from '@binect/js';
|
2026-01-14 16:50:57 +01:00
|
|
|
|
2026-01-15 14:41:44 +01:00
|
|
|
// Re-export types for backward compatibility
|
|
|
|
|
export interface Options {
|
|
|
|
|
simplex?: boolean; // if false, it's duplex
|
|
|
|
|
color?: boolean; // if false, it's black and white
|
|
|
|
|
envelope?: 'DINLANG' | 'C4';
|
|
|
|
|
franking?: 'UNSPECIFIED' | 'STANDARD_FRANKING' | 'DV_FRANKING';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Document type matching what popup.ts expects
|
2026-01-14 16:50:57 +01:00
|
|
|
export interface Document {
|
|
|
|
|
id: number;
|
|
|
|
|
filename: string;
|
|
|
|
|
numberOfPages?: number;
|
|
|
|
|
status: {
|
|
|
|
|
code: number;
|
|
|
|
|
text: string;
|
|
|
|
|
};
|
|
|
|
|
documentType: 'Letter' | 'SerialLetter';
|
|
|
|
|
letter?: {
|
|
|
|
|
letterType: 'LetterData' | 'Error';
|
|
|
|
|
letterData?: {
|
|
|
|
|
recipientAddress: string;
|
|
|
|
|
price: {
|
|
|
|
|
priceBeforeTax: number;
|
|
|
|
|
priceAfterTax: number;
|
|
|
|
|
unit: string;
|
|
|
|
|
taxInPercent: number;
|
|
|
|
|
};
|
|
|
|
|
international: boolean;
|
|
|
|
|
options: Options;
|
|
|
|
|
};
|
|
|
|
|
errors?: Array<{
|
|
|
|
|
code: number;
|
|
|
|
|
text: string;
|
|
|
|
|
blankText: string;
|
|
|
|
|
}>;
|
|
|
|
|
};
|
2026-01-13 00:30:39 +01:00
|
|
|
}
|
|
|
|
|
|
2026-01-15 14:41:44 +01:00
|
|
|
/**
|
|
|
|
|
* Custom error class for Binect API errors
|
|
|
|
|
* Wraps errors from @binect/js for backward compatibility
|
|
|
|
|
*/
|
2026-01-13 00:30:39 +01:00
|
|
|
export class BinectAPIError extends Error {
|
|
|
|
|
constructor(
|
|
|
|
|
message: string,
|
|
|
|
|
public statusCode?: number,
|
|
|
|
|
public response?: unknown
|
|
|
|
|
) {
|
|
|
|
|
super(message);
|
|
|
|
|
this.name = 'BinectAPIError';
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-15 14:41:44 +01:00
|
|
|
/**
|
|
|
|
|
* Create from a @binect/js error
|
|
|
|
|
*/
|
|
|
|
|
static fromBinectError(error: BinectApiError | BinectAuthError): BinectAPIError {
|
|
|
|
|
if (error instanceof BinectAuthError) {
|
|
|
|
|
return new BinectAPIError('Invalid credentials', 401);
|
|
|
|
|
}
|
|
|
|
|
return new BinectAPIError(
|
|
|
|
|
error.message,
|
|
|
|
|
error.status,
|
|
|
|
|
error.response
|
|
|
|
|
);
|
|
|
|
|
}
|
2026-01-14 16:50:57 +01:00
|
|
|
}
|
2026-01-13 00:30:39 +01:00
|
|
|
|
2026-01-14 16:50:57 +01:00
|
|
|
/**
|
2026-01-15 14:41:44 +01:00
|
|
|
* Convert ArrayBuffer to base64 string (browser-compatible)
|
2026-01-14 16:50:57 +01:00
|
|
|
*/
|
|
|
|
|
function arrayBufferToBase64(buffer: ArrayBuffer): string {
|
|
|
|
|
const bytes = new Uint8Array(buffer);
|
|
|
|
|
let binary = '';
|
|
|
|
|
for (let i = 0; i < bytes.byteLength; i++) {
|
|
|
|
|
binary += String.fromCharCode(bytes[i]);
|
2026-01-13 00:30:39 +01:00
|
|
|
}
|
2026-01-14 16:50:57 +01:00
|
|
|
return btoa(binary);
|
2026-01-13 00:30:39 +01:00
|
|
|
}
|
|
|
|
|
|
2026-01-15 14:41:44 +01:00
|
|
|
/**
|
|
|
|
|
* Map local Options to DocumentUploadOptions
|
|
|
|
|
*/
|
|
|
|
|
function mapOptions(options?: Options): Partial<DocumentUploadOptions> {
|
|
|
|
|
if (!options) {
|
|
|
|
|
return {
|
|
|
|
|
simplex: false, // duplex by default
|
|
|
|
|
color: false, // black and white by default
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const mapped: Partial<DocumentUploadOptions> = {
|
|
|
|
|
simplex: options.simplex ?? false,
|
|
|
|
|
color: options.color ?? false,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (options.envelope) {
|
|
|
|
|
mapped.envelope = options.envelope === 'C4' ? EnvelopeType.C4 : EnvelopeType.DINLANG;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (options.franking) {
|
|
|
|
|
switch (options.franking) {
|
|
|
|
|
case 'STANDARD_FRANKING':
|
|
|
|
|
mapped.franking = FrankingType.STANDARD_FRANKING;
|
|
|
|
|
break;
|
|
|
|
|
case 'DV_FRANKING':
|
|
|
|
|
mapped.franking = FrankingType.DV_FRANKING;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
mapped.franking = FrankingType.UNSPECIFIED;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return mapped;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Convert BinectDocument to our Document interface
|
|
|
|
|
*/
|
|
|
|
|
function mapDocument(doc: BinectDocument): Document {
|
|
|
|
|
// Build letter data if present
|
|
|
|
|
let letterData: Document['letter'] = undefined;
|
|
|
|
|
|
|
|
|
|
if (doc.letter) {
|
|
|
|
|
const ld = doc.letter.letterData;
|
|
|
|
|
letterData = {
|
|
|
|
|
letterType: (doc.letter.letterType || 'LetterData') as 'LetterData' | 'Error',
|
|
|
|
|
letterData: ld ? {
|
|
|
|
|
recipientAddress: ld.recipientAddress || '',
|
|
|
|
|
price: ld.price || { priceBeforeTax: 0, priceAfterTax: 0, unit: 'EUROCENT', taxInPercent: 0 },
|
|
|
|
|
international: ld.international || false,
|
|
|
|
|
options: {
|
|
|
|
|
simplex: ld.options?.simplex,
|
|
|
|
|
color: ld.options?.color,
|
|
|
|
|
envelope: ld.options?.envelope as 'DINLANG' | 'C4' | undefined,
|
|
|
|
|
franking: ld.options?.franking as 'UNSPECIFIED' | 'STANDARD_FRANKING' | 'DV_FRANKING' | undefined,
|
|
|
|
|
},
|
|
|
|
|
} : undefined,
|
|
|
|
|
errors: doc.letter.errors?.map(e => ({
|
|
|
|
|
code: parseInt(e.code, 10) || 0,
|
|
|
|
|
text: e.message,
|
|
|
|
|
blankText: e.message,
|
|
|
|
|
})),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
id: doc.id,
|
|
|
|
|
filename: doc.filename || 'document.pdf',
|
|
|
|
|
numberOfPages: doc.numberOfPages,
|
|
|
|
|
status: {
|
|
|
|
|
code: doc.status.code,
|
|
|
|
|
text: doc.status.text,
|
|
|
|
|
},
|
|
|
|
|
documentType: (doc.documentType === 'SERIALLETTER' ? 'SerialLetter' : 'Letter') as 'Letter' | 'SerialLetter',
|
|
|
|
|
letter: letterData,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-13 00:30:39 +01:00
|
|
|
/**
|
2026-01-14 16:50:57 +01:00
|
|
|
* Upload PDF to Binect API
|
|
|
|
|
*
|
2026-01-15 14:41:44 +01:00
|
|
|
* Uses the @binect/js library to upload the PDF.
|
2026-01-14 16:50:57 +01:00
|
|
|
*
|
|
|
|
|
* @param pdfData - PDF file as ArrayBuffer
|
|
|
|
|
* @param filename - Name of the PDF file
|
|
|
|
|
* @param username - Binect username for authentication
|
|
|
|
|
* @param password - Binect password for authentication
|
|
|
|
|
* @param options - Optional printing options (simplex/duplex, color/bw, etc.)
|
|
|
|
|
* @returns Document object with ID and status
|
2026-01-13 00:30:39 +01:00
|
|
|
*/
|
|
|
|
|
export async function uploadPDF(
|
|
|
|
|
pdfData: ArrayBuffer,
|
|
|
|
|
filename: string,
|
2026-01-14 16:50:57 +01:00
|
|
|
username: string,
|
|
|
|
|
password: string,
|
|
|
|
|
options?: Options
|
|
|
|
|
): Promise<Document> {
|
2026-01-15 14:41:44 +01:00
|
|
|
console.log('[Binect API] Uploading PDF to Binect via @binect/js...');
|
2026-01-14 16:50:57 +01:00
|
|
|
console.log('[Binect API] Filename:', filename);
|
|
|
|
|
console.log('[Binect API] PDF size:', pdfData.byteLength, 'bytes');
|
|
|
|
|
|
2026-01-13 00:30:39 +01:00
|
|
|
try {
|
2026-01-15 14:41:44 +01:00
|
|
|
// Create client with credentials
|
|
|
|
|
const client = new BinectClient({
|
|
|
|
|
username,
|
|
|
|
|
password,
|
|
|
|
|
});
|
|
|
|
|
|
2026-01-14 16:50:57 +01:00
|
|
|
// Convert PDF to base64
|
|
|
|
|
console.log('[Binect API] Converting PDF to base64...');
|
|
|
|
|
const base64Content = arrayBufferToBase64(pdfData);
|
|
|
|
|
console.log('[Binect API] Base64 length:', base64Content.length, 'characters');
|
|
|
|
|
|
2026-01-15 14:41:44 +01:00
|
|
|
// Map options
|
|
|
|
|
const uploadOptions = mapOptions(options);
|
|
|
|
|
console.log('[Binect API] Upload options:', uploadOptions);
|
2026-01-13 00:30:39 +01:00
|
|
|
|
2026-01-15 14:41:44 +01:00
|
|
|
// Upload document
|
|
|
|
|
const doc = await client.documents.upload({
|
|
|
|
|
content: base64Content,
|
|
|
|
|
filename,
|
|
|
|
|
...uploadOptions,
|
2026-01-13 00:30:39 +01:00
|
|
|
});
|
|
|
|
|
|
2026-01-14 16:50:57 +01:00
|
|
|
console.log('[Binect API] Upload successful!');
|
2026-01-15 14:41:44 +01:00
|
|
|
console.log('[Binect API] Document ID:', doc.id);
|
|
|
|
|
console.log('[Binect API] Document status:', doc.status);
|
2026-01-14 16:50:57 +01:00
|
|
|
|
|
|
|
|
// Check if document has errors
|
2026-01-15 14:41:44 +01:00
|
|
|
if (doc.letter?.letterType === 'Error' && doc.letter.errors) {
|
|
|
|
|
console.warn('[Binect API] Document has errors:', doc.letter.errors);
|
|
|
|
|
const errorMessages = doc.letter.errors.map(e => e.message).join('; ');
|
2026-01-14 16:50:57 +01:00
|
|
|
throw new BinectAPIError(
|
|
|
|
|
`Document validation failed: ${errorMessages}`,
|
|
|
|
|
200,
|
2026-01-15 14:41:44 +01:00
|
|
|
doc
|
2026-01-14 16:50:57 +01:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-15 14:41:44 +01:00
|
|
|
// Status code 7 = erroneous
|
|
|
|
|
if (doc.status.code === 7) {
|
|
|
|
|
console.error('[Binect API] Document is erroneous:', doc.status.text);
|
2026-01-14 16:50:57 +01:00
|
|
|
throw new BinectAPIError(
|
2026-01-15 14:41:44 +01:00
|
|
|
`Document is erroneous: ${doc.status.text}`,
|
2026-01-14 16:50:57 +01:00
|
|
|
200,
|
2026-01-15 14:41:44 +01:00
|
|
|
doc
|
2026-01-13 00:30:39 +01:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-15 14:41:44 +01:00
|
|
|
return mapDocument(doc);
|
2026-01-13 00:30:39 +01:00
|
|
|
} catch (error) {
|
2026-01-14 16:50:57 +01:00
|
|
|
console.error('[Binect API] Upload error:', error);
|
|
|
|
|
|
2026-01-15 14:41:44 +01:00
|
|
|
// Handle @binect/js errors
|
|
|
|
|
if (error instanceof BinectAuthError) {
|
|
|
|
|
throw new BinectAPIError('Invalid credentials', 401);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (error instanceof BinectApiError) {
|
|
|
|
|
// Map specific status codes
|
|
|
|
|
if (error.status === 400) {
|
|
|
|
|
throw new BinectAPIError(
|
|
|
|
|
'Invalid request. Please check the PDF format and size.',
|
|
|
|
|
400
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
if (error.status === 413) {
|
|
|
|
|
throw new BinectAPIError('File size exceeds limit (12 MB)', 413);
|
|
|
|
|
}
|
|
|
|
|
throw BinectAPIError.fromBinectError(error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Already a BinectAPIError
|
2026-01-13 00:30:39 +01:00
|
|
|
if (error instanceof BinectAPIError) {
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
2026-01-14 16:50:57 +01:00
|
|
|
|
2026-01-15 14:41:44 +01:00
|
|
|
// Network or other errors
|
2026-01-14 16:50:57 +01:00
|
|
|
if (error instanceof TypeError && error.message.includes('fetch')) {
|
|
|
|
|
throw new BinectAPIError(
|
2026-01-15 14:41:44 +01:00
|
|
|
'Cannot reach Binect API. Please check your internet connection.'
|
2026-01-14 16:50:57 +01:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-13 00:30:39 +01:00
|
|
|
throw new BinectAPIError(
|
|
|
|
|
`Network error: ${error instanceof Error ? error.message : 'Unknown error'}`
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2026-01-14 16:50:57 +01:00
|
|
|
* Test API connectivity by fetching account information
|
|
|
|
|
*
|
|
|
|
|
* @param username - Binect username
|
|
|
|
|
* @param password - Binect password
|
|
|
|
|
* @returns true if authentication successful, false otherwise
|
2026-01-13 00:30:39 +01:00
|
|
|
*/
|
2026-01-14 16:50:57 +01:00
|
|
|
export async function testConnection(username: string, password: string): Promise<boolean> {
|
2026-01-15 14:41:44 +01:00
|
|
|
console.log('[Binect API] Testing connection via @binect/js...');
|
2026-01-14 16:50:57 +01:00
|
|
|
|
2026-01-13 00:30:39 +01:00
|
|
|
try {
|
2026-01-15 14:41:44 +01:00
|
|
|
const client = new BinectClient({
|
|
|
|
|
username,
|
|
|
|
|
password,
|
2026-01-13 00:30:39 +01:00
|
|
|
});
|
2026-01-14 16:50:57 +01:00
|
|
|
|
2026-01-15 14:41:44 +01:00
|
|
|
// Attempt to get account info
|
|
|
|
|
await client.accounts.get();
|
2026-01-14 16:50:57 +01:00
|
|
|
|
2026-01-15 14:41:44 +01:00
|
|
|
console.log('[Binect API] Connection successful');
|
|
|
|
|
return true;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
if (error instanceof BinectAuthError) {
|
2026-01-14 16:50:57 +01:00
|
|
|
console.log('[Binect API] Authentication failed');
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-15 14:41:44 +01:00
|
|
|
if (error instanceof BinectApiError) {
|
|
|
|
|
console.warn('[Binect API] API error:', error.message);
|
|
|
|
|
// If we get any other API error, credentials might still be valid
|
|
|
|
|
// but there's another issue
|
|
|
|
|
return false;
|
2026-01-14 16:50:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.error('[Binect API] Connection test error:', error);
|
2026-01-13 00:30:39 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|