intentful
Version:
Create Custom Skills with less headache
251 lines (250 loc) • 9.72 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.APLDocument = void 0;
const Alexa = __importStar(require("ask-sdk-core"));
const fs = __importStar(require("fs"));
const uuid = __importStar(require("uuid"));
const component_1 = require("./components/component");
const helpers_1 = require("./interfaces/helpers");
const helpers_2 = require("../skill/helpers");
class APLDocument extends component_1.APLComponent {
constructor(props) {
super('APL', props);
}
addComponents(..._components) {
this.props.items.push(..._components);
return this;
}
componentSpecificModel() {
return {
version: this.props.version ? this.props.version : '2023.1',
import: [
{
name: 'alexa-layouts',
version: '1.6.0'
},
{
name: 'alexa-viewport-profiles',
version: '1.5.0'
},
{
name: 'alexa-styles',
version: '1.5.0'
},
...(this.props.import ? this.props.import : [])
],
mainTemplate: {
parameters: [
...(this.props.dynamicTokenLists ? Object.keys(this.props.dynamicTokenLists) : []),
...(this.props.dynamicIndexLists ? Object.keys(this.props.dynamicIndexLists) : []),
...(this.props.staticData ? Object.keys(this.props.staticData) : [])
],
bind: this.props.bind,
items: this.props.items
},
export: this.props.export,
// commands: this.props.commands,
extensions: this.props.extensions,
graphics: this.props.graphics,
resources: this.props.resources,
/**
* Override standard background color.
*/
background: this.props.background,
/**
* An optional description of this document.
*/
description: this.props.description,
handleTick: this.props.handleTick,
environment: this.props.environment,
/**
* A list of commands to execute when the document configuration changes.
*/
onConfigChange: this.props.onConfigChange,
/**
* A list of the commands to execute whenever the display state of the document changes
*/
onDisplayStateChange: this.props.onDisplayStateChange,
/**
* A list of commands to execute when the document is first displayed.
*/
onMount: this.props.onMount,
/**
* A map of document-wide settings.
*/
settings: this.props.settings,
/**
* A map of style name to style definition
*/
styles: this.props.styles,
/**
* The sample templates are under Amazon Software License
*/
license: this.props.license,
theme: this.props.theme
};
}
componentSpecificRequestHandlers() {
return [
...(0, helpers_1.convertComponentListToRequestHandlers)(this.props.items),
...this.convertDynamicTokenListPropsToRequestHandlers(this.props.dynamicTokenLists),
...this.convertDynamicIndexListPropsToRequestHandlers(this.props.dynamicIndexLists)
];
}
getDirective(additionalData) {
return {
type: 'Alexa.Presentation.APL.RenderDocument',
token: uuid.v4(),
document: {
src: `doc://alexa/apl/documents/${this.props.documentName}`,
type: 'Link'
},
datasources: this.buildDatasources()
};
}
getFullDirective(additionalData) {
return {
type: 'Alexa.Presentation.APL.RenderDocument',
token: uuid.v4(),
document: this.model(),
datasources: this.buildDatasources(additionalData)
};
}
buildDatasources(additionalData) {
return {
...(additionalData ? additionalData : {}),
...this.props.staticData,
...this.convertDynamicTokenListPropsToModel(this.props.dynamicTokenLists),
...this.convertDynamicIndexListPropsToModel(this.props.dynamicIndexLists)
};
}
writeToFile(baseSkillPackagePath) {
const aplDocumentFolder = (baseSkillPackagePath ? baseSkillPackagePath : './') + `response/display/${this.props.documentName}`;
if (!fs.existsSync(aplDocumentFolder)) {
fs.mkdirSync(aplDocumentFolder, { recursive: true });
}
const model = this.model();
// write interaction models
fs.writeFileSync(`${aplDocumentFolder}/document.json`, JSON.stringify(model, null, 2));
if (!fs.existsSync(`${aplDocumentFolder}/datasources`)) {
fs.mkdirSync(`${aplDocumentFolder}/datasources`, { recursive: true });
}
fs.writeFileSync(`${aplDocumentFolder}/datasources/default.json`, JSON.stringify(this.buildDatasources(), null, 2));
}
convertDynamicIndexListPropsToModel(props) {
if (props) {
const keys = Object.keys(props);
return keys
.map((key) => {
const converted = {
[key]: {
type: 'dynamicIndexList',
listId: key,
startIndex: props[key].startIndex,
minimumInclusiveIndex: props[key].minimumInclusiveIndex,
maximumExclusiveIndex: props[key].maximumExclusiveIndex,
items: props[key].items
}
};
return converted;
})
.reduce((l, r) => {
return {
...l,
...r
};
});
}
return undefined;
}
convertDynamicTokenListPropsToModel(props) {
if (props) {
const keys = Object.keys(props);
return keys
.map((key) => {
const converted = {
[key]: {
type: 'dynamicTokenList',
pageToken: props[key].pageToken,
listId: key,
forwardPageToken: props[key].forwardPageToken,
backwardPageToken: props[key].backwardPageToken,
items: props[key].items
}
};
return converted;
})
.reduce((l, r) => {
return {
...l,
...r
};
});
}
return undefined;
}
convertDynamicTokenListPropsToRequestHandlers(props) {
if (props) {
const keys = Object.keys(props);
return keys.map((key) => {
return {
uniqueId: key,
canHandle: (input) => {
const requestType = Alexa.getRequestType(input.requestEnvelope);
if (requestType === 'Alexa.Presentation.APL.LoadTokenListData') {
const request = Alexa.getRequest(input.requestEnvelope);
return request.listId === key;
}
return false;
},
handle: (0, helpers_2.wrapHandler)(props[key].handler)
};
});
}
return [];
}
convertDynamicIndexListPropsToRequestHandlers(props) {
if (props) {
const keys = Object.keys(props);
return keys.map((key) => {
return {
uniqueId: key,
canHandle: (input) => {
const requestType = Alexa.getRequestType(input.requestEnvelope);
if (requestType === 'Alexa.Presentation.APL.LoadIndexListData') {
const request = Alexa.getRequest(input.requestEnvelope);
return request.listId === key;
}
return false;
},
handle: (0, helpers_2.wrapHandler)(props[key].handler)
};
});
}
return [];
}
}
exports.APLDocument = APLDocument;