Implement an activity that creates a QR Code image using a Third Party Library
Implementing a custom activity allows you to build a reusable, completely custom behavior to augment the existing activities that come with VertiGIS Studio Workflow.
In this article, you will learn how to create a custom activity that generates a QR code image as a data URL using a third party library.
Prerequisites
Follow the instructions in the Web Applications SDK page to set up your development environment.
A working knowledge of TypeScript is recommended before extending Workflow for web-based hosts.
Overview
Implementing a QR code activity for web applications using a third party library consists of the following steps:
- Adding third party library dependencies to your project.
- Creating the custom activity.
- Implementing the custom activity using the third party library.
Adding Dependencies
- Run
npm install qrcode
in the terminal to install the third party qrcode library that generates QR codes. - Run
npm install @types/qrcode
in the terminal to install type information for the third party library. This is optional, but it provides an improved developer experience.
Set up Activity Skeleton
To create a new activity:
- Open the Workflow activity SDK in Visual Studio Code
- Run
npm run generate
in the terminal. - When prompted enter the name of the activity you would like to create and press
Enter
. For example,CreateQrCodeImage
. - Open the newly created
src/activities/CreateQrCodeImage.ts
file.
Implement the Activity
Modify the skeleton activity implementation in src/activities/CreateQrCodeImage.ts
to match the following example which provides a minimal QR code implementation.
import type { IActivityHandler } from "@vertigis/workflow/IActivityHandler";
import QRCode, { QRCodeToDataURLOptions } from "qrcode";
/** An interface that defines the inputs of the activity. */
export interface CreateQrCodeImageInputs {
/**
* @description The text to encode into the QR code.
* @required
*/
text: string;
}
/** An interface that defines the outputs of the activity. */
export interface CreateQrCodeImageOutputs {
/**
* @description The data URL of the resulting the QR code image.
*/
result: string;
}
/**
* @displayName Create QR Code Image
* @category Custom Activities
* @defaultName qrCode
* @description Creates a QR code image as a data URL.
*/
export class CreateQrCodeImage implements IActivityHandler {
async execute(
inputs: CreateQrCodeImageInputs
): Promise<CreateQrCodeImageOutputs> {
const { text } = inputs;
// Generate the QR code
const dataUrl = await QRCode.toDataURL(text);
return {
result: dataUrl,
};
}
}