Skip to content
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
npm-debug.log
package-lock.json
.idea/
235 changes: 158 additions & 77 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
32 changes: 26 additions & 6 deletions lib/documentTypes.js
Original file line number Diff line number Diff line change
@@ -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);
}
}

Expand Down
Loading