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 a unique querystring package instead of three different ones #8300

Merged
merged 4 commits into from
Aug 1, 2018
Merged
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: 4 additions & 2 deletions core-blocks/embed/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
*/
import { parse } from 'url';
import { includes, kebabCase, toLower } from 'lodash';
import { stringify } from 'querystring';
import memoize from 'memize';
import classnames from 'classnames';

Expand All @@ -16,6 +15,7 @@ import { Button, Placeholder, Spinner, SandBox, IconButton, Toolbar } from '@wor
import { createBlock } from '@wordpress/blocks';
import { RichText, BlockControls } from '@wordpress/editor';
import apiFetch from '@wordpress/api-fetch';
import { addQueryArgs } from '@wordpress/url';

/**
* Internal dependencies
Expand All @@ -27,7 +27,9 @@ import './editor.scss';
const HOSTS_NO_PREVIEWS = [ 'facebook.com' ];

// Caches the embed API calls, so if blocks get transformed, or deleted and added again, we don't spam the API.
const wpEmbedAPI = memoize( ( url ) => apiFetch( { path: `/oembed/1.0/proxy?${ stringify( { url } ) }` } ) );
const wpEmbedAPI = memoize( ( url ) =>
apiFetch( { path: addQueryArgs( '/oembed/1.0/proxy', { url } ) } )
);

const matchesPatterns = ( url, patterns = [] ) => {
return patterns.some( ( pattern ) => {
Expand Down
14 changes: 7 additions & 7 deletions core-blocks/latest-posts/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import { get, isUndefined, pickBy } from 'lodash';
import moment from 'moment';
import classnames from 'classnames';
import { stringify } from 'querystringify';

/**
* WordPress dependencies
Expand All @@ -27,6 +26,7 @@ import {
BlockAlignmentToolbar,
BlockControls,
} from '@wordpress/editor';
import { addQueryArgs } from '@wordpress/url';

/**
* Internal dependencies
Expand Down Expand Up @@ -160,19 +160,19 @@ class LatestPostsEdit extends Component {

export default withAPIData( ( props ) => {
const { postsToShow, order, orderBy, categories } = props.attributes;
const latestPostsQuery = stringify( pickBy( {
const latestPostsQuery = pickBy( {
categories,
order,
orderby: orderBy,
per_page: postsToShow,
_fields: [ 'date_gmt', 'link', 'title' ],
}, ( value ) => ! isUndefined( value ) ) );
const categoriesListQuery = stringify( {
}, ( value ) => ! isUndefined( value ) );
const categoriesListQuery = {
per_page: 100,
_fields: [ 'id', 'name', 'parent' ],
} );
};
return {
latestPosts: `/wp/v2/posts?${ latestPostsQuery }`,
categoriesList: `/wp/v2/categories?${ categoriesListQuery }`,
latestPosts: addQueryArgs( '/wp/v2/posts', latestPostsQuery ),
categoriesList: addQueryArgs( '/wp/v2/categories', categoriesListQuery ),
};
} )( LatestPostsEdit );
23 changes: 6 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
"moment": "2.22.1",
"moment-timezone": "0.5.16",
"prop-types": "15.5.10",
"querystringify": "1.0.0",
"re-resizable": "4.7.1",
"react": "16.4.1",
"react-dom": "16.4.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@
"@wordpress/i18n": "file:../i18n",
"@wordpress/is-shallow-equal": "file:../is-shallow-equal",
"@wordpress/keycodes": "file:../keycodes",
"@wordpress/url": "file:../url",
"classnames": "^2.2.5",
"clipboard": "^1.7.1",
"dom-scroll-into-view": "^1.2.1",
"element-closest": "^2.0.2",
"http-build-query": "^0.7.0",
"lodash": "^4.17.10",
"memize": "^1.0.5",
"moment": "^2.22.1",
Expand Down
8 changes: 5 additions & 3 deletions packages/components/src/server-side-render/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
} from '@wordpress/element';
import { __, sprintf } from '@wordpress/i18n';
import apiFetch from '@wordpress/api-fetch';
import httpBuildQuery from 'http-build-query';
import { addQueryArgs } from '@wordpress/url';

/**
* Internal dependencies.
Expand All @@ -21,8 +21,10 @@ import Placeholder from '../placeholder';
import Spinner from '../spinner';

export function rendererPathWithAttributes( block, attributes = null ) {
return `/gutenberg/v1/block-renderer/${ block }?context=edit` +
( null !== attributes ? '&' + httpBuildQuery( { attributes } ) : '' );
return addQueryArgs( `/gutenberg/v1/block-renderer/${ block }`, {
context: 'edit',
...( null !== attributes ? { attributes } : {} ),
} );
}

export class ServerSideRender extends Component {
Expand Down
2 changes: 0 additions & 2 deletions packages/editor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@
"element-closest": "^2.0.2",
"lodash": "^4.17.10",
"memize": "^1.0.5",
"querystring": "^0.2.0",
"querystringify": "^1.0.0",
"react-autosize-textarea": "^3.0.2",
"redux-multi": "^0.1.12",
"redux-optimist": "^1.0.0",
Expand Down
8 changes: 4 additions & 4 deletions packages/editor/src/components/page-attributes/parent.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* External dependencies
*/
import { get } from 'lodash';
import { stringify } from 'querystringify';

