Skip to content

Commit eedfdba

Browse files
committed
Use spread operator instead of node.extend to reduce bundle size
1 parent faaadd9 commit eedfdba

File tree

7 files changed

+31
-23
lines changed

7 files changed

+31
-23
lines changed

lib/constructors/wp-request.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
const qs = require( 'qs' );
44
const _unique = require( 'lodash.uniq' );
5-
const extend = require( 'node.extend' );
65

76
const alphaNumericSort = require( '../util/alphanumeric-sort' );
87
const keyValToObj = require( '../util/key-val-to-obj' );
@@ -227,11 +226,16 @@ const validatePathLevel = ( levelDefinitions, levelContents ) => {
227226
*/
228227
WPRequest.prototype._renderQuery = function() {
229228
// Build the full query parameters object
230-
const queryParams = extend( {}, populated( this._params ) );
229+
const queryParams = {
230+
...populated( this._params ),
231+
};
231232

232233
// Prepare any taxonomies and merge with other filter values
233234
const taxonomies = prepareTaxonomies( this._taxonomyFilters );
234-
queryParams.filter = extend( {}, populated( this._filters ), taxonomies );
235+
queryParams.filter = {
236+
...populated( this._filters ),
237+
...taxonomies,
238+
};
235239

236240
// Parse query parameters object into a query string, sorting the object
237241
// properties by alphabetical order (consistent property ordering can make

lib/endpoint-factories.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
*/
77
'use strict';
88

9-
const extend = require( 'node.extend' );
109
const createResourceHandlerSpec = require( './resource-handler-spec' ).create;
1110
const createEndpointRequest = require( './endpoint-request' ).create;
1211
const objectReduce = require( './util/object-reduce' );
@@ -39,7 +38,10 @@ function generateEndpointFactories( routesByNamespace ) {
3938
// "handler" object is now fully prepared; create the factory method that
4039
// will instantiate and return a handler instance
4140
handlers[ resource ] = function( options ) {
42-
return new EndpointRequest( extend( {}, this._options, options ) );
41+
return new EndpointRequest( {
42+
...this._options,
43+
...options,
44+
} );
4345
};
4446

4547
// Expose the constructor as a property on the factory function, so that

lib/http-transport.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ const url = require( 'url' );
99

1010
const WPRequest = require( './constructors/wp-request' );
1111
const checkMethodSupport = require( './util/check-method-support' );
12-
const extend = require( 'node.extend' );
1312
const objectReduce = require( './util/object-reduce' );
1413
const isEmptyObject = require( './util/is-empty-object' );
1514

@@ -177,18 +176,20 @@ function createPaginationObject( result, options, httpTransport ) {
177176

178177
// Create a WPRequest instance pre-bound to the "next" page, if available
179178
if ( links.next ) {
180-
_paging.next = new WPRequest( extend( {}, options, {
179+
_paging.next = new WPRequest( {
180+
...options,
181181
transport: httpTransport,
182182
endpoint: mergeUrl( endpoint, links.next ),
183-
} ) );
183+
} );
184184
}
185185

186186
// Create a WPRequest instance pre-bound to the "prev" page, if available
187187
if ( links.prev ) {
188-
_paging.prev = new WPRequest( extend( {}, options, {
188+
_paging.prev = new WPRequest( {
189+
...options,
189190
transport: httpTransport,
190191
endpoint: mergeUrl( endpoint, links.prev ),
191-
} ) );
192+
} );
192193
}
193194

194195
return _paging;

lib/mixins/filters.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
'use strict';
55

66
const _unique = require( 'lodash.uniq' );
7-
const extend = require( 'node.extend' );
87

98
const alphaNumericSort = require( '../util/alphanumeric-sort' );
109
const keyValToObj = require( '../util/key-val-to-obj' );
@@ -57,7 +56,10 @@ filterMixins.filter = function( props, value ) {
5756
props = keyValToObj( props, value );
5857
}
5958

60-
this._filters = extend( this._filters, props );
59+
this._filters = {
60+
...this._filters,
61+
...props,
62+
};
6163

6264
return this;
6365
};

lib/wp-register-route.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
'use strict';
22

3-
const extend = require( 'node.extend' );
4-
53
const buildRouteTree = require( './route-tree' ).build;
64
const generateEndpointFactories = require( './endpoint-factories' ).generate;
75
const paramSetter = require( './util/parameter-setter' );
@@ -90,10 +88,11 @@ function registerRoute( namespace, restBase, options = {} ) {
9088
} );
9189
}
9290

93-
function endpointFactory( options ) {
94-
options = options || {};
95-
options = extend( options, this && this._options );
96-
return new EndpointRequest( options );
91+
function endpointFactory( options = {} ) {
92+
return new EndpointRequest( {
93+
...options,
94+
...( this ? this._options : {} ),
95+
} );
9796
}
9897
endpointFactory.Ctor = EndpointRequest;
9998

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@
6060
"dependencies": {
6161
"li": "^1.0.1",
6262
"lodash.uniq": "^4.3.0",
63-
"node.extend": "^1.1.5",
6463
"parse-link-header": "^0.4.1",
6564
"qs": "^6.2.0",
6665
"route-parser": "0.0.4",

wpapi.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
*/
1515
'use strict';
1616

17-
const extend = require( 'node.extend' );
1817
const objectReduce = require( './lib/util/object-reduce' );
1918

2019
// This JSON file provides enough data to create handler methods for all valid
@@ -232,10 +231,10 @@ WPAPI.site = function( endpoint, routes ) {
232231
* @returns {WPRequest} A WPRequest object bound to the provided URL
233232
*/
234233
WPAPI.prototype.url = function( url ) {
235-
const options = extend( {}, this._options, {
234+
return new WPRequest( {
235+
...this._options,
236236
endpoint: url,
237237
} );
238-
return new WPRequest( options );
239238
};
240239

241240
/**
@@ -248,7 +247,9 @@ WPAPI.prototype.url = function( url ) {
248247
*/
249248
WPAPI.prototype.root = function( relativePath ) {
250249
relativePath = relativePath || '';
251-
const options = extend( {}, this._options );
250+
const options = {
251+
...this._options,
252+
};
252253
// Request should be
253254
const request = new WPRequest( options );
254255

0 commit comments

Comments
 (0)