Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use request package instead of node-fetch #13

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const nodeFetch = require( 'node-fetch' );
const {
defaultFetch,
fetchNotifications,
getAdditionalDataFetcher,
sendMarkNotificationRead
Expand All @@ -8,7 +8,7 @@ const { makeConverter } = require( './lib/converter' );

function createNoteGetter( options = {} ) {
const defaultOptions = {
fetch: nodeFetch,
fetch: defaultFetch,
log: () => {},
};
const mergedOptions = Object.assign( {}, defaultOptions, options );
Expand All @@ -23,7 +23,7 @@ function createNoteGetter( options = {} ) {

function createNoteMarkRead( options = {} ) {
const defaultOptions = {
fetch: nodeFetch,
fetch: defaultFetch,
log: () => {},
};
const mergedOptions = Object.assign( {}, defaultOptions, options );
Expand Down
38 changes: 38 additions & 0 deletions lib/fetchers.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
const get = require( 'lodash.get' );
const request = require( 'request' );
const withQuery = require( 'with-query' ).default;

function getFetchInit( token, method = 'GET' ) {
return {
method,
headers: {
'User-Agent': 'request',
Authorization: 'token ' + token,
},
};
}

function convertToJson( result ) {
if ( ! result.json ) {
return result;
}
return result.json();
}

Expand Down Expand Up @@ -118,8 +123,41 @@ function sendMarkNotificationRead( options, token, note, params ) {
.then( checkForHttpErrors );
}

/**
* Transforms the result of `request` into a response object like the one used
* by `fetch`.
*/
function getResponseObjectFromRequest( json, responseData ) {
return {
ok: responseData && responseData.statusCode > 199 && responseData.statusCode < 300,
status: responseData ? responseData.statusCode : 500,
statusText: responseData ? ( responseData.statusText || 'OK' ) : 'NOT-OK',
json: () => Promise.resolve( JSON.parse( json ) ),
};
}

/**
* Makes an http request using `request`, but the response is returned by a
* Promise in the same format as `fetch`.
*/
function defaultFetch( url, options ) {
options.url = url;
return new Promise( ( resolve, reject ) => {
request( options, ( error, response, body ) => {
if ( error ) {
return reject( error );
}
if ( response.statusCode !== 200 ) {
return reject( response );
}
resolve( getResponseObjectFromRequest( body, response ) );
} );
} );
}

module.exports = {
fetchNotifications,
getAdditionalDataFetcher,
sendMarkNotificationRead,
defaultFetch,
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"dependencies": {
"lodash.get": "^4.4.2",
"md5-hex": "^2.0.0",
"node-fetch": "^2.2.0",
"request": "^2.88.0",
"with-query": "^1.1.2"
},
"devDependencies": {
Expand Down
Loading