Skip to content

Commit

Permalink
Replaced the SuperAgent HTTP client by node-fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
cedx committed Aug 12, 2017
1 parent 77db173 commit a8f596e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 22 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ client.onRequest.subscribe(request =>
);

client.onResponse.subscribe(response =>
console.log(`Server response: ${response.statusCode}`)
console.log(`Server response: ${response.status}`)
);
```

Expand Down
41 changes: 20 additions & 21 deletions lib/client.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict';

const {default: fetch, Request} = require('node-fetch');
const {stringify} = require('querystring');
const {Observable, Subject} = require('rxjs');
const superagent = require('superagent');
const {URL} = require('url');

const {Blog} = require('./blog');
Expand Down Expand Up @@ -69,28 +70,28 @@ exports.Client = class Client {

/**
* The handler of "request" events.
* @type {Subject<superagent.Request>}
* @type {Subject<Request>}
*/
this._onRequest = new Subject();

/**
* The handler of "response" events.
* @type {Subject<superagent.Response>}
* @type {Subject<Response>}
*/
this._onResponse = new Subject();
}

/**
* The stream of "request" events.
* @type {Observable<superagent.Request>}
* @type {Observable<Request>}
*/
get onRequest() {
return this._onRequest.asObservable();
}

/**
* The stream of "response" events.
* @type {Observable<superagent.Response>}
* @type {Observable<Response>}
*/
get onResponse() {
return this._onResponse.asObservable();
Expand All @@ -102,8 +103,7 @@ exports.Client = class Client {
* @return {Observable<boolean>} A boolean value indicating whether it is spam.
*/
checkComment(comment) {
let baseURL = `${this.endPoint.protocol}//${this.apiKey}.${this.endPoint.host}`;
let endPoint = new URL('1.1/comment-check', baseURL);
let endPoint = new URL('1.1/comment-check', `${this.endPoint.protocol}//${this.apiKey}.${this.endPoint.host}`);
return this._fetch(endPoint, comment.toJSON()).map(res => res == 'true');
}

Expand All @@ -113,8 +113,7 @@ exports.Client = class Client {
* @return {Observable} Completes once the comment has been submitted.
*/
submitHam(comment) {
let baseURL = `${this.endPoint.protocol}//${this.apiKey}.${this.endPoint.host}`;
let endPoint = new URL('1.1/submit-ham', baseURL);
let endPoint = new URL('1.1/submit-ham', `${this.endPoint.protocol}//${this.apiKey}.${this.endPoint.host}`);
return this._fetch(endPoint, comment.toJSON());
}

Expand All @@ -124,8 +123,7 @@ exports.Client = class Client {
* @return {Observable} Completes once the comment has been submitted.
*/
submitSpam(comment) {
let baseURL = `${this.endPoint.protocol}//${this.apiKey}.${this.endPoint.host}`;
let endPoint = new URL('1.1/submit-spam', baseURL);
let endPoint = new URL('1.1/submit-spam', `${this.endPoint.protocol}//${this.apiKey}.${this.endPoint.host}`);
return this._fetch(endPoint, comment.toJSON());
}

Expand Down Expand Up @@ -165,26 +163,27 @@ exports.Client = class Client {
* @param {URL} endPoint The URL of the end point to query.
* @param {object} fields The fields describing the query body.
* @return {Observable<string>} The response as string.
* @emits {superagent~Request} The "request" event.
* @emits {superagent~Response} The "response" event.
* @emits {Request} The "request" event.
* @emits {Response} The "response" event.
*/
_fetch(endPoint, fields) {
if (!this.apiKey.length || !this.blog) return Observable.throw(new Error('The API key or the blog URL is empty.'));

let bodyFields = Object.assign(this.blog.toJSON(), fields);
if (this.isTest) bodyFields.is_test = '1';

let req = superagent.post(endPoint.href)
.type('form')
.set('User-Agent', this.userAgent)
.send(bodyFields);
let req = new Request(endPoint.href, {
method: 'POST',
headers: {'Content-Type': 'application/x-www-form-urlencoded', 'User-Agent': this.userAgent},
body: stringify(bodyFields)
});

this._onRequest.next(req);
return Observable.from(req).map(res => {
return Observable.from(fetch(req)).mergeMap(res => {
this._onResponse.next(res);
if (!res.ok) throw new Error(`${res.status} ${res.statusText}`);
if (Client.DEBUG_HEADER in res.header) throw new Error(res.header[Client.DEBUG_HEADER]);
return res.text;
if (!res.ok) return Observable.throw(new Error(`${res.status} ${res.statusText}`));
if (res.headers.has(Client.DEBUG_HEADER)) return Observable.throw(new Error(res.headers.get(Client.DEBUG_HEADER)));
return Observable.from(res.text());
});
}
};

0 comments on commit a8f596e

Please sign in to comment.