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 API_DRAWER_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")}

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