/**
* WordPress dependencies
Expand All @@ -11,6 +10,7 @@ import { __ } from '@wordpress/i18n';
import { TreeSelect, withAPIData } from '@wordpress/components';
import { compose } from '@wordpress/compose';
import { withSelect, withDispatch } from '@wordpress/data';
import { addQueryArgs } from '@wordpress/url';

/**
* Internal dependencies
Expand Down Expand Up @@ -65,17 +65,17 @@ const applyWithDispatch = withDispatch( ( dispatch ) => {
const applyWithAPIDataItems = withAPIData( ( { postType, postId } ) => {
const isHierarchical = get( postType, [ 'hierarchical' ], false );
const restBase = get( postType, [ 'rest_base' ], false );
const queryString = stringify( {
const query = {
context: 'edit',
per_page: -1,
exclude: postId,
parent_exclude: postId,
_fields: [ 'id', 'parent', 'title' ],
orderby: 'menu_order',
order: 'asc',
} );
};
return isHierarchical && restBase ?
{ items: `/wp/v2/${ restBase }?${ queryString }` } :
{ items: addQueryArgs( `/wp/v2/${ restBase }`, query ) } :
{};
} );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* External dependencies
*/
import { isEmpty, get, unescape as unescapeString, find, throttle, uniqBy, invoke } from 'lodash';
import { stringify } from 'querystring';

/**
* WordPress dependencies
Expand All @@ -13,6 +12,7 @@ import { FormTokenField } from '@wordpress/components';
import { withSelect, withDispatch } from '@wordpress/data';
import { compose } from '@wordpress/compose';
import apiFetch from '@wordpress/api-fetch';
import { addQueryArgs } from '@wordpress/url';

/**
* Module constants
Expand Down Expand Up @@ -77,7 +77,9 @@ class FlatTermSelector extends Component {
fetchTerms( params = {} ) {
const { taxonomy } = this.props;
const query = { ...DEFAULT_QUERY, ...params };
const request = apiFetch( { path: `/wp/v2/${ taxonomy.rest_base }?${ stringify( query ) }` } );
const request = apiFetch( {
path: addQueryArgs( `/wp/v2/${ taxonomy.rest_base }`, query ),
} );
request.then( ( terms ) => {
this.setState( ( state ) => ( {
availableTerms: state.availableTerms.concat(
Expand Down Expand Up @@ -116,7 +118,7 @@ class FlatTermSelector extends Component {
if ( errorCode === 'term_exists' ) {
// If the terms exist, fetch it instead of creating a new one.
this.addRequest = apiFetch( {
path: `/wp/v2/${ taxonomy.rest_base }?${ stringify( { ...DEFAULT_QUERY, search: termName } ) }`,
path: addQueryArgs( `/wp/v2/${ taxonomy.rest_base }`, { ...DEFAULT_QUERY, search: termName } ),
} );
return this.addRequest.then( ( searchResult ) => {
return find( searchResult, ( result ) => isSameTermName( result.name, termName ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* External dependencies
*/
import { get, unescape as unescapeString, without, find, some, invoke } from 'lodash';
import { stringify } from 'querystring';

