Page 1 of 1

App Get App Icon

Posted: Fri Aug 12, 2022 10:34 am
by guidoolijslager
Hi Freddy/Laurent,

I just watched the video "Advanced Scripting and best practices with Node.js in Switch" again.
This kind of video's makes me enthusiastic to start scripting (Please make more of them).
As an exercise I wanted to convert the javascript example "Get app icon" to Typescript.
I've got a lot of errors with the javascript code in Typescript.
Is it possible to publish an Typescript example on the Github page?

Regards,

Guido

Re: App Get App Icon

Posted: Fri Aug 12, 2022 11:05 am
by freddyp
All the examples on https://github.com/EnfocusSW are TypeScript.

Re: App Get App Icon

Posted: Fri Aug 12, 2022 12:10 pm
by guidoolijslager
Hi Freddy, yes I know these examples.
I started to convert the javascript example "Gett App icon" to Typescript from the video the video "Advanced Scripting and best practices with Node.js in Switch".
I run into a lot of errors and I don't know how to solve them.
It would be nice to make an video on how to convert an existing javascript example "Gett App icon" to Typescript from scratch.

Regards,

Guido

Re: App Get App Icon

Posted: Thu Aug 18, 2022 10:43 am
by guidoolijslager
With some help I got the Typescript version of the example "GetAppIcon" working.
Its not that easy to transform the javascript version to Typescript but it works.


import {writeFileSync, unlinkSync} from "fs";
import axios, {AxiosError} from "axios";
import tmp from "tmp";
import download from "download";

import { XMLParser, XMLBuilder, XMLValidator} from "fast-xml-parser";
var parser = new XMLParser();


async function jobArrived(s: Switch, flowElement: FlowElement, job: Job): Promise<void> {

let appName = await flowElement.getPropertyStringValue("appName");
// get app icon url
let config = { method: 'get', url: 'https://www.enfocus.com/en/appstorefeed' };
let APIresp = await APIcall(config);

if(APIresp?.status !== 200) {
await job.log(LogLevel.Warning, "Geen lijst gevonden");
return;
}

let appsDetails = APIresp.data; // xml
appsDetails = parser.parse(appsDetails); // json
let appIconUrl = "";
for (let i = 0; i < appsDetails.rss.channel.item.length; i++) {
if (appsDetails.rss.channel.item.title == appName) {
appIconUrl = appsDetails.rss.channel.item.guid;
}
}

// download icon to temp path
const tmpobj = tmp.fileSync();
writeFileSync(tmpobj.name, await download(appIconUrl));

//create child job and send
let newJob = await job.createChild(tmpobj.name);
let newName = appName + ".png";
await newJob.sendToSingle(newName);

//remove input job
await job.sendToNull();

//remove temp file
unlinkSync(tmpobj.name);
}

async function getLibraryForProperty(s: Switch, flowElement: any, tag: string) {
if (tag === "appName") {
let config = { method: 'get', url: 'https://www.enfocus.com/en/appstorefeed' };
let APIresp = await APIcall(config);

if(APIresp?.status !== 200) {
// await job.log(LogLevel.Warning, "Geen lijst gevonden");
return;
}

let appsDetails = APIresp.data; // xml
appsDetails = parser.parse(appsDetails); // json
let appsList = []
for (let i = 0; i < appsDetails.rss.channel.item.length; i++) {
appsList.push(appsDetails.rss.channel.item.title)
}
return (appsList); // array
}
}

async function APIcall(config: any ) {
try {
return await axios(config);
} catch (error) {
const err = error as AxiosError;
console.error(err);
console.error(err.response?.status);
console.error(err.response?.data);
return err.response;
}
}