File size: 2,952 Bytes
4e1096a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { Book, BookNote, HighlightColor } from '@/types/book';
import { ReadwiseSettings } from '@/types/settings';
import { READWISE_API_BASE_URL } from '@/services/constants';

const READEST_TO_READWISE_COLOR: Record<HighlightColor, string> = {
  red: 'pink',
  yellow: 'yellow',
  green: 'green',
  blue: 'blue',
  violet: 'purple',
};

export class ReadwiseClient {
  private config: ReadwiseSettings;

  constructor(config: ReadwiseSettings) {
    this.config = config;
  }

  private async request(
    endpoint: string,
    options: { method?: 'GET' | 'POST'; body?: string } = {},
  ): Promise<Response> {
    const { method = 'GET', body } = options;
    return fetch(`${READWISE_API_BASE_URL}${endpoint}`, {
      method,
      headers: {
        Authorization: `Token ${this.config.accessToken}`,
        ...(body ? { 'Content-Type': 'application/json' } : {}),
      },
      body,
    });
  }

  async validateToken(): Promise<{ valid: boolean; isNetworkError?: boolean }> {
    try {
      const res = await this.request('/auth/');
      return { valid: res.status === 204 };
    } catch {
      return { valid: false, isNetworkError: true };
    }
  }

  async pushHighlights(
    notes: BookNote[],
    book: Book,
  ): Promise<{ success: boolean; message?: string; isNetworkError?: boolean }> {
    const syncable = notes.filter(
      (n) => (n.type === 'annotation' || n.type === 'excerpt') && !n.deletedAt && n.text,
    );
    if (syncable.length === 0) return { success: true };

    const isPublicUrl = (url?: string | null) =>
      !!url && /^https?:\/\/(?!localhost|127\.|asset\.localhost)/.test(url);

    const highlights = syncable.map((note) => ({
      text: note.text!,
      title: book.title,
      author: book.author,
      ...(isPublicUrl(book.coverImageUrl) ? { image_url: book.coverImageUrl } : {}),
      source_type: 'readest',
      category: 'books',
      note: note.note || undefined,
      location: 0,
      location_type: 'none',
      highlighted_at: new Date(note.createdAt).toISOString(),
      highlight_url: `readest://annotation/${book.hash}/${note.id}`,
      color: note.color ? (READEST_TO_READWISE_COLOR[note.color] ?? 'yellow') : 'yellow',
    }));

    try {
      const res = await this.request('/highlights/', {
        method: 'POST',
        body: JSON.stringify({ highlights }),
      });
      if (!res.ok) {
        const errText = await res.text().catch(() => '');
        console.error('Readwise API error:', res.status, errText);
        let message = `HTTP ${res.status}`;
        try {
          const err = JSON.parse(errText);
          message = err.detail || err.message || JSON.stringify(err) || message;
        } catch {
          if (errText) message = errText;
        }
        return { success: false, message };
      }
      return { success: true };
    } catch (e) {
      return { success: false, message: (e as Error).message, isNetworkError: true };
    }
  }
}