import { axios } from "@pipedream/platform";
import verifalia from "../../verifalia.app.mjs";
import common from "../../common.mjs";
import {
CancellationToken,
OperationCanceledError,
} from "verifalia";
export default {
name: "Verify List of Email Address",
description: "Verify a list of email address and check if it is properly formatted, really exists and can accept mails, " +
"flagging spam traps, disposable emails and much more. [See the docs](https://verifalia.com/developers#email-validations-creating) for more information",
key: "verifalia-verify-list-emails",
version: "0.1.0",
type: "action",
props: {
verifalia,
emailAddresses: {
type: "string[]",
label: "Email Addresses",
description: "Enter a list of email address to verify (e.g. `batman@gmail.com`)",
optional: false,
},
quality: {
type: "string",
label: "Quality Level",
description: "The higher the quality level the longer it could take to complete the verification; by default, we use the configured default quality level for your Verifalia user.",
optional: true,
options: common.qualityLevelOptions,
},
retention: {
type: "string",
label: "Data Retention Period",
description: "The data retention period to observe for the validation job, expressed in the format `dd.hh:mm:ss` " +
"(where dd: days, hh: hours, mm: minutes, ss: seconds); the initial `dd.` part is added only for periods of " +
"more than 24 hours. The value has a minimum of 5 minutes (`0:5:0`) and a maximum of 30 days (`30.0:0:0`): " +
"Verifalia will delete the job and its data once its data retention period is over, starting to count when " +
"it gets completed.",
optional: true,
},
},
async run({ $ }) {
const { run } = $.context;
const verifaliaClient = this.verifalia.buildVerifaliaRestClient();
if (run.runs === 1) {
if (this.retention && !this.verifalia.isValidTimeSpan(this.retention)) {
throw new Error(`The specified data retention period '${this.retention}' is incorrect: must be in the ` +
"format dd.hh:mm:ss (where dd: days, hh: hours, mm: minutes, ss: seconds).");
}
let resumeUrl = null;
let waitForResults = false;
let cancellationToken = null;
if ($.context.test) {
waitForResults = true;
cancellationToken = new CancellationToken();
setTimeout(() => cancellationToken.cancel(), 20 * 1000);
} else {
resumeUrl = $.flow.rerun(86400 * 1000, {}, 1).resume_url;
}
let job;
try {
const parsedEmails = !Array.isArray(this.emailAddresses)
? JSON.parse(this.emailAddresses)
: this.emailAddresses;
const formattedEmails = parsedEmails.map((emailAddress) => ({
inputData: emailAddress,
}));
job = await this.verifalia.wrapVerifaliaApiInvocation(async () => {
return await verifaliaClient
.emailValidations
.submit({
quality: this.quality,
entries: formattedEmails,
retention: this.retention,
callback: {
url: resumeUrl,
},
},
waitForResults,
cancellationToken);
});
} catch (error) {
if (error instanceof OperationCanceledError) {
throw new Error("This operation would need some more time to complete and that would not work properly in " +
"Pipedream's test mode. Please deploy your workflow to get a meaningful email verification result.");
}
throw error;
}
if (job.overview.status === "Completed") {
if (!resumeUrl) {
return job;
}
return axios($, {
method: "POST",
url: resumeUrl,
data: {
event: {
type: "email-validation.completed",
data: {
id: job.overview.id,
},
},
},
returnFullResponse: true,
});
}
} else {
const callbackRequest = run.callback_request;
if (callbackRequest.body.event.type === "email-validation.completed") {
const jobId = callbackRequest.body.event.data.id;
return await this.verifalia.wrapVerifaliaApiInvocation(() => verifaliaClient
.emailValidations
.get(jobId));
}
console.log(callbackRequest);
throw new Error("An error occurred while processing your request, sorry.");
}
},
};