node-swallowjs
Version:
epp portal
269 lines (250 loc) • 8.97 kB
JavaScript
;
(function (global, factory) {
if (typeof define === "function" && define.amd) {
define(["exports", "http", "querystring"], factory);
} else if (typeof exports !== "undefined") {
factory(exports, require("http"), require("querystring"));
} else {
var mod = {
exports: {}
};
factory(mod.exports, global.http, global.querystring);
global.eppAPI = mod.exports;
}
})(this, function (exports, http, qs) {
Object.defineProperty(exports, "__esModule", {
value: true
});
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
var _createClass = (function () {
function defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
}
return function (Constructor, protoProps, staticProps) {
if (protoProps) defineProperties(Constructor.prototype, protoProps);
if (staticProps) defineProperties(Constructor, staticProps);
return Constructor;
};
})();
var eppAPI = (function () {
function eppAPI(path, host, port) {
_classCallCheck(this, eppAPI);
function post(host, port, path) {
var sessionID = "swallow_epp_sessionid";
var sessionValue = "";
return function (command, json, cb) {
var content = JSON.stringify(json);
var options = {
host: host,
port: port,
path: path + "/api/" + command,
method: "POST",
headers: {
"Content-Type": "application/json; charset=utf-8",
"Content-Length": content.length,
"Cookie": sessionID + "=" + sessionValue
}
};
var req = http.request(options, function (res) {
var content = '';
res.on('data', function (data) {
content += data;
});
res.on('end', function () {
if (res.statusCode === 200) {
var cookie = res.headers["set-cookie"];
for (var i in cookie) {
var c = qs.parse(cookie[i], /;\s*/);
if (c[sessionID]) sessionValue = c[sessionID];
}
var _json = JSON.parse(content);
cb(_json.code, _json.data);
} else cb();
});
});
req.on('error', cb);
req.write(content);
req.end();
};
}
function ajax(path) {
return function (command, pair, cb) {
var xhr = new XMLHttpRequest();
var state = 0;
xhr.onreadystatechange = function (e) {
if (state === this.readyState) return;
state = this.readyState;
switch (this.readyState) {
case 4:
this.status === 200 ? cb(this.response.code, this.response.data) : cb();
}
};
xhr.open('POST', path + '/api/' + command, true);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(JSON.stringify(pair));
xhr.responseType = 'json';
};
}
this.post = typeof require === 'function' ? post(host, port, path) : ajax(path);
}
_createClass(eppAPI, [{
key: "login",
value: function login(Username, Password, cb) {
this.post('login', {
Username: Username,
Password: Password
}, cb);
}
}, {
key: "check",
value: function check(obj, name, cb) {
this.post('check', {
obj: obj,
name: name
}, cb);
}
}, {
key: "info",
value: function info(obj, name, cb) {
this.post('info', {
obj: obj,
name: name
}, cb);
}
}, {
key: "create",
value: function create(obj, json, cb) {
this.post('create', {
obj: obj,
json: json
}, cb);
}
}, {
key: "delete",
value: function _delete(obj, name, cb) {
this.post('delete', {
obj: obj,
name: name
}, cb);
}
}, {
key: "renew",
value: function renew(name, y, cb) {
this.post('renew', {
name: name,
y: y
}, cb);
}
}, {
key: "transfer",
value: function transfer(op, json, cb) {
this.post('transfer', {
op: op,
json: json
}, cb);
}
}, {
key: "update",
value: function update(obj, json, cb) {
this.post('update', {
obj: obj,
json: json
}, cb);
}
}, {
key: "restore",
value: function restore(op, json, cb) {
this.post('restore', {
op: op,
json: json
}, cb);
}
}, {
key: "list",
value: function list(obj, page, where, cb) {
this.post('list', {
obj: obj,
page: page,
where: where
}, cb);
}
}, {
key: "show",
value: function show(obj) {
for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
args[_key - 1] = arguments[_key];
}
var cb = args.pop(),
name = args[0];
this.post('show', {
obj: obj,
name: name
}, cb);
}
}, {
key: "load",
value: function load(obj, name, cb) {
this.post('load', {
obj: obj,
name: name
}, cb);
}
}, {
key: "pdns",
value: function pdns(action, domain, record, cb) {
this.post('pdns', {
action: action,
domain: domain,
record: record
}, cb);
}
}, {
key: "sendVerifyCode",
value: function sendVerifyCode(email, cb) {
this.post('sendVerifyCode', {
email: email
}, cb);
}
}, {
key: "compVerifyCode",
value: function compVerifyCode(email, verifyCode, cb) {
this.post('compVerifyCode', {
email: email,
verifyCode: verifyCode
}, cb);
}
}, {
key: "resultRegistrant",
value: function resultRegistrant(contactId, cb) {
this.post('resultRegistrant', {
contactId: contactId
}, cb);
}
}, {
key: "uploadRegistrant",
value: function uploadRegistrant(contactId, json, cb) {
this.post('uploadRegistrant', {
contactId: contactId,
json: json
}, cb);
}
}, {
key: "logout",
value: function logout(cb) {
this.post('logout', {}, cb);
}
}]);
return eppAPI;
})();
exports.default = eppAPI;
});