diff --git a/.gitignore b/.gitignore index 55fda48..2e2baef 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ node_modules npm-debug.log package-lock.json +.idea/ diff --git a/lib/client.js b/lib/client.js index 5f38385..4863363 100644 --- a/lib/client.js +++ b/lib/client.js @@ -15,97 +15,178 @@ Client.prototype = { get: function(path, params, callback) { var uri = this._buildUriComponents(path, params) - request({ - method: 'GET', - qs: uri.params, - url: this.baseUrl + uri.path, - auth: { - user: this.config.apiKey - }, - headers: { - 'X-Swiftype-Client': this.clientName, - 'X-Swiftype-Client-Version': this.clientVersion - } - }, function(error, response, body) { - if (error) - return callback(error) - else if (!(response.statusCode.toString().match(/2[\d]{2}/))) - return callback(JSON.parse(response.body)) - else - return callback(null, JSON.parse(response.body)) - }) + return new Promise(function(resolve, reject) { + request({ + method: 'GET', + qs: uri.params, + url: this.baseUrl + uri.path, + auth: { + user: this.config.apiKey + }, + headers: { + 'X-Swiftype-Client': this.clientName, + 'X-Swiftype-Client-Version': this.clientVersion + } + }, function (error, response, body) { + if (error) { + if (callback) { + callback(error); + } + else { + reject(error); + } + } + else if (!( response.statusCode.toString().match(/2[\d]{2}/) )) { + if (callback) { + callback(JSON.parse(response.body)) + } + else { + resolve(JSON.parse(response.body)); + } + } + else { + if (callback) { + callback(null, JSON.parse(response.body)) + } + else { + resolve(null, JSON.parse(response.body)); + } + } + }); + }); }, - post: function(path, params, callback) { + post: async function(path, params, callback) { var uri = this._buildUriComponents(path, params) - request({ - method: 'POST', - json: uri.params, - url: this.baseUrl + uri.path, - auth: { - user: this.config.apiKey - }, - headers: { - 'X-Swiftype-Client': this.clientName, - 'X-Swiftype-Client-Version': this.clientVersion - } - }, function(error, response, body) { - if (error) - return callback(error) - else if (!(response.statusCode.toString().match(/2[\d]{2}/))) - return callback(response.body) - else - return callback(null, response.body) - }) + return new Promise(function(resolve, reject) + { + request({ + method: 'POST', + json: uri.params, + url: this.baseUrl + uri.path, + auth: { + user: this.config.apiKey + }, + headers: { + 'X-Swiftype-Client': this.clientName, + 'X-Swiftype-Client-Version': this.clientVersion + } + }, function (error, response, body) { + if (error) { + if (callback) { + callback(error); + } + else { + reject(error); + } + } + else if (!( response.statusCode.toString().match(/2[\d]{2}/) )) { + if (callback) { + callback(response.body); + } + else { + resolve(response.body); + } + } + else { + if (callback) { + callback(null, response.body); + } + else { + resolve(null, response.body); + } + } + }); + }); }, put: function(path, params, callback) { var uri = this._buildUriComponents(path, params) - request({ - method: 'PUT', - json: uri.params, - url: this.baseUrl + uri.path, - auth: { - user: this.config.apiKey - }, - headers: { - 'X-Swiftype-Client': this.clientName, - 'X-Swiftype-Client-Version': this.clientVersion - } - }, function(error, response, body) { - if (error) - return callback(error) - else if (!(response.statusCode.toString().match(/2[\d]{2}/))) - return callback(response.body) - else - return callback(null, response.body) - }) + return new Promise(function(resolve, reject) { + request({ + method: 'PUT', + json: uri.params, + url: this.baseUrl + uri.path, + auth: { + user: this.config.apiKey + }, + headers: { + 'X-Swiftype-Client': this.clientName, + 'X-Swiftype-Client-Version': this.clientVersion + } + }, function (error, response, body) { + if (error) { + if (callback) { + callback(error); + } + else { + reject(error); + } + } + else if (!( response.statusCode.toString().match(/2[\d]{2}/) )) { + if (callback) { + callback(response.body); + } + else { + resolve(response.body); + } + } + else { + if (callback) { + callback(null, response.body); + } + else { + resolve(null, response.body); + } + } + }); + }); }, delete: function(path, params, callback) { var uri = this._buildUriComponents(path, params) - request({ - method: 'DELETE', - url: this.baseUrl + uri.path, - auth: { - user: this.config.apiKey - }, - headers: { - 'Accept': '*/*', - 'X-Swiftype-Client': this.clientName, - 'X-Swiftype-Client-Version': this.clientVersion - } - }, function(error, response, body) { - if (error) - return callback(error) - else if (!(response.statusCode.toString().match(/2[\d]{2}/))) - return callback(response) - else - return callback(null, response) - }) + return new Promise(function(resolve, reject) { + request({ + method: 'DELETE', + url: this.baseUrl + uri.path, + auth: { + user: this.config.apiKey + }, + headers: { + 'Accept': '*/*', + 'X-Swiftype-Client': this.clientName, + 'X-Swiftype-Client-Version': this.clientVersion + } + }, function (error, response, body) { + if (error) { + if (callback) { + callback(error); + } + else { + reject(error); + } + } + else if (!( response.statusCode.toString().match(/2[\d]{2}/) )) { + if (callback) { + callback(response); + } + else { + resolve(response); + } + } + else { + if (callback) { + callback(null, response); + } + else { + resolve(null, response); + } + } + }); + }); }, _serializeParams: function(obj, prefix) { diff --git a/lib/documentTypes.js b/lib/documentTypes.js index a1fcd35..bc59ec9 100644 --- a/lib/documentTypes.js +++ b/lib/documentTypes.js @@ -1,28 +1,48 @@ 'use strict'; var DocumentTypes = function(client) { - this.client = client + this.client = client; } DocumentTypes.prototype = { list: function(params, callback) { - this.client.get('/engines/{engine}/document_types.json', params, callback) + this.client.get('/engines/{engine}/document_types.json', params, callback); + }, + + listAsync: async function(params) { + return await this.client.get('/engines/{engine}/document_types.json', params); }, get: function(params, callback) { - this.client.get('/engines/{engine}/document_types/{documentType}.json', params, callback) + this.client.get('/engines/{engine}/document_types/{documentType}.json', params, callback); + }, + + getAsync: async function(params) { + return await this.client.get('/engines/{engine}/document_types/{documentType}.json', params); }, create: function(params, callback) { - this.client.post('/engines/{engine}/document_types.json', params, callback) + this.client.post('/engines/{engine}/document_types.json', params, callback); + }, + + createAsync: async function(params) { + return await this.client.post('/engines/{engine}/document_types.json', params); }, search: function(params, callback) { - this.client.get('/engines/{engine}/document_types/{documentType}/search.json', params, callback) + this.client.get('/engines/{engine}/document_types/{documentType}/search.json', params, callback); + }, + + searchAsync: async function(params) { + return await this.client.get('/engines/{engine}/document_types/{documentType}/search.json', params); }, destroy: function(params, callback) { - this.client.delete('/engines/{engine}/document_types/{documentType}', params, callback) + this.client.delete('/engines/{engine}/document_types/{documentType}', params, callback); + }, + + destroyAsync: async function(params) { + return await this.client.delete('/engines/{engine}/document_types/{documentType}', params); } } diff --git a/lib/documents.js b/lib/documents.js index 8a0e4ec..8d278ce 100644 --- a/lib/documents.js +++ b/lib/documents.js @@ -9,22 +9,39 @@ var Documents = function(client) { Documents.prototype = { list: function(params, callback) { - this.client.get('/engines/{engine}/document_types/{documentType}/documents.json', params, callback) + this.client.get('/engines/{engine}/document_types/{documentType}/documents.json', params, callback); + }, + + listAsync: async function(params) { + return await this.client.get('/engines/{engine}/document_types/{documentType}/documents.json', params); }, get: function(params, callback) { - this.client.get('/engines/{engine}/document_types/{documentType}/documents/{externalId}.json', params, callback) + this.client.get('/engines/{engine}/document_types/{documentType}/documents/{externalId}.json', params, callback); + }, + + getAsync: async function(params, callback) { + return await this.client.get('/engines/{engine}/document_types/{documentType}/documents/{externalId}.json', params); }, create: function(params, callback) { - this.client.post('/engines/{engine}/document_types/{documentType}/documents/create_or_update.json', params, callback) + this.client.post('/engines/{engine}/document_types/{documentType}/documents/create_or_update.json', params, callback); }, + createAsync: async function(params, callback) { + return await this.client.post('/engines/{engine}/document_types/{documentType}/documents/create_or_update.json', params); + }, + + bulkCreate: function(params, callback) { - this.client.post('/engines/{engine}/document_types/{documentType}/documents/bulk_create_or_update.json', params, callback) + this.client.post('/engines/{engine}/document_types/{documentType}/documents/bulk_create_or_update.json', params, callback); }, - batchCreate: function(params, documents, batchSize, doneCallback){ + bulkCreateAsync: async function(params, callback) { + return await this.client.post('/engines/{engine}/document_types/{documentType}/documents/bulk_create_or_update.json', params); + }, + + batchCreate: function(params, documents, batchSize, doneCallback) { if (!doneCallback) { doneCallback = batchSize batchSize = 10 @@ -37,7 +54,7 @@ Documents.prototype = { var _this = this - var tasks = batches.map(function(batch){ + var tasks = batches.map(function(batch) { var batchParams = { engine: params.engine, documentType: params.documentType, @@ -51,17 +68,49 @@ Documents.prototype = { async.parallel(tasks, doneCallback); }, + batchCreateAsync: async function(params, documents, batchSize=10) { + + var batches = [] + for (var i=0; i < documents.length; i+=batchSize) { + batches.push(documents.slice(i, i+batchSize)); + } + + var _this = this; + + var tasks = batches.map(async function(batch){ + return await _this.bulkCreateAsync({ + engine: params.engine, + documentType: params.documentType, + documents: batch + }); + }) + + return await async.parallel(tasks); + }, + update: function(params, callback) { - this.client.put('/engines/{engine}/document_types/{documentType}/documents/{externalId}/update_fields.json', params, callback) + this.client.put('/engines/{engine}/document_types/{documentType}/documents/{externalId}/update_fields.json', params, callback); + }, + + updateAsync: async function(params) { + return await this.client.put('/engines/{engine}/document_types/{documentType}/documents/{externalId}/update_fields.json', params); }, destroy: function(params, callback) { - this.client.delete('/engines/{engine}/document_types/{documentType}/documents/{externalId}', params, callback) + this.client.delete('/engines/{engine}/document_types/{documentType}/documents/{externalId}', params, callback); + }, + + destroyAsync: async function(params) { + return await this.client.delete('/engines/{engine}/document_types/{documentType}/documents/{externalId}', params); }, bulkDestroy: function(params, callback) { - this.client.post('/engines/{engine}/document_types/{documentType}/documents/bulk_destroy.json', params, callback) + this.client.post('/engines/{engine}/document_types/{documentType}/documents/bulk_destroy.json', params, callback); + }, + + bulkDestroyAsync: async function(params, callback) { + return await this.client.post('/engines/{engine}/document_types/{documentType}/documents/bulk_destroy.json', params); } } diff --git a/lib/engines.js b/lib/engines.js index 593eaca..83b43dc 100644 --- a/lib/engines.js +++ b/lib/engines.js @@ -5,21 +5,37 @@ var Engines = function(client) { } Engines.prototype = { - + list: function(callback) { - this.client.get('/engines.json', {}, callback) + this.client.get('/engines.json', {}, callback); + }, + + listAsync: async function() { + return await this.client.get('/engines.json', {}); }, get: function(params, callback) { - this.client.get('/engines/{engine}.json', params, callback) + this.client.get('/engines/{engine}.json', params, callback); + }, + + getAsync: async function(params) { + return await this.client.get('/engines/{engine}.json', params); }, create: function(params, callback) { - this.client.post('/engines.json', params, callback) + this.client.post('/engines.json', params, callback); + }, + + createAsync: async function(params) { + return await this.client.post('/engines.json', params); }, destroy: function(params, callback) { - this.client.delete('/engines/{engine}', params, callback) + this.client.delete('/engines/{engine}', params, callback); + }, + + destroyAsync: async function(params) { + return await this.client.delete('/engines/{engine}', params); } } diff --git a/lib/siteSearch.js b/lib/siteSearch.js index ecc67cd..822ab0e 100644 --- a/lib/siteSearch.js +++ b/lib/siteSearch.js @@ -26,6 +26,10 @@ SiteSearchClient.prototype = { this.client.post('/engines/{engine}/search.json', params, callback) }, + searchAsync: async function(params) { + return await this.client.post('/engines/{engine}/search.json', params) + }, + suggest: function(params, callback) { this.client.post('/engines/{engine}/suggest.json', params, callback) },