I am trying to make the migration of my autotask from javascript to typescript. My autotask is designed to use sentinel event triggers to run (and additionally extract information from the event to process in the autotask).
Initially, the design I used to run locally was to pass the event object into the handler the way it would receive it if triggered by the sentinel:
if (require.main === module) {
require("dotenv").config();
const { RELAYER_KEY: apiKey, RELAYER_SECRET: apiSecret } = process.env;
const event = {
apiKey,
apiSecret,
request: {
body: {
matchReasons: [
{},
{
params: {
param1,
param2,
param3,
},
},
],
},
},
};
exports
.handler(iCredentials)
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
}
My handler begins with identifying whether or not the autotask was triggered via an even trigger (sentinel) by checking if there is a "request" key (my understanding is that this key is omitted when autotask is manually invoked).
export async function handler(event) {
if (Object.prototype.hasOwnProperty.call(event, "request")) {
const params = event.request.body.matchReasons[1].params
} else {
param1 = defaultValue1;
param2 = defaultValue2;
param3 = defaultValue3;
}
}
My conversion to typescript got this far.
export async function handler(event: AutotaskEvent & RelayerParams) {
if (Object.prototype.hasOwnProperty.call(event, "request")) {
const trigger = event.request.body as SentinelTriggerEvent
const paramsObject = trigger.matchReasons[1] as unknown as IParams
const params = paramsObject.params
} else {
param1 = defaultValue1;
param2 = defaultValue2;
param3 = defaultValue3;
}
}
export interface IParams {
params: {
param1: string,
param2: number,
param3: string,
},
}
I am wondering if there is a more elegant solution than the 'unknown' type declaration in this case.