Skip to content

Commit

Permalink
web: provide default endpoint api configuration (#10319)
Browse files Browse the repository at this point in the history
* web: fix esbuild issue with style sheets

Getting ESBuild, Lit, and Storybook to all agree on how to read and parse stylesheets is a serious
pain. This fix better identifies the value types (instances) being passed from various sources in
the repo to the three *different* kinds of style processors we're using (the native one, the
polyfill one, and whatever the heck Storybook does internally).

Falling back to using older CSS instantiating techniques one era at a time seems to do the trick.
It's ugly, but in the face of the aggressive styling we use to avoid Flashes of Unstyled Content
(FLoUC), it's the logic with which we're left.

In standard mode, the following warning appears on the console when running a Flow:

```
Autofocus processing was blocked because a document already has a focused element.
```

In compatibility mode, the following **error** appears on the console when running a Flow:

```
crawler-inject.js:1106 Uncaught TypeError: Failed to execute 'observe' on 'MutationObserver': parameter 1 is not of type 'Node'.
    at initDomMutationObservers (crawler-inject.js:1106:18)
    at crawler-inject.js:1114:24
    at Array.forEach (<anonymous>)
    at initDomMutationObservers (crawler-inject.js:1114:10)
    at crawler-inject.js:1549:1
initDomMutationObservers @ crawler-inject.js:1106
(anonymous) @ crawler-inject.js:1114
initDomMutationObservers @ crawler-inject.js:1114
(anonymous) @ crawler-inject.js:1549
```

Despite this error, nothing seems to be broken and flows work as anticipated.

* Intermediate; prepping for remove that may fail.

* web: provide a default table endpoint configuration

This commit finds 19 places where the exact same configuration is
used to describe a table's API endpoint, and replaces that configuration
with a provided default from a parent class.

While examining the logs for our build, I noted that this particular
sequence is duplicated multiple times throughout our code base,
accounting for a bloat of 169 lines or so of the estimated 5552
lines of bloat.  By providing a default endpoint configuration and
substituting it (mechanically) wherever the default is required,
we reduce our code duplication issue from 9.26% of the codesabe
to 8.99%.

... which is a start.

* Didn't need the duplication.

* remove page argument while we're at it

Signed-off-by: Jens Langhammer <jens@goauthentik.io>

* actually use it everywhere

Signed-off-by: Jens Langhammer <jens@goauthentik.io>

* web: fix inconsistent method signature for LogViewer

Removed the `_page` parameter from LogViewer's apiEndpoint() method.

The `page: number` parameter is no longer a part of this method's signature.

* web: restore reduced page size to Overview:Recent Events card

---------

Signed-off-by: Jens Langhammer <jens@goauthentik.io>
Co-authored-by: Jens Langhammer <jens@goauthentik.io>
  • Loading branch information
