1 | import { ApifyClient } from 'apify-client@^2.19.0'; |
2 |
|
3 | type ApifyApiKey = { |
4 | api_key: string; |
5 | }; |
6 |
|
7 | type Apify = { |
8 | token: string; |
9 | }; |
10 |
|
11 | const createClient = (api_key?: ApifyApiKey, oauth_token?: Apify): ApifyClient => { |
12 | const token = oauth_token?.token ?? api_key?.api_key; |
13 | if (!token) { |
14 | throw new Error('Missing Apify API key or OAuth token'); |
15 | } |
16 |
|
17 | return new ApifyClient({ |
18 | token: token, |
19 | requestInterceptors: [ |
20 | (request) => { |
21 | if (!request.headers) { |
22 | request.headers = {}; |
23 | } |
24 | request.headers['x-apify-integration-platform'] = 'windmill'; |
25 | return request; |
26 | }, |
27 | ], |
28 | }); |
29 | }; |
30 |
|
31 | type DynSelect_dataset = string; |
32 | export async function dataset(api_key?: ApifyApiKey, oauth_token?: Apify) { |
33 | if (!api_key?.api_key && !oauth_token?.token) { |
34 | return [{ value: '', label: 'Missing Apify API key or OAuth token' }]; |
35 | } |
36 |
|
37 | const apifyClient = createClient(api_key, oauth_token); |
38 |
|
39 | try { |
40 | const dataset = await apifyClient.datasets().list({ |
41 | desc: true, |
42 | }); |
43 |
|
44 | return dataset.items.map(item => ({ |
45 | value: item.id, |
46 | label: `${item.title || item.name} (${item.itemCount} ${pluralize(item.itemCount)} - ${item.id})` |
47 | })); |
48 | } catch (e: any) { |
49 | return { |
50 | error: `Failed to fetch dataset list. Reason: ${e.message}` |
51 | }; |
52 | } |
53 | } |
54 |
|
55 | function pluralize(n: number) { |
56 | return n === 1 ? "item" : "items"; |
57 | } |
58 |
|
59 | export async function main( |
60 | api_key?: ApifyApiKey, |
61 | oauth_token?: Apify, |
62 | datasetId?: string, |
63 | dataset?: DynSelect_dataset, |
64 | offset?: number, |
65 | limit?: number, |
66 | fields?: string[], |
67 | omit?: string[], |
68 | ) { |
69 |
|
70 | const id = datasetId || dataset; |
71 |
|
72 | if (!id) { |
73 | return { |
74 | error: "A Dataset ID is required. Please select one from the list or provide it manually" |
75 | }; |
76 | } |
77 |
|
78 | const apifyClient = createClient(api_key, oauth_token); |
79 |
|
80 | try { |
81 | const result = await apifyClient.dataset(id).listItems({ |
82 | |
83 | offset: offset ?? undefined, |
84 | limit: limit ?? undefined, |
85 | fields: fields ?? undefined, |
86 | omit: omit ?? undefined, |
87 | }); |
88 |
|
89 | if (!result) { |
90 | return { error: `Dataset with ID "${id}" not found` }; |
91 | } |
92 |
|
93 | return result.items; |
94 | } catch (e: any) { |
95 | return { error: `Failed to fetch items from dataset "${id}". Reason: ${e.message}` }; |
96 | } |
97 | } |