Home Manual Reference Source Test

Akismet for JS

Runtime Release License Downloads Dependencies Coverage Build

Prevent comment spam using Akismet service, in JavaScript.

Features

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:

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.