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")
export class AdminLoginAuthorizeChart extends AKChart<LoginMetrics> {
apiRequest(): Promise<LoginMetrics> {
async apiRequest(): Promise<LoginMetrics> {
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.`;
}
send = (data: { forUser: number }): Promise<PolicyTestResult> => {
send = async (data: { forUser: number }): Promise<PolicyTestResult> => {
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 {

View File

@ -26,23 +26,19 @@ export class FlowImportForm extends Form<Flow> {
return super.styles.concat(PFDescriptionList);
}
// eslint-disable-next-line
send = (data: Flow): Promise<FlowImportResult> => {
send = async (): Promise<FlowImportResult> => {
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 {

View File

@ -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 {

View File

@ -39,14 +39,13 @@ export class PolicyTestForm extends Form<PolicyTestRequest> {
return t`Successfully sent test-request.`;
}
send = (data: PolicyTestRequest): Promise<PolicyTestResult> => {
send = async (data: PolicyTestRequest): Promise<PolicyTestResult> => {
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[] {

View File

@ -37,15 +37,14 @@ export class PolicyTestForm extends Form<PolicyTestRequest> {
return t`Successfully sent test-request.`;
}
send = (data: PolicyTestRequest): Promise<PropertyMappingTestResult> => {
send = async (data: PolicyTestRequest): Promise<PropertyMappingTestResult> => {
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 {

View File

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

View File

@ -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 {

View File

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

View File

@ -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: {

View File

@ -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<T> 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":

View File

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

View File

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

View File

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

View File

@ -40,8 +40,7 @@ export class DeleteObjectsTable<T> extends Table<T> {
return super.styles.concat(PFList);
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async apiEndpoint(page: number): Promise<PaginatedResponse<T>> {
async apiEndpoint(): Promise<PaginatedResponse<T>> {
return Promise.resolve({
pagination: {
count: this.objects.length,
@ -114,10 +113,9 @@ export class DeleteObjectsTable<T> extends Table<T> {
}
@customElement("ak-forms-delete-bulk")
export class DeleteBulkForm extends ModalButton {
export class DeleteBulkForm<T> 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<string, unknown>;
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[]>;
usedBy?: (item: T) => Promise<UsedBy[]>;
@property({ attribute: false })
// eslint-disable-next-line @typescript-eslint/no-explicit-any
delete!: (item: any) => Promise<any>;
delete!: (item: T) => Promise<T>;
confirm(): Promise<void> {
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<void> {
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 {

View File

@ -27,8 +27,7 @@ export class Radio<T> 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;
};

View File

@ -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];

View File

@ -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<PaginatedResponse<Device>> {
async apiEndpoint(): Promise<PaginatedResponse<Device>> {
return new AuthenticatorsApi(DEFAULT_CONFIG)
.authenticatorsAdminAllList({
user: this.userId,

View File

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

View File

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