File size: 4,436 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
import wpcom from 'calypso/lib/wp';
import { getCategories } from 'calypso/my-sites/plugins/categories/use-categories';
import { RETURNABLE_FIELDS } from './constants';
import type { SearchParams } from './types';
// Maps sort values to values expected by the API
const SORT_QUERY_MAP = new Map( [
[ 'oldest', 'date_asc' ],
[ 'newest', 'date_desc' ],
[ 'relevance', 'score_default' ],
] );
/**
* Map sort values to ones compatible with the API.
* @param {string} sort - Sort value.
* @returns {string} Mapped sort value.
*/
function mapSortToApiValue( sort: string ) {
// Some sorts don't need to be mapped
if (
[ 'price_asc', 'price_desc', 'rating_desc', 'active_installs', 'plugin_modified' ].includes(
sort
)
) {
return sort;
}
return SORT_QUERY_MAP.get( sort ) ?? 'score_default';
}
/**
* Generate the query string for an API request
* @returns {string} The generated query string.
*/
function generateApiQueryString( {
query,
author,
groupId,
category,
pageHandle,
pageSize,
locale,
slugs,
}: SearchParams ) {
const sort = 'score_default';
const params: {
fields: string[];
filter?: { bool: object };
page_handle?: string;
query: string;
sort: string;
size: number;
group_id: string;
category?: string;
from?: number;
lang: string;
track_total_hits: boolean;
} = {
fields: [ ...RETURNABLE_FIELDS ],
page_handle: pageHandle,
query: encodeURIComponent( query ?? '' ),
sort: mapSortToApiValue( sort ),
size: pageSize,
lang: locale,
group_id: groupId,
track_total_hits: false,
};
if ( author ) {
params.filter = getFilterbyAuthor( author );
}
if ( category ) {
switch ( category ) {
case 'featured':
params.filter = getFilterFeaturedPlugins();
break;
case 'popular':
params.sort = 'active_installs';
params.track_total_hits = true;
break;
case 'new':
params.sort = 'date_desc';
break;
case 'updated':
params.sort = 'plugin_modified';
break;
default:
if ( Array.isArray( slugs ) && slugs.length ) {
params.filter = getFilterbySlugs( slugs || [] );
} else {
params.filter = getFilterByCategory( category );
}
params.sort = 'active_installs';
}
}
return params;
}
const marketplaceSearchApiBase = '/marketplace/search';
const apiVersion = '1.3';
/**
* Perform a search.
* @param {Object} options - Search options
* @returns {Promise} A promise to the JSON response object
*/
export function search( options: SearchParams ) {
const queryString = generateApiQueryString( options );
return wpcom.req.get(
{
path: marketplaceSearchApiBase,
},
{ ...queryString, apiVersion }
);
}
export function searchBySlug(
slug: string,
locale: string,
options?: { fields?: Array< string > | undefined; group_id?: string }
) {
const params = {
lang: locale,
filter: getFilterbySlug( slug ),
fields: options?.fields ?? RETURNABLE_FIELDS,
group_id: options?.group_id ?? 'wporg',
};
const queryString = params;
return wpcom.req.get(
{
path: marketplaceSearchApiBase,
},
{ ...queryString, apiVersion }
);
}
function getFilterbyAuthor( author: string ): {
bool: {
must: { term: object }[];
};
} {
return {
bool: {
must: [ { term: { 'plugin.author.raw': author } } ],
},
};
}
function getFilterbySlug( slug: string ): {
bool: {
must: { term: object }[];
};
} {
return {
bool: {
must: [ { term: { slug } } ],
},
};
}
function getFilterbySlugs( slugs: string[] ): {
bool: {
should: { terms: object }[];
};
} {
return {
bool: {
should: [ { terms: { slug: slugs } } ],
},
};
}
function getFilterByCategory( category: string ): {
bool: object;
} {
const categoryTags = getCategories()[ category ]?.tags;
return {
bool: {
should: [
// matching category name from titles
{ match: { 'plugin.title.en': category } },
// matching wp.org categories and tags
{ term: { 'taxonomy.plugin_category.slug': category } },
{ terms: { 'taxonomy.plugin_tags.slug': categoryTags || [ category ] } },
// matching wc.com categories and tags
{ term: { 'taxonomy.wpcom_marketplace_categories.slug': category } },
{ terms: { 'taxonomy.plugin_tag.slug': categoryTags || [ category ] } },
],
},
};
}
function getFilterFeaturedPlugins(): {
bool: {
must: { term: object }[];
};
} {
return {
bool: {
must: [ { term: { 'taxonomy.plugin_section.slug': 'featured' } } ],
},
};
}
|