pdfdancer-client-typescript 1.0.13 → 1.0.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/models.ts CHANGED
@@ -255,28 +255,25 @@ export enum FontType {
255
255
  }
256
256
 
257
257
  /**
258
- * Represents a font recommendation with similarity score.
258
+ * Represents font mapping information for a document font.
259
259
  */
260
- export class FontRecommendation {
260
+ export class DocumentFontInfo {
261
261
  constructor(
262
- public fontName: string,
263
- public fontType: FontType,
264
- public similarityScore: number
262
+ public documentFontName: string,
263
+ public systemFontName: string
265
264
  ) {}
266
265
 
267
- getFontName(): string {
268
- return this.fontName;
269
- }
270
-
271
- getFontType(): FontType {
272
- return this.fontType;
266
+ getDocumentFontName(): string {
267
+ return this.documentFontName;
273
268
  }
274
269
 
275
- getSimilarityScore(): number {
276
- return this.similarityScore;
270
+ getSystemFontName(): string {
271
+ return this.systemFontName;
277
272
  }
278
273
  }
279
274
 
275
+ export { DocumentFontInfo as FontRecommendation };
276
+
280
277
  /**
281
278
  * Status information for text objects.
282
279
  */
@@ -285,7 +282,7 @@ export class TextStatus {
285
282
  public modified: boolean,
286
283
  public encodable: boolean,
287
284
  public fontType: FontType,
288
- public fontRecommendation: FontRecommendation
285
+ public fontInfo?: DocumentFontInfo
289
286
  ) {}
290
287
 
291
288
  isModified(): boolean {
@@ -300,8 +297,15 @@ export class TextStatus {
300
297
  return this.fontType;
301
298
  }
302
299
 
303
- getFontRecommendation(): FontRecommendation {
304
- return this.fontRecommendation;
300
+ getFontInfo(): DocumentFontInfo | undefined {
301
+ return this.fontInfo;
302
+ }
303
+
304
+ /**
305
+ * @deprecated Use getFontInfo() instead.
306
+ */
307
+ getFontRecommendation(): DocumentFontInfo | undefined {
308
+ return this.getFontInfo();
305
309
  }
306
310
  }
307
311
 
@@ -986,7 +990,7 @@ export class PageSnapshot {
986
990
  export class DocumentSnapshot {
987
991
  constructor(
988
992
  public pageCount: number,
989
- public fonts: FontRecommendation[],
993
+ public fonts: DocumentFontInfo[],
990
994
  public pages: PageSnapshot[]
991
995
  ) {}
992
996
 
@@ -23,7 +23,7 @@ import {
23
23
  DocumentSnapshot,
24
24
  FindRequest,
25
25
  Font,
26
- FontRecommendation,
26
+ DocumentFontInfo,
27
27
  FontType,
28
28
  FormFieldRef,
29
29
  Image,
@@ -359,7 +359,7 @@ export class PDFDancer {
359
359
  token: string,
360
360
  pdfData: Uint8Array | File | ArrayBuffer,
361
361
  baseUrl: string | null = null,
362
- readTimeout: number = 30000
362
+ readTimeout: number = 60000
363
363
  ) {
364
364
 
365
365
  if (!token || !token.trim()) {
@@ -403,15 +403,15 @@ export class PDFDancer {
403
403
  }
404
404
 
405
405
  static async open(pdfData: Uint8Array, token?: string, baseUrl?: string, timeout?: number): Promise<PDFDancer> {
406
- const resolvedToken = token ?? process.env.PDFDANCER_TOKEN;
407
406
  const resolvedBaseUrl =
408
407
  baseUrl ??
409
408
  process.env.PDFDANCER_BASE_URL ??
410
409
  "https://api.pdfdancer.com";
411
- const resolvedTimeout = timeout ?? 30000;
410
+ const resolvedTimeout = timeout ?? 60000;
412
411
 
412
+ let resolvedToken = token?.trim() ?? process.env.PDFDANCER_TOKEN?.trim() ?? null;
413
413
  if (!resolvedToken) {
414
- throw new ValidationException("Missing PDFDancer API token. Pass a token via the `token` argument or set the PDFDANCER_TOKEN environment variable.");
414
+ resolvedToken = await PDFDancer._obtainAnonymousToken(resolvedBaseUrl, resolvedTimeout);
415
415
  }
416
416
 
417
417
  const client = new PDFDancer(resolvedToken, pdfData, resolvedBaseUrl, resolvedTimeout);
@@ -427,7 +427,7 @@ export class PDFDancer {
427
427
  * @param options.initialPageCount Number of initial pages (default: 1)
428
428
  * @param token Authentication token (optional, can use PDFDANCER_TOKEN env var)
429
429
  * @param baseUrl Base URL for the PDFDancer API (optional)
430
- * @param timeout Request timeout in milliseconds (default: 30000)
430
+ * @param timeout Request timeout in milliseconds (default: 60000)
431
431
  */
432
432
  static async new(
433
433
  options?: {
@@ -439,15 +439,15 @@ export class PDFDancer {
439
439
  baseUrl?: string,
440
440
  timeout?: number
441
441
  ): Promise<PDFDancer> {
442
- const resolvedToken = token ?? process.env.PDFDANCER_TOKEN;
443
442
  const resolvedBaseUrl =
444
443
  baseUrl ??
445
444
  process.env.PDFDANCER_BASE_URL ??
446
445
  "https://api.pdfdancer.com";
447
- const resolvedTimeout = timeout ?? 30000;
446
+ const resolvedTimeout = timeout ?? 60000;
448
447
 
448
+ let resolvedToken = token?.trim() ?? process.env.PDFDANCER_TOKEN?.trim() ?? null;
449
449
  if (!resolvedToken) {
450
- throw new ValidationException("Missing PDFDancer token (pass it explicitly or set PDFDANCER_TOKEN in environment).");
450
+ resolvedToken = await PDFDancer._obtainAnonymousToken(resolvedBaseUrl, resolvedTimeout);
451
451
  }
452
452
 
453
453
  let createRequest: CreatePdfRequest;
@@ -517,6 +517,47 @@ export class PDFDancer {
517
517
  }
518
518
  }
519
519
 
520
+ private static async _obtainAnonymousToken(baseUrl: string, timeout: number = 60000): Promise<string> {
521
+ const normalizedBaseUrl = (baseUrl || "https://api.pdfdancer.com").replace(/\/+$/, '');
522
+ const url = `${normalizedBaseUrl}/keys/anon`;
523
+
524
+ try {
525
+ const fingerprint = await generateFingerprint();
526
+ const response = await fetch(url, {
527
+ method: 'POST',
528
+ headers: {
529
+ 'Content-Type': 'application/json',
530
+ 'X-Fingerprint': fingerprint,
531
+ 'X-Generated-At': generateTimestamp()
532
+ },
533
+ signal: timeout > 0 ? AbortSignal.timeout(timeout) : undefined
534
+ });
535
+
536
+ if (!response.ok) {
537
+ const errorText = await response.text().catch(() => '');
538
+ throw new HttpClientException(
539
+ `Failed to obtain anonymous token: ${errorText || `HTTP ${response.status}`}`,
540
+ response
541
+ );
542
+ }
543
+
544
+ const tokenPayload: any = await response.json().catch(() => null);
545
+ const tokenValue = typeof tokenPayload?.token === 'string' ? tokenPayload.token.trim() : '';
546
+
547
+ if (!tokenValue) {
548
+ throw new HttpClientException("Invalid anonymous token response format", response);
549
+ }
550
+
551
+ return tokenValue;
552
+ } catch (error) {
553
+ if (error instanceof HttpClientException) {
554
+ throw error;
555
+ }
556
+ const errorMessage = error instanceof Error ? error.message : String(error);
557
+ throw new HttpClientException(`Failed to obtain anonymous token: ${errorMessage}`, undefined, error as Error);
558
+ }
559
+ }
560
+
520
561
  /**
521
562
  * Process PDF data from various input types with strict validation.
522
563
  */
@@ -1428,7 +1469,7 @@ export class PDFDancer {
1428
1469
  'X-Fingerprint': fingerprint
1429
1470
  },
1430
1471
  body: formData,
1431
- signal: AbortSignal.timeout(30000)
1472
+ signal: AbortSignal.timeout(60000)
1432
1473
  });
1433
1474
 
1434
1475
  logGeneratedAtHeader(response, 'POST', '/font/register');
@@ -1534,25 +1575,30 @@ export class PDFDancer {
1534
1575
  let status: TextStatus | undefined;
1535
1576
  const statusData = objData.status;
1536
1577
  if (statusData && typeof statusData === 'object') {
1537
- // Parse font recommendation
1538
- const fontRecData = statusData.fontRecommendation;
1539
- let fontRec: FontRecommendation;
1540
- if (fontRecData && typeof fontRecData === 'object') {
1541
- fontRec = new FontRecommendation(
1542
- fontRecData.fontName || '',
1543
- (fontRecData.fontType as FontType) || FontType.SYSTEM,
1544
- fontRecData.similarityScore || 0.0
1545
- );
1546
- } else {
1547
- // Create empty font recommendation if not provided
1548
- fontRec = new FontRecommendation('', FontType.SYSTEM, 0.0);
1578
+ const fontInfoSource = statusData.fontInfoDto ?? statusData.fontRecommendation;
1579
+ let fontInfo: DocumentFontInfo | undefined;
1580
+ if (fontInfoSource && typeof fontInfoSource === 'object') {
1581
+ const documentFontName = typeof fontInfoSource.documentFontName === 'string'
1582
+ ? fontInfoSource.documentFontName
1583
+ : (typeof fontInfoSource.fontName === 'string' ? fontInfoSource.fontName : '');
1584
+ const systemFontName = typeof fontInfoSource.systemFontName === 'string'
1585
+ ? fontInfoSource.systemFontName
1586
+ : (typeof fontInfoSource.fontName === 'string' ? fontInfoSource.fontName : '');
1587
+ fontInfo = new DocumentFontInfo(documentFontName, systemFontName);
1549
1588
  }
1550
1589
 
1590
+ const modified = statusData.modified !== undefined ? Boolean(statusData.modified) : false;
1591
+ const encodable = statusData.encodable !== undefined ? Boolean(statusData.encodable) : true;
1592
+ const fontTypeValue = typeof statusData.fontType === 'string'
1593
+ && (Object.values(FontType) as string[]).includes(statusData.fontType)
1594
+ ? statusData.fontType as FontType
1595
+ : FontType.SYSTEM;
1596
+
1551
1597
  status = new TextStatus(
1552
- statusData.modified || false,
1553
- statusData.encodable !== undefined ? statusData.encodable : true,
1554
- (statusData.fontType as FontType) || FontType.SYSTEM,
1555
- fontRec
1598
+ modified,
1599
+ encodable,
1600
+ fontTypeValue,
1601
+ fontInfo
1556
1602
  );
1557
1603
  }
1558
1604
 
@@ -1704,14 +1750,17 @@ export class PDFDancer {
1704
1750
  const pageCount = typeof data.pageCount === 'number' ? data.pageCount : 0;
1705
1751
 
1706
1752
  // Parse fonts
1707
- const fonts: FontRecommendation[] = [];
1753
+ const fonts: DocumentFontInfo[] = [];
1708
1754
  if (Array.isArray(data.fonts)) {
1709
1755
  for (const fontData of data.fonts) {
1710
1756
  if (fontData && typeof fontData === 'object') {
1711
- const fontName = fontData.fontName || '';
1712
- const fontType = fontData.fontType as FontType || FontType.SYSTEM;
1713
- const similarityScore = typeof fontData.similarityScore === 'number' ? fontData.similarityScore : 0;
1714
- fonts.push(new FontRecommendation(fontName, fontType, similarityScore));
1757
+ const documentFontName = typeof fontData.documentFontName === 'string'
1758
+ ? fontData.documentFontName
1759
+ : (typeof fontData.fontName === 'string' ? fontData.fontName : '');
1760
+ const systemFontName = typeof fontData.systemFontName === 'string'
1761
+ ? fontData.systemFontName
1762
+ : (typeof fontData.fontName === 'string' ? fontData.fontName : '');
1763
+ fonts.push(new DocumentFontInfo(documentFontName, systemFontName));
1715
1764
  }
1716
1765
  }
1717
1766
  }