π A self-hostable personal dashboard built for you. Includes status-checking, widgets, themes, icon packs, a UI editor and tons more!
Vue awesomedashboarddockerhomelabhomepagemitnodejsorganizationproductivitypwaself-hostedstartpagevue
β¨ Adds custom api widget (#2191)
Alicia Sykes13262567d044
diff --git a/src/components/Widgets/CustomApi.vue b/src/components/Widgets/CustomApi.vuenew file mode 100644index 00000000..ed2715ba--- /dev/null+++ b/src/components/Widgets/CustomApi.vue@@ -0,0 +1,142 @@+<template>+ <div class="customapi-wrapper" :class="display">+ <div class="row" v-for="(row, i) in results" :key="i">+ <span class="lbl" v-if="row.label">{{ row.label }}</span>+ <span class="val">+ {{ row.value }}+ <span+ v-if="row.additional"+ class="additional"+ :class="`color-${row.additional.color || 'default'}`"+ >{{ row.additional.value }}</span>+ </span>+ </div>+ </div>+</template>++<script>+import WidgetMixin from '@/mixins/WidgetMixin';+import { resolveField, formatValue, adaptiveColor } from '@/utils/CustomApiHelpers';++export default {+ mixins: [WidgetMixin],+ data() {+ return {+ results: [],+ };+ },+ computed: {+ url() {+ return this.parseAsEnvVar(this.options.url);+ },+ /* Basic-auth header, when username and password are supplied */+ authHeaders() {+ if (this.options.username && this.options.password) {+ const username = this.parseAsEnvVar(this.options.username);+ const password = this.parseAsEnvVar(this.options.password);+ return { Authorization: `Basic ${window.btoa(`${username}:${password}`)}` };+ }+ return {};+ },+ /* User headers (env-vars resolved) merged over the auth header */+ mergedHeaders() {+ const userHeaders = this.options.headers || {};+ const resolved = {};+ Object.keys(userHeaders).forEach((key) => {+ resolved[key] = this.parseAsEnvVar(userHeaders[key]);+ });+ return { ...this.authHeaders, ...resolved };+ },+ method() {+ return (this.options.method || 'GET').toUpperCase();+ },+ display() {+ return this.options.display === 'list' ? 'list' : 'block';+ },+ mappings() {+ const { mappings } = this.options;+ return Array.isArray(mappings) && mappings.length ? mappings : [{ label: '', format: 'text' }];+ },+ /* Prefer homepage-style refreshInterval (ms), else fall back to native updateInterval (secs) */+ updateInterval() {+ const ms = this.options.refreshInterval;+ if (ms === 0 || ms === false) return 0;+ if (typeof ms === 'number' && ms >= 1000) return ms;+ const secs = this.options.updateInterval;+ if (typeof secs === 'boolean') return secs ? 30000 : 0;+ if (typeof secs === 'number' && secs >= 2 && secs <= 7200) return secs * 1000;+ return 10000;+ },+ },+ methods: {+ fetchData() {+ if (!this.url) { this.error('A `url` is required'); this.finishLoading(); return; }+ this.makeRequest(this.url, this.mergedHeaders, this.method, this.options.requestBody)+ .then(this.processData)+ .catch(() => { /* error already surfaced by the mixin */ });+ },+ /* Map each configured field to a labelled, formatted value */+ processData(data) {+ try {+ this.results = this.mappings.map((m) => ({+ label: m.label || '',+ value: formatValue(resolveField(data, m.field), m, data),+ additional: this.buildAdditional(m.additionalField, data),+ }));+ } catch (e) {+ this.error('Failed to parse API response', e);+ }+ },+ /* Resolve an optional secondary value; `adaptive` colour derives from the value's sign */+ buildAdditional(field, data) {+ if (!field) return null;+ const raw = resolveField(data, field.field);+ return {+ value: formatValue(raw, field, data),+ color: field.color === 'adaptive' ? adaptiveColor(raw) : (field.color || ''),+ };+ },+ },+};+</script>++<style scoped lang="scss">+.customapi-wrapper {+ color: var(--widget-text-color);+ .row {+ display: flex;+ align-items: center;+ justify-content: space-between;+ padding: 0.25rem 0.1rem;+ &:not(:last-child) {+ border-bottom: 1px dashed var(--widget-text-color);+ }+ .lbl {+ font-weight: bold;+ margin-right: 0.5rem;+ }+ .val {+ font-family: var(--font-monospace);+ text-align: right;+ min-width: 0;+ overflow-wrap: anywhere;+ .additional {+ margin-left: 0.5rem;+ opacity: var(--dimming-factor);+ &.color-theme { color: var(--primary); }+ &.color-success { color: var(--success); }+ &.color-warning { color: var(--warning); }+ &.color-error { color: var(--error); }+ &.color-info { color: var(--info); }+ &.color-black { color: #000; }+ &.color-white { color: #fff; }+ }+ }+ }+ &.list .row {+ flex-direction: column;+ align-items: flex-start;+ .val { text-align: left; }+ }+}+</style>diff --git a/src/components/Widgets/WidgetBase.vue b/src/components/Widgets/WidgetBase.vueindex e6e8326a..f715ff0c 100644--- a/src/components/Widgets/WidgetBase.vue+++ b/src/components/Widgets/WidgetBase.vue@@ -66,6 +66,8 @@ const COMPAT = {'crypto-watch-list': 'CryptoWatchList','custom-search': 'CustomSearch','custom-list': 'CustomList',+ customapi: 'CustomApi',+ 'custom-api': 'CustomApi','cve-vulnerabilities': 'CveVulnerabilities','domain-monitor': 'DomainMonitor','drone-ci': 'DroneCi',diff --git a/src/utils/CustomApiHelpers.js b/src/utils/CustomApiHelpers.jsnew file mode 100644index 00000000..a0d8ff4d--- /dev/null+++ b/src/utils/CustomApiHelpers.js@@ -0,0 +1,80 @@+/* Field resolution and value formatting for the CustomApi widget */++/* Get a nested value from an object by dot-path, e.g. 'a.b.0.c'. Empty path returns the root */+export const resolveField = (obj, path) => {+ if (!path) return obj;+ return String(path).split('.').reduce((acc, key) => (acc == null ? acc : acc[key]), obj);+};++/* Map a value's sign to an action colour, for `color: adaptive` fields. Non-numeric stays neutral */+export const adaptiveColor = (raw) => {+ const n = Number(raw);+ if (Number.isNaN(n)) return '';+ if (n > 0) return 'success';+ if (n < 0) return 'error';+ return 'info';+};++/* Largest-first periods, used to pick a unit for relative dates */+const RELATIVE_UNITS = [+ { unit: 'year', secs: 31557600 },+ { unit: 'month', secs: 2628000 },+ { unit: 'week', secs: 604800 },+ { unit: 'day', secs: 86400 },+ { unit: 'hour', secs: 3600 },+ { unit: 'minute', secs: 60 },+ { unit: 'second', secs: 1 },+];++/* Format a date as relative to now, e.g. '2 days ago' or 'in 3 hours' */+const formatRelativeDate = (raw, mapping, locale) => {+ const time = new Date(raw).getTime();+ if (Number.isNaN(time)) return String(raw);+ const diffSecs = (time - Date.now()) / 1000;+ const period = RELATIVE_UNITS.find((p) => Math.abs(diffSecs) >= p.secs)+ || RELATIVE_UNITS[RELATIVE_UNITS.length - 1];+ const rtf = new Intl.RelativeTimeFormat(locale, {+ style: mapping.style || 'long',+ numeric: mapping.numeric || 'always',+ });+ return rtf.format(Math.round(diffSecs / period.secs), period.unit);+};++/* Format a raw value per a mapping's `format`. `root` is the full response, used by `size` */+export const formatValue = (raw, mapping = {}, root) => {+ const format = mapping.format || 'text';+ const locale = mapping.locale || navigator.language;++ // `size` counts array elements / object keys (root when no field is given)+ if (format === 'size') {+ const target = mapping.field == null ? root : raw;+ if (Array.isArray(target)) return String(target.length);+ if (target && typeof target === 'object') return String(Object.keys(target).length);+ return target == null ? '' : String(target);+ }++ if (raw == null) return '';++ switch (format) {+ case 'number': {+ const n = Number(raw);+ return Number.isNaN(n) ? String(raw) : new Intl.NumberFormat(locale).format(n);+ }+ case 'percent': {+ const n = Number(raw);+ return Number.isNaN(n) ? String(raw)+ : new Intl.NumberFormat(locale, { style: 'percent', maximumFractionDigits: 2 }).format(n / 100);+ }+ case 'date': {+ const date = new Date(raw);+ if (Number.isNaN(date.getTime())) return String(raw);+ const opts = { dateStyle: mapping.dateStyle || 'long' };+ if (mapping.timeStyle) opts.timeStyle = mapping.timeStyle;+ return new Intl.DateTimeFormat(locale, opts).format(date);+ }+ case 'relativeDate':+ return formatRelativeDate(raw, mapping, locale);+ default:+ return String(raw);+ }+};