rest-on-couch-client
Advanced tools
Comparing version 0.0.5 to 0.0.6
@@ -0,1 +1,6 @@ | ||
<a name="0.0.6"></a> | ||
## [0.0.6](https://github.com/cheminfo/rest-on-couch-client/compare/v0.0.5...v0.0.6) (2018-02-07) | ||
<a name="0.0.5"></a> | ||
@@ -2,0 +7,0 @@ ## [0.0.5](https://github.com/cheminfo/rest-on-couch-client/compare/v0.0.4...v0.0.5) (2017-02-22) |
{ | ||
"name": "rest-on-couch-client", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"description": "A rest-on-couch client for node.js", | ||
@@ -11,13 +11,16 @@ "main": "src/Roc.js", | ||
"eslint": "eslint src", | ||
"test": "npm run eslint" | ||
"test": "npm run eslint", | ||
"eslint-fix": "eslint src --fix" | ||
}, | ||
"dependencies": { | ||
"detect-node": "^2.0.3", | ||
"superagent": "^3.4.1" | ||
"superagent": "^3.8.2" | ||
}, | ||
"devDependencies": { | ||
"eslint": "^3.15.0", | ||
"eslint-config-cheminfo": "^1.6.0", | ||
"eslint": "^4.17.0", | ||
"eslint-config-cheminfo": "^1.15.1", | ||
"eslint-plugin-import": "^2.8.0", | ||
"eslint-plugin-jest": "^21.7.0", | ||
"eslint-plugin-no-only-tests": "^1.1.0" | ||
} | ||
} |
309
src/Roc.js
'use strict'; | ||
const superagent = require('superagent'); | ||
const isNode = require('detect-node'); | ||
const URL = isNode ? require('url').URL : window.URL; | ||
const URL = isNode ? require('url').URL : window.URL; // eslint-disable-line import/order | ||
const viewSearchJsonify = ['key', 'startkey', 'endkey']; | ||
@@ -12,171 +15,181 @@ const viewSearch = ['limit', 'mine', 'groups', 'descending', 'reduce']; | ||
class Roc { | ||
constructor(opts) { | ||
opts = opts || {}; | ||
for (const key in opts) { | ||
this[key] = opts[key]; | ||
} | ||
for (let i = 0; i < mandatoryOptions.length; i++) { | ||
if (!this[mandatoryOptions[i]]) { | ||
throw new Error(`${mandatoryOptions[i]} is a mandatory option`); | ||
} | ||
} | ||
this.authTimeout = this.authTimeout || 0; | ||
this.agent = isNode ? superagent.agent() : superagent; | ||
this.url = new URL(this.url); | ||
this.databaseUrl = new URL(`db/${this.database}/`, this.url); | ||
this.authUrl = new URL('auth/login/couchdb', this.url); | ||
this.lastSuccess = 0; | ||
constructor(opts) { | ||
opts = opts || {}; | ||
for (const key in opts) { | ||
this[key] = opts[key]; | ||
} | ||
auth() { | ||
if (!this.username || !this.password) return Promise.resolve(); | ||
if (Date.now() - this.lastSuccess < this.authTimeout) return Promise.resolve(); | ||
return this.agent | ||
.post(this.authUrl.href) | ||
.withCredentials() | ||
.send({ | ||
username: this.username, | ||
password: this.password | ||
}) | ||
.then(res => { | ||
if (res.status === 200) { | ||
this.lastSuccess = Date.now(); | ||
} | ||
}); | ||
for (let i = 0; i < mandatoryOptions.length; i++) { | ||
if (!this[mandatoryOptions[i]]) { | ||
throw new Error(`${mandatoryOptions[i]} is a mandatory option`); | ||
} | ||
} | ||
get(entry) { | ||
return this.auth().then(() => { | ||
const uuid = getUuid(entry); | ||
const url = new URL(`entry/${uuid}`, this.databaseUrl); | ||
return this.agent.get(url.href) | ||
.withCredentials() | ||
.then(res => { | ||
if (res.body && res.status === 200) { | ||
return res.body; | ||
} | ||
return null; | ||
}); | ||
}); | ||
} | ||
this.authTimeout = this.authTimeout || 0; | ||
this.agent = isNode ? superagent.agent() : superagent; | ||
this.url = new URL(this.url); | ||
this.databaseUrl = new URL(`db/${this.database}/`, this.url); | ||
this.authUrl = new URL('auth/login/couchdb', this.url); | ||
this.lastSuccess = 0; | ||
} | ||
create(entry) { | ||
return this.auth() | ||
.then(() => { | ||
if (!entry.$kind) { | ||
entry.$kind = this.kind; | ||
} | ||
const url = new URL('entry', this.databaseUrl); | ||
return this.agent.post(url.href) | ||
.withCredentials() | ||
.send(entry) | ||
.then(res => { | ||
if (res.body && res.status <= 201) { | ||
entry._id = res.body.id; | ||
entry._rev = res.body.rev; | ||
return entry; | ||
} | ||
return null; | ||
}); | ||
}); | ||
} | ||
_withCredentials(request) { | ||
return isNode ? request.set('Cookie', this.cookies) : request.withCredentials(); | ||
} | ||
update(entry) { | ||
return this.auth().then(() => { | ||
const url = new URL(`entry/${entry._id}`, this.databaseUrl); | ||
return this.agent.put(url.href) | ||
.withCredentials() | ||
.send(entry) | ||
.then(res => { | ||
if (res.body && res.status === 200) { | ||
entry._rev = res.body.rev; | ||
entry.$creationDate = res.body.$creationDate; | ||
entry.$modificationDate = res.body.$modificationDate; | ||
} | ||
return entry; | ||
}); | ||
}); | ||
auth() { | ||
if (!this.username || !this.password) return Promise.resolve(); | ||
if (Date.now() - this.lastSuccess < this.authTimeout) { | ||
return Promise.resolve(); | ||
} | ||
return this.agent | ||
.post(this.authUrl.href) | ||
.send({ | ||
username: this.username, | ||
password: this.password | ||
}) | ||
.then((res) => { | ||
this.cookies = res.headers['set-cookie']; | ||
if (res.status === 200) { | ||
this.lastSuccess = Date.now(); | ||
} | ||
}); | ||
} | ||
view(viewName, options) { | ||
options = options || {}; | ||
return this.auth().then(() => { | ||
const url = new URL(`_view/${viewName}`, this.databaseUrl); | ||
addSearch(url, options); | ||
return this.agent.get(url.href) | ||
.withCredentials() | ||
.then(res => { | ||
if (res && res.body && res.status === 200) { | ||
if (options.filter) { | ||
res.body = res.body.filter(options.filter); | ||
} | ||
if (options.sort) { | ||
res.body = res.body.sort(options.sort); | ||
} | ||
} | ||
return res.body; | ||
}); | ||
}); | ||
} | ||
get(entry) { | ||
return this.auth().then(() => { | ||
const uuid = getUuid(entry); | ||
const url = new URL(`entry/${uuid}`, this.databaseUrl); | ||
return this._withCredentials( | ||
this.agent.get(url.href) | ||
) .then((res) => { | ||
if (res.body && res.status === 200) { | ||
return res.body; | ||
} | ||
return null; | ||
}); | ||
}); | ||
} | ||
query(viewName, options) { | ||
return this.auth().then(() => { | ||
let requestUrl = new URL(`_query/${viewName}`, this.databaseUrl); | ||
addSearch(requestUrl, options); | ||
create(entry) { | ||
return this.auth().then(() => { | ||
if (!entry.$kind) { | ||
entry.$kind = this.kind; | ||
} | ||
const url = new URL('entry', this.databaseUrl); | ||
return this._withCredentials( | ||
this.agent | ||
.post(url.href) | ||
.send(entry) | ||
).then((res) => { | ||
if (res.body && res.status <= 201) { | ||
entry._id = res.body.id; | ||
entry._rev = res.body.rev; | ||
return entry; | ||
} | ||
return null; | ||
}); | ||
}); | ||
} | ||
return this.agent.get(requestUrl.href) | ||
.withCredentials() | ||
.then(res => { | ||
if (res && res.body && res.status === 200) { | ||
if (options.filter) { | ||
res.body = res.body.filter(options.filter); | ||
} | ||
if (options.sort) { | ||
res.body = res.body.sort(options.sort); | ||
} | ||
} | ||
return res.body; | ||
}); | ||
}); | ||
} | ||
update(entry) { | ||
return this.auth().then(() => { | ||
const url = new URL(`entry/${entry._id}`, this.databaseUrl); | ||
return this._withCredentials(this.agent | ||
.put(url.href) | ||
.send(entry) | ||
).then((res) => { | ||
if (res.body && res.status === 200) { | ||
entry._rev = res.body.rev; | ||
entry.$creationDate = res.body.$creationDate; | ||
entry.$modificationDate = res.body.$modificationDate; | ||
} | ||
return entry; | ||
}); | ||
}); | ||
} | ||
session() { | ||
return this.auth().then(() => { | ||
let requestUrl = new URL('auth/session', this.url); | ||
return this.agent.get(requestUrl.href) | ||
.withCredentials() | ||
.then(res => res.body); | ||
}); | ||
} | ||
view(viewName, options) { | ||
options = options || {}; | ||
return this.auth().then(() => { | ||
const url = new URL(`_view/${viewName}`, this.databaseUrl); | ||
addSearch(url, options); | ||
return this._withCredentials( | ||
this.agent.get(url.href) | ||
).then((res) => { | ||
if (res && res.body && res.status === 200) { | ||
if (options.filter) { | ||
res.body = res.body.filter(options.filter); | ||
} | ||
if (options.sort) { | ||
res.body = res.body.sort(options.sort); | ||
} | ||
} | ||
return res.body; | ||
}); | ||
}); | ||
} | ||
query(viewName, options) { | ||
return this.auth().then(() => { | ||
let requestUrl = new URL(`_query/${viewName}`, this.databaseUrl); | ||
addSearch(requestUrl, options); | ||
return this._withCredentials( | ||
this.agent | ||
.get(requestUrl.href) | ||
).then((res) => { | ||
if (res && res.body && res.status === 200) { | ||
if (options.filter) { | ||
res.body = res.body.filter(options.filter); | ||
} | ||
if (options.sort) { | ||
res.body = res.body.sort(options.sort); | ||
} | ||
} | ||
return res.body; | ||
}); | ||
}); | ||
} | ||
session() { | ||
return this.auth().then(() => { | ||
let requestUrl = new URL('auth/session', this.url); | ||
return this._withCredentials( | ||
this.agent | ||
.get(requestUrl.href) | ||
).then((res) => res.body); | ||
}); | ||
} | ||
} | ||
function getUuid(entry) { | ||
let uuid; | ||
const type = typeof entry; | ||
if (type === 'string') { | ||
uuid = entry; | ||
} else if (type === 'object') { | ||
uuid = entry._id; | ||
} else { | ||
throw new Error('Bad arguments'); | ||
} | ||
return uuid; | ||
let uuid; | ||
const type = typeof entry; | ||
if (type === 'string') { | ||
uuid = entry; | ||
} else if (type === 'object') { | ||
uuid = entry._id; | ||
} else { | ||
throw new Error('Bad arguments'); | ||
} | ||
return uuid; | ||
} | ||
function addSearch(requestUrl, options) { | ||
for (let i = 0; i < viewSearchJsonify.length; i++) { | ||
if (options[viewSearchJsonify[i]]) { | ||
requestUrl.searchParams.append(viewSearchJsonify[i], JSON.stringify(options[viewSearchJsonify[i]])); | ||
} | ||
for (let i = 0; i < viewSearchJsonify.length; i++) { | ||
if (options[viewSearchJsonify[i]]) { | ||
requestUrl.searchParams.append( | ||
viewSearchJsonify[i], | ||
JSON.stringify(options[viewSearchJsonify[i]]) | ||
); | ||
} | ||
} | ||
for (let i = 0; i < viewSearch.length; i++) { | ||
if (options[viewSearch[i]]) { | ||
requestUrl.searchParams.append(viewSearch[i], options[viewSearch[i]]); | ||
} | ||
for (let i = 0; i < viewSearch.length; i++) { | ||
if (options[viewSearch[i]]) { | ||
requestUrl.searchParams.append(viewSearch[i], options[viewSearch[i]]); | ||
} | ||
} | ||
} | ||
module.exports = Roc; |
174
37722
5
7
Updatedsuperagent@^3.8.2