From a8f596ee11d478cdbd31d105a49a5c7f76ad91f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Belin?= Date: Sat, 12 Aug 2017 02:41:04 +0200 Subject: [PATCH] Replaced the SuperAgent HTTP client by `node-fetch` --- README.md | 2 +- lib/client.js | 41 ++++++++++++++++++++--------------------- 2 files changed, 21 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 93a03b35..e3d7ce0c 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,7 @@ client.onRequest.subscribe(request => ); client.onResponse.subscribe(response => - console.log(`Server response: ${response.statusCode}`) + console.log(`Server response: ${response.status}`) ); ``` diff --git a/lib/client.js b/lib/client.js index fc7d2478..efb45d08 100644 --- a/lib/client.js +++ b/lib/client.js @@ -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'); @@ -69,20 +70,20 @@ exports.Client = class Client { /** * The handler of "request" events. - * @type {Subject} + * @type {Subject} */ this._onRequest = new Subject(); /** * The handler of "response" events. - * @type {Subject} + * @type {Subject} */ this._onResponse = new Subject(); } /** * The stream of "request" events. - * @type {Observable} + * @type {Observable} */ get onRequest() { return this._onRequest.asObservable(); @@ -90,7 +91,7 @@ exports.Client = class Client { /** * The stream of "response" events. - * @type {Observable} + * @type {Observable} */ get onResponse() { return this._onResponse.asObservable(); @@ -102,8 +103,7 @@ exports.Client = class Client { * @return {Observable} 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'); } @@ -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()); } @@ -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()); } @@ -165,8 +163,8 @@ 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} 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.')); @@ -174,17 +172,18 @@ exports.Client = class Client { 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()); }); } };