Akismet for JS
Prevent comment spam using Akismet service, in JavaScript.
Features
- Key verification: checks an Akismet API key and gets a value indicating whether it is valid.
- Comment check: checks a comment and gets a value indicating whether it is spam.
- Submit spam: submits a comment that was not marked as spam but should have been.
- Submit ham: submits a comment that was incorrectly marked as spam but should not have been.
Requirements
The latest Node.js and npm versions. If you plan to play with the sources, you will also need the latest Gulp.js version.
Installing via npm
From a command prompt, run:
$ npm install --save @cedx/akismet
Usage
Key verification
const {Client} = require('@cedx/akismet');
try {
let client = new Client('YourAPIKey', 'http://your.blog.url');
let isValid = await client.verifyKey();
console.log(isValid ? 'Your API key is valid.' : 'Your API key is invalid.');
}
catch (error) {
console.log(`An error occurred: ${error}`);
}
Comment check
const {Author, Comment} = require('@cedx/akismet');
try {
let comment = new Comment(
new Author('127.0.0.1', 'Mozilla/5.0'),
'A comment.'
);
let isSpam = await client.checkComment(comment);
console.log(isSpam ? 'The comment is marked as spam.' : 'The comment is marked as ham.');
}
catch (error) {
console.log(`An error occurred: ${error}`);
}
Submit spam/ham
try {
await client.submitSpam(comment);
console.log('Spam submitted.');
await client.submitHam(comment);
console.log('Ham submitted.');
}
catch (error) {
console.log(`An error occurred: ${error}`);
}
Events
The Client
class is an EventEmitter.
During its life cycle, it emits these events:
request
: emitted every time a request is made to the remote service.response
: emitted every time a response is received from the remote service.
You can subscribe to them using the on()
method:
client.on('request', response => console.log(`Client request: ${request.url}`));
client.on('response', response => console.log(`Server response: ${response.statusCode}`));
Unit tests
In order to run the tests, you must set the AKISMET_API_KEY
environment variable to the value of your Akismet API key:
$ export AKISMET_API_KEY="<YourAPIKey>"
Then, you can run the test
script from the command prompt:
$ npm test
See also
License
Akismet for JS is distributed under the Apache License, version 2.0.