import { ApifyClient } from 'apify-client@^2.19.0';
type ApifyApiKey = {
api_key: string;
};
type Apify = {
token: string;
};
const createClient = (api_key?: ApifyApiKey, oauth_token?: Apify): ApifyClient => {
const token = oauth_token?.token ?? api_key?.api_key;
if (!token) {
throw new Error('Missing Apify API key or OAuth token');
}
return new ApifyClient({
token: token,
requestInterceptors: [
(request) => {
if (!request.headers) {
request.headers = {};
}
request.headers['x-apify-integration-platform'] = 'windmill';
return request;
},
],
});
};
type DynSelect_store = string;
export async function store(api_key?: ApifyApiKey, oauth_token?: Apify) {
if (!api_key?.api_key && !oauth_token?.token) {
return [{ value: '', label: 'Missing Apify API key or OAuth token' }];
}
const apifyClient = createClient(api_key, oauth_token);
try {
const stores = await apifyClient.keyValueStores().list({
desc: true,
});
return stores.items.map(item => ({
value: item.id,
label: item.title || item.name
}));
} catch (e: any) {
return {
error: `Failed to fetch Key-Value store list. Reason: ${e.message}`
};
}
}
type DynSelect_keys = string;
export async function keys(store: DynSelect_store, storeId: string, api_key?: ApifyApiKey, oauth_token?: Apify) {
if (!api_key?.api_key && !oauth_token?.token) {
return [{ value: '', label: 'Missing Apify API key or OAuth token' }];
}
const apifyClient = createClient(api_key, oauth_token);
const id = storeId || store
if (!id) {
return {
error: "Please select a store first to see its available keys."
};
}
try {
const keys = await apifyClient.keyValueStore(id).listKeys();
return keys.items.map(item => ({
value: item.key,
label: item.key
}));
} catch (e: any) {
return {
error: `Failed to fetch keys for store "${id}". Reason: ${e.message}`
};
}
}
export async function main(
key: DynSelect_keys,
store?: DynSelect_store,
storeId?: string,
api_key?: ApifyApiKey,
oauth_token?: Apify,
) {
const id = storeId || store;
if (!id) {
return {
error: "Store ID is required. Please select one from the list or provide it manually."
};
}
if (!key) {
return {
error: "Please select a record key."
};
}
const apifyClient = createClient(api_key, oauth_token);
try {
const record = await apifyClient.keyValueStore(id).getRecord(key);
if (!record) {
return { error: `Record with key "${key}" could not be found in store "${id}".` };
}
// Using Windmill's special return types for rich UI rendering
switch (record.contentType) {
case "image/png":
case "image/jpeg":
const imageType = record.contentType.split('/')[1];
if (record.value instanceof Buffer) {
return {
[imageType]: {
content: record.value.toString('base64'),
},
};
} else if (typeof record.value === 'string') {
return {
[imageType]: record.value
};
}
return { error: `Unsupported image type: ${typeof record.value}` };
case "image/svg+xml":
return {
"svg": record.value,
};
case "text/html": {
return {
"html": record.value,
};
}
case "text/plain":
return record.value;
default:
if (record.value instanceof Buffer) {
const base64string = (record.value as Buffer).toString('base64');
return {
"file": {
content: base64string,
filename: record.key || "download"
},
};
}
return record.value;
}
} catch (e: any) {
return { error: `Failed to fetch record "${key}" from store "${id}". Reason: ${e.message}` };
}
}Submitted by jakub.drobnik222 88 days ago