From 2988a792396de7be2406accf61f57f3b94215993 Mon Sep 17 00:00:00 2001 From: Martin Helmich Date: Tue, 28 May 2024 16:52:53 +0200 Subject: [PATCH 1/5] Provide a default implementation for ListBaseCommand.mapData --- src/lib/basecommands/ListBaseCommand.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/lib/basecommands/ListBaseCommand.ts b/src/lib/basecommands/ListBaseCommand.ts index 62241bfc..5102fd71 100644 --- a/src/lib/basecommands/ListBaseCommand.ts +++ b/src/lib/basecommands/ListBaseCommand.ts @@ -56,9 +56,11 @@ export abstract class ListBaseCommand< protected abstract getData(): Promise; - protected abstract mapData( + protected mapData( data: SuccessfulResponse["data"], - ): TItem[] | Promise; + ): TItem[] | Promise { + return data as TItem[]; + } protected getColumns( data: TItem[], From d944ebabd93522d083e1fdba31307ac087dd797e Mon Sep 17 00:00:00 2001 From: Martin Helmich Date: Tue, 28 May 2024 16:54:56 +0200 Subject: [PATCH 2/5] Add option to provide a sorter if desired --- src/lib/basecommands/ListBaseCommand.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/lib/basecommands/ListBaseCommand.ts b/src/lib/basecommands/ListBaseCommand.ts index 5102fd71..760eb559 100644 --- a/src/lib/basecommands/ListBaseCommand.ts +++ b/src/lib/basecommands/ListBaseCommand.ts @@ -31,6 +31,7 @@ export abstract class ListBaseCommand< }; protected formatter: ListFormatter = new ListFormatter(); + protected sorter?: (a: TItem, b: TItem) => number; public async init(): Promise { await super.init(); @@ -59,6 +60,10 @@ export abstract class ListBaseCommand< protected mapData( data: SuccessfulResponse["data"], ): TItem[] | Promise { + if (this.sorter !== undefined) { + (data as TItem[]).sort(this.sorter); + } + return data as TItem[]; } From 52d13ad0a72864318b37835adff59ad65bad3eb3 Mon Sep 17 00:00:00 2001 From: Martin Helmich Date: Tue, 28 May 2024 17:13:32 +0200 Subject: [PATCH 3/5] Remove unnecessary implementations of mapData --- src/commands/app/dependency/list.ts | 16 +++++----- src/commands/app/dependency/versions.ts | 16 +++++----- src/commands/backup/list.ts | 6 ---- src/commands/backup/schedule/list.ts | 4 --- src/commands/conversation/categories.ts | 8 ++--- src/commands/conversation/list.ts | 4 --- src/commands/cronjob/execution/list.ts | 4 --- src/commands/cronjob/list.ts | 7 ----- src/commands/database/mysql/charsets.ts | 8 ++--- src/commands/database/mysql/list.ts | 5 ---- src/commands/database/mysql/user/list.ts | 5 ---- src/commands/database/mysql/versions.ts | 5 ---- src/commands/database/redis/list.ts | 5 ---- src/commands/database/redis/versions.ts | 5 ---- src/commands/domain/dnszone/list.ts | 7 ----- src/commands/domain/list.ts | 7 ----- src/commands/domain/virtualhost/list.ts | 7 ----- src/commands/mail/address/list.ts | 5 ---- src/commands/mail/deliverybox/list.ts | 5 ---- src/commands/org/invite/list.ts | 27 +++++++---------- src/commands/org/list.ts | 5 ---- src/commands/project/invite/list-own.ts | 5 ---- src/commands/project/invite/list.ts | 29 ++++++++----------- src/commands/project/list.ts | 7 ----- src/commands/project/membership/list-own.ts | 26 +++++++---------- src/commands/server/list.ts | 5 ---- src/commands/sftp-user/list.ts | 10 ------- src/commands/ssh-user/list.ts | 10 ------- src/commands/user/api-token/list.ts | 5 ---- src/commands/user/session/list.ts | 21 ++++++-------- src/lib/basecommands/ListBaseCommand.ts | 4 ++- .../formatter/ListDateColumnFormatter.ts | 3 ++ 32 files changed, 66 insertions(+), 220 deletions(-) diff --git a/src/commands/app/dependency/list.ts b/src/commands/app/dependency/list.ts index e6406560..ace5ae16 100644 --- a/src/commands/app/dependency/list.ts +++ b/src/commands/app/dependency/list.ts @@ -1,7 +1,10 @@ import { Simplify } from "@mittwald/api-client-commons"; import { MittwaldAPIV2, MittwaldAPIV2Client } from "@mittwald/api-client"; import { SuccessfulResponse } from "../../../lib/apiutil/SuccessfulResponse.js"; -import { ListBaseCommand } from "../../../lib/basecommands/ListBaseCommand.js"; +import { + ListBaseCommand, + SorterFunction, +} from "../../../lib/basecommands/ListBaseCommand.js"; import { ListColumns } from "../../../rendering/formatter/ListFormatter.js"; type ResponseItem = Simplify< @@ -19,20 +22,15 @@ export class List extends ListBaseCommand { ...ListBaseCommand.baseFlags, }; + protected sorter: SorterFunction = (a, b) => + a.tags[0].localeCompare(b.tags[0]) || a.name.localeCompare(b.name); + public async getData(): Promise { return await this.apiClient.app.listSystemsoftwares({ pathParameters: {}, } as Parameters[0]); } - protected mapData(data: SuccessfulResponse["data"]) { - data.sort( - (a, b) => - a.tags[0].localeCompare(b.tags[0]) || a.name.localeCompare(b.name), - ); - return data; - } - protected getColumns(data: ResponseItem[]): ListColumns { const { id } = super.getColumns(data); return { diff --git a/src/commands/app/dependency/versions.ts b/src/commands/app/dependency/versions.ts index 989f4d6e..d49a67cc 100644 --- a/src/commands/app/dependency/versions.ts +++ b/src/commands/app/dependency/versions.ts @@ -1,7 +1,9 @@ import { assertStatus, Simplify } from "@mittwald/api-client-commons"; import { MittwaldAPIV2, MittwaldAPIV2Client } from "@mittwald/api-client"; -import { ListBaseCommand } from "../../../lib/basecommands/ListBaseCommand.js"; -import { SuccessfulResponse } from "../../../lib/apiutil/SuccessfulResponse.js"; +import { + ListBaseCommand, + SorterFunction, +} from "../../../lib/basecommands/ListBaseCommand.js"; import { ListColumns } from "../../../rendering/formatter/ListFormatter.js"; import { SemVer } from "semver"; import { Args } from "@oclif/core"; @@ -30,6 +32,9 @@ export class Versions extends ListBaseCommand< ...ListBaseCommand.baseFlags, }; + sorter: SorterFunction = (a, b) => + new SemVer(a.externalVersion).compare(b.externalVersion); + public async getData(): Promise { const systemSoftwareName = this.args["systemsoftware"]; @@ -49,13 +54,6 @@ export class Versions extends ListBaseCommand< } as Parameters[0]); } - protected mapData(data: SuccessfulResponse["data"]) { - data.sort((a, b) => - new SemVer(a.externalVersion).compare(b.externalVersion), - ); - return data; - } - protected getColumns(data: ResponseItem[]): ListColumns { const baseColumns = super.getColumns(data); return { diff --git a/src/commands/backup/list.ts b/src/commands/backup/list.ts index 16654b21..24e4cd7c 100644 --- a/src/commands/backup/list.ts +++ b/src/commands/backup/list.ts @@ -22,12 +22,6 @@ export class List extends ListBaseCommand { static aliases = ["project:backup:list"]; static deprecateAliases = true; - protected mapData( - data: SuccessfulResponse["data"], - ): ListItem[] | Promise { - return data; - } - public async getData(): Promise { const projectId = await this.withProjectId(List); return await this.apiClient.backup.listProjectBackups({ diff --git a/src/commands/backup/schedule/list.ts b/src/commands/backup/schedule/list.ts index 753da448..7be0dac1 100644 --- a/src/commands/backup/schedule/list.ts +++ b/src/commands/backup/schedule/list.ts @@ -42,10 +42,6 @@ export class List extends ListBaseCommand { }); } - protected mapData(data: SuccessfulResponse["data"]) { - return data; - } - protected getColumns(data: ResponseItem[]): ListColumns { const { id, createdAt } = super.getColumns(data); return { diff --git a/src/commands/conversation/categories.ts b/src/commands/conversation/categories.ts index 746f4a00..5ec03274 100644 --- a/src/commands/conversation/categories.ts +++ b/src/commands/conversation/categories.ts @@ -23,12 +23,8 @@ export default class Categories extends ListBaseCommand< ...ListBaseCommand.baseFlags, }; - public async getData(): Promise { - return await this.apiClient.conversation.listCategories(); - } - - protected mapData(data: SuccessfulResponse["data"]) { - return data; + public getData(): Promise { + return this.apiClient.conversation.listCategories(); } protected getColumns(): ListColumns { diff --git a/src/commands/conversation/list.ts b/src/commands/conversation/list.ts index be05849c..b7bd0436 100644 --- a/src/commands/conversation/list.ts +++ b/src/commands/conversation/list.ts @@ -29,10 +29,6 @@ export default class List extends ListBaseCommand< return await this.apiClient.conversation.listConversations(); } - protected mapData(data: SuccessfulResponse["data"]) { - return data; - } - protected getColumns(): ListColumns { return { conversationId: { diff --git a/src/commands/cronjob/execution/list.ts b/src/commands/cronjob/execution/list.ts index de37c66c..bde5e124 100644 --- a/src/commands/cronjob/execution/list.ts +++ b/src/commands/cronjob/execution/list.ts @@ -35,10 +35,6 @@ export class List extends ListBaseCommand { return await this.apiClient.cronjob.listExecutions({ cronjobId }); } - protected mapData(data: SuccessfulResponse["data"]) { - return data; - } - protected getColumns(): ListColumns { return { id: {}, diff --git a/src/commands/cronjob/list.ts b/src/commands/cronjob/list.ts index 5675f188..e8190edc 100644 --- a/src/commands/cronjob/list.ts +++ b/src/commands/cronjob/list.ts @@ -1,6 +1,5 @@ import { Simplify } from "@mittwald/api-client-commons"; import { MittwaldAPIV2, MittwaldAPIV2Client } from "@mittwald/api-client"; -import { SuccessfulResponse } from "../../lib/apiutil/SuccessfulResponse.js"; import { ListBaseCommand } from "../../lib/basecommands/ListBaseCommand.js"; import { projectFlags } from "../../lib/resources/project/flags.js"; import { ListColumns } from "../../rendering/formatter/ListFormatter.js"; @@ -31,12 +30,6 @@ export class List extends ListBaseCommand { return await this.apiClient.cronjob.listCronjobs({ projectId }); } - protected mapData( - data: SuccessfulResponse["data"], - ): ResponseItem[] | Promise { - return data; - } - protected getColumns(data: ResponseItem[]): ListColumns { const { id, shortId, createdAt } = super.getColumns(data); return { diff --git a/src/commands/database/mysql/charsets.ts b/src/commands/database/mysql/charsets.ts index a55b1040..3d923656 100644 --- a/src/commands/database/mysql/charsets.ts +++ b/src/commands/database/mysql/charsets.ts @@ -24,15 +24,13 @@ export class Charsets extends ListBaseCommand< ...ListBaseCommand.baseFlags, }; + protected sorter = (a: ResponseItem, b: ResponseItem) => + a.name.localeCompare(b.name); + public async getData(): Promise { return await this.apiClient.database.listMysqlCharsets({}); } - protected mapData(data: SuccessfulResponse["data"]) { - data.sort((a, b) => a.name.localeCompare(b.name)); - return data; - } - protected getColumns(): ListColumns { return { name: {}, diff --git a/src/commands/database/mysql/list.ts b/src/commands/database/mysql/list.ts index 6777ad9b..023348cb 100644 --- a/src/commands/database/mysql/list.ts +++ b/src/commands/database/mysql/list.ts @@ -1,6 +1,5 @@ import { Simplify } from "@mittwald/api-client-commons"; import { MittwaldAPIV2, MittwaldAPIV2Client } from "@mittwald/api-client"; -import { SuccessfulResponse } from "../../../lib/apiutil/SuccessfulResponse.js"; import { ListColumns } from "../../../rendering/formatter/ListFormatter.js"; import { ListBaseCommand } from "../../../lib/basecommands/ListBaseCommand.js"; import { projectFlags } from "../../../lib/resources/project/flags.js"; @@ -28,10 +27,6 @@ export class List extends ListBaseCommand { }); } - protected mapData(data: SuccessfulResponse["data"]) { - return data; - } - protected getColumns(ignoredData: ResponseItem[]): ListColumns { const { id, name, createdAt } = super.getColumns(ignoredData, { shortIdKey: "name", diff --git a/src/commands/database/mysql/user/list.ts b/src/commands/database/mysql/user/list.ts index f1da2c0f..0cbe85f1 100644 --- a/src/commands/database/mysql/user/list.ts +++ b/src/commands/database/mysql/user/list.ts @@ -1,6 +1,5 @@ import { Simplify } from "@mittwald/api-client-commons"; import { MittwaldAPIV2, MittwaldAPIV2Client } from "@mittwald/api-client"; -import { SuccessfulResponse } from "../../../../lib/apiutil/SuccessfulResponse.js"; import { ListBaseCommand } from "../../../../lib/basecommands/ListBaseCommand.js"; import { Flags } from "@oclif/core"; import { ListColumns } from "../../../../rendering/formatter/ListFormatter.js"; @@ -30,10 +29,6 @@ export class List extends ListBaseCommand { }); } - protected mapData(data: SuccessfulResponse["data"]) { - return data; - } - protected getColumns(data: ResponseItem[]): ListColumns { const { id, name, createdAt } = super.getColumns(data, { shortIdKey: "name", diff --git a/src/commands/database/mysql/versions.ts b/src/commands/database/mysql/versions.ts index 8e8ef5db..8789b609 100644 --- a/src/commands/database/mysql/versions.ts +++ b/src/commands/database/mysql/versions.ts @@ -1,6 +1,5 @@ import { Simplify } from "@mittwald/api-client-commons"; import { MittwaldAPIV2, MittwaldAPIV2Client } from "@mittwald/api-client"; -import { SuccessfulResponse } from "../../../lib/apiutil/SuccessfulResponse.js"; import { ListBaseCommand } from "../../../lib/basecommands/ListBaseCommand.js"; import { ListColumns } from "../../../rendering/formatter/ListFormatter.js"; @@ -27,10 +26,6 @@ export class Versions extends ListBaseCommand< return await this.apiClient.database.listMysqlVersions({}); } - protected mapData(data: SuccessfulResponse["data"]) { - return data; - } - protected getColumns(): ListColumns { return { id: { header: "ID" }, diff --git a/src/commands/database/redis/list.ts b/src/commands/database/redis/list.ts index 0e72a504..aa4d708c 100644 --- a/src/commands/database/redis/list.ts +++ b/src/commands/database/redis/list.ts @@ -1,6 +1,5 @@ import { Simplify } from "@mittwald/api-client-commons"; import { MittwaldAPIV2, MittwaldAPIV2Client } from "@mittwald/api-client"; -import { SuccessfulResponse } from "../../../lib/apiutil/SuccessfulResponse.js"; import { ListBaseCommand } from "../../../lib/basecommands/ListBaseCommand.js"; import { projectFlags } from "../../../lib/resources/project/flags.js"; import { ListColumns } from "../../../rendering/formatter/ListFormatter.js"; @@ -26,10 +25,6 @@ export class List extends ListBaseCommand { return await this.apiClient.database.listRedisDatabases({ projectId }); } - protected mapData(data: SuccessfulResponse["data"]) { - return data; - } - protected getColumns(data: ResponseItem[]): ListColumns { const { id, name, createdAt } = super.getColumns(data, { shortIdKey: "name", diff --git a/src/commands/database/redis/versions.ts b/src/commands/database/redis/versions.ts index 6925e04a..4f23916c 100644 --- a/src/commands/database/redis/versions.ts +++ b/src/commands/database/redis/versions.ts @@ -1,6 +1,5 @@ import { Simplify } from "@mittwald/api-client-commons"; import { MittwaldAPIV2, MittwaldAPIV2Client } from "@mittwald/api-client"; -import { SuccessfulResponse } from "../../../lib/apiutil/SuccessfulResponse.js"; import { ListBaseCommand } from "../../../lib/basecommands/ListBaseCommand.js"; import { projectFlags } from "../../../lib/resources/project/flags.js"; import { ListColumns } from "../../../rendering/formatter/ListFormatter.js"; @@ -32,10 +31,6 @@ export default class Versions extends ListBaseCommand< }); } - protected mapData(data: SuccessfulResponse["data"]) { - return data; - } - protected getColumns(): ListColumns { return { id: { header: "ID" }, diff --git a/src/commands/domain/dnszone/list.ts b/src/commands/domain/dnszone/list.ts index ee48fb4c..07fdd215 100644 --- a/src/commands/domain/dnszone/list.ts +++ b/src/commands/domain/dnszone/list.ts @@ -2,7 +2,6 @@ import { MittwaldAPIV2, MittwaldAPIV2Client } from "@mittwald/api-client"; import { projectFlags } from "../../../lib/resources/project/flags.js"; import { ListBaseCommand } from "../../../lib/basecommands/ListBaseCommand.js"; import { Simplify } from "@mittwald/api-client-commons"; -import { SuccessfulResponse } from "../../../lib/apiutil/SuccessfulResponse.js"; import { ListColumns } from "../../../rendering/formatter/ListFormatter.js"; import { isCustomARecord, @@ -38,12 +37,6 @@ export default class List extends ListBaseCommand< return this.apiClient.domain.dnsListDnsZones({ projectId }); } - protected mapData( - data: SuccessfulResponse["data"], - ): ResponseItem[] | Promise { - return data; - } - protected getColumns(data: ResponseItem[]): ListColumns { const { id } = super.getColumns(data); return { diff --git a/src/commands/domain/list.ts b/src/commands/domain/list.ts index 4b4eaa13..3489dc58 100644 --- a/src/commands/domain/list.ts +++ b/src/commands/domain/list.ts @@ -2,7 +2,6 @@ import { Simplify } from "@mittwald/api-client-commons"; import { MittwaldAPIV2, MittwaldAPIV2Client } from "@mittwald/api-client"; import { ListBaseCommand } from "../../lib/basecommands/ListBaseCommand.js"; import { projectFlags } from "../../lib/resources/project/flags.js"; -import { SuccessfulResponse } from "../../lib/apiutil/SuccessfulResponse.js"; import { ListColumns } from "../../rendering/formatter/ListFormatter.js"; type ResponseItem = Simplify< @@ -28,12 +27,6 @@ export class List extends ListBaseCommand { }); } - protected mapData( - data: SuccessfulResponse["data"], - ): ResponseItem[] | Promise { - return data; - } - protected getColumns(): ListColumns { return { id: { diff --git a/src/commands/domain/virtualhost/list.ts b/src/commands/domain/virtualhost/list.ts index 1cb88865..c07b9f21 100644 --- a/src/commands/domain/virtualhost/list.ts +++ b/src/commands/domain/virtualhost/list.ts @@ -1,6 +1,5 @@ import { Simplify } from "@mittwald/api-client-commons"; import { MittwaldAPIV2, MittwaldAPIV2Client } from "@mittwald/api-client"; -import { SuccessfulResponse } from "../../../lib/apiutil/SuccessfulResponse.js"; import { ListBaseCommand } from "../../../lib/basecommands/ListBaseCommand.js"; import { Flags } from "@oclif/core"; import { ListColumns } from "../../../rendering/formatter/ListFormatter.js"; @@ -40,12 +39,6 @@ export class List extends ListBaseCommand { }); } - protected mapData( - data: SuccessfulResponse["data"], - ): ResponseItem[] | Promise { - return data; - } - protected getColumns(data: ResponseItem[]): ListColumns { const baseColumns = super.getColumns(data); const columns: ListColumns = { diff --git a/src/commands/mail/address/list.ts b/src/commands/mail/address/list.ts index bcdbac4a..b06c85f7 100644 --- a/src/commands/mail/address/list.ts +++ b/src/commands/mail/address/list.ts @@ -1,6 +1,5 @@ import { Simplify } from "@mittwald/api-client-commons"; import { MittwaldAPIV2, MittwaldAPIV2Client } from "@mittwald/api-client"; -import { SuccessfulResponse } from "../../../lib/apiutil/SuccessfulResponse.js"; import { ListColumns } from "../../../rendering/formatter/ListFormatter.js"; import { formatRelativeDate } from "../../../rendering/textformat/formatDate.js"; import { ListBaseCommand } from "../../../lib/basecommands/ListBaseCommand.js"; @@ -29,10 +28,6 @@ export class List extends ListBaseCommand { return this.apiClient.mail.listMailAddresses({ projectId }); } - protected mapData(data: SuccessfulResponse["data"]) { - return data; - } - protected getColumns(data: ResponseItem[]): ListColumns { const baseColumns = super.getColumns(data); return { diff --git a/src/commands/mail/deliverybox/list.ts b/src/commands/mail/deliverybox/list.ts index d03565ba..503108f3 100644 --- a/src/commands/mail/deliverybox/list.ts +++ b/src/commands/mail/deliverybox/list.ts @@ -1,6 +1,5 @@ import { Simplify } from "@mittwald/api-client-commons"; import { MittwaldAPIV2, MittwaldAPIV2Client } from "@mittwald/api-client"; -import { SuccessfulResponse } from "../../../lib/apiutil/SuccessfulResponse.js"; import { ListColumns } from "../../../rendering/formatter/ListFormatter.js"; import { formatRelativeDate } from "../../../rendering/textformat/formatDate.js"; import { ListBaseCommand } from "../../../lib/basecommands/ListBaseCommand.js"; @@ -29,10 +28,6 @@ export class List extends ListBaseCommand { return this.apiClient.mail.listDeliveryBoxes({ projectId }); } - protected mapData(data: SuccessfulResponse["data"]) { - return data; - } - protected getColumns(data: ResponseItem[]): ListColumns { const baseColumns = super.getColumns(data); return { diff --git a/src/commands/org/invite/list.ts b/src/commands/org/invite/list.ts index e00f19a9..c226d1cd 100644 --- a/src/commands/org/invite/list.ts +++ b/src/commands/org/invite/list.ts @@ -1,17 +1,14 @@ import { Simplify } from "@mittwald/api-client-commons"; import { MittwaldAPIV2, MittwaldAPIV2Client } from "@mittwald/api-client"; -import { SuccessfulResponse } from "../../../lib/apiutil/SuccessfulResponse.js"; import { ListBaseCommand } from "../../../lib/basecommands/ListBaseCommand.js"; import { orgFlags, withOrgId } from "../../../lib/resources/org/flags.js"; import { ListColumns } from "../../../rendering/formatter/ListFormatter.js"; -import { formatRelativeDate } from "../../../rendering/textformat/formatDate.js"; +import ListDateColumnFormatter from "../../../rendering/formatter/ListDateColumnFormatter.js"; type ResponseItem = Simplify< MittwaldAPIV2.Paths.V2CustomersCustomerIdInvites.Get.Responses.$200.Content.ApplicationJson[number] >; -export type PathParams = - MittwaldAPIV2.Paths.V2CustomersCustomerIdInvites.Get.Parameters.Path; -export type Response = Awaited< +type Response = Awaited< ReturnType >; @@ -39,27 +36,23 @@ export abstract class List extends ListBaseCommand< return await this.apiClient.customer.listInvitesForCustomer({ customerId, - } as Parameters[0]); - } - - protected mapData(data: SuccessfulResponse["data"]) { - return data; + }); } protected getColumns(data: ResponseItem[]): ListColumns { + const dateColumnBuilder = new ListDateColumnFormatter(this.flags); + const expiresAt = dateColumnBuilder.buildColumn({ + header: "Expires", + column: "membershipExpiresAt", + fallback: "(never)", + }); return { id: super.getColumns(data).id, role: {}, email: { get: (item) => item.mailAddress, }, - expiresAt: { - header: "Expires", - get: (item) => - item.membershipExpiresAt - ? formatRelativeDate(item.membershipExpiresAt) - : "(never)", - }, + expiresAt, }; } } diff --git a/src/commands/org/list.ts b/src/commands/org/list.ts index 2f894cbc..17ec72e5 100644 --- a/src/commands/org/list.ts +++ b/src/commands/org/list.ts @@ -1,6 +1,5 @@ import { Simplify } from "@mittwald/api-client-commons"; import { MittwaldAPIV2, MittwaldAPIV2Client } from "@mittwald/api-client"; -import { SuccessfulResponse } from "../../lib/apiutil/SuccessfulResponse.js"; import { ListColumns } from "../../rendering/formatter/ListFormatter.js"; import { ListBaseCommand } from "../../lib/basecommands/ListBaseCommand.js"; @@ -34,10 +33,6 @@ export class List extends ListBaseCommand { return input; } - protected mapData(data: SuccessfulResponse["data"]) { - return data; - } - protected getColumns(data: ResponseItem[]): ListColumns { const { id, customerNumber } = super.getColumns(data, { shortIdKey: "customerNumber", diff --git a/src/commands/project/invite/list-own.ts b/src/commands/project/invite/list-own.ts index a6ce3682..d992f19d 100644 --- a/src/commands/project/invite/list-own.ts +++ b/src/commands/project/invite/list-own.ts @@ -1,6 +1,5 @@ import { Simplify } from "@mittwald/api-client-commons"; import { MittwaldAPIV2, MittwaldAPIV2Client } from "@mittwald/api-client"; -import { SuccessfulResponse } from "../../../lib/apiutil/SuccessfulResponse.js"; import { ListColumns } from "../../../rendering/formatter/ListFormatter.js"; import { formatRelativeDate } from "../../../rendering/textformat/formatDate.js"; import { ListBaseCommand } from "../../../lib/basecommands/ListBaseCommand.js"; @@ -28,10 +27,6 @@ export default class List extends ListBaseCommand< return await this.apiClient.project.listProjectInvites(); } - protected mapData(data: SuccessfulResponse["data"]) { - return data; - } - protected getColumns(): ListColumns { return { id: { diff --git a/src/commands/project/invite/list.ts b/src/commands/project/invite/list.ts index 406107d0..33c520ed 100644 --- a/src/commands/project/invite/list.ts +++ b/src/commands/project/invite/list.ts @@ -1,10 +1,9 @@ import { Simplify } from "@mittwald/api-client-commons"; import { MittwaldAPIV2, MittwaldAPIV2Client } from "@mittwald/api-client"; -import { SuccessfulResponse } from "../../../lib/apiutil/SuccessfulResponse.js"; import { ListColumns } from "../../../rendering/formatter/ListFormatter.js"; -import { formatRelativeDate } from "../../../rendering/textformat/formatDate.js"; import { ListBaseCommand } from "../../../lib/basecommands/ListBaseCommand.js"; import { projectFlags } from "../../../lib/resources/project/flags.js"; +import ListDateColumnFormatter from "../../../rendering/formatter/ListDateColumnFormatter.js"; type ResponseItem = Simplify< MittwaldAPIV2.Paths.V2ProjectsProjectIdInvites.Get.Responses.$200.Content.ApplicationJson[number] @@ -31,27 +30,23 @@ export default class List extends ListBaseCommand< return await this.apiClient.project.listInvitesForProject({ projectId }); } - protected mapData(data: SuccessfulResponse["data"]) { - return data; - } - protected getColumns(): ListColumns { + const dateColumnBuilder = new ListDateColumnFormatter(this.flags); + const expires = { + ...dateColumnBuilder.buildColumn({ + header: "Expires", + fallback: "(never)", + column: "membershipExpiresAt", + }), + extended: true, + }; + return { id: { header: "ID", minWidth: 36, }, - expires: { - header: "Expires", - extended: true, - get: (row) => { - if (!row.membershipExpiresAt) { - return "never"; - } - - return formatRelativeDate(new Date(row.membershipExpiresAt)); - }, - }, + expires, mailAddress: { header: "Email" }, role: { header: "Role" }, }; diff --git a/src/commands/project/list.ts b/src/commands/project/list.ts index 212aa471..b39d79fe 100644 --- a/src/commands/project/list.ts +++ b/src/commands/project/list.ts @@ -5,7 +5,6 @@ import { MittwaldAPIV2Client, MittwaldAPIV2Client as MittwaldAPIClient, } from "@mittwald/api-client"; -import { SuccessfulResponse } from "../../lib/apiutil/SuccessfulResponse.js"; import { ListBaseCommand } from "../../lib/basecommands/ListBaseCommand.js"; type ProjectResponse = Awaited< @@ -35,12 +34,6 @@ export class List extends ListBaseCommand< return await this.apiClient.project.listProjects(); } - protected mapData( - data: SuccessfulResponse["data"], - ): ProjectResponseItem[] { - return data; - } - protected getColumns( ignoredData: ProjectResponseItem[], ): ListColumns { diff --git a/src/commands/project/membership/list-own.ts b/src/commands/project/membership/list-own.ts index 0bad90cf..aa259c24 100644 --- a/src/commands/project/membership/list-own.ts +++ b/src/commands/project/membership/list-own.ts @@ -1,9 +1,9 @@ import { Simplify } from "@mittwald/api-client-commons"; import { MittwaldAPIV2, MittwaldAPIV2Client } from "@mittwald/api-client"; -import { SuccessfulResponse } from "../../../lib/apiutil/SuccessfulResponse.js"; import { ListColumns } from "../../../rendering/formatter/ListFormatter.js"; import { formatRelativeDate } from "../../../rendering/textformat/formatDate.js"; import { ListBaseCommand } from "../../../lib/basecommands/ListBaseCommand.js"; +import ListDateColumnFormatter from "../../../rendering/formatter/ListDateColumnFormatter.js"; type ResponseItem = Simplify< MittwaldAPIV2.Paths.V2ProjectMemberships.Get.Responses.$200.Content.ApplicationJson[number] @@ -29,28 +29,22 @@ export default class ListOwn extends ListBaseCommand< return await this.apiClient.project.listProjectMemberships(); } - protected mapData(data: SuccessfulResponse["data"]) { - return data; - } - protected getColumns(): ListColumns { + const dateColumnBuilder = new ListDateColumnFormatter(this.flags); + const expires = dateColumnBuilder.buildColumn({ + header: "Expires", + column: "expiresAt", + fallback: "never", + extended: true, + }); + return { id: { header: "ID", minWidth: 36, extended: true, }, - expires: { - header: "Expires", - extended: true, - get: (row) => { - if (!row.expiresAt) { - return "never"; - } - - return formatRelativeDate(new Date(row.expiresAt)); - }, - }, + expires, projectId: { header: "Project Id" }, role: { header: "Role" }, }; diff --git a/src/commands/server/list.ts b/src/commands/server/list.ts index 02651db5..c1d77dc4 100644 --- a/src/commands/server/list.ts +++ b/src/commands/server/list.ts @@ -1,6 +1,5 @@ import { Simplify } from "@mittwald/api-client-commons"; import { MittwaldAPIV2, MittwaldAPIV2Client } from "@mittwald/api-client"; -import { SuccessfulResponse } from "../../lib/apiutil/SuccessfulResponse.js"; import { ListColumns } from "../../rendering/formatter/ListFormatter.js"; import { ListBaseCommand } from "../../lib/basecommands/ListBaseCommand.js"; @@ -27,10 +26,6 @@ export default class List extends ListBaseCommand< return await this.apiClient.project.listServers(); } - protected mapData(data: SuccessfulResponse["data"]) { - return data; - } - protected getColumns(ignoredData: ResponseItem[]): ListColumns { const { id, shortId } = super.getColumns(ignoredData); return { diff --git a/src/commands/sftp-user/list.ts b/src/commands/sftp-user/list.ts index 6b128c9b..10351887 100644 --- a/src/commands/sftp-user/list.ts +++ b/src/commands/sftp-user/list.ts @@ -1,13 +1,9 @@ import { MittwaldAPIV2, MittwaldAPIV2Client } from "@mittwald/api-client"; import { Simplify } from "@mittwald/api-client-commons"; -import { SuccessfulResponse } from "../../lib/apiutil/SuccessfulResponse.js"; import { ListColumns } from "../../rendering/formatter/ListFormatter.js"; import { ListBaseCommand } from "../../lib/basecommands/ListBaseCommand.js"; import { projectFlags } from "../../lib/resources/project/flags.js"; -type SftpUserResponse = Awaited< - ReturnType ->; type SftpUserResponseItem = Simplify< MittwaldAPIV2.Paths.V2ProjectsProjectIdSftpUsers.Get.Responses.$200.Content.ApplicationJson[number] >; @@ -38,12 +34,6 @@ export default class List extends ListBaseCommand< }); } - protected mapData( - data: SuccessfulResponse["data"], - ): SftpUserResponseItem[] { - return data; - } - protected getColumns( data: SftpUserResponseItem[], ): ListColumns { diff --git a/src/commands/ssh-user/list.ts b/src/commands/ssh-user/list.ts index 07138402..46df8719 100644 --- a/src/commands/ssh-user/list.ts +++ b/src/commands/ssh-user/list.ts @@ -1,13 +1,9 @@ import { MittwaldAPIV2, MittwaldAPIV2Client } from "@mittwald/api-client"; import { Simplify } from "@mittwald/api-client-commons"; -import { SuccessfulResponse } from "../../lib/apiutil/SuccessfulResponse.js"; import { ListColumns } from "../../rendering/formatter/ListFormatter.js"; import { ListBaseCommand } from "../../lib/basecommands/ListBaseCommand.js"; import { projectFlags } from "../../lib/resources/project/flags.js"; -type SshUserResponse = Awaited< - ReturnType ->; type SshUserResponseItem = Simplify< MittwaldAPIV2.Paths.V2ProjectsProjectIdSftpUsers.Get.Responses.$200.Content.ApplicationJson[number] >; @@ -36,12 +32,6 @@ export class List extends ListBaseCommand< return await this.apiClient.sshsftpUser.sshUserListSshUsers({ projectId }); } - protected mapData( - data: SuccessfulResponse["data"], - ): SshUserResponseItem[] { - return data; - } - protected getColumns( data: SshUserResponseItem[], ): ListColumns { diff --git a/src/commands/user/api-token/list.ts b/src/commands/user/api-token/list.ts index f506e8d2..09181bbe 100644 --- a/src/commands/user/api-token/list.ts +++ b/src/commands/user/api-token/list.ts @@ -1,6 +1,5 @@ import { Simplify } from "@mittwald/api-client-commons"; import { MittwaldAPIV2, MittwaldAPIV2Client } from "@mittwald/api-client"; -import { SuccessfulResponse } from "../../../lib/apiutil/SuccessfulResponse.js"; import { ListColumns } from "../../../rendering/formatter/ListFormatter.js"; import { ListBaseCommand } from "../../../lib/basecommands/ListBaseCommand.js"; @@ -29,10 +28,6 @@ export default class List extends ListBaseCommand< return await this.apiClient.user.listApiTokens(); } - protected mapData(data: SuccessfulResponse["data"]) { - return data; - } - protected getColumns(): ListColumns { const dateColumnBuilder = new ListDateColumnFormatter(this.flags); const createdAt = dateColumnBuilder.buildColumn(); diff --git a/src/commands/user/session/list.ts b/src/commands/user/session/list.ts index 6e28ccbc..c15b78e5 100644 --- a/src/commands/user/session/list.ts +++ b/src/commands/user/session/list.ts @@ -1,9 +1,9 @@ import { Simplify } from "@mittwald/api-client-commons"; import { MittwaldAPIV2, MittwaldAPIV2Client } from "@mittwald/api-client"; -import { SuccessfulResponse } from "../../../lib/apiutil/SuccessfulResponse.js"; import { ListColumns } from "../../../rendering/formatter/ListFormatter.js"; import { formatRelativeDate } from "../../../rendering/textformat/formatDate.js"; import { ListBaseCommand } from "../../../lib/basecommands/ListBaseCommand.js"; +import ListDateColumnFormatter from "../../../rendering/formatter/ListDateColumnFormatter.js"; type ResponseItem = Simplify< MittwaldAPIV2.Paths.V2UsersSelfSessions.Get.Responses.$200.Content.ApplicationJson[number] @@ -28,11 +28,14 @@ export default class List extends ListBaseCommand< return await this.apiClient.user.listSessions(); } - protected mapData(data: SuccessfulResponse["data"]) { - return data; - } - protected getColumns(): ListColumns { + const dateColumnBuilder = new ListDateColumnFormatter(this.flags); + const lastAccess = dateColumnBuilder.buildColumn({ + header: "Last access", + column: "lastAccess", + fallback: "(never)", + }); + return { tokenId: { header: "ID", minWidth: 36 }, device: { @@ -43,13 +46,7 @@ export default class List extends ListBaseCommand< header: "Location", get: (row) => row.location?.country, }, - lastAccess: { - header: "Last Access", - get: (row) => - row.lastAccess - ? formatRelativeDate(new Date(`${row.lastAccess}`)) - : "never", - }, + lastAccess, }; } } diff --git a/src/lib/basecommands/ListBaseCommand.ts b/src/lib/basecommands/ListBaseCommand.ts index 760eb559..116a89c0 100644 --- a/src/lib/basecommands/ListBaseCommand.ts +++ b/src/lib/basecommands/ListBaseCommand.ts @@ -21,6 +21,8 @@ export type ColumnOpts = { outputFormat?: string; }; +export type SorterFunction = (a: TItem, b: TItem) => number; + export abstract class ListBaseCommand< T extends typeof BaseCommand, TItem extends Record, @@ -31,7 +33,7 @@ export abstract class ListBaseCommand< }; protected formatter: ListFormatter = new ListFormatter(); - protected sorter?: (a: TItem, b: TItem) => number; + protected sorter?: SorterFunction; public async init(): Promise { await super.init(); diff --git a/src/rendering/formatter/ListDateColumnFormatter.ts b/src/rendering/formatter/ListDateColumnFormatter.ts index be6418c6..f71ae1aa 100644 --- a/src/rendering/formatter/ListDateColumnFormatter.ts +++ b/src/rendering/formatter/ListDateColumnFormatter.ts @@ -39,13 +39,16 @@ export default class ListDateColumnFormatter { header = "Created at", fallback = "unknown", column = "createdAt" as const, + extended = false, }: { header?: string; fallback?: string; column?: TColumnName | "createdAt"; + extended?: boolean; } = {}): Partial>> { return { header, + extended, get: (row) => row[column] ? this.renderDate(new Date(`${row[column]}`)) : fallback, }; From 9c257c4ef0817fe90746d6e399472b3737eec120 Mon Sep 17 00:00:00 2001 From: Martin Helmich Date: Tue, 28 May 2024 17:18:43 +0200 Subject: [PATCH 4/5] Optimize imports --- src/commands/app/dependency/list.ts | 1 - src/commands/app/download.tsx | 2 +- src/commands/backup/list.ts | 1 - src/commands/backup/schedule/list.ts | 1 - src/commands/conversation/categories.ts | 1 - src/commands/conversation/list.ts | 1 - src/commands/cronjob/execution/list.ts | 1 - src/commands/database/mysql/charsets.ts | 1 - src/commands/mail/address/create.tsx | 1 + src/commands/project/membership/list-own.ts | 1 - src/commands/user/session/list.ts | 1 - 11 files changed, 2 insertions(+), 10 deletions(-) diff --git a/src/commands/app/dependency/list.ts b/src/commands/app/dependency/list.ts index ace5ae16..09450539 100644 --- a/src/commands/app/dependency/list.ts +++ b/src/commands/app/dependency/list.ts @@ -1,6 +1,5 @@ import { Simplify } from "@mittwald/api-client-commons"; import { MittwaldAPIV2, MittwaldAPIV2Client } from "@mittwald/api-client"; -import { SuccessfulResponse } from "../../../lib/apiutil/SuccessfulResponse.js"; import { ListBaseCommand, SorterFunction, diff --git a/src/commands/app/download.tsx b/src/commands/app/download.tsx index a70fb01f..e7d00f73 100644 --- a/src/commands/app/download.tsx +++ b/src/commands/app/download.tsx @@ -12,10 +12,10 @@ import { spawnInProcess } from "../../rendering/process/process_exec.js"; import { sshConnectionFlags } from "../../lib/resources/ssh/flags.js"; import { sshUsageDocumentation } from "../../lib/resources/ssh/doc.js"; import { - filterFileToRsyncFlagsIfPresent, appInstallationSyncFlags, appInstallationSyncFlagsToRsyncFlags, filterFileDocumentation, + filterFileToRsyncFlagsIfPresent, } from "../../lib/resources/app/sync.js"; import { hasBinaryInPath } from "../../lib/util/fs/hasBinaryInPath.js"; diff --git a/src/commands/backup/list.ts b/src/commands/backup/list.ts index 24e4cd7c..0ba2a744 100644 --- a/src/commands/backup/list.ts +++ b/src/commands/backup/list.ts @@ -1,6 +1,5 @@ import { Response, Simplify } from "@mittwald/api-client-commons"; import type { MittwaldAPIV2 } from "@mittwald/api-client"; -import { SuccessfulResponse } from "../../lib/apiutil/SuccessfulResponse.js"; import { ListBaseCommand } from "../../lib/basecommands/ListBaseCommand.js"; import { projectFlags } from "../../lib/resources/project/flags.js"; import { ListColumns } from "../../rendering/formatter/ListFormatter.js"; diff --git a/src/commands/backup/schedule/list.ts b/src/commands/backup/schedule/list.ts index 7be0dac1..93aee10b 100644 --- a/src/commands/backup/schedule/list.ts +++ b/src/commands/backup/schedule/list.ts @@ -1,6 +1,5 @@ import { Simplify } from "@mittwald/api-client-commons"; import { MittwaldAPIV2, MittwaldAPIV2Client } from "@mittwald/api-client"; -import { SuccessfulResponse } from "../../../lib/apiutil/SuccessfulResponse.js"; import { ListColumns } from "../../../rendering/formatter/ListFormatter.js"; import { ListBaseCommand } from "../../../lib/basecommands/ListBaseCommand.js"; import { diff --git a/src/commands/conversation/categories.ts b/src/commands/conversation/categories.ts index 5ec03274..36b4e154 100644 --- a/src/commands/conversation/categories.ts +++ b/src/commands/conversation/categories.ts @@ -1,6 +1,5 @@ import { Simplify } from "@mittwald/api-client-commons"; import { MittwaldAPIV2, MittwaldAPIV2Client } from "@mittwald/api-client"; -import { SuccessfulResponse } from "../../lib/apiutil/SuccessfulResponse.js"; import { ListColumns } from "../../rendering/formatter/ListFormatter.js"; import { ListBaseCommand } from "../../lib/basecommands/ListBaseCommand.js"; diff --git a/src/commands/conversation/list.ts b/src/commands/conversation/list.ts index b7bd0436..5854ae17 100644 --- a/src/commands/conversation/list.ts +++ b/src/commands/conversation/list.ts @@ -1,6 +1,5 @@ import { Simplify } from "@mittwald/api-client-commons"; import { MittwaldAPIV2, MittwaldAPIV2Client } from "@mittwald/api-client"; -import { SuccessfulResponse } from "../../lib/apiutil/SuccessfulResponse.js"; import { ListColumns } from "../../rendering/formatter/ListFormatter.js"; import { formatRelativeDate } from "../../rendering/textformat/formatDate.js"; import { ListBaseCommand } from "../../lib/basecommands/ListBaseCommand.js"; diff --git a/src/commands/cronjob/execution/list.ts b/src/commands/cronjob/execution/list.ts index bde5e124..32692631 100644 --- a/src/commands/cronjob/execution/list.ts +++ b/src/commands/cronjob/execution/list.ts @@ -1,6 +1,5 @@ import { Simplify } from "@mittwald/api-client-commons"; import { MittwaldAPIV2, MittwaldAPIV2Client } from "@mittwald/api-client"; -import { SuccessfulResponse } from "../../../lib/apiutil/SuccessfulResponse.js"; import { ListColumns } from "../../../rendering/formatter/ListFormatter.js"; import { ListBaseCommand } from "../../../lib/basecommands/ListBaseCommand.js"; import { Flags } from "@oclif/core"; diff --git a/src/commands/database/mysql/charsets.ts b/src/commands/database/mysql/charsets.ts index 3d923656..f7565911 100644 --- a/src/commands/database/mysql/charsets.ts +++ b/src/commands/database/mysql/charsets.ts @@ -1,6 +1,5 @@ import { Simplify } from "@mittwald/api-client-commons"; import { MittwaldAPIV2, MittwaldAPIV2Client } from "@mittwald/api-client"; -import { SuccessfulResponse } from "../../../lib/apiutil/SuccessfulResponse.js"; import { ListBaseCommand } from "../../../lib/basecommands/ListBaseCommand.js"; import { ListColumns } from "../../../rendering/formatter/ListFormatter.js"; diff --git a/src/commands/mail/address/create.tsx b/src/commands/mail/address/create.tsx index 34227aff..67a17409 100644 --- a/src/commands/mail/address/create.tsx +++ b/src/commands/mail/address/create.tsx @@ -14,6 +14,7 @@ import * as crypto from "crypto"; import { Value } from "../../../rendering/react/components/Value.js"; import { FlagInput, OutputFlags } from "@oclif/core/lib/interfaces/parser.js"; import ByteQuantity from "../../../lib/units/ByteQuantity.js"; + type CreateResult = { addressId: string; generatedPassword: string | null; diff --git a/src/commands/project/membership/list-own.ts b/src/commands/project/membership/list-own.ts index aa259c24..886e889b 100644 --- a/src/commands/project/membership/list-own.ts +++ b/src/commands/project/membership/list-own.ts @@ -1,7 +1,6 @@ import { Simplify } from "@mittwald/api-client-commons"; import { MittwaldAPIV2, MittwaldAPIV2Client } from "@mittwald/api-client"; import { ListColumns } from "../../../rendering/formatter/ListFormatter.js"; -import { formatRelativeDate } from "../../../rendering/textformat/formatDate.js"; import { ListBaseCommand } from "../../../lib/basecommands/ListBaseCommand.js"; import ListDateColumnFormatter from "../../../rendering/formatter/ListDateColumnFormatter.js"; diff --git a/src/commands/user/session/list.ts b/src/commands/user/session/list.ts index c15b78e5..bc62999e 100644 --- a/src/commands/user/session/list.ts +++ b/src/commands/user/session/list.ts @@ -1,7 +1,6 @@ import { Simplify } from "@mittwald/api-client-commons"; import { MittwaldAPIV2, MittwaldAPIV2Client } from "@mittwald/api-client"; import { ListColumns } from "../../../rendering/formatter/ListFormatter.js"; -import { formatRelativeDate } from "../../../rendering/textformat/formatDate.js"; import { ListBaseCommand } from "../../../lib/basecommands/ListBaseCommand.js"; import ListDateColumnFormatter from "../../../rendering/formatter/ListDateColumnFormatter.js"; From c31753c06e79f645a9b4999c42b5d6aea25f5a52 Mon Sep 17 00:00:00 2001 From: Martin Helmich Date: Tue, 28 May 2024 17:20:54 +0200 Subject: [PATCH 5/5] Remove unused type --- src/commands/project/list.ts | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/commands/project/list.ts b/src/commands/project/list.ts index b39d79fe..381749fe 100644 --- a/src/commands/project/list.ts +++ b/src/commands/project/list.ts @@ -1,15 +1,8 @@ import { Simplify } from "@mittwald/api-client-commons"; import { ListColumns } from "../../rendering/formatter/ListFormatter.js"; -import { - MittwaldAPIV2, - MittwaldAPIV2Client, - MittwaldAPIV2Client as MittwaldAPIClient, -} from "@mittwald/api-client"; +import { MittwaldAPIV2, MittwaldAPIV2Client } from "@mittwald/api-client"; import { ListBaseCommand } from "../../lib/basecommands/ListBaseCommand.js"; -type ProjectResponse = Awaited< - ReturnType ->; type ProjectResponseItem = Simplify< MittwaldAPIV2.Paths.V2Projects.Get.Responses.$200.Content.ApplicationJson[number] >;