diff --git a/README.md b/README.md index 29dcf52..bfb74e3 100644 --- a/README.md +++ b/README.md @@ -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!) } | {...}[]; } ``` diff --git a/src/index.ts b/src/index.ts index bb754fe..1652301 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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);