From ffed653caecf06984d60055a4e76b7d825e27d56 Mon Sep 17 00:00:00 2001 From: Jens L Date: Mon, 2 Jan 2023 16:13:07 +0100 Subject: [PATCH] web/admin: migrate api calls to async (#4335) migrate api calls to async Signed-off-by: Jens Langhammer Signed-off-by: Jens Langhammer --- .../charts/AdminLoginAuthorizeChart.ts | 2 +- .../ApplicationCheckAccessForm.ts | 13 ++--- web/src/admin/flows/FlowImportForm.ts | 22 +++----- web/src/admin/groups/RelatedGroupList.ts | 9 ++- web/src/admin/policies/PolicyTestForm.ts | 13 ++--- .../PropertyMappingTestForm.ts | 15 +++-- .../providers/saml/SAMLProviderImportForm.ts | 1 - web/src/admin/users/RelatedUserList.ts | 9 ++- web/src/admin/users/ServiceAccountForm.ts | 17 +++--- web/src/common/ui/locale.ts | 3 +- web/src/elements/CodeMirror.ts | 11 ++-- web/src/elements/charts/AdminModelPerDay.ts | 2 +- .../charts/ApplicationAuthorizeChart.ts | 2 +- web/src/elements/charts/UserChart.ts | 2 +- web/src/elements/forms/DeleteBulkForm.ts | 56 +++++++++---------- web/src/elements/forms/Radio.ts | 3 +- web/src/elements/table/TablePagination.ts | 5 +- web/src/elements/user/UserDevicesList.ts | 3 +- web/src/elements/wizard/WizardFormPage.ts | 3 +- .../user/user-settings/mfa/MFADevicesPage.ts | 3 +- 20 files changed, 85 insertions(+), 109 deletions(-) diff --git a/web/src/admin/admin-overview/charts/AdminLoginAuthorizeChart.ts b/web/src/admin/admin-overview/charts/AdminLoginAuthorizeChart.ts index 76f380bcf..7df331ff0 100644 --- a/web/src/admin/admin-overview/charts/AdminLoginAuthorizeChart.ts +++ b/web/src/admin/admin-overview/charts/AdminLoginAuthorizeChart.ts @@ -10,7 +10,7 @@ import { AdminApi, LoginMetrics } from "@goauthentik/api"; @customElement("ak-charts-admin-login-authorization") export class AdminLoginAuthorizeChart extends AKChart { - apiRequest(): Promise { + async apiRequest(): Promise { return new AdminApi(DEFAULT_CONFIG).adminMetricsRetrieve(); } diff --git a/web/src/admin/applications/ApplicationCheckAccessForm.ts b/web/src/admin/applications/ApplicationCheckAccessForm.ts index b2c7e8ab9..ea5f6937c 100644 --- a/web/src/admin/applications/ApplicationCheckAccessForm.ts +++ b/web/src/admin/applications/ApplicationCheckAccessForm.ts @@ -34,14 +34,13 @@ export class ApplicationCheckAccessForm extends Form<{ forUser: number }> { return t`Successfully sent test-request.`; } - send = (data: { forUser: number }): Promise => { + send = async (data: { forUser: number }): Promise => { this.request = data.forUser; - return new CoreApi(DEFAULT_CONFIG) - .coreApplicationsCheckAccessRetrieve({ - slug: this.application?.slug, - forUser: data.forUser, - }) - .then((result) => (this.result = result)); + const result = await new CoreApi(DEFAULT_CONFIG).coreApplicationsCheckAccessRetrieve({ + slug: this.application?.slug, + forUser: data.forUser, + }); + return (this.result = result); }; resetForm(): void { diff --git a/web/src/admin/flows/FlowImportForm.ts b/web/src/admin/flows/FlowImportForm.ts index 6c966f581..8f576abbf 100644 --- a/web/src/admin/flows/FlowImportForm.ts +++ b/web/src/admin/flows/FlowImportForm.ts @@ -26,23 +26,19 @@ export class FlowImportForm extends Form { return super.styles.concat(PFDescriptionList); } - // eslint-disable-next-line - send = (data: Flow): Promise => { + send = async (): Promise => { const file = this.getFormFiles()["flow"]; if (!file) { throw new SentryIgnoredError("No form data"); } - return new FlowsApi(DEFAULT_CONFIG) - .flowsInstancesImportCreate({ - file: file, - }) - .then((result) => { - if (!result.success) { - this.result = result; - throw new SentryIgnoredError("Failed to import flow"); - } - return result; - }); + const result = await new FlowsApi(DEFAULT_CONFIG).flowsInstancesImportCreate({ + file: file, + }); + if (!result.success) { + this.result = result; + throw new SentryIgnoredError("Failed to import flow"); + } + return result; }; renderResult(): TemplateResult { diff --git a/web/src/admin/groups/RelatedGroupList.ts b/web/src/admin/groups/RelatedGroupList.ts index 06f35a90d..70996f500 100644 --- a/web/src/admin/groups/RelatedGroupList.ts +++ b/web/src/admin/groups/RelatedGroupList.ts @@ -32,8 +32,8 @@ export class RelatedGroupAdd extends Form<{ groups: string[] }> { return t`Successfully added user to group(s).`; } - send = (data: { groups: string[] }): Promise<{ groups: string[] }> => { - return Promise.all( + send = async (data: { groups: string[] }): Promise<{ groups: string[] }> => { + await Promise.all( data.groups.map((group) => { return new CoreApi(DEFAULT_CONFIG).coreGroupsAddUserCreate({ groupUuid: group, @@ -42,9 +42,8 @@ export class RelatedGroupAdd extends Form<{ groups: string[] }> { }, }); }), - ).then(() => { - return data; - }); + ); + return data; }; renderForm(): TemplateResult { diff --git a/web/src/admin/policies/PolicyTestForm.ts b/web/src/admin/policies/PolicyTestForm.ts index 157369dfc..9549a6389 100644 --- a/web/src/admin/policies/PolicyTestForm.ts +++ b/web/src/admin/policies/PolicyTestForm.ts @@ -39,14 +39,13 @@ export class PolicyTestForm extends Form { return t`Successfully sent test-request.`; } - send = (data: PolicyTestRequest): Promise => { + send = async (data: PolicyTestRequest): Promise => { this.request = data; - return new PoliciesApi(DEFAULT_CONFIG) - .policiesAllTestCreate({ - policyUuid: this.policy?.pk || "", - policyTestRequest: data, - }) - .then((result) => (this.result = result)); + const result = await new PoliciesApi(DEFAULT_CONFIG).policiesAllTestCreate({ + policyUuid: this.policy?.pk || "", + policyTestRequest: data, + }); + return (this.result = result); }; static get styles(): CSSResult[] { diff --git a/web/src/admin/property-mappings/PropertyMappingTestForm.ts b/web/src/admin/property-mappings/PropertyMappingTestForm.ts index 16dfa6b2f..d0108e993 100644 --- a/web/src/admin/property-mappings/PropertyMappingTestForm.ts +++ b/web/src/admin/property-mappings/PropertyMappingTestForm.ts @@ -37,15 +37,14 @@ export class PolicyTestForm extends Form { return t`Successfully sent test-request.`; } - send = (data: PolicyTestRequest): Promise => { + send = async (data: PolicyTestRequest): Promise => { this.request = data; - return new PropertymappingsApi(DEFAULT_CONFIG) - .propertymappingsAllTestCreate({ - pmUuid: this.mapping?.pk || "", - policyTestRequest: data, - formatResult: true, - }) - .then((result) => (this.result = result)); + const result = await new PropertymappingsApi(DEFAULT_CONFIG).propertymappingsAllTestCreate({ + pmUuid: this.mapping?.pk || "", + policyTestRequest: data, + formatResult: true, + }); + return (this.result = result); }; renderResult(): TemplateResult { diff --git a/web/src/admin/providers/saml/SAMLProviderImportForm.ts b/web/src/admin/providers/saml/SAMLProviderImportForm.ts index a18e97d76..9b7a9d359 100644 --- a/web/src/admin/providers/saml/SAMLProviderImportForm.ts +++ b/web/src/admin/providers/saml/SAMLProviderImportForm.ts @@ -24,7 +24,6 @@ export class SAMLProviderImportForm extends Form { return t`Successfully imported provider.`; } - // eslint-disable-next-line send = (data: SAMLProvider): Promise => { const file = this.getFormFiles()["metadata"]; if (!file) { diff --git a/web/src/admin/users/RelatedUserList.ts b/web/src/admin/users/RelatedUserList.ts index e2ec9461f..eeedfabf6 100644 --- a/web/src/admin/users/RelatedUserList.ts +++ b/web/src/admin/users/RelatedUserList.ts @@ -44,8 +44,8 @@ export class RelatedUserAdd extends Form<{ users: number[] }> { return t`Successfully added user(s).`; } - send = (data: { users: number[] }): Promise<{ users: number[] }> => { - return Promise.all( + send = async (data: { users: number[] }): Promise<{ users: number[] }> => { + await Promise.all( data.users.map((user) => { return new CoreApi(DEFAULT_CONFIG).coreGroupsAddUserCreate({ groupUuid: this.group?.pk || "", @@ -54,9 +54,8 @@ export class RelatedUserAdd extends Form<{ users: number[] }> { }, }); }), - ).then(() => { - return data; - }); + ); + return data; }; renderForm(): TemplateResult { diff --git a/web/src/admin/users/ServiceAccountForm.ts b/web/src/admin/users/ServiceAccountForm.ts index ba0f50a33..d3e6cc500 100644 --- a/web/src/admin/users/ServiceAccountForm.ts +++ b/web/src/admin/users/ServiceAccountForm.ts @@ -20,16 +20,13 @@ export class ServiceAccountForm extends Form { return t`Successfully created user.`; } - send = (data: UserServiceAccountRequest): Promise => { - return new CoreApi(DEFAULT_CONFIG) - .coreUsersServiceAccountCreate({ - userServiceAccountRequest: data, - }) - .then((result) => { - this.result = result; - (this.parentElement as ModalForm).showSubmitButton = false; - return result; - }); + send = async (data: UserServiceAccountRequest): Promise => { + const result = await new CoreApi(DEFAULT_CONFIG).coreUsersServiceAccountCreate({ + userServiceAccountRequest: data, + }); + this.result = result; + (this.parentElement as ModalForm).showSubmitButton = false; + return result; }; resetForm(): void { diff --git a/web/src/common/ui/locale.ts b/web/src/common/ui/locale.ts index 8b6ee9194..2faf515fb 100644 --- a/web/src/common/ui/locale.ts +++ b/web/src/common/ui/locale.ts @@ -7,8 +7,7 @@ import { t } from "@lingui/macro"; interface Locale { locale: Messages; - // eslint-disable-next-line @typescript-eslint/ban-types - plurals: Function; + plurals: (n: string | number, ord?: boolean | undefined) => string; } export const LOCALES: { diff --git a/web/src/elements/CodeMirror.ts b/web/src/elements/CodeMirror.ts index 08b4dc581..cd12db95a 100644 --- a/web/src/elements/CodeMirror.ts +++ b/web/src/elements/CodeMirror.ts @@ -18,7 +18,7 @@ import YAML from "yaml"; import { customElement, property } from "lit/decorators.js"; @customElement("ak-codemirror") -export class CodeMirrorTextarea extends AKElement { +export class CodeMirrorTextarea extends AKElement { @property({ type: Boolean }) readOnly = false; @@ -39,7 +39,7 @@ export class CodeMirrorTextarea extends AKElement { @property() // eslint-disable-next-line @typescript-eslint/no-explicit-any,@typescript-eslint/explicit-module-boundary-types - set value(v: any) { + set value(v: T | string) { if (v === null || v === undefined) return; // Value might be an object if within an iron-form, as that calls the getter of value // in the beginning and the calls this setter on reset @@ -59,15 +59,14 @@ export class CodeMirrorTextarea extends AKElement { } if (this.editor) { this.editor.dispatch({ - changes: { from: 0, to: this.editor.state.doc.length, insert: textValue }, + changes: { from: 0, to: this.editor.state.doc.length, insert: textValue as string }, }); } else { - this._value = textValue; + this._value = textValue as string; } } - // eslint-disable-next-line @typescript-eslint/no-explicit-any - get value(): any { + get value(): T | string { try { switch (this.mode.toLowerCase()) { case "yaml": diff --git a/web/src/elements/charts/AdminModelPerDay.ts b/web/src/elements/charts/AdminModelPerDay.ts index 12c20e393..2ba6605c9 100644 --- a/web/src/elements/charts/AdminModelPerDay.ts +++ b/web/src/elements/charts/AdminModelPerDay.ts @@ -16,7 +16,7 @@ export class AdminModelPerDay extends AKChart { @property({ attribute: false }) query?: { [key: string]: unknown } | undefined; - apiRequest(): Promise { + async apiRequest(): Promise { return new EventsApi(DEFAULT_CONFIG).eventsEventsPerMonthList({ action: this.action, query: JSON.stringify(this.query || {}), diff --git a/web/src/elements/charts/ApplicationAuthorizeChart.ts b/web/src/elements/charts/ApplicationAuthorizeChart.ts index cf70db6f3..b502a5788 100644 --- a/web/src/elements/charts/ApplicationAuthorizeChart.ts +++ b/web/src/elements/charts/ApplicationAuthorizeChart.ts @@ -13,7 +13,7 @@ export class ApplicationAuthorizeChart extends AKChart { @property() applicationSlug!: string; - apiRequest(): Promise { + async apiRequest(): Promise { return new CoreApi(DEFAULT_CONFIG).coreApplicationsMetricsList({ slug: this.applicationSlug, }); diff --git a/web/src/elements/charts/UserChart.ts b/web/src/elements/charts/UserChart.ts index bad3ff02d..356ab4c74 100644 --- a/web/src/elements/charts/UserChart.ts +++ b/web/src/elements/charts/UserChart.ts @@ -13,7 +13,7 @@ export class UserChart extends AKChart { @property({ type: Number }) userId?: number; - apiRequest(): Promise { + async apiRequest(): Promise { return new CoreApi(DEFAULT_CONFIG).coreUsersMetricsRetrieve({ id: this.userId || 0, }); diff --git a/web/src/elements/forms/DeleteBulkForm.ts b/web/src/elements/forms/DeleteBulkForm.ts index 3b87569a6..1fc7eb29b 100644 --- a/web/src/elements/forms/DeleteBulkForm.ts +++ b/web/src/elements/forms/DeleteBulkForm.ts @@ -40,8 +40,7 @@ export class DeleteObjectsTable extends Table { return super.styles.concat(PFList); } - // eslint-disable-next-line @typescript-eslint/no-unused-vars - async apiEndpoint(page: number): Promise> { + async apiEndpoint(): Promise> { return Promise.resolve({ pagination: { count: this.objects.length, @@ -114,10 +113,9 @@ export class DeleteObjectsTable extends Table { } @customElement("ak-forms-delete-bulk") -export class DeleteBulkForm extends ModalButton { +export class DeleteBulkForm extends ModalButton { @property({ attribute: false }) - // eslint-disable-next-line @typescript-eslint/no-explicit-any - objects: any[] = []; + objects: T[] = []; @property() objectLabel?: string; @@ -129,8 +127,7 @@ export class DeleteBulkForm extends ModalButton { actionSubtext?: string; @property({ attribute: false }) - // eslint-disable-next-line @typescript-eslint/no-explicit-any - metadata: (item: any) => BulkDeleteMetadata = (item: any) => { + metadata: (item: T) => BulkDeleteMetadata = (item: T) => { const rec = item as Record; const meta = []; if (Object.prototype.hasOwnProperty.call(rec, "name")) { @@ -143,33 +140,30 @@ export class DeleteBulkForm extends ModalButton { }; @property({ attribute: false }) - // eslint-disable-next-line @typescript-eslint/no-explicit-any - usedBy?: (item: any) => Promise; + usedBy?: (item: T) => Promise; @property({ attribute: false }) - // eslint-disable-next-line @typescript-eslint/no-explicit-any - delete!: (item: any) => Promise; + delete!: (item: T) => Promise; - confirm(): Promise { - return Promise.all( - this.objects.map((item) => { - return this.delete(item); - }), - ) - .then(() => { - this.onSuccess(); - this.open = false; - this.dispatchEvent( - new CustomEvent(EVENT_REFRESH, { - bubbles: true, - composed: true, - }), - ); - }) - .catch((e) => { - this.onError(e); - throw e; - }); + async confirm(): Promise { + try { + await Promise.all( + this.objects.map((item) => { + return this.delete(item); + }), + ); + this.onSuccess(); + this.open = false; + this.dispatchEvent( + new CustomEvent(EVENT_REFRESH, { + bubbles: true, + composed: true, + }), + ); + } catch (e) { + this.onError(e as Error); + throw e; + } } onSuccess(): void { diff --git a/web/src/elements/forms/Radio.ts b/web/src/elements/forms/Radio.ts index eba11dd15..0f028cf40 100644 --- a/web/src/elements/forms/Radio.ts +++ b/web/src/elements/forms/Radio.ts @@ -27,8 +27,7 @@ export class Radio extends AKElement { value?: T; @property({ attribute: false }) - // eslint-disable-next-line @typescript-eslint/no-unused-vars - onChange: (value: T) => void = (value: T) => { + onChange: (value: T) => void = () => { return; }; diff --git a/web/src/elements/table/TablePagination.ts b/web/src/elements/table/TablePagination.ts index c0e076379..ce0565181 100644 --- a/web/src/elements/table/TablePagination.ts +++ b/web/src/elements/table/TablePagination.ts @@ -28,8 +28,9 @@ export class TablePagination extends AKElement { pages?: Pagination; @property({ attribute: false }) - // eslint-disable-next-line - pageChangeHandler: (page: number) => void = (page: number) => {}; + pageChangeHandler: (page: number) => void = () => { + return; + }; static get styles(): CSSResult[] { return [PFBase, PFButton, PFPagination, AKGlobal]; diff --git a/web/src/elements/user/UserDevicesList.ts b/web/src/elements/user/UserDevicesList.ts index e57a8a223..872cfcfe0 100644 --- a/web/src/elements/user/UserDevicesList.ts +++ b/web/src/elements/user/UserDevicesList.ts @@ -16,8 +16,7 @@ export class UserDeviceList extends MFADevicesPage { @property({ type: Number }) userId?: number; - // eslint-disable-next-line @typescript-eslint/no-unused-vars - async apiEndpoint(page: number): Promise> { + async apiEndpoint(): Promise> { return new AuthenticatorsApi(DEFAULT_CONFIG) .authenticatorsAdminAllList({ user: this.userId, diff --git a/web/src/elements/wizard/WizardFormPage.ts b/web/src/elements/wizard/WizardFormPage.ts index 85acc4b25..5c42349c1 100644 --- a/web/src/elements/wizard/WizardFormPage.ts +++ b/web/src/elements/wizard/WizardFormPage.ts @@ -58,8 +58,7 @@ export class WizardFormPage extends WizardPage { return response; }; - // eslint-disable-next-line @typescript-eslint/no-unused-vars - nextDataCallback: (data: KeyUnknown) => Promise = async (data): Promise => { + nextDataCallback: (data: KeyUnknown) => Promise = async (): Promise => { return false; }; diff --git a/web/src/user/user-settings/mfa/MFADevicesPage.ts b/web/src/user/user-settings/mfa/MFADevicesPage.ts index 93c22f2d9..8e69a545d 100644 --- a/web/src/user/user-settings/mfa/MFADevicesPage.ts +++ b/web/src/user/user-settings/mfa/MFADevicesPage.ts @@ -51,8 +51,7 @@ export class MFADevicesPage extends Table { checkbox = true; - // eslint-disable-next-line @typescript-eslint/no-unused-vars - async apiEndpoint(page: number): Promise> { + async apiEndpoint(): Promise> { const devices = await new AuthenticatorsApi(DEFAULT_CONFIG).authenticatorsAllList(); return { pagination: {