-
Notifications
You must be signed in to change notification settings - Fork 1
/
algolia-index.ts
114 lines (102 loc) · 3.6 KB
/
algolia-index.ts
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
import algoliasearch, { SearchClient, SearchIndex } from 'algoliasearch';
import { ApiTool } from 'utils/types';
import { Tool } from '@components/tools';
import https from 'https';
const algoliaClient: SearchClient = algoliasearch(
process.env.ALGOLIA_APP_ID as string,
process.env.ALGOLIA_ADMIN_KEY as string,
);
const index: SearchIndex = algoliaClient.initIndex(
process.env.ALGOLIA_INDEX_NAME as string,
);
async function fetchToolsFromApi(): Promise<Tool[]> {
return new Promise((resolve, reject) => {
const apiURL = 'https://analysis-tools.dev/api/tools';
https
.get(apiURL, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
try {
const jsonResponse = JSON.parse(data);
if (
!jsonResponse.data ||
jsonResponse.data.length === 0
) {
reject(
new Error(
'No tools data found or empty array returned',
),
);
} else {
resolve(jsonResponse.data);
}
} catch (e) {
reject(new Error('Failed to parse tools data'));
}
});
})
.on('error', (e) => {
reject(new Error('Failed to fetch tools data'));
});
});
}
async function updateIndex(data: ApiTool[]): Promise<void> {
console.log(`Indexing ${data.length} ApiTool entries...`);
try {
const response = await index.clearObjects();
console.log('Algolia index cleared', response);
const algoliaResponse = await index.saveObjects(data);
console.log('Algolia index updated', algoliaResponse);
} catch (error) {
console.error(
'An error occurred while updating the Algolia index:',
error,
);
}
}
async function fetchDataForIndexing(): Promise<ApiTool[]> {
try {
const toolsData = await fetchToolsFromApi();
console.log(`Fetched ${toolsData.length} tools from the API`);
// Convert the tools data into an array of Algolia records
const algoliaRecords = toolsData.map((tool, _index) => ({
objectID: tool.id, // Use the tool's ID as the objectID for Algolia
name: tool.name,
fields: {
slug: `/tool/${tool.id}`,
},
categories: tool.categories,
languages: tool.languages,
licenses: tool.licenses,
types: tool.types,
homepage: tool.homepage,
source: tool.source,
pricing: tool.pricing,
plans: tool.plans,
description: tool.description,
discussion: tool.discussion,
deprecated: tool.deprecated,
resources: tool.resources,
wrapper: tool.wrapper,
votes: tool.votes,
other: tool.other,
}));
return algoliaRecords;
} catch (error) {
console.error(error);
throw error;
}
}
async function main(): Promise<void> {
try {
const dataToIndex = await fetchDataForIndexing();
await updateIndex(dataToIndex);
} catch (error) {
console.error('Failed to fetch or index data:', error);
process.exit(1);
}
}
main();