forked from jaeksoft/node-oss-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
207 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,16 @@ | ||
# Node.js client for OSS | ||
# Node.js client for OSS | ||
|
||
A Node.js client for [Open Search Server](http://www.open-search-server.com/). | ||
|
||
## Usage | ||
|
||
```js | ||
var oss = require('node-oss-client'), | ||
client = oss.createClient(); | ||
|
||
client.search('my_index', { | ||
query: 'my query' | ||
}, function (err, res) { | ||
// ... | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
var oss = require('../index'), | ||
client = oss.createClient(); | ||
|
||
client.search('my_index', { | ||
query: 'my query' | ||
}, function (err, res) { | ||
if (err) return console.log(err); | ||
|
||
console.log(res.body); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('./lib/oss'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
var request = require('request'), | ||
_ = require('lodash'), | ||
urlParser = require('url'); | ||
|
||
var Client = function (options) { | ||
this.options = _.defaults(options || {}, { | ||
hostname: 'localhost', | ||
port: 9090 | ||
}); | ||
}; | ||
|
||
Client.prototype.request = function (options, callback) { | ||
|
||
var url = urlParser.format(_(this.options) | ||
.pick(['hostname', 'port', 'protocol']) | ||
.defaults({ | ||
protocol: 'http', | ||
pathname: options.pathname | ||
}).value()); | ||
|
||
request(_(options) | ||
.omit(['hostname', 'port', 'protocol']) | ||
.defaults({ | ||
url: url, | ||
json: true | ||
}) | ||
.value(), callback); | ||
}; | ||
|
||
|
||
var searchFactory = require('./search'); | ||
|
||
Client.prototype.search = function () { | ||
searchFactory(this).apply(this, arguments); | ||
}; | ||
|
||
|
||
module.exports = Client; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
var Client = require('./client'); | ||
|
||
function createClient(options) { | ||
return new Client(options); | ||
} | ||
|
||
exports.Client = Client; | ||
exports.createClient = createClient; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
var _ = require('lodash'); | ||
|
||
function search(client) { | ||
return function (index, options, callback) { | ||
options = _.defaults(options || {}, { | ||
type: 'field' | ||
}); | ||
|
||
client.request({ | ||
pathname: '/services/rest/index/' + index + '/search/' + options.type, | ||
method: 'POST', | ||
json: _.omit(options, 'type') | ||
}, callback); | ||
}; | ||
} | ||
|
||
module.exports = search; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{ | ||
"name": "node-oss-client", | ||
"version": "0.1.0", | ||
"description": "Node.js client for Open Search Server.", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "mocha" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/lemonde/node-oss-client.git" | ||
}, | ||
"keywords": [ | ||
"oss", | ||
"open", | ||
"search", | ||
"server" | ||
], | ||
"author": "Greg Bergé <berge.greg@gmail.com>", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/lemonde/node-oss-client/issues" | ||
}, | ||
"dependencies": { | ||
"request": "~2.27.0", | ||
"lodash": "~2.2.1" | ||
}, | ||
"devDependencies": { | ||
"mocha": "~1.13.0", | ||
"chai": "~1.8.1", | ||
"nock": "~0.22.1", | ||
"sinon": "~1.7.3", | ||
"sinon-chai": "~2.4.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* globals describe, it, beforeEach */ | ||
|
||
var expect = require('chai').expect, | ||
nock = require('nock'), | ||
Client = require('../lib/client'); | ||
|
||
describe('Client', function () { | ||
|
||
it('should have default options', function () { | ||
var client = new Client(); | ||
expect(client.options).to.have.property('hostname'); | ||
expect(client.options).to.have.property('port'); | ||
}); | ||
|
||
describe('#request', function () { | ||
|
||
beforeEach(function () { | ||
nock('http://localhost:9090') | ||
.post('/my-path') | ||
.reply(200, { | ||
foo: 'bar' | ||
}); | ||
}); | ||
|
||
it('should be possible to make a request', function (done) { | ||
var client = new Client(); | ||
|
||
client.request({ | ||
pathname: '/my-path', | ||
method: 'POST', | ||
json: { | ||
kung: 'foo' | ||
} | ||
}, function (err, res) { | ||
if (err) return done(err); | ||
|
||
expect(res.body).to.deep.equal({ | ||
foo: 'bar' | ||
}); | ||
|
||
done(); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* globals describe, it, beforeEach */ | ||
|
||
var chai = require('chai').use(require('sinon-chai')), | ||
expect = chai.expect, | ||
sinon = require('sinon'), | ||
searchFactory = require('../lib/search'); | ||
|
||
describe('Search', function () { | ||
|
||
var client, search; | ||
|
||
beforeEach(function () { | ||
client = { | ||
request: sinon.spy() | ||
}; | ||
|
||
search = searchFactory(client); | ||
}); | ||
|
||
it('should be possible to make a search', function () { | ||
|
||
var callback = function () {}; | ||
|
||
search('my_index', { | ||
query: 'My query' | ||
}, callback); | ||
|
||
expect(client.request).to.be.calledWith({ | ||
json: { | ||
query: 'My query' | ||
}, | ||
method: 'POST', | ||
pathname: '/services/rest/index/my_index/search/field' | ||
}, callback); | ||
}); | ||
}); |