Skip to content

Public SDK for DevRev's Airdrop as a Service (ADaaS)

Notifications You must be signed in to change notification settings

devrev/adaas-sdk

Repository files navigation

Airdrop SDK

Coverage Status

Overview

The Airdrop SDK for TypeScript helps developers build snap-ins that integrate with DevRev’s Airdrop platform. This SDK simplifies the workflow for handling data extraction and loading, event-driven actions, state management, and artifact handling.

It provides features such as:

  • Type Definitions: Structured types for Airdrop control protocol
  • Event Management: Easily emit events for different extraction or loading phases
  • State Handling: Update and access state in real-time within tasks
  • Artifact Management: Supports batched storage of artifacts
  • Error & Timeout Support: Error handling and timeout management for long-running tasks

Installation

npm install @devrev/ts-adaas

Reference

spawn function

This function initializes a new worker thread and oversees its lifecycle. It should be invoked when the snap-in receives a message from the Airdrop platform. The worker script provided then handles the event accordingly.

Usage

spawn({ event, initialState, workerPath, options })

Parameters

  • event

    Required. An object of type AirdropEvent that is received from the Airdrop platform.

  • initialState

    Required. Object of any type that represents the initial state of the snap-in.

  • workerPath

    Required. A string that represents the path to the worker file.

  • options

    Optional. An object of type WorkerAdapterOptions, which will be passed to the newly created worker. This worker will then initialize a WorkerAdapter by invoking the processTask function. The options include:

    • isLocalDevelopment

      A boolean flag. If set to true, intermediary files containing extracted data will be stored on the local machine, which is useful during development. The default value is false.

    • timeout

      A number that specifies the timeout duration for the lambda function, in milliseconds. The default is 10 minutes (10 * 60 * 1000 milliseconds), with a maximum allowable duration of 13 minutes (13 * 60 * 1000 milliseconds).

    • batchSize

      A number that determines the maximum number of items to be processed and saved to an intermediary file before being sent to the Airdrop platform. The default batch size is 2,000.

Return value

A promise that resolves once the worker has completed processing.

Example

const run = async (events: AirdropEvent[]) => {
  for (const event of events) {
    const file = getWorkerPerExtractionPhase(event);
    await spawn<ExtractorState>({
      event,
      initialState,
      workerPath: file,
    });
  }
};

processTask function

The processTask function retrieves the current state from the Airdrop platform and initializes a new WorkerAdapter. It executes the code specified in the task parameter, which contains the worker's functionality. If a timeout occurs, the function handles it by executing the onTimeout callback, ensuring the worker exits gracefully. Both functions receive an adapter parameter, representing the initialized WorkerAdapter object.

Usage

processTask({ task, onTimeout })

Parameters

  • task

    Required. A function that defines the logic associated with the given event type.

  • onTimeout

    Required. A function managing the timeout of the lambda invocation, including saving any necessary progress at the time of timeout.

Example

// External sync units extraction
processTask({
  task: async ({ adapter }) => {
    const httpClient = new HttpClient(adapter.event);

    const todoLists = await httpClient.getTodoLists();

    const externalSyncUnits: ExternalSyncUnit[] = todoLists.map((todoList) => normalizeTodoList(todoList));

    await adapter.emit(ExtractorEventType.ExtractionExternalSyncUnitsDone, {
      external_sync_units: externalSyncUnits,
    });
  },
  onTimeout: async ({ adapter }) => {
    await adapter.emit(ExtractorEventType.ExtractionExternalSyncUnitsError, {
      error: {
        message: 'Failed to extract external sync units. Lambda timeout.',
      },
    });
  },
});

WorkerAdapter class

Used to interact with Airdrop platform. Provides utilities to emit events to the Airdrop platform, update the state of the snap-in and upload artifacts (files with data) to the platform.

Usage

new WorkerAdapter({
  event,
  adapterState,
  options,
});

Parameters

  • event

    Required. An object of type AirdropEvent that is received from the Airdrop platform.

  • adapterState

    Required. An object of type State, which represents the initial state of the adapter.

  • options

    Optional. An object of type WorkerAdapterOptions that specifies additional configuration options for the WorkerAdapter. This object is passed via the spawn function.

Example

const adapter = new WorkerAdapter<ConnectorState>({
  event,
  adapterState,
  options,
});

WorkerAdapter.state property

Getter and setter methods for working with the adapter state.

Usage

// get state
const adapterState = adapter.state;

// set state
adapter.state = newAdapterState;

Example

export const initialState: ExtractorState = {
  users: { completed: false },
  tasks: { completed: false },
  attachments: { completed: false },
};

adapter.state = initialState;

WorkerAdapter.initializeRepos method

Initializes a Repo object for each item provided.

Usage

adapter.initializeRepos(repos);

Parameters

  • repos

    Required. An array of objects of type RepoInterface.

Example

This should typically be called within the function passed as a parameter to the processTask function in the data extraction phase.

const repos = [
  {
    itemType: 'tasks',
    normalize: normalizeTask,
  }
];

adapter.initializeRepos(repos);

WorkerAdapter.getRepo method

Finds a Repo from the initialized repos.

Usage

adapter.getRepo(itemType);

Parameters

  • itemType

    Required. A string that represents the itemType property for the searched repo.

Return value

An object of type Repo if the repo is found, otherwise undefined.

Example

This should typically be called within the function passed as a parameter to the processTask function.

// Push users to the repository designated for 'users' data.
await adapter.getRepo('users')?.push(users);

WorkerAdapter.emit method

Emits an event to the Airdrop platform.

Usage

adapter.emit( newEventType, data ):

Parameters

  • newEventType

    Required. The event type to be emitted, of type ExtractorEventType or LoaderEventType.

  • data

    Optional. An object of type EventData which represents the data to be sent with the event.

Return value

A promise, which resolves to undefined after the emit function completes its execution or rejects with an error.

Example

This should typically be called within the function passed as a parameter to the processTask function.

// Emitting successfully finished data extraction.
await adapter.emit(ExtractorEventType.ExtractionDataDone);

// Emitting a delay in attachments extraction phase.
await adapter.emit(ExtractorEventType.ExtractionAttachmentsDelay, {
  delay: 10,
});