UNPKG

@selenite/graph-editor

Version:

A graph editor for visual programming, based on rete and svelte.

55 lines (54 loc) 1.82 kB
import { getBackendAddress } from '../../utils/config'; import { Node } from './Node.svelte'; export class APINode extends Node { url; constructor(params) { const { url, label = 'API', height = 225, width = 150, factory, defaultOutExec = true } = params; super({ label, height: height, width: width, factory }); this.url = getBackendAddress(`/api/v1` + url); this.addInExec(); if (defaultOutExec) this.addOutExec(); } async getBody() { const inputs = (await this.fetchInputs()); const body = {}; for (const key in this.inputs) { if (key != 'exec') { const data = this.getData(key, inputs); // console.log(data); body[key] = data; } } // console.log("body:",JSON.stringify(body)); return body; } async execute(input, forward) { // notifications.show({ title: 'API Node', message: `Execution de l'appel API ${this.url}` }); try { const response = await fetch(this.url, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(await this.getBody()) }); const responseData = await response.json(); await this.processResponseData(responseData); super.execute(input, forward); } catch (error) { { super.execute(input, forward); return; } } } async processResponseData(responseData) { for (const key in responseData) { this.setData(key, responseData[key]); } this.processDataflow(); } } APINode.__isAbstract = true;