netcup-node
Version:
A node wrapper for the Netcup CCP API (https://www.netcup-wiki.de/wiki/CCP_API)
60 lines (59 loc) • 1.66 kB
JavaScript
import axios from 'axios';
import { Actions } from './@types/Actions.js';
import { Formats } from './@types/Formats.js';
import { getFormattedUrl } from './utils.js';
export function defaultResponseHandler(response) {
if (response.data && response.data.statuscode !== 2000) {
throw new Error(response.data.longmessage);
}
return response;
}
export default class NetcupRestApi {
axios = axios.create();
format = Formats.JSON;
constructor(format) {
if (format) {
this.format = format;
}
this.axios.interceptors.response.use(defaultResponseHandler);
}
async postJson(url, data, options) {
const res = await this.axios.post(url, data, {
...options,
responseType: 'json',
});
return res.data;
}
login(params) {
return this.postJson(getFormattedUrl(this.format), {
action: Actions.login,
param: {
...params,
},
});
}
infoDnsZone(params) {
return this.postJson(getFormattedUrl(this.format), {
action: Actions.infoDnsZone,
param: {
...params,
},
});
}
infoDnsRecords(params) {
return this.postJson(getFormattedUrl(this.format), {
action: Actions.infoDnsRecords,
param: {
...params,
},
});
}
updateDnsRecords(params) {
return this.postJson(getFormattedUrl(this.format), {
action: Actions.updateDnsRecords,
param: {
...params,
},
});
}
}