@ibm-adw/skill-toolkit
Version:
Developing your own skills with IBM Automation Digital Worker Skill Toolkit
134 lines (116 loc) • 5.54 kB
JavaScript
/*
Licensed Materials - Property of IBM
5737-I23
Copyright IBM Corp. 2019, 2020. All Rights Reserved.
U.S. Government Users Restricted Rights:
Use, duplication or disclosure restricted by GSA ADP Schedule
Contract with IBM Corp.
*/
;
const FINAL_STEP = '__baiw_final';
const INIT_STEP = '__baiw_init';
const SKILL_INITIAL_STEP = 'init';
const SKILL_REOPEN_STEP = 'reopen';
const Ajv = require('ajv');
const ajv = new Ajv();
const path = require('path');
const utils = require('./util/locales');
module.exports = {
post: async (req, res) => {
const SKILL_DIR_1 = process.argv[2];
const SKILL_DIR = path.resolve(process.env.PWD, SKILL_DIR_1);
try {
const skill_to_be_tested = require(`${SKILL_DIR}/skill-config`);
const skill_final_schema = require(`${SKILL_DIR}/skill-spec`);
let step = req.query.event;
let configuration = req.body;
const DEFAULT_LOCALE = 'en';
const browserLocales = req.headers['accept-language'];
const supportedLocales = req.headers['content-language'];
// enforcedLocale is the locale given as param in client url
const enforcedLocale = req.query.currentLocale;
// TODO fallback locale should be sent to skill Issue #2191
// if enforcedLocale has a region then fallbackLocale is the language with no region
// if enforcedLocale has no region then fallbackLocale is client default language ('en')
// if not provided we set it to DEFAULT_LOCALE
// const fallbackLocale = req.query.fallbackLocale || DEFAULT_LOCALE;
if (step === INIT_STEP) {
if (global.skillConfiguration) {
configuration = global.skillConfiguration;
step = SKILL_REOPEN_STEP;
} else {
step = SKILL_INITIAL_STEP;
}
}
if (step !== FINAL_STEP) {
if (skill_to_be_tested.form) {
const filteredLocales = (enforcedLocale ? utils.getLocales(browserLocales, enforcedLocale, DEFAULT_LOCALE) : utils.getLocales(browserLocales, supportedLocales, DEFAULT_LOCALE));
// TODO fallback locale should be sent to skill Issue #2191
// if (enforcedLocale) {
// // if enforcedLocale and fallbackLocale represent the same language, default language must be sent as fallback to the skill
// // if enforcedLocale and fallbackLocale DO NOT represent the same language, fallback locale must be sent as fallback to the skill
// }
skill_to_be_tested.form(configuration, step, filteredLocales)
.then(value => {
if (step === SKILL_INITIAL_STEP) {
value.isInitial = true;
}
if (Object.keys(configuration).length > 0) {
global.skillConfiguration = configuration;
}
res.status(200).json(value);
})
.catch(err => {
console.error(err);
res.status(500).json({
name: err.name,
message: err.message
});
});
} else {
console.log('The function \'form\' is not implemented');
res.status(404).send('The function \'form\' is not implemented');
}
} else {
if (skill_final_schema.config_schema) {
// If final validation schema is provided, validate data against schema
// Force async validation for schema
skill_final_schema.config_schema['$async'] = true;
try {
const validate = ajv.compile(skill_final_schema.config_schema);
global.skillConfiguration = configuration;
await validate(configuration);
res.status(200).json({});
} catch (errors) {
console.error(errors);
if (JSON.stringify(errors) !== '{}') {
res.status(500).json(
{
errors: errors,
schema: skill_final_schema.config_schema
});
} else {
res.status(500).json({
errors: { message: errors.message, name: errors.name },
schema: skill_final_schema.config_schema
});
}
}
} else {
global.skillConfiguration = configuration;
res.status(501).json({});
}
}
} catch (error) {
console.log(error);
res.status(500).json({
name: error.name,
message: error.message
});
}
},
delete: async (req, res) => {
global.skillConfiguration = undefined;
res.status(200).json({});
}
};