Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

wpcom-xhr-request

Package Overview
Dependencies
Maintainers
5
Versions
30
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

wpcom-xhr-request - npm Package Compare versions

Comparing version 1.0.0-beta.4 to 1.0.0-beta.5

241

build/index.js

@@ -33,3 +33,3 @@ 'use strict';

/**
* WordPress.com REST API base endpoint.
* Defauts
*/

@@ -40,118 +40,30 @@

*/
var proxyOrigin = 'https://public-api.wordpress.com';
var defaults = {
apiVersion: '1',
apiNamespace: 'wp/v2',
authToken: null,
body: null,
formData: null,
headers: null,
method: 'get',
query: null,
processResponseInEnvelopeMode: true,
proxyOrigin: 'https://public-api.wordpress.com',
url: ''
};
/**
* Default WordPress.com REST API Version.
*/
var defaultApiVersion = '1';
/**
* Performs an XMLHttpRequest against the WordPress.com REST API.
* Send the request
*
* @param {Object|String} originalParams - request parameters
* @param {Function} fn - callback function
* @return { XHR } xhr instance
* @api public
* @param {Superagent} req - request instance
* @param {Object} settings - request settings
* @param {Function} fn - callback function
* @return {Superagent} request instance
*/
function request(originalParams, fn) {
var params = (0, _assign2.default)({}, originalParams);
var sendResponse = function sendResponse(req, settings, fn) {
var isEnvelopeMode = settings.isEnvelopeMode;
var isRestAPI = settings.isRestAPI;
var processResponseInEnvelopeMode = settings.processResponseInEnvelopeMode;
if ('string' === typeof params) {
params = { path: params };
}
// method
var requestMethod = (params.method || 'GET').toLowerCase();
debug('API HTTP Method: %o', requestMethod);
delete params.method;
// apiNamespace (WP-API)
var _params = params;
var apiNamespace = _params.apiNamespace;
delete params.apiNamespace;
// apiVersion (REST-API)
var apiVersion = params.apiVersion || defaultApiVersion;
delete params.apiVersion;
// proxyOrigin
proxyOrigin = params.proxyOrigin || proxyOrigin;
delete params.proxyOrigin;
// request base path
var basePath = '/rest/v' + apiVersion;
// If this is a WP-API request, adjust basePath
if (apiNamespace && /\//.test(apiNamespace)) {
// New-style WP-API URL: /wpcom/v2/sites/%s/post-counts
basePath = '/' + apiNamespace;
} else if (apiNamespace) {
// Old-style WP-API URL (deprecated): /wp-json/sites/%s/wpcom/v2/post-counts
basePath = '/wp-json';
}
// is REST-API api?
var isRestAPI = apiNamespace === undefined;
// Envelope mode FALSE as default
var isEnvelopeMode = false;
// process response in evelope mode TRUE as default
var processResponseInEnvelopeMode = params.processResponseInEnvelopeMode !== false;
var url = proxyOrigin + basePath + params.path;
debug('API URL: %o', url);
delete params.path;
// create HTTP Request object
var req = _superagent2.default[requestMethod](url);
// Token authentication
if (params.authToken) {
req.set('Authorization', 'Bearer ' + params.authToken);
delete params.authToken;
}
// URL querystring values
if (params.query) {
req.query(params.query);
debug('API send URL querystring: %o', params.query);
isEnvelopeMode = isRestAPI ? params.query.http_envelope : params.query._envelope;
delete params.query;
}
// POST API request body
if (params.body) {
req.send(params.body);
debug('API send POST body: %o', params.body);
delete params.body;
}
// POST FormData (for `multipart/form-data`, usually a file upload)
if (params.formData) {
for (var i = 0; i < params.formData.length; i++) {
var data = params.formData[i];
var key = data[0];
var value = data[1];
debug('adding FormData field %o', key);
req.field(key, value);
}
}
var requestHeaders = params.headers || {};
for (var _key in requestHeaders) {
var _value = requestHeaders[_key];
debug('adding HTTP header %o: %o', _key, _value);
req.set(_key, _value);
}
if (!req.get('Accept')) {
// set a default "Accept" header preferring a JSON response
req.set('Accept', '*/json,*/*');
}
// start the request
req.end(function (error, response) {

@@ -172,11 +84,6 @@ if (error && !response) {

debug('%o -> %o headers', url, headers);
debug('%o -> %o status code', url, statusCode);
if (ok) {
if (isEnvelopeMode && processResponseInEnvelopeMode) {
debug('processing response in envelope mode');
// override `error`, body` and `headers`
if (isRestAPI) {
// in the REST-API the response comes wrapped in `code`, `headers` and `body` fields
headers = body.headers;

@@ -186,3 +93,2 @@ statusCode = body.code;

} else {
// in the WP-API the response comes wrapped in `body`, `status` and `headers`
headers = body.headers;

@@ -193,3 +99,2 @@ statusCode = body.status;

// let's add the status into the headers
headers.status = statusCode;

@@ -210,2 +115,100 @@

return req;
};
/**
* Performs an XMLHttpRequest against the WordPress.com REST API.
*
* @param {Object|String} options - `request path` or `request parameters`
* @param {Function} fn - callback function
* @return { XHR } xhr instance
* @api public
*/
function request(options, fn) {
if ('string' === typeof options) {
options = { path: options };
}
var settings = (0, _assign2.default)({}, defaults, options);
// is REST-API api?
settings.isRestAPI = options.apiNamespace === undefined;
var apiNamespace = settings.apiNamespace;
var apiVersion = settings.apiVersion;
var authToken = settings.authToken;
var body = settings.body;
var formData = settings.formData;
var headers = settings.headers;
var isRestAPI = settings.isRestAPI;
var method = settings.method;
var query = settings.query;
var proxyOrigin = settings.proxyOrigin;
settings.method = method.toLowerCase();
// request base path
var basePath = void 0;
if (isRestAPI) {
basePath = '/rest/v' + apiVersion;
} else if (apiNamespace && /\//.test(apiNamespace)) {
basePath = '/' + apiNamespace; // wpcom/v2
} else {
basePath = '/wp-json'; // /wp-json/sites/%s/wpcom/v2 (deprecated)
}
// Envelope mode FALSE as default
settings.isEnvelopeMode = false;
settings.url = proxyOrigin + basePath + settings.path;
debug('API URL: %o', settings.url);
// create HTTP Request instance
var req = _superagent2.default[method](settings.url);
// querystring
if (query) {
req.query(query);
debug('API send URL querystring: %o', query);
settings.isEnvelopeMode = isRestAPI ? query.http_envelope : query._envelope;
debug('envelope mode: %o', settings.isEnvelopeMode);
}
// body
if (body) {
req.send(body);
debug('API send POST body: %o', body);
}
// POST FormData (for `multipart/form-data`, usually a file upload)
if (formData) {
for (var i = 0; i < formData.length; i++) {
var data = formData[i];
var key = data[0];
var value = data[1];
debug('adding FormData field %o', key);
req.field(key, value);
}
}
// headers
if (headers) {
req.set(headers);
debug('adding HTTP headers: %o', headers);
}
if (authToken) {
req.set('Authorization', 'Bearer ' + authToken);
}
if (!req.get('Accept')) {
// set a default "Accept" header preferring a JSON response
req.set('Accept', '*/json,*/*');
}
sendResponse(req, settings, fn);
return req.xhr;

@@ -212,0 +215,0 @@ }

@@ -12,3 +12,3 @@ {

],
"version": "1.0.0-beta.4",
"version": "1.0.0-beta.5",
"scripts": {

@@ -15,0 +15,0 @@ "prepublish": "make build"

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc