aboutsummaryrefslogtreecommitdiff
path: root/node_modules/algoliasearch/src/buildSearchMethod.js
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/algoliasearch/src/buildSearchMethod.js')
-rw-r--r--node_modules/algoliasearch/src/buildSearchMethod.js67
1 files changed, 67 insertions, 0 deletions
diff --git a/node_modules/algoliasearch/src/buildSearchMethod.js b/node_modules/algoliasearch/src/buildSearchMethod.js
new file mode 100644
index 00000000..33b1d52b
--- /dev/null
+++ b/node_modules/algoliasearch/src/buildSearchMethod.js
@@ -0,0 +1,67 @@
+module.exports = buildSearchMethod;
+
+var errors = require('./errors.js');
+
+/**
+ * Creates a search method to be used in clients
+ * @param {string} queryParam the name of the attribute used for the query
+ * @param {string} url the url
+ * @return {function} the search method
+ */
+function buildSearchMethod(queryParam, url) {
+ /**
+ * The search method. Prepares the data and send the query to Algolia.
+ * @param {string} query the string used for query search
+ * @param {object} args additional parameters to send with the search
+ * @param {function} [callback] the callback to be called with the client gets the answer
+ * @return {undefined|Promise} If the callback is not provided then this methods returns a Promise
+ */
+ return function search(query, args, callback) {
+ // warn V2 users on how to search
+ if (typeof query === 'function' && typeof args === 'object' ||
+ typeof callback === 'object') {
+ // .search(query, params, cb)
+ // .search(cb, params)
+ throw new errors.AlgoliaSearchError('index.search usage is index.search(query, params, cb)');
+ }
+
+ // Normalizing the function signature
+ if (arguments.length === 0 || typeof query === 'function') {
+ // Usage : .search(), .search(cb)
+ callback = query;
+ query = '';
+ } else if (arguments.length === 1 || typeof args === 'function') {
+ // Usage : .search(query/args), .search(query, cb)
+ callback = args;
+ args = undefined;
+ }
+ // At this point we have 3 arguments with values
+
+ // Usage : .search(args) // careful: typeof null === 'object'
+ if (typeof query === 'object' && query !== null) {
+ args = query;
+ query = undefined;
+ } else if (query === undefined || query === null) { // .search(undefined/null)
+ query = '';
+ }
+
+ var params = '';
+
+ if (query !== undefined) {
+ params += queryParam + '=' + encodeURIComponent(query);
+ }
+
+ var additionalUA;
+ if (args !== undefined) {
+ if (args.additionalUA) {
+ additionalUA = args.additionalUA;
+ delete args.additionalUA;
+ }
+ // `_getSearchParams` will augment params, do not be fooled by the = versus += from previous if
+ params = this.as._getSearchParams(args, params);
+ }
+
+
+ return this._search(params, url, callback, additionalUA);
+ };
+}