kensternberg-authentik and BeryJu authored Jul 2, 2024
1 parent cb7b44e commit 453f7b8
Show file tree
Hide file tree
Showing 61 changed files with 149 additions and 366 deletions.
6 changes: 2 additions & 4 deletions web/src/admin/admin-overview/cards/RecentEventsCard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,10 @@ export class RecentEventsCard extends Table<Event> {
@property({ type: Number })
pageSize = 10;

async apiEndpoint(page: number): Promise<PaginatedResponse<Event>> {
async apiEndpoint(): Promise<PaginatedResponse<Event>> {
return new EventsApi(DEFAULT_CONFIG).eventsEventsList({
ordering: this.order,
page: page,
...(await this.defaultEndpointConfig()),
pageSize: this.pageSize,
search: this.search || "",
});
}

Expand Down
8 changes: 2 additions & 6 deletions web/src/admin/applications/ApplicationListPage.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import "@goauthentik/admin/applications/ApplicationForm";
import { DEFAULT_CONFIG } from "@goauthentik/common/api/config";
import { PFSize } from "@goauthentik/common/enums.js";
import { uiConfig } from "@goauthentik/common/ui/config";
import "@goauthentik/components/ak-app-icon";
import MDApplication from "@goauthentik/docs/applications/index.md";
import "@goauthentik/elements/Markdown";
Expand Down Expand Up @@ -63,12 +62,9 @@ export class ApplicationListPage extends TablePage<Application> {
@property()
order = "name";

async apiEndpoint(page: number): Promise<PaginatedResponse<Application>> {
async apiEndpoint(): Promise<PaginatedResponse<Application>> {
return new CoreApi(DEFAULT_CONFIG).coreApplicationsList({
ordering: this.order,
page: page,
pageSize: (await uiConfig()).pagination.perPage,
search: this.search || "",
...(await this.defaultEndpointConfig()),
superuserFullList: true,
});
}
Expand Down
8 changes: 2 additions & 6 deletions web/src/admin/applications/ProviderSelectModal.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { DEFAULT_CONFIG } from "@goauthentik/common/api/config";
import { uiConfig } from "@goauthentik/common/ui/config";
import "@goauthentik/elements/buttons/SpinnerButton";
import { PaginatedResponse } from "@goauthentik/elements/table/Table";
import { TableColumn } from "@goauthentik/elements/table/Table";
Expand Down Expand Up @@ -28,12 +27,9 @@ export class ProviderSelectModal extends TableModal<Provider> {

order = "name";

async apiEndpoint(page: number): Promise<PaginatedResponse<Provider>> {
async apiEndpoint(): Promise<PaginatedResponse<Provider>> {
return new ProvidersApi(DEFAULT_CONFIG).providersAllList({
ordering: this.order,
page: page,
pageSize: (await uiConfig()).pagination.perPage,
search: this.search || "",
...(await this.defaultEndpointConfig()),
backchannel: this.backchannel,
});
}
Expand Down
12 changes: 4 additions & 8 deletions web/src/admin/blueprints/BlueprintListPage.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import "@goauthentik/admin/blueprints/BlueprintForm";
import { DEFAULT_CONFIG } from "@goauthentik/common/api/config";
import { EVENT_REFRESH } from "@goauthentik/common/constants";
import { uiConfig } from "@goauthentik/common/ui/config";
import { getRelativeTime } from "@goauthentik/common/utils";
import "@goauthentik/components/ak-status-label";
import "@goauthentik/elements/buttons/ActionButton";
Expand Down Expand Up @@ -68,13 +67,10 @@ export class BlueprintListPage extends TablePage<BlueprintInstance> {
return super.styles.concat(PFDescriptionList);
}

async apiEndpoint(page: number): Promise<PaginatedResponse<BlueprintInstance>> {
return new ManagedApi(DEFAULT_CONFIG).managedBlueprintsList({
ordering: this.order,
page: page,
pageSize: (await uiConfig()).pagination.perPage,
search: this.search || "",
});
async apiEndpoint(): Promise<PaginatedResponse<BlueprintInstance>> {
return new ManagedApi(DEFAULT_CONFIG).managedBlueprintsList(
await this.defaultEndpointConfig(),
);
}

columns(): TableColumn[] {
Expand Down
10 changes: 2 additions & 8 deletions web/src/admin/brands/BrandListPage.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import "@goauthentik/admin/brands/BrandForm";
import { DEFAULT_CONFIG } from "@goauthentik/common/api/config";
import { uiConfig } from "@goauthentik/common/ui/config";
import "@goauthentik/components/ak-status-label";
import "@goauthentik/components/ak-status-label";
import "@goauthentik/elements/buttons/SpinnerButton";
Expand Down Expand Up @@ -39,13 +38,8 @@ export class BrandListPage extends TablePage<Brand> {
@property()
order = "domain";

async apiEndpoint(page: number): Promise<PaginatedResponse<Brand>> {
return new CoreApi(DEFAULT_CONFIG).coreBrandsList({
ordering: this.order,
page: page,
pageSize: (await uiConfig()).pagination.perPage,
search: this.search || "",
});
async apiEndpoint(): Promise<PaginatedResponse<Brand>> {
return new CoreApi(DEFAULT_CONFIG).coreBrandsList(await this.defaultEndpointConfig());
}

columns(): TableColumn[] {
Expand Down
12 changes: 4 additions & 8 deletions web/src/admin/crypto/CertificateKeyPairListPage.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import "@goauthentik/admin/crypto/CertificateGenerateForm";
import "@goauthentik/admin/crypto/CertificateKeyPairForm";
import { DEFAULT_CONFIG } from "@goauthentik/common/api/config";
import { uiConfig } from "@goauthentik/common/ui/config";
import "@goauthentik/components/ak-status-label";
import { PFColor } from "@goauthentik/elements/Label";
import "@goauthentik/elements/buttons/SpinnerButton";
Expand Down Expand Up @@ -53,13 +52,10 @@ export class CertificateKeyPairListPage extends TablePage<CertificateKeyPair> {
return super.styles.concat(PFDescriptionList);
}

async apiEndpoint(page: number): Promise<PaginatedResponse<CertificateKeyPair>> {
return new CryptoApi(DEFAULT_CONFIG).cryptoCertificatekeypairsList({
ordering: this.order,
page: page,
pageSize: (await uiConfig()).pagination.perPage,
search: this.search || "",
});
async apiEndpoint(): Promise<PaginatedResponse<CertificateKeyPair>> {
return new CryptoApi(DEFAULT_CONFIG).cryptoCertificatekeypairsList(
await this.defaultEndpointConfig(),
);
}

columns(): TableColumn[] {
Expand Down
12 changes: 4 additions & 8 deletions web/src/admin/enterprise/EnterpriseLicenseListPage.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import "@goauthentik/admin/enterprise/EnterpriseLicenseForm";
import { DEFAULT_CONFIG } from "@goauthentik/common/api/config";
import { uiConfig } from "@goauthentik/common/ui/config";
import { getRelativeTime } from "@goauthentik/common/utils";
import { PFColor } from "@goauthentik/elements/Label";
import "@goauthentik/elements/Spinner";
Expand Down Expand Up @@ -82,18 +81,15 @@ export class EnterpriseLicenseListPage extends TablePage<License> {
);
}

async apiEndpoint(page: number): Promise<PaginatedResponse<License>> {
async apiEndpoint(): Promise<PaginatedResponse<License>> {
this.forecast = await new EnterpriseApi(DEFAULT_CONFIG).enterpriseLicenseForecastRetrieve();
this.summary = await new EnterpriseApi(DEFAULT_CONFIG).enterpriseLicenseSummaryRetrieve();
this.installID = (
await new EnterpriseApi(DEFAULT_CONFIG).enterpriseLicenseGetInstallIdRetrieve()
).installId;
return new EnterpriseApi(DEFAULT_CONFIG).enterpriseLicenseList({
ordering: this.order,
page: page,
pageSize: (await uiConfig()).pagination.perPage,
search: this.search || "",
});
return new EnterpriseApi(DEFAULT_CONFIG).enterpriseLicenseList(
await this.defaultEndpointConfig(),
);
}

columns(): TableColumn[] {
Expand Down
10 changes: 2 additions & 8 deletions web/src/admin/events/EventListPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { EventGeo, EventUser } from "@goauthentik/admin/events/utils";
import { DEFAULT_CONFIG } from "@goauthentik/common/api/config";
import { EventWithContext } from "@goauthentik/common/events";
import { actionToLabel } from "@goauthentik/common/labels";
import { uiConfig } from "@goauthentik/common/ui/config";
import { getRelativeTime } from "@goauthentik/common/utils";
import "@goauthentik/components/ak-event-info";
import { PaginatedResponse } from "@goauthentik/elements/table/Table";
Expand Down Expand Up @@ -45,13 +44,8 @@ export class EventListPage extends TablePage<Event> {
`);
}

async apiEndpoint(page: number): Promise<PaginatedResponse<Event>> {
return new EventsApi(DEFAULT_CONFIG).eventsEventsList({
ordering: this.order,
page: page,
pageSize: (await uiConfig()).pagination.perPage,
search: this.search || "",
});
async apiEndpoint(): Promise<PaginatedResponse<Event>> {
return new EventsApi(DEFAULT_CONFIG).eventsEventsList(await this.defaultEndpointConfig());
}

columns(): TableColumn[] {
Expand Down
10 changes: 2 additions & 8 deletions web/src/admin/events/RuleListPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import "@goauthentik/admin/events/RuleForm";
import "@goauthentik/admin/policies/BoundPoliciesList";
import { DEFAULT_CONFIG } from "@goauthentik/common/api/config";
import { severityToLabel } from "@goauthentik/common/labels";
import { uiConfig } from "@goauthentik/common/ui/config";
import "@goauthentik/elements/buttons/SpinnerButton";
import "@goauthentik/elements/forms/DeleteBulkForm";
import "@goauthentik/elements/forms/ModalForm";
Expand Down Expand Up @@ -47,13 +46,8 @@ export class RuleListPage extends TablePage<NotificationRule> {
@property()
order = "name";

async apiEndpoint(page: number): Promise<PaginatedResponse<NotificationRule>> {
return new EventsApi(DEFAULT_CONFIG).eventsRulesList({
ordering: this.order,
page: page,
pageSize: (await uiConfig()).pagination.perPage,
search: this.search || "",
});
async apiEndpoint(): Promise<PaginatedResponse<NotificationRule>> {
return new EventsApi(DEFAULT_CONFIG).eventsRulesList(await this.defaultEndpointConfig());
}

columns(): TableColumn[] {
Expand Down
12 changes: 4 additions & 8 deletions web/src/admin/events/TransportListPage.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import "@goauthentik/admin/events/TransportForm";
import { DEFAULT_CONFIG } from "@goauthentik/common/api/config";
import { uiConfig } from "@goauthentik/common/ui/config";
import "@goauthentik/elements/buttons/ActionButton";
import "@goauthentik/elements/buttons/SpinnerButton";
import "@goauthentik/elements/forms/DeleteBulkForm";
Expand Down Expand Up @@ -43,13 +42,10 @@ export class TransportListPage extends TablePage<NotificationTransport> {
@property()
order = "name";

async apiEndpoint(page: number): Promise<PaginatedResponse<NotificationTransport>> {
return new EventsApi(DEFAULT_CONFIG).eventsTransportsList({
ordering: this.order,
page: page,
pageSize: (await uiConfig()).pagination.perPage,
search: this.search || "",
});
async apiEndpoint(): Promise<PaginatedResponse<NotificationTransport>> {
return new EventsApi(DEFAULT_CONFIG).eventsTransportsList(
await this.defaultEndpointConfig(),
);
}

columns(): TableColumn[] {
Expand Down
7 changes: 2 additions & 5 deletions web/src/admin/flows/BoundStagesList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import "@goauthentik/admin/flows/StageBindingForm";
import "@goauthentik/admin/policies/BoundPoliciesList";
import "@goauthentik/admin/stages/StageWizard";
import { DEFAULT_CONFIG } from "@goauthentik/common/api/config";
import { uiConfig } from "@goauthentik/common/ui/config";
import "@goauthentik/elements/Tabs";
import "@goauthentik/elements/forms/DeleteBulkForm";
import "@goauthentik/elements/forms/ModalForm";
Expand All @@ -28,12 +27,10 @@ export class BoundStagesList extends Table<FlowStageBinding> {
@property()
target?: string;

async apiEndpoint(page: number): Promise<PaginatedResponse<FlowStageBinding>> {
async apiEndpoint(): Promise<PaginatedResponse<FlowStageBinding>> {
return new FlowsApi(DEFAULT_CONFIG).flowsBindingsList({
...(await this.defaultEndpointConfig()),
target: this.target || "",
ordering: this.order,
page: page,
pageSize: (await uiConfig()).pagination.perPage,
});
}

Expand Down
10 changes: 2 additions & 8 deletions web/src/admin/flows/FlowListPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import "@goauthentik/admin/flows/FlowForm";
import "@goauthentik/admin/flows/FlowImportForm";
import { DesignationToLabel } from "@goauthentik/admin/flows/utils";
import { AndNext, DEFAULT_CONFIG } from "@goauthentik/common/api/config";
import { uiConfig } from "@goauthentik/common/ui/config";
import { groupBy } from "@goauthentik/common/utils";
import "@goauthentik/elements/buttons/SpinnerButton";
import "@goauthentik/elements/forms/ConfirmationForm";
Expand Down Expand Up @@ -42,13 +41,8 @@ export class FlowListPage extends TablePage<Flow> {
@property()
order = "slug";

async apiEndpoint(page: number): Promise<PaginatedResponse<Flow>> {
return new FlowsApi(DEFAULT_CONFIG).flowsInstancesList({
ordering: this.order,
page: page,
pageSize: (await uiConfig()).pagination.perPage,
search: this.search || "",
});
async apiEndpoint(): Promise<PaginatedResponse<Flow>> {
return new FlowsApi(DEFAULT_CONFIG).flowsInstancesList(await this.defaultEndpointConfig());
}

groupBy(items: Flow[]): [string, Flow[]][] {
Expand Down
8 changes: 2 additions & 6 deletions web/src/admin/groups/GroupListPage.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import "@goauthentik/admin/groups/GroupForm";
import { DEFAULT_CONFIG } from "@goauthentik/common/api/config";
import { uiConfig } from "@goauthentik/common/ui/config";
import "@goauthentik/components/ak-status-label";
import "@goauthentik/elements/buttons/SpinnerButton";
import "@goauthentik/elements/forms/DeleteBulkForm";
Expand Down Expand Up @@ -36,12 +35,9 @@ export class GroupListPage extends TablePage<Group> {
@property()
order = "name";

async apiEndpoint(page: number): Promise<PaginatedResponse<Group>> {
async apiEndpoint(): Promise<PaginatedResponse<Group>> {
return new CoreApi(DEFAULT_CONFIG).coreGroupsList({
ordering: this.order,
page: page,
pageSize: (await uiConfig()).pagination.perPage,
search: this.search || "",
...(await this.defaultEndpointConfig()),
includeUsers: false,
});
}
Expand Down
8 changes: 2 additions & 6 deletions web/src/admin/groups/MemberSelectModal.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { DEFAULT_CONFIG } from "@goauthentik/common/api/config";
import { uiConfig } from "@goauthentik/common/ui/config";
import { getRelativeTime } from "@goauthentik/common/utils";
import "@goauthentik/components/ak-status-label";
import "@goauthentik/elements/buttons/SpinnerButton";
Expand Down Expand Up @@ -27,12 +26,9 @@ export class MemberSelectTable extends TableModal<User> {

order = "username";

async apiEndpoint(page: number): Promise<PaginatedResponse<User>> {
async apiEndpoint(): Promise<PaginatedResponse<User>> {
return new CoreApi(DEFAULT_CONFIG).coreUsersList({
ordering: this.order,
page: page,
pageSize: (await uiConfig()).pagination.perPage,
search: this.search || "",
...(await this.defaultEndpointConfig()),
includeGroups: false,
});
}
Expand Down
8 changes: 2 additions & 6 deletions web/src/admin/groups/RelatedGroupList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import "@goauthentik/admin/groups/GroupForm";
import "@goauthentik/admin/groups/GroupForm";
import "@goauthentik/admin/users/GroupSelectModal";
import { DEFAULT_CONFIG } from "@goauthentik/common/api/config";
import { uiConfig } from "@goauthentik/common/ui/config";
import "@goauthentik/components/ak-status-label";
import "@goauthentik/elements/buttons/SpinnerButton";
import "@goauthentik/elements/forms/DeleteBulkForm";
Expand Down Expand Up @@ -98,12 +97,9 @@ export class RelatedGroupList extends Table<Group> {
@property({ attribute: false })
targetUser?: User;

async apiEndpoint(page: number): Promise<PaginatedResponse<Group>> {
async apiEndpoint(): Promise<PaginatedResponse<Group>> {
return new CoreApi(DEFAULT_CONFIG).coreGroupsList({
ordering: this.order,
page: page,
pageSize: (await uiConfig()).pagination.perPage,
search: this.search || "",
...(await this.defaultEndpointConfig()),
membersByPk: this.targetUser ? [this.targetUser.pk] : [],
includeUsers: false,
});
Expand Down
8 changes: 2 additions & 6 deletions web/src/admin/groups/RelatedUserList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import "@goauthentik/admin/users/UserPasswordForm";
import "@goauthentik/admin/users/UserResetEmailForm";
import { DEFAULT_CONFIG } from "@goauthentik/common/api/config";
import { MessageLevel } from "@goauthentik/common/messages";
import { uiConfig } from "@goauthentik/common/ui/config";
import { me } from "@goauthentik/common/users";
import { getRelativeTime } from "@goauthentik/common/utils";
import "@goauthentik/components/ak-status-label";
Expand Down Expand Up @@ -135,12 +134,9 @@ export class RelatedUserList extends WithBrandConfig(WithCapabilitiesConfig(Tabl
return super.styles.concat(PFDescriptionList, PFAlert, PFBanner);
}

async apiEndpoint(page: number): Promise<PaginatedResponse<User>> {
async apiEndpoint(): Promise<PaginatedResponse<User>> {
const users = await new CoreApi(DEFAULT_CONFIG).coreUsersList({
ordering: this.order,
page: page,
pageSize: (await uiConfig()).pagination.perPage,
search: this.search || "",
...(await this.defaultEndpointConfig()),
groupsByPk: this.targetGroup ? [this.targetGroup.pk] : [],
type: this.hideServiceAccounts
? [CoreUsersListTypeEnum.External, CoreUsersListTypeEnum.Internal]
Expand Down
12 changes: 4 additions & 8 deletions web/src/admin/outposts/OutpostListPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import "@goauthentik/admin/outposts/OutpostHealth";
import "@goauthentik/admin/outposts/OutpostHealthSimple";
import { DEFAULT_CONFIG } from "@goauthentik/common/api/config";
import { PFSize } from "@goauthentik/common/enums.js";
import { uiConfig } from "@goauthentik/common/ui/config";
import { PFColor } from "@goauthentik/elements/Label";
import "@goauthentik/elements/buttons/SpinnerButton";
import "@goauthentik/elements/forms/DeleteBulkForm";
Expand Down Expand Up @@ -67,13 +66,10 @@ export class OutpostListPage extends TablePage<Outpost> {
return true;
}

async apiEndpoint(page: number): Promise<PaginatedResponse<Outpost>> {
const outposts = await new OutpostsApi(DEFAULT_CONFIG).outpostsInstancesList({
ordering: this.order,
page: page,
pageSize: (await uiConfig()).pagination.perPage,
search: this.search || "",
});
async apiEndpoint(): Promise<PaginatedResponse<Outpost>> {
const outposts = await new OutpostsApi(DEFAULT_CONFIG).outpostsInstancesList(
await this.defaultEndpointConfig(),
);
Promise.all(
outposts.results.map((outpost) => {
return new OutpostsApi(DEFAULT_CONFIG)
Expand Down
Loading

0 comments on commit 453f7b8

Please sign in to comment.