web/admin: migrate api calls to async (#4335)

migrate api calls to async

Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>

Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
Jens L 2023-01-02 16:13:07 +01:00 committed by GitHub
parent ba5cd6e719
commit ffed653cae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 85 additions and 109 deletions

View File

@ -10,7 +10,7 @@ import { AdminApi, LoginMetrics } from "@goauthentik/api";
@customElement("ak-charts-admin-login-authorization") @customElement("ak-charts-admin-login-authorization")
export class AdminLoginAuthorizeChart extends AKChart<LoginMetrics> { export class AdminLoginAuthorizeChart extends AKChart<LoginMetrics> {
apiRequest(): Promise<LoginMetrics> { async apiRequest(): Promise<LoginMetrics> {
return new AdminApi(DEFAULT_CONFIG).adminMetricsRetrieve(); return new AdminApi(DEFAULT_CONFIG).adminMetricsRetrieve();
} }

View File

@ -34,14 +34,13 @@ export class ApplicationCheckAccessForm extends Form<{ forUser: number }> {
return t`Successfully sent test-request.`; return t`Successfully sent test-request.`;
} }
send = (data: { forUser: number }): Promise<PolicyTestResult> => { send = async (data: { forUser: number }): Promise<PolicyTestResult> => {
this.request = data.forUser; this.request = data.forUser;
return new CoreApi(DEFAULT_CONFIG) const result = await new CoreApi(DEFAULT_CONFIG).coreApplicationsCheckAccessRetrieve({
.coreApplicationsCheckAccessRetrieve({ slug: this.application?.slug,
slug: this.application?.slug, forUser: data.forUser,
forUser: data.forUser, });
}) return (this.result = result);
.then((result) => (this.result = result));
}; };
resetForm(): void { resetForm(): void {

View File

@ -26,23 +26,19 @@ export class FlowImportForm extends Form<Flow> {
return super.styles.concat(PFDescriptionList); return super.styles.concat(PFDescriptionList);
} }
// eslint-disable-next-line send = async (): Promise<FlowImportResult> => {
send = (data: Flow): Promise<FlowImportResult> => {
const file = this.getFormFiles()["flow"]; const file = this.getFormFiles()["flow"];
if (!file) { if (!file) {
throw new SentryIgnoredError("No form data"); throw new SentryIgnoredError("No form data");
} }
return new FlowsApi(DEFAULT_CONFIG) const result = await new FlowsApi(DEFAULT_CONFIG).flowsInstancesImportCreate({
.flowsInstancesImportCreate({ file: file,
file: file, });
}) if (!result.success) {
.then((result) => { this.result = result;
if (!result.success) { throw new SentryIgnoredError("Failed to import flow");
this.result = result; }
throw new SentryIgnoredError("Failed to import flow"); return result;
}
return result;
});
}; };
renderResult(): TemplateResult { renderResult(): TemplateResult {

View File

@ -32,8 +32,8 @@ export class RelatedGroupAdd extends Form<{ groups: string[] }> {
return t`Successfully added user to group(s).`; return t`Successfully added user to group(s).`;
} }
send = (data: { groups: string[] }): Promise<{ groups: string[] }> => { send = async (data: { groups: string[] }): Promise<{ groups: string[] }> => {
return Promise.all( await Promise.all(
data.groups.map((group) => { data.groups.map((group) => {
return new CoreApi(DEFAULT_CONFIG).coreGroupsAddUserCreate({ return new CoreApi(DEFAULT_CONFIG).coreGroupsAddUserCreate({
groupUuid: group, groupUuid: group,
@ -42,9 +42,8 @@ export class RelatedGroupAdd extends Form<{ groups: string[] }> {
}, },
}); });
}), }),
).then(() => { );
return data; return data;
});
}; };
renderForm(): TemplateResult { renderForm(): TemplateResult {

View File

@ -39,14 +39,13 @@ export class PolicyTestForm extends Form<PolicyTestRequest> {
return t`Successfully sent test-request.`; return t`Successfully sent test-request.`;
} }
send = (data: PolicyTestRequest): Promise<PolicyTestResult> => { send = async (data: PolicyTestRequest): Promise<PolicyTestResult> => {
this.request = data; this.request = data;
return new PoliciesApi(DEFAULT_CONFIG) const result = await new PoliciesApi(DEFAULT_CONFIG).policiesAllTestCreate({
.policiesAllTestCreate({ policyUuid: this.policy?.pk || "",
policyUuid: this.policy?.pk || "", policyTestRequest: data,
policyTestRequest: data, });
}) return (this.result = result);
.then((result) => (this.result = result));
}; };
static get styles(): CSSResult[] { static get styles(): CSSResult[] {

View File

@ -37,15 +37,14 @@ export class PolicyTestForm extends Form<PolicyTestRequest> {
return t`Successfully sent test-request.`; return t`Successfully sent test-request.`;
} }
send = (data: PolicyTestRequest): Promise<PropertyMappingTestResult> => { send = async (data: PolicyTestRequest): Promise<PropertyMappingTestResult> => {
this.request = data; this.request = data;
return new PropertymappingsApi(DEFAULT_CONFIG) const result = await new PropertymappingsApi(DEFAULT_CONFIG).propertymappingsAllTestCreate({
.propertymappingsAllTestCreate({ pmUuid: this.mapping?.pk || "",
pmUuid: this.mapping?.pk || "", policyTestRequest: data,
policyTestRequest: data, formatResult: true,
formatResult: true, });
}) return (this.result = result);
.then((result) => (this.result = result));
}; };
renderResult(): TemplateResult { renderResult(): TemplateResult {

View File

@ -24,7 +24,6 @@ export class SAMLProviderImportForm extends Form<SAMLProvider> {
return t`Successfully imported provider.`; return t`Successfully imported provider.`;
} }
// eslint-disable-next-line
send = (data: SAMLProvider): Promise<void> => { send = (data: SAMLProvider): Promise<void> => {
const file = this.getFormFiles()["metadata"]; const file = this.getFormFiles()["metadata"];
if (!file) { if (!file) {

View File

@ -44,8 +44,8 @@ export class RelatedUserAdd extends Form<{ users: number[] }> {
return t`Successfully added user(s).`; return t`Successfully added user(s).`;
} }
send = (data: { users: number[] }): Promise<{ users: number[] }> => { send = async (data: { users: number[] }): Promise<{ users: number[] }> => {
return Promise.all( await Promise.all(
data.users.map((user) => { data.users.map((user) => {
return new CoreApi(DEFAULT_CONFIG).coreGroupsAddUserCreate({ return new CoreApi(DEFAULT_CONFIG).coreGroupsAddUserCreate({
groupUuid: this.group?.pk || "", groupUuid: this.group?.pk || "",
@ -54,9 +54,8 @@ export class RelatedUserAdd extends Form<{ users: number[] }> {
}, },
}); });
}), }),
).then(() => { );
return data; return data;
});
}; };
renderForm(): TemplateResult { renderForm(): TemplateResult {

View File

@ -20,16 +20,13 @@ export class ServiceAccountForm extends Form<UserServiceAccountRequest> {
return t`Successfully created user.`; return t`Successfully created user.`;
} }
send = (data: UserServiceAccountRequest): Promise<UserServiceAccountResponse> => { send = async (data: UserServiceAccountRequest): Promise<UserServiceAccountResponse> => {
return new CoreApi(DEFAULT_CONFIG) const result = await new CoreApi(DEFAULT_CONFIG).coreUsersServiceAccountCreate({
.coreUsersServiceAccountCreate({ userServiceAccountRequest: data,
userServiceAccountRequest: data, });
}) this.result = result;
.then((result) => { (this.parentElement as ModalForm).showSubmitButton = false;
this.result = result; return result;
(this.parentElement as ModalForm).showSubmitButton = false;
return result;
});
}; };
resetForm(): void { resetForm(): void {

View File

@ -7,8 +7,7 @@ import { t } from "@lingui/macro";
interface Locale { interface Locale {
locale: Messages; locale: Messages;
// eslint-disable-next-line @typescript-eslint/ban-types plurals: (n: string | number, ord?: boolean | undefined) => string;
plurals: Function;
} }
export const LOCALES: { export const LOCALES: {

View File

@ -18,7 +18,7 @@ import YAML from "yaml";
import { customElement, property } from "lit/decorators.js"; import { customElement, property } from "lit/decorators.js";
@customElement("ak-codemirror") @customElement("ak-codemirror")
export class CodeMirrorTextarea extends AKElement { export class CodeMirrorTextarea<T> extends AKElement {
@property({ type: Boolean }) @property({ type: Boolean })
readOnly = false; readOnly = false;
@ -39,7 +39,7 @@ export class CodeMirrorTextarea extends AKElement {
@property() @property()
// eslint-disable-next-line @typescript-eslint/no-explicit-any,@typescript-eslint/explicit-module-boundary-types // 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; if (v === null || v === undefined) return;
// Value might be an object if within an iron-form, as that calls the getter of value // 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 // in the beginning and the calls this setter on reset
@ -59,15 +59,14 @@ export class CodeMirrorTextarea extends AKElement {
} }
if (this.editor) { if (this.editor) {
this.editor.dispatch({ 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 { } else {
this._value = textValue; this._value = textValue as string;
} }
} }
// eslint-disable-next-line @typescript-eslint/no-explicit-any get value(): T | string {
get value(): any {
try { try {
switch (this.mode.toLowerCase()) { switch (this.mode.toLowerCase()) {
case "yaml": case "yaml":

View File

@ -16,7 +16,7 @@ export class AdminModelPerDay extends AKChart<Coordinate[]> {
@property({ attribute: false }) @property({ attribute: false })
query?: { [key: string]: unknown } | undefined; query?: { [key: string]: unknown } | undefined;
apiRequest(): Promise<Coordinate[]> { async apiRequest(): Promise<Coordinate[]> {
return new EventsApi(DEFAULT_CONFIG).eventsEventsPerMonthList({ return new EventsApi(DEFAULT_CONFIG).eventsEventsPerMonthList({
action: this.action, action: this.action,
query: JSON.stringify(this.query || {}), query: JSON.stringify(this.query || {}),

View File

@ -13,7 +13,7 @@ export class ApplicationAuthorizeChart extends AKChart<Coordinate[]> {
@property() @property()
applicationSlug!: string; applicationSlug!: string;
apiRequest(): Promise<Coordinate[]> { async apiRequest(): Promise<Coordinate[]> {
return new CoreApi(DEFAULT_CONFIG).coreApplicationsMetricsList({ return new CoreApi(DEFAULT_CONFIG).coreApplicationsMetricsList({
slug: this.applicationSlug, slug: this.applicationSlug,
}); });

View File

@ -13,7 +13,7 @@ export class UserChart extends AKChart<UserMetrics> {
@property({ type: Number }) @property({ type: Number })
userId?: number; userId?: number;
apiRequest(): Promise<UserMetrics> { async apiRequest(): Promise<UserMetrics> {
return new CoreApi(DEFAULT_CONFIG).coreUsersMetricsRetrieve({ return new CoreApi(DEFAULT_CONFIG).coreUsersMetricsRetrieve({
id: this.userId || 0, id: this.userId || 0,
}); });

View File

@ -40,8 +40,7 @@ export class DeleteObjectsTable<T> extends Table<T> {
return super.styles.concat(PFList); return super.styles.concat(PFList);
} }
// eslint-disable-next-line @typescript-eslint/no-unused-vars async apiEndpoint(): Promise<PaginatedResponse<T>> {
async apiEndpoint(page: number): Promise<PaginatedResponse<T>> {
return Promise.resolve({ return Promise.resolve({
pagination: { pagination: {
count: this.objects.length, count: this.objects.length,
@ -114,10 +113,9 @@ export class DeleteObjectsTable<T> extends Table<T> {
} }
@customElement("ak-forms-delete-bulk") @customElement("ak-forms-delete-bulk")
export class DeleteBulkForm extends ModalButton { export class DeleteBulkForm<T> extends ModalButton {
@property({ attribute: false }) @property({ attribute: false })
// eslint-disable-next-line @typescript-eslint/no-explicit-any objects: T[] = [];
objects: any[] = [];
@property() @property()
objectLabel?: string; objectLabel?: string;
@ -129,8 +127,7 @@ export class DeleteBulkForm extends ModalButton {
actionSubtext?: string; actionSubtext?: string;
@property({ attribute: false }) @property({ attribute: false })
// eslint-disable-next-line @typescript-eslint/no-explicit-any metadata: (item: T) => BulkDeleteMetadata = (item: T) => {
metadata: (item: any) => BulkDeleteMetadata = (item: any) => {
const rec = item as Record<string, unknown>; const rec = item as Record<string, unknown>;
const meta = []; const meta = [];
if (Object.prototype.hasOwnProperty.call(rec, "name")) { if (Object.prototype.hasOwnProperty.call(rec, "name")) {
@ -143,33 +140,30 @@ export class DeleteBulkForm extends ModalButton {
}; };
@property({ attribute: false }) @property({ attribute: false })
// eslint-disable-next-line @typescript-eslint/no-explicit-any usedBy?: (item: T) => Promise<UsedBy[]>;
usedBy?: (item: any) => Promise<UsedBy[]>;
@property({ attribute: false }) @property({ attribute: false })
// eslint-disable-next-line @typescript-eslint/no-explicit-any delete!: (item: T) => Promise<T>;
delete!: (item: any) => Promise<any>;
confirm(): Promise<void> { async confirm(): Promise<void> {
return Promise.all( try {
this.objects.map((item) => { await Promise.all(
return this.delete(item); this.objects.map((item) => {
}), return this.delete(item);
) }),
.then(() => { );
this.onSuccess(); this.onSuccess();
this.open = false; this.open = false;
this.dispatchEvent( this.dispatchEvent(
new CustomEvent(EVENT_REFRESH, { new CustomEvent(EVENT_REFRESH, {
bubbles: true, bubbles: true,
composed: true, composed: true,
}), }),
); );
}) } catch (e) {
.catch((e) => { this.onError(e as Error);
this.onError(e); throw e;
throw e; }
});
} }
onSuccess(): void { onSuccess(): void {

View File

@ -27,8 +27,7 @@ export class Radio<T> extends AKElement {
value?: T; value?: T;
@property({ attribute: false }) @property({ attribute: false })
// eslint-disable-next-line @typescript-eslint/no-unused-vars onChange: (value: T) => void = () => {
onChange: (value: T) => void = (value: T) => {
return; return;
}; };

View File

@ -28,8 +28,9 @@ export class TablePagination extends AKElement {
pages?: Pagination; pages?: Pagination;
@property({ attribute: false }) @property({ attribute: false })
// eslint-disable-next-line pageChangeHandler: (page: number) => void = () => {
pageChangeHandler: (page: number) => void = (page: number) => {}; return;
};
static get styles(): CSSResult[] { static get styles(): CSSResult[] {
return [PFBase, PFButton, PFPagination, AKGlobal]; return [PFBase, PFButton, PFPagination, AKGlobal];

View File

@ -16,8 +16,7 @@ export class UserDeviceList extends MFADevicesPage {
@property({ type: Number }) @property({ type: Number })
userId?: number; userId?: number;
// eslint-disable-next-line @typescript-eslint/no-unused-vars async apiEndpoint(): Promise<PaginatedResponse<Device>> {
async apiEndpoint(page: number): Promise<PaginatedResponse<Device>> {
return new AuthenticatorsApi(DEFAULT_CONFIG) return new AuthenticatorsApi(DEFAULT_CONFIG)
.authenticatorsAdminAllList({ .authenticatorsAdminAllList({
user: this.userId, user: this.userId,

View File

@ -58,8 +58,7 @@ export class WizardFormPage extends WizardPage {
return response; return response;
}; };
// eslint-disable-next-line @typescript-eslint/no-unused-vars nextDataCallback: (data: KeyUnknown) => Promise<boolean> = async (): Promise<boolean> => {
nextDataCallback: (data: KeyUnknown) => Promise<boolean> = async (data): Promise<boolean> => {
return false; return false;
}; };

View File

@ -51,8 +51,7 @@ export class MFADevicesPage extends Table<Device> {
checkbox = true; checkbox = true;
// eslint-disable-next-line @typescript-eslint/no-unused-vars async apiEndpoint(): Promise<PaginatedResponse<Device>> {
async apiEndpoint(page: number): Promise<PaginatedResponse<Device>> {
const devices = await new AuthenticatorsApi(DEFAULT_CONFIG).authenticatorsAllList(); const devices = await new AuthenticatorsApi(DEFAULT_CONFIG).authenticatorsAllList();
return { return {
pagination: { pagination: {