2021-03-24 20:16:03 +00:00
|
|
|
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";
|
2021-04-03 17:26:43 +00:00
|
|
|
import { t } from "@lingui/macro";
|
2021-03-24 20:16:03 +00:00
|
|
|
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<Response | void> {
|
|
|
|
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;
|
2021-03-27 22:18:51 +00:00
|
|
|
export const API_DRAWER_MIDDLEWARE = new APIMiddleware();
|
2021-03-24 20:16:03 +00:00
|
|
|
|
|
|
|
@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`<li class="pf-c-notification-drawer__list-item pf-m-read">
|
|
|
|
<div class="pf-c-notification-drawer__list-item-header">
|
|
|
|
<h2 class="pf-c-notification-drawer__list-item-header-title">
|
|
|
|
${item.method}
|
|
|
|
</h2>
|
|
|
|
</div>
|
|
|
|
<p class="pf-c-notification-drawer__list-item-description">${item.path}</p>
|
|
|
|
</li>`;
|
|
|
|
}
|
|
|
|
|
|
|
|
render(): TemplateResult {
|
|
|
|
return html`<div class="pf-c-drawer__body pf-m-no-padding">
|
|
|
|
<div class="pf-c-notification-drawer">
|
|
|
|
<div class="pf-c-notification-drawer__header pf-c-content">
|
|
|
|
<h1>
|
2021-04-03 17:26:43 +00:00
|
|
|
${t`API Requests`}
|
2021-03-24 20:16:03 +00:00
|
|
|
</h1>
|
|
|
|
</div>
|
|
|
|
<div class="pf-c-notification-drawer__body">
|
|
|
|
<ul class="pf-c-notification-drawer__list">
|
2021-03-27 22:18:51 +00:00
|
|
|
${API_DRAWER_MIDDLEWARE.requests.map(n => this.renderItem(n))}
|
2021-03-24 20:16:03 +00:00
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>`;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|