Skip to content
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
83 changes: 65 additions & 18 deletions modules/search/instant-search/components/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,73 @@
*/
import fetch from 'unfetch';

const FIELDS = [
'author',
'comment_count',
'date',
'excerpt_html',
'gravatar_url',
'permalink.url.raw',
'title_html',
];
/**
* @preserve jquery-param (c) 2015 KNOWLEDGECODE | MIT
* @param {object} a - parameters to add
* @returns {string} url query string
* From https://github.com/knowledgecode/jquery-param
*/
function query_params( a ) {
//eslint-disable-next-line no-var
var s = [];
const add = function( k, v ) {
v = typeof v === 'function' ? v() : v;
//eslint-disable-next-line no-nested-ternary
v = v === null ? '' : v === undefined ? '' : v;
s[ s.length ] = encodeURIComponent( k ) + '=' + encodeURIComponent( v );
};
const buildParams = function( prefix, obj ) {
let i, len, key;

function stringifyArray( fieldName, array ) {
return array.map( ( element, index ) => `${ fieldName }[${ index }]=${ element }` ).join( '&' );
if ( prefix ) {
if ( Array.isArray( obj ) ) {
for ( i = 0, len = obj.length; i < len; i++ ) {
buildParams(
prefix + '[' + ( typeof obj[ i ] === 'object' && obj[ i ] ? i : '' ) + ']',
obj[ i ]
);
}
} else if ( String( obj ) === '[object Object]' ) {
for ( key in obj ) {
buildParams( prefix + '[' + key + ']', obj[ key ] );
}
} else {
add( prefix, obj );
}
} else if ( Array.isArray( obj ) ) {
for ( i = 0, len = obj.length; i < len; i++ ) {
add( obj[ i ].name, obj[ i ].value );
}
} else {
for ( key in obj ) {
buildParams( key, obj[ key ] );
}
}
return s;
};
return buildParams( '', a ).join( '&' );
}

function getAPIUrl( siteId, query ) {
return `https://public-api.wordpress.com/rest/v1.3/sites/${ siteId }/search?query=${ encodeURIComponent(
query
) }&${ stringifyArray( 'fields', FIELDS ) }`;
}
export function search( siteId, query, aggs, filter, resultFormat ) {
let fields = [];
let highlight_fields = [];
switch ( resultFormat ) {
case 'engagement':
case 'product':
case 'minimal':
default:
highlight_fields = [ 'title', 'content', 'comments' ];
fields = [ 'date', 'permalink.url.raw', 'tag.name.default', 'category.name.default' ];
}

export function search( siteId, query ) {
return fetch( getAPIUrl( siteId, query ) );
const obj = {
query: query,
fields: fields,
highlight_fields: highlight_fields,
aggregations: aggs,
filter: filter,
};
return fetch(
Copy link
Contributor

Choose a reason for hiding this comment

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

How should we handle failed API requests?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good question. Probably depends on what the error code was. Let's deal with it in #13366

`https://public-api.wordpress.com/rest/v1.3/sites/${ siteId }/search?${ query_params( obj ) }`
);
}
Loading