-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.ts
331 lines (296 loc) · 9.22 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
import { create as createDomain } from "domain";
import { post as postRequest, Response } from "request";
import { format as formatURL } from "url";
/**
* Defines the settings for the Akismet client.
*/
interface AkismetOptions {
/**
* The front page or home URL of the instance making the request. For a blog
* or wiki this would be the front page.
*
* _Note:_ Must be a full URI, including `http://`.
*/
blog?: string;
/**
* Akismet API key
*/
apiKey: string;
/**
* Akismet API URL, defaults to `rest.akismet.com`
*/
host?: string;
/**
* Akismet API endpoint, defaults to `apiKey.rest.akismet.com`
*/
endPoint?: string;
/**
* Akismet API server port, defaults to `80`
*/
port?: number;
/**
* The user agent string passed to Akismet API, defaults to
* `Generic Node.js/1.0.0 | Akismet/3.1.7`
*/
userAgent?: string;
/**
* The character set of comment texts, defaults to `utf-8`
*/
charSet?: string;
}
/**
* Defines the set of parameters passed to the Akismet API.
*/
interface AkismetParameters {
/**
* The front page or home URL of the instance making the request. For a blog
* or wiki this would be the front page.
*
* _Note:_ Must be a full URI, including `http://`.
*/
blog?: string;
/**
* IP address of the comment submitter.
*/
user_ip: string;
/**
* User agent string of the web browser submitting the comment - typically the
* `HTTP_USER_AGENT` cgi variable. Not to be confused with the user agent of
* your Akismet library.
*/
user_agent?: string;
/**
* The content of the `HTTP_REFERER` header should be sent here.
* (note spelling of `referrer`).
*/
referrer?: string;
/**
* The full permanent URL of the entry the comment was submitted to.
*/
permalink?: string;
/**
* A string that describes the type of content being sent. Examples:
* - `comment`: A blog comment.
* - `forum-post`: A top-level forum post.
* - `reply`: A reply to a top-level forum post.
* - `blog-post`: A blog post.
* - `contact-form`: A contact form or feedback form submission.
* - `signup`: A new user account.
* - `message`: A message sent between just a few users.
*
* You may send a value not listed above if none of them accurately describe
* your content. This is further explained here:
* http://blog.akismet.com/2012/06/19/pro-tip-tell-us-your-comment_type/
*/
comment_type?: string;
/**
* Name submitted with the comment.
*/
comment_author?: string;
/**
* Email address submitted with the comment.
*/
comment_author_email?: string;
/**
* URL submitted with comment.
*/
comment_author_url?: string;
/**
* The content that was submitted.
*/
comment_content?: string;
/**
* The UTC timestamp of the creation of the comment, in ISO 8601 format.
* May be omitted if the comment is sent to the API at the time it is created.
*/
comment_date_gmt?: string;
/**
* The UTC timestamp of the publication time for the post, page or thread on
* which the comment was posted.
*/
comment_post_modified_gmt?: string;
/**
* Indicates the language(s) in use on the blog or site, in ISO 639-1 format,
* comma-separated. A site with articles in English and French might use `en, fr_ca`.
*/
blog_lang?: string;
/**
* The character encoding for the form values included in `comment_*` parameters,
* such as `UTF-8` or `ISO-8859-1`.
*/
blog_charset?: string;
/**
* The user role of the user who submitted the comment. This is an optional parameter.
* If you set it to `administrator`, Akismet will always return `false`.
*/
user_role?: string;
/**
* This is an optional parameter. You can pass `1` when submitting test queries to Akismet.
*/
is_test?: number;
/**
* **Other server environmental variables:**
* In PHP, there is an array of environmental variables called `$_SERVER` that
* contains information about the Web server itself as well as a key/value for
* every HTTP header sent with the request. This data is highly useful to Akismet.
* How the submitted content interacts with the server can be very telling,
* so please include as much of it as possible.
*/
[key: string]: any;
}
/**
* Defines the callback for the `verifyKey` function.
*
* @param err - error message returned from the server.
* @param isValid - `true` if the API key is valid; otherwise `false`.
*/
type VerifyKeyCallback = (err: string | null, isValid: boolean) => void;
/**
* Defines the callback for the `checkComment` function.
*
* @param err - error message returned from the server.
* @param isSpam - `true` if the content is spam; otherwise `false`.
*/
type CheckCommentCallback = (err: string | null, isSpam: boolean) => void;
/**
* Defines the callback for the `submitSpam` and `submitHam` functions.
*
* @param err - error message returned from the server.
*/
type SubmitCallback = (err: string | null) => void;
/**
* Defines the callback for the `_postRequest` function.
*
* @param err - error message returned from the server.
* @param status - HTTP status code.
* @param body - response body.
*/
type PostRequestCallback = (err: string | null, status: number, body: string) => void;
/**
* Represents the Akismet API client.
*/
export class AkismetClient {
private _blog: string;
private _apiKey: string;
private _host: string;
private _endPoint: string;
private _port: number;
private _userAgent: string;
private _charSet: string;
/**
* Initialize the Akismet client.
*
* @param options - client settings
*/
constructor(options: AkismetOptions) {
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
*/
public verifyKey(callback: VerifyKeyCallback): void {
const options = { key: this._apiKey, blog: this._blog };
this._postRequest(this._host, "/1.1/verify-key", options,
(err: string | null, status: number, body: string) => {
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
*/
public checkComment(options: AkismetParameters, callback: CheckCommentCallback): void {
options.blog = this._blog;
options.user_agent = this._userAgent;
this._postRequest(this._endPoint, "/1.1/comment-check", options,
(err: string | null, status: number, body: string) => {
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
*/
public submitSpam(options: AkismetParameters, callback: SubmitCallback): void {
options.blog = this._blog;
options.user_agent = this._userAgent;
this._postRequest(this._endPoint, "/1.1/submit-spam", options,
(err: string | null, status: number, body: string) => {
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
*/
public submitHam(options: AkismetParameters, callback: SubmitCallback): void {
options.blog = this._blog;
options.user_agent = this._userAgent;
this._postRequest(this._endPoint, "/1.1/submit-ham", options,
(err: string | null, status: number, body: string) => {
callback(err);
});
}
/**
* Posts a request to the Akismet API server.
*/
private _postRequest(hostname: string, path: string,
query: { [key: string]: any }, callback: PostRequestCallback): void {
const requestUrl = formatURL({
protocol: this._port === 443 ? "https" : "http",
hostname: hostname,
pathname: path,
port: this._port
});
const options = {
"url": requestUrl,
"form": query,
"headers": {
"content-type": "charset=" + this._charSet,
"user-agent": this._userAgent
}
};
const dom = createDomain();
dom.on("error", (err) => callback(err, 0, ""));
dom.run(() => {
postRequest(options, (err: any, response: Response, body: any) => {
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
*/
private checkSpam(options: AkismetParameters, callback: CheckCommentCallback): void {
this.checkComment(options, callback);
}
}
/**
* Creates and returns a new Akismet client.
*
* @param options - client settings
*/
export function client(options: AkismetOptions): AkismetClient {
return new AkismetClient(options);
}