import { ApifyClient } from 'apify-client@^2.19.0';
type ApifyApiKey = {
api_key: string;
};
type Apify = {
token: string;
};
type ApifyWebhookConfig = {
url: string;
token: string;
};
function prettifyEvent(e: string) {
return e.replace(/^ACTOR\.RUN\./, '');
}
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;
},
],
});
};
export type DynSelect_webhookToDelete = string;
export async function webhookToDelete(
apifyWebhookConfig: ApifyWebhookConfig,
api_key?: ApifyApiKey,
oauth_token?: Apify,
) {
if (!api_key?.api_key && !oauth_token?.token) {
return [{ value: '', label: 'Missing Apify API key or OAuth token' }];
}
try {
const client = createClient(api_key, oauth_token);
const webhooks = await client.webhooks().list({
limit: 1000,
offset: 0,
});
return webhooks.items
.filter((webhook) => webhook.requestUrl === apifyWebhookConfig.url)
.map((webhook) => {
const webhookEvents = webhook.eventTypes
.map((event) => prettifyEvent(event))
.join(', ');
const condition = webhook.condition as any;
const fallbackLabel = `${condition.actorId ?? condition.actorTaskId}: ${webhookEvents}`;
return {
value: webhook.id,
label: webhook?.description || fallbackLabel,
};
});
} catch (error: any) {
return [
{
value: '',
label: `Failed to load webhooks: ${error.message || error}`,
},
];
}
}
export async function main(
apifyWebhookConfig: ApifyWebhookConfig,
webhookToDelete: DynSelect_webhookToDelete,
api_key?: ApifyApiKey,
oauth_token?: Apify,
) {
if (!webhookToDelete) {
return { error: 'Please select webhook ID for deletion.' };
}
const client = createClient(api_key, oauth_token);
try {
await client.webhook(webhookToDelete).delete();
return { success: 'Webhook deleted successfully' };
} catch (e: any) {
return { error: `Failed to delete the webhook. Reason: ${e.message}` };
}
}
Submitted by jakub.drobnik222 88 days ago