Convert to record api format

This commit is contained in:
Melon 2024-07-28 23:59:29 +01:00
parent 9909f06a40
commit 0c6df00a9d
Signed by: melon
GPG Key ID: 6C9D970C50D26A25
3 changed files with 66 additions and 3 deletions

View File

@ -10,6 +10,12 @@ export const DnsTypeCAA = 257;
export type AllRecords = SoaRecord | NsRecord | MxRecord | ARecord | AaaaRecord | CnameRecord | TxtRecord | SrvRecord | CaaRecord; export type AllRecords = SoaRecord | NsRecord | MxRecord | ARecord | AaaaRecord | CnameRecord | TxtRecord | SrvRecord | CaaRecord;
export interface ApiRecordFormat {
name: string;
type: number;
value: any;
}
export interface RecordHeader { export interface RecordHeader {
Name: string; Name: string;
Rrtype: number; Rrtype: number;

View File

@ -3,7 +3,7 @@
import type {RestItem, RestTable} from "../utils/rest-table"; import type {RestItem, RestTable} from "../utils/rest-table";
import PromiseTable from "../components/PromiseTable.svelte"; import PromiseTable from "../components/PromiseTable.svelte";
import PromiseLike from "../components/PromiseLike.svelte"; import PromiseLike from "../components/PromiseLike.svelte";
import type {AllRecords, UnknownRecord} from "../types/records"; import type {AllRecords, ApiRecordFormat, UnknownRecord} from "../types/records";
import ActionPopup from "../components/ActionPopup.svelte"; import ActionPopup from "../components/ActionPopup.svelte";
type T = $$Generic<UnknownRecord>; type T = $$Generic<UnknownRecord>;
@ -11,6 +11,7 @@
export let recordName: string; export let recordName: string;
export let table: RestTable<AllRecords>; export let table: RestTable<AllRecords>;
export let emptyRecord: (() => any) | null; export let emptyRecord: (() => any) | null;
export let convert: (t: T) => ApiRecordFormat;
export let rowOrdering: (rows: RestItem<AllRecords>[], domain: string, isTRecord: (t: UnknownRecord) => t is T) => RestItem<T>[]; export let rowOrdering: (rows: RestItem<AllRecords>[], domain: string, isTRecord: (t: UnknownRecord) => t is T) => RestItem<T>[];
export let isTRecord: (t: UnknownRecord) => t is T; export let isTRecord: (t: UnknownRecord) => t is T;
@ -18,7 +19,8 @@
let createPopup: boolean = false; let createPopup: boolean = false;
function createRecord() { function createRecord() {
table.addItem(createItem as any); if (createItem == null) return;
table.addItem(convert(createItem) as any);
} }
</script> </script>

View File

@ -21,6 +21,7 @@
type ARecord, type ARecord,
type AaaaRecord, type AaaaRecord,
type AllRecords, type AllRecords,
type ApiRecordFormat,
type CaaRecord, type CaaRecord,
type CnameRecord, type CnameRecord,
type MxRecord, type MxRecord,
@ -133,6 +134,11 @@
}, },
Ns: "", Ns: "",
}), }),
convert: (t: NsRecord): ApiRecordFormat => ({
name: t.Hdr.Name,
type: t.Hdr.Rrtype,
value: t.Ns,
}),
}, },
{ {
name: "MX", name: "MX",
@ -150,6 +156,14 @@
Mx: "", Mx: "",
Preference: 0, Preference: 0,
}), }),
convert: (t: MxRecord): ApiRecordFormat => ({
name: t.Hdr.Name,
type: t.Hdr.Rrtype,
value: {
mx: t.Mx,
preference: t.Preference,
},
}),
}, },
{ {
name: "A/AAAA", name: "A/AAAA",
@ -167,6 +181,11 @@
A: "", A: "",
AAAA: "", AAAA: "",
}), }),
convert: (t: ARecord | AaaaRecord): ApiRecordFormat => ({
name: t.Hdr.Name,
type: t.Hdr.Rrtype,
value: isARecord(t) ? t.A : isAaaaRecord(t) ? t.AAAA : "",
}),
}, },
{ {
name: "CNAME", name: "CNAME",
@ -183,6 +202,11 @@
}, },
Target: "", Target: "",
}), }),
convert: (t: CnameRecord): ApiRecordFormat => ({
name: t.Hdr.Name,
type: t.Hdr.Rrtype,
value: t.Target,
}),
}, },
{ {
name: "TXT", name: "TXT",
@ -199,6 +223,11 @@
}, },
Txt: [""], Txt: [""],
}), }),
convert: (t: TxtRecord): ApiRecordFormat => ({
name: t.Hdr.Name,
type: t.Hdr.Rrtype,
value: t.Txt,
}),
}, },
{ {
name: "SRV", name: "SRV",
@ -218,6 +247,16 @@
Port: 0, Port: 0,
Target: "", Target: "",
}), }),
convert: (t: SrvRecord): ApiRecordFormat => ({
name: t.Hdr.Name,
type: t.Hdr.Rrtype,
value: {
priority: t.Priority,
weight: t.Weight,
port: t.Port,
target: t.Target,
},
}),
}, },
{ {
name: "CAA", name: "CAA",
@ -236,6 +275,15 @@
Tag: "", Tag: "",
Value: "", Value: "",
}), }),
convert: (t: CaaRecord): ApiRecordFormat => ({
name: t.Hdr.Name,
type: t.Hdr.Rrtype,
value: {
flag: t.Flag,
tag: t.Tag,
value: t.Value,
},
}),
}, },
]; ];
@ -252,7 +300,14 @@
{/if} {/if}
{#each recordTypes as recordType} {#each recordTypes as recordType}
<DomainTableView recordName={recordType.name} {table} emptyRecord={recordType.empty} {rowOrdering} isTRecord={recordType.filter}> <DomainTableView
recordName={recordType.name}
{table}
emptyRecord={recordType.empty}
convert={recordType.convert}
{rowOrdering}
isTRecord={recordType.filter}
>
<tr slot="headers"> <tr slot="headers">
{#each recordType.headers as header} {#each recordType.headers as header}
<th>{header}</th> <th>{header}</th>