adaptive-card-transformer
v1.0.10
Published
APIs to transform other Cards to equivalent Adaptive Cards
Downloads
4
Readme
AdaptiveCard Transformer Library
This package contains the APIs to transform non-Adaptive Cards to Adaptive Cards. Presently it supports below cards:
- Hero Card
- Thumbnail Card
- O365 Connector Card
Installation
To install the stable version:
npm
npm install adaptive-card-transformer --save-dev
yarn
yarn add adaptive-card-transformer --dev
Unit Tests
yarn test
Usage
Transform card
Below is an example on how to transform a Hero card to Adaptive card.
import * as adaptiveCardTransformer from "adaptive-card-transformer";
// Define a HeroCard object
let heroCard: adaptiveCardTransformer.Hero.HeroCard = {
title: "Hero title",
subtitle: "Hero subtitle",
text: "Hero text",
images: [{
"url": "https://hero.com/image.jpg"
}],
buttons: [{
"type": "openUrl",
"title": "View website",
"value": "https://hero.com/website"
}]
};
// Wrap it within a CardAttachment object
let heroCardAttachment: adaptiveCardTransformer.CardAttachment = {
contentType: adaptiveCardTransformer.CardContentType.HeroCard,
content: heroCard
};
// Transform the CardAttachment object
let transformedCardAttachment = adaptiveCardTransformer.transformCardAttachment(heroCardAttachment);
// Extract the transformed AdaptiveCard object
let adaptiveCard: adaptiveCardTransformer.Adaptive.AdaptiveCard = transformedCardAttachment.content;
Utilities
Additionally this package also provides few utilities like below.
import * as adaptiveCardTransformer from "adaptive-card-transformer";
// To recursively extract any type of elements from an Adaptive card json
let allImages = adaptiveCardTransformer.filterCardElements(adaptiveCardJson.body, "Image");
let first5TextBlocks = adaptiveCardTransformer.filterCardElements(adaptiveCardJson.body, "TextBlock", 5);
let allOpenUrlActions = adaptiveCardTransformer.filterCardElements(adaptiveCardJson.actions, "Action.OpenUrl");
// To derive the fallback text from an Adaptive card json
// This will return the adaptiveCard.fallbackText, if any, or
// the first TextBlock content, if any
let fallbackText = adaptiveCardTransformer.getCardFallbackText(adaptiveCardJson);
// To derive the fallback url from an Adaptive card json
// This will return the first Action.OpenUrl target giving preference to below order:
// 1. adaptiveCard.selectAction
// 2. adaptiveCard.actions
// 3. actionSet.actions within adaptiveCard.body
let fallbackUrl = adaptiveCardTransformer.getCardFallbackUrl(adaptiveCardJson);