summaryrefslogtreecommitdiffstats
path: root/src/lib/search/meilisearch.ts
blob: 9b4c0d14411e6c1dc0a55367b181d7816a8a88d1 (plain) (blame)
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
export interface MeilisearchConfig {
    host: string;
    apiKey?: string;
}

export interface SearchFilters {
    query?: string;
    source?: string;
    category?: string;
    lang?: string;
    nsfw?: boolean;
    page?: number;
    limit?: number;
}

interface MeilisearchClient {
    host: string;
    apiKey: string;
}

let client: MeilisearchClient | null = null;

export function initMeilisearch(config: MeilisearchConfig) {
    if (!config.host) {
        console.warn('Meilisearch not configured');
        return null;
    }
    client = { host: config.host, apiKey: config.apiKey ?? '' };
    return client;
}

export function isMeilisearchEnabled(): boolean {
    return client !== null;
}

/**
 * Transforms a Meilisearch hit to EnrichedExtension format
 */
export function transformMeilisearchHit(hit: any) {
    return {
        name: hit.name,
        pkg: hit.pkg,
        apk: hit.apk,
        lang: hit.lang,
        code: hit.code,
        version: hit.version,
        nsfw: hit.nsfw,
        repoUrl: hit.repoUrl,
        sourceName: hit.sourceName,
        formattedSourceName: hit.formattedSourceName
    };
}

export async function searchExtensions(filters: SearchFilters) {
    if (!client) {
        throw new Error('Meilisearch client not initialized');
    }

    const filterConditions: string[] = [];

    if (filters.source && filters.source !== 'all')
        filterConditions.push(`formattedSourceName = "${filters.source}"`);
    if (filters.category && filters.category !== 'all')
        filterConditions.push(`category = "${filters.category}"`);
    if (filters.lang && filters.lang !== 'all') filterConditions.push(`lang = "${filters.lang}"`);
    if (filters.nsfw === false) filterConditions.push('nsfw = 0');

    const page = filters.page || 1;
    const limit = filters.limit || 50;
    const offset = (page - 1) * limit;

    const body: Record<string, any> = {
        q: filters.query || '',
        limit,
        offset
    };

    if (filterConditions.length > 0) body.filter = filterConditions;

    const response = await fetch(`${client.host}/indexes/extensions/search`, {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            Authorization: `Bearer ${client.apiKey}`
        },
        body: JSON.stringify(body)
    });

    if (!response.ok) {
        throw new Error(`Meilisearch error: ${response.status} ${response.statusText}`);
    }

    return await response.json();
}

export async function getFilterOptions() {
    if (!client) {
        throw new Error('Meilisearch client not initialized');
    }

    const response = await fetch(`${client.host}/indexes/extensions/search`, {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            Authorization: `Bearer ${client.apiKey}`
        },
        body: JSON.stringify({
            q: '',
            limit: 0,
            facets: ['formattedSourceName', 'category', 'lang']
        })
    });

    if (!response.ok) {
        throw new Error(`Meilisearch error: ${response.status} ${response.statusText}`);
    }

    const result = await response.json();

    return {
        sources: Object.keys(result.facetDistribution?.formattedSourceName || {}),
        categories: Object.keys(result.facetDistribution?.category || {}),
        languages: Object.keys(result.facetDistribution?.lang || {})
    };
}