Initializing Autotask handlers locally in Typescript

In the Autotasks documentation, the Typescript support section shows the following code:

import { AutotaskEvent } from 'defender-autotask-utils';

export async function handler(event: AutotaskEvent) {
  ...
}

The handler constructor takes an AutotaskEvent, which features a credentials?: string; property.

Conversely, the A complete example section shows the following code in JS:

exports.handler = async function(event) {
  ...
}

if (require.main === module) {
  const { API_KEY: apiKey, API_SECRET: apiSecret } = process.env;
  exports.handler({ apiKey, apiSecret })
    .then(() => process.exit(0))
    .catch(error => { console.error(error); process.exit(1); });
}

…in which the exports.handler constructor accepts an object of type { apiKey: string; apiSecret: string; }.

How can I initialize handler locally in Typescript with an AutotaskEvent?

1 Like

Hey @dtp5! Good catch! The AutotaskEvent type includes only the fields injected during Autotask runtime, but not those needed for running the autotask locally. The main difference is that, during an Autotask execution, the Relayer accepts a credentials: string injected by Defender, whereas when running locally you need to use an { apiKey, apiSecret } object with the API key/secret of the Relayer.

You can work around this issue by doing something like:

import { AutotaskEvent } from 'defender-autotask-utils';
import { RelayerParams, Relayer } from 'defender-relay-client';

export async function handler(event: AutotaskEvent & RelayerParams) {
  const relay = new Relayer(event);
}

if (require.main === module) {
  handler({ apiKey: 'foo', apiSecret: 'bar' });
}
3 Likes