insert at index, change index

This commit is contained in:
Evert Prants 2021-05-16 19:18:07 +03:00
parent cc2f4fea84
commit 621d6b812c
Signed by: evert
GPG Key ID: 1688DA83D222D0B5
2 changed files with 40 additions and 7 deletions

View File

@ -88,7 +88,7 @@ Returns all of `:domain`'s DNS records or performs a search based on provided qu
```
### `POST /zone/records/:domain`
Updates or marks for deletion a single or multiple DNS records of `:domain` at `index`.
Updates or marks for deletion a single or multiple DNS records of `:domain` at `index`. **Warning:** `setIndex` will cause your other records to shift around, so it is currently only recommended to use for a single record at a time.
**Body:**
```typescript
@ -98,6 +98,7 @@ Updates or marks for deletion a single or multiple DNS records of `:domain` at `
name?: string;
type?: string;
value?: string;
setIndex?: number;
forDeletion?: boolean;
} | {...}[];
}
@ -126,6 +127,7 @@ Creates a single or multiple new DNS records for `:domain`.
name: string;
type: string;
value: string;
index?: number; // insert at this exact index (not after this index, at it!)
} | {...}[];
}
```

View File

@ -107,6 +107,7 @@ api.get('/zone/records/:domain', domainAuthorization, async (req, res) => {
* name?: string;
* type?: DNSRecordType;
* value?: string;
* setIndex?: number;
* forDeletion?: boolean;
* }[];
*/
@ -191,8 +192,23 @@ api.post('/zone/records/:domain', domainAuthorization, async (req, res) => {
continue;
}
zone.records[index] = record;
changed.push(record);
if (setter.setIndex) {
const afterI = parseInt(setter.setIndex, 10);
if (isNaN(afterI) || afterI > zone.records.length) {
errors.push({
message: 'Invalid insertion location!',
record
});
continue;
}
zone.records[index] = { ...record, forDeletion: true };
zone.records.splice(afterI, 0, record);
changed.push({ ...record, index: afterI });
} else {
zone.records[index] = record;
changed.push(record);
}
}
await cache.update(domain, cached);
@ -264,6 +280,7 @@ api.delete('/zone/records/:domain', domainAuthorization, async (req, res) => {
* name: string;
* type: DNSRecordType;
* value: string;
* index?: number;
* }[];
*/
api.put('/zone/records/:domain', domainAuthorization, async (req, res) => {
@ -320,7 +337,7 @@ api.put('/zone/records/:domain', domainAuthorization, async (req, res) => {
if (!validator.validateRecord(newRecord)) {
errors.push({
message: 'Validation error: Invalid characters',
record: setter
record: newRecord
});
continue;
}
@ -328,13 +345,27 @@ api.put('/zone/records/:domain', domainAuthorization, async (req, res) => {
if (cache.search(cached, name, upperType, value, true).length) {
errors.push({
message: 'Exact same record already exists. No need to duplicate records!',
record: setter
record: newRecord
});
continue;
}
const index = zone.records.push(newRecord) - 1;
created.push({ ...newRecord, index });
if (setter.index) {
const afterI = parseInt(setter.index, 10);
if (isNaN(afterI) || afterI > zone.records.length) {
errors.push({
message: 'Invalid insertion location!',
record: newRecord
});
continue;
}
zone.records.splice(afterI, 0, newRecord);
created.push({ ...newRecord, index: afterI });
} else {
const index = zone.records.push(newRecord) - 1;
created.push({ ...newRecord, index });
}
}
await cache.update(domain, cached);