UNPKG

@intres/azure-text-analytics

Version:

Node-red nodes for Microsoft Azure Text Analytics

33 lines (27 loc) 1.44 kB
const axios = require('axios').default; const TextAnalyticsBase = require('../utilities/text-analytics-base'); class DetectLanguageDriver extends TextAnalyticsBase { constructor(node, key, region) { super(node, key, region); this.baseUrl = `https://${this.getRegion()}.api.cognitive.microsoft.com/text/analytics/v3.2-preview.2/languages`; } preProcess(options) { // Test msg.payload, it should be an array if (!Array.isArray(options.requestArray)) throw new Error('msg.payload should be an array'); const availableSet = new Set(['id', 'text', 'countryHint']); for (let obj of options.requestArray) { const ownProperties = Object.getOwnPropertyNames(obj); for (let p of ownProperties) { if (availableSet.has(p)) continue; throw new Error(`Property '${p}' is not allowed. Only 'id', 'text' and 'countryHint' are allowed.`); } } options.requestBody = { documents: options.requestArray }; } async analyzeInternal({ showStats, loggingOptOut, requestBody, modelVersion, config }) { this.setStatus({ fill: 'green', shape: 'dot', text: 'Detecting' }); const res = await axios.post(`${this.baseUrl}?showStats=${showStats}&loggingOptOut=${loggingOptOut}&modelVersion=${modelVersion}`, requestBody, config); return res.data; } } module.exports = DetectLanguageDriver;