/**
* WordPress dependencies
Expand All @@ -13,6 +12,7 @@ import { TreeSelect, withSpokenMessages, Button } from '@wordpress/components';
import { withSelect, withDispatch } from '@wordpress/data';
import { withInstanceId, compose } from '@wordpress/compose';
import apiFetch from '@wordpress/api-fetch';
import { addQueryArgs } from '@wordpress/url';

/**
* Internal dependencies
Expand Down Expand Up @@ -121,7 +121,10 @@ class HierarchicalTermSelector extends Component {
if ( errorCode === 'term_exists' ) {
// search the new category created since last fetch
this.addRequest = apiFetch( {
path: `/wp/v2/${ taxonomy.rest_base }?${ stringify( { ...DEFAULT_QUERY, parent: formParent || 0, search: formName } ) }`,
path: addQueryArgs(
`/wp/v2/${ taxonomy.rest_base }`,
{ ...DEFAULT_QUERY, parent: formParent || 0, search: formName }
),
} );
return this.addRequest
.then( ( searchResult ) => {
Expand Down Expand Up @@ -183,7 +186,9 @@ class HierarchicalTermSelector extends Component {
if ( ! taxonomy ) {
return;
}
this.fetchRequest = apiFetch( { path: `/wp/v2/${ taxonomy.rest_base }?${ stringify( DEFAULT_QUERY ) }` } );
this.fetchRequest = apiFetch( {
path: addQueryArgs( `/wp/v2/${ taxonomy.rest_base }`, DEFAULT_QUERY ),
} );
this.fetchRequest.then(
( terms ) => { // resolve
const availableTermsTree = buildTermsTree( terms );
Expand Down
6 changes: 3 additions & 3 deletions packages/editor/src/components/url-input/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import { throttle } from 'lodash';
import classnames from 'classnames';
import scrollIntoView from 'dom-scroll-into-view';
import { stringify } from 'querystringify';

/**
* WordPress dependencies
Expand All @@ -17,6 +16,7 @@ import { Spinner, withSpokenMessages, Popover } from '@wordpress/components';
import { withInstanceId } from '@wordpress/compose';
import apiFetch from '@wordpress/api-fetch';
import deprecated from '@wordpress/deprecated';
import { addQueryArgs } from '@wordpress/url';

// Since URLInput is rendered in the context of other inputs, but should be
// considered a separate modal node, prevent keyboard events from propagating
Expand Down Expand Up @@ -91,11 +91,11 @@ class URLInput extends Component {
} );

const request = apiFetch( {
path: `/gutenberg/v1/search?${ stringify( {
path: addQueryArgs( '/gutenberg/v1/search', {
search: value,
per_page: 20,
type: 'post',
} ) }`,
} ),
} );

request.then( ( posts ) => {
Expand Down
3 changes: 2 additions & 1 deletion packages/url/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"main": "build/index.js",
"module": "build-module/index.js",
"dependencies": {
"@babel/runtime": "^7.0.0-beta.52"
"@babel/runtime": "^7.0.0-beta.52",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aside: Why is @babel/runtime a dependency for this package?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We put it as a dependency for all packages that have transpiled code published to npm. We didn't find a better solution so far. This is what Babel docs recommend:

NOTE - Production vs. development dependencies

In most cases, you should install @babel/plugin-transform-runtime as a development dependency (with --save-dev).

npm install --save-dev @babel/plugin-transform-runtime

and @babel/runtime as a production dependency (with --save).

npm install --save @babel/runtime

The transformation plugin is typically used only in development, but the runtime itself will be depended on by your deployed/published code. See the examples below for more details.

https://new.babeljs.io/docs/en/next/babel-plugin-transform-runtime.html#installation

"qs": "^6.5.2s"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See the issue on this line? 😱

We really need to do something to hold ourselves more accountable for published packages working as advertised. This line makes the packages largely unusable, or at least in my attempts to use @wordpress/components on plnkr.co

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder how it worked on Calypso :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, maybe npm is resilient enough to be allowing the extra character? At least based on the resolved qs in package-lock.json.

},
"publishConfig": {
"access": "public"
Expand Down
10 changes: 5 additions & 5 deletions packages/url/src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { parse, format } from 'url';
import { parse, stringify } from 'qs';

const EMAIL_REGEXP = /^(mailto:)?[a-z0-9._%+-]+@[a-z0-9][a-z0-9.-]*\.[a-z]{2,63}$/i;
const USABLE_HREF_REGEXP = /^(?:[a-z]+:|#|\?|\.|\/)/i;
Expand All @@ -15,11 +15,11 @@ const USABLE_HREF_REGEXP = /^(?:[a-z]+:|#|\?|\.|\/)/i;
* @return {string} Updated URL
*/
export function addQueryArgs( url, args ) {
const parsedURL = parse( url, true );
const query = { ...parsedURL.query, ...args };
delete parsedURL.search;
const queryStringIndex = url.indexOf( '?' );
const query = queryStringIndex !== -1 ? parse( url.substr( queryStringIndex + 1 ) ) : {};
const baseUrl = queryStringIndex !== -1 ? url.substr( 0, queryStringIndex ) : url;

return format( { ...parsedURL, query } );
return baseUrl + '?' + stringify( { ...query, ...args } );
}

/**
Expand Down
Loading