From ec8dee35888d4a7f6a0569991bc7b37d289d9696 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Wed, 24 Mar 2021 21:16:03 +0100 Subject: [PATCH] web: add API Drawer Signed-off-by: Jens Langhammer --- web/src/api/Config.ts | 6 +- web/src/constants.ts | 1 + web/src/elements/notifications/APIDrawer.ts | 86 +++++++++++++++++++++ 3 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 web/src/elements/notifications/APIDrawer.ts diff --git a/web/src/api/Config.ts b/web/src/api/Config.ts index fbb388c43..10a96aa27 100644 --- a/web/src/api/Config.ts +++ b/web/src/api/Config.ts @@ -4,13 +4,17 @@ import { VERSION } from "../constants"; import { SentryIgnoredError } from "../common/errors"; import { Config, Configuration, RootApi } from "authentik-api"; import { getCookie } from "../utils"; +import { MIDDLEWARE } from "../elements/notifications/APIDrawer"; export const DEFAULT_CONFIG = new Configuration({ basePath: "/api/v2beta", headers: { "X-CSRFToken": getCookie("authentik_csrf"), "X-Authentik-Prevent-Basic": "true" - } + }, + middleware: [ + MIDDLEWARE + ], }); export function configureSentry(): Promise { diff --git a/web/src/constants.ts b/web/src/constants.ts index a2a8f1429..04f480d9c 100644 --- a/web/src/constants.ts +++ b/web/src/constants.ts @@ -8,3 +8,4 @@ export const PAGE_SIZE = 20; export const EVENT_REFRESH = "ak-refresh"; export const EVENT_NOTIFICATION_TOGGLE = "ak-notification-toggle"; export const EVENT_SIDEBAR_TOGGLE = "ak-sidebar-toggle"; +export const EVENT_API_DRAWER_REFRESH = "ak-api-drawer-refresh"; diff --git a/web/src/elements/notifications/APIDrawer.ts b/web/src/elements/notifications/APIDrawer.ts new file mode 100644 index 000000000..7134b6dee --- /dev/null +++ b/web/src/elements/notifications/APIDrawer.ts @@ -0,0 +1,86 @@ +import { Middleware, ResponseContext } from "authentik-api"; +import { CSSResult, customElement, html, LitElement, TemplateResult } from "lit-element"; +import PFBase from "@patternfly/patternfly/patternfly-base.css"; +import PFNotificationDrawer from "@patternfly/patternfly/components/NotificationDrawer/notification-drawer.css"; +import PFDropdown from "@patternfly/patternfly/components/Dropdown/dropdown.css"; +import AKGlobal from "../../authentik.css"; +import PFContent from "@patternfly/patternfly/components/Content/content.css"; +import { gettext } from "django"; +import { EVENT_API_DRAWER_REFRESH } from "../../constants"; + +export interface RequestInfo { + method: string; + path: string; +} + +export class APIMiddleware implements Middleware { + requests: RequestInfo[]; + + constructor() { + this.requests = []; + } + + post?(context: ResponseContext): Promise { + this.requests.push({ + method: (context.init.method || "GET").toUpperCase(), + path: context.url, + }); + if (this.requests.length > MAX_REQUESTS) { + this.requests.shift(); + } + window.dispatchEvent( + new CustomEvent(EVENT_API_DRAWER_REFRESH, { + bubbles: true, + composed: true, + }) + ); + return Promise.resolve(context.response); + } +} + +export const MAX_REQUESTS = 50; +export const MIDDLEWARE = new APIMiddleware(); + +@customElement("ak-api-drawer") +export class APIDrawer extends LitElement { + + static get styles(): CSSResult[] { + return [PFBase, PFNotificationDrawer, PFContent, PFDropdown, AKGlobal]; + } + + constructor() { + super(); + this.addEventListener(EVENT_API_DRAWER_REFRESH, () => { + this.requestUpdate(); + }); + } + + renderItem(item: RequestInfo): TemplateResult { + return html`
  • +
    +

    + ${item.method} +

    +
    +

    ${item.path}

    +
  • `; + } + + render(): TemplateResult { + return html`
    +
    +
    +

    + ${gettext("API Requests")} +

    +
    +
    +
      + ${MIDDLEWARE.requests.map(n => this.renderItem(n))} +
    +
    +
    +
    `; + } + +}