akismet
Version:
Akismet API client for node.js
127 lines (126 loc) • 4.6 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.client = exports.AkismetClient = void 0;
var domain_1 = require("domain");
var request_1 = require("request");
var url_1 = require("url");
/**
* Represents the Akismet API client.
*/
var AkismetClient = /** @class */ (function () {
/**
* Initialize the Akismet client.
*
* @param options - client settings
*/
function AkismetClient(options) {
this._blog = options.blog || "";
this._apiKey = options.apiKey;
this._host = options.host || "rest.akismet.com";
this._endPoint = options.endPoint || (this._apiKey + "." + this._host);
this._port = options.port || 80;
this._userAgent = options.userAgent || "Generic Node.js/1.0.0 | Akismet/3.1.7";
this._charSet = options.charSet || "utf-8";
}
/**
* Verifies the API key.
*
* @param callback - callback function
*/
AkismetClient.prototype.verifyKey = function (callback) {
var options = { key: this._apiKey, blog: this._blog };
this._postRequest(this._host, "/1.1/verify-key", options, function (err, status, body) {
callback(err, status >= 200 && status < 300 && body === "valid");
});
};
/**
* Checks if a comment is spam.
*
* @param options - options to send to the Akismet API
* @param callback - callback function
*/
AkismetClient.prototype.checkComment = function (options, callback) {
options.blog = options.blog || this._blog;
options.user_agent = options.user_agent || this._userAgent;
this._postRequest(this._endPoint, "/1.1/comment-check", options, function (err, status, body) {
callback(err, status >= 200 && status < 300 && body === "true");
});
};
/**
* Marks a comment as spam and reports to the Akismet API.
*
* @param options - options to send to the Akismet API
* @param callback - callback function
*/
AkismetClient.prototype.submitSpam = function (options, callback) {
options.blog = options.blog || this._blog;
options.user_agent = options.user_agent || this._userAgent;
this._postRequest(this._endPoint, "/1.1/submit-spam", options, function (err, status, body) {
callback(err);
});
};
/**
* Marks a comment as NOT spam and reports to the Akismet API.
*
* @param options - options to send to the Akismet API
* @param callback - callback function
*/
AkismetClient.prototype.submitHam = function (options, callback) {
options.blog = options.blog || this._blog;
options.user_agent = options.user_agent || this._userAgent;
this._postRequest(this._endPoint, "/1.1/submit-ham", options, function (err, status, body) {
callback(err);
});
};
/**
* Posts a request to the Akismet API server.
*/
AkismetClient.prototype._postRequest = function (hostname, path, query, callback) {
var requestUrl = (0, url_1.format)({
protocol: this._port === 443 ? "https" : "http",
hostname: hostname,
pathname: path,
port: this._port
});
var options = {
"url": requestUrl,
"form": query,
"headers": {
"content-type": "charset=" + this._charSet,
"user-agent": this._userAgent
}
};
var dom = (0, domain_1.create)();
dom.on("error", function (err) { return callback(err, 0, ""); });
dom.run(function () {
(0, request_1.post)(options, function (err, response, body) {
if (err) {
callback(err, 0, "");
}
else {
callback(null, response.statusCode, body);
}
});
});
};
/**
* Historic alias of `checkComment`.
*
* @param options - options to send to the Akismet API
* @param callback - callback function
*/
AkismetClient.prototype.checkSpam = function (options, callback) {
this.checkComment(options, callback);
};
return AkismetClient;
}());
exports.AkismetClient = AkismetClient;
/**
* Creates and returns a new Akismet client.
*
* @param options - client settings
*/
function client(options) {
return new AkismetClient(options);
}
exports.client = client;