|
| 1 | +import { html, HTMLTemplateResult, PropertyValues } from 'lit' |
| 2 | +import { customElement, property, state } from 'lit/decorators.js' |
| 3 | +import { TransformRESTElement } from './transform-rest.ts' |
| 4 | +import { DefaultApiFactory } from 'seed-xml-transformer-ts-client/api.ts' |
| 5 | + |
| 6 | +// define the web component |
| 7 | +@customElement("seed-transform-rest") |
| 8 | +export class SeedTransformREST extends TransformRESTElement { |
| 9 | + |
| 10 | + @property({ type: String }) |
| 11 | + apiBase: string = ""; |
| 12 | + |
| 13 | + @property() // { type: Array<String> }) |
| 14 | + transformation: string | null = null; |
| 15 | + |
| 16 | + @property() |
| 17 | + parameters: { [key: string]: string } = {}; |
| 18 | + |
| 19 | + @property() |
| 20 | + href: string | null = null; |
| 21 | + |
| 22 | + @property() |
| 23 | + src: File | null = null; |
| 24 | + |
| 25 | + @state() |
| 26 | + _result: File | null = null; |
| 27 | + |
| 28 | + @state() |
| 29 | + _error: any = null; |
| 30 | + |
| 31 | + override async willUpdate(changedProperties: PropertyValues<this>) { |
| 32 | + console.log("will update called"); |
| 33 | + if (this.transformation != null && |
| 34 | + (changedProperties.has("transformation") || |
| 35 | + changedProperties.has("parameters") || |
| 36 | + changedProperties.has("href") || |
| 37 | + changedProperties.has("src"))) { |
| 38 | + console.log("has transformation and properties changed"); |
| 39 | + if (this.src === null && this.href === null) { |
| 40 | + console.log("not enough information"); |
| 41 | + // TODO: nothing |
| 42 | + this._result = null; |
| 43 | + } else if (this.src === null && this.href != null) { |
| 44 | + // GET |
| 45 | + console.log("from URL, POST parameters"); |
| 46 | + try { |
| 47 | + const api = DefaultApiFactory(this.getConfiguration()); |
| 48 | + const response = await api.transformTransformationUrlPost(this.transformation, this.href, this.parameters); |
| 49 | + this._result = response.data; |
| 50 | + } catch (err) { |
| 51 | + console.log("transformTransformationUrlGet failed", err); |
| 52 | + this._error = err; |
| 53 | + } |
| 54 | + } else if (this.src != null) { |
| 55 | + // POST |
| 56 | + console.log("POST file and parameters") |
| 57 | + console.log("src", this.src, this.src.text()); |
| 58 | + try { |
| 59 | + // systemId has to be a valid URL, if empty string, we pass undefined instead |
| 60 | + let systemId: string | undefined = this.href ?? undefined; |
| 61 | + if (this.href?.length === 0 || this.href === null) { |
| 62 | + systemId = undefined; |
| 63 | + } |
| 64 | + const api = DefaultApiFactory(this.getConfiguration()); |
| 65 | + const response = await api.transformTransformationPost(this.transformation, this.src, systemId, this.parameters); |
| 66 | + this._result = response.data; |
| 67 | + } catch (err) { |
| 68 | + console.log("transformTransformationUrlGet failed", err); |
| 69 | + this._error = err; |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + render(): HTMLTemplateResult { |
| 76 | + return html`<div>${this._result}</div>`; |
| 77 | + } |
| 78 | + |
| 79 | + |
| 80 | +} |
| 81 | + |
| 82 | +declare global { |
| 83 | + interface HTMLElementTagNameMap { |
| 84 | + "seed-transform-rest": SeedTransformREST; |
| 85 | + } |
| 86 | +} |
0 commit comments