Comparing version 0.4.2 to 0.4.3
59
index.js
@@ -5,6 +5,7 @@ /*! | ||
*/ | ||
/*jshint node: true, strict: true, globalstrict: true, indent: 4, immed: true, undef: true, sub: true, newcap: false */ | ||
/*jshint node: true, strict: true, globalstrict: true, esversion: 6, varstmt: true, indent: 4, immed: true, undef: true, sub: true, newcap: false */ | ||
'use strict'; | ||
var util = require('util'), | ||
const | ||
util = require('util'), | ||
crypto = require('crypto'), | ||
@@ -46,3 +47,3 @@ Q = require('./lib/promise'), // we're currently using Bluebird, but Q is a shorter name | ||
function nameValue2Object(arr) { | ||
var o = {}; | ||
const o = {}; | ||
arr.forEach(function (e) { | ||
@@ -78,3 +79,3 @@ o[e.name] = e.value; | ||
}).then(function (resp) { | ||
var data = resp.body; | ||
const data = resp.body; | ||
if (typeof data == 'object' && 'message' in data) | ||
@@ -96,3 +97,3 @@ throw new TDoc.Error(method, data.code, data.message); | ||
if ('etag' in resp.header) { | ||
var h = crypto.createHash('sha256').update(resp.body).digest('hex').toUpperCase(); | ||
const h = crypto.createHash('sha256').update(resp.body).digest('hex').toUpperCase(); | ||
if (resp.header.etag.indexOf(h) != 1) | ||
@@ -114,3 +115,3 @@ throw new Error('Hash value mismatch.'); | ||
}).then(function (resp) { | ||
var data = resp.body; | ||
const data = resp.body; | ||
if (typeof data == 'object' && 'message' in data) | ||
@@ -123,3 +124,3 @@ throw new TDoc.Error(method, data.code, data.message); | ||
function documentPOST(me, method, data, document) { | ||
var r = req | ||
const r = req | ||
.post(me.address + method) | ||
@@ -134,3 +135,3 @@ .auth(me.username, me.password) | ||
}).then(function (resp) { | ||
var data = resp.body; | ||
const data = resp.body; | ||
if (typeof data == 'object' && 'message' in data) | ||
@@ -156,3 +157,3 @@ throw new TDoc.Error(method, data.code, data.message); | ||
function commonUploadParams(p) { | ||
var s = {}; | ||
const s = {}; | ||
if (p.mimetype) | ||
@@ -194,4 +195,3 @@ s.mimetype = p.mimetype; | ||
return Q.reject(new Error('if the document is ‘ready’ it must have a content as either ‘file’ or ‘data’')); | ||
var s = commonUploadParams(p), | ||
d = null; | ||
const s = commonUploadParams(p); | ||
s.doctype = p.doctype; | ||
@@ -202,3 +202,3 @@ return documentPOST(me, 'docs/upload', s, p.file || p.data); | ||
function update(me, p) { | ||
var s = commonUploadParams(p); | ||
const s = commonUploadParams(p); | ||
return documentPOST(me, 'docs/update', s, p.file || p.data); | ||
@@ -208,3 +208,3 @@ } | ||
function updateMeta(me, p) { | ||
var data = { | ||
const data = { | ||
meta: p.meta, | ||
@@ -219,3 +219,3 @@ value: p.value | ||
function search(me, p) { | ||
var data = { | ||
const data = { | ||
doctype: p.doctype, | ||
@@ -230,6 +230,5 @@ meta: JSON.stringify(p.meta) | ||
return POST(me, 'docs/search', data).then(function (data) { | ||
var d = []; | ||
if (typeof data == 'object' && 'documents' in data) | ||
d = data.documents; | ||
return d; | ||
return data.documents; | ||
throw new Error('malformed response'); | ||
}); | ||
@@ -239,3 +238,3 @@ } | ||
function document(me, p) { | ||
var data = {}; | ||
const data = {}; | ||
if (p.user) data.user = p.user; | ||
@@ -247,3 +246,3 @@ if (p.company) data.company = p.company; | ||
function documentMeta(me, p) { | ||
var data = {}; | ||
const data = {}; | ||
if (p.user) data.user = p.user; | ||
@@ -255,3 +254,3 @@ if (p.company) data.company = p.company; | ||
function documentLink(me, p) { | ||
var data = {}; | ||
const data = {}; | ||
if (p.user) data.user = p.user; | ||
@@ -263,3 +262,3 @@ if (p.company) data.company = p.company; | ||
function documentDelete(me, p) { | ||
var data = {}; | ||
const data = {}; | ||
if (p.user) data.user = p.user; | ||
@@ -272,9 +271,7 @@ if (p.company) data.company = p.company; | ||
p.limit = 2; // we need 1 but limit to 2 to know if search was not unique | ||
p.complete = 1; // download each metadata directly to avoid one round-trip | ||
return search(me, p).then(function (data) { | ||
if (data.length != 1) | ||
throw new Error('Search result was not a single document'); | ||
var p2 = { id: data[0] }; | ||
if (p.user) p2.user = p.user; | ||
if (p.company) p2.company = p.company; | ||
return documentMeta(me, p2); | ||
return data[0]; | ||
}); | ||
@@ -284,3 +281,3 @@ } | ||
function parcelCreate(me, p) { | ||
var data = { | ||
const data = { | ||
company: p.company, | ||
@@ -295,3 +292,3 @@ doctype: p.doctype, | ||
function parcelClose(me, p) { | ||
var data = { | ||
const data = { | ||
parcel: p.id | ||
@@ -306,3 +303,3 @@ }; | ||
function parcelDelete(me, p) { | ||
var data = { | ||
const data = { | ||
parcel: p.id | ||
@@ -318,3 +315,3 @@ }; | ||
function companyList(me, p) { | ||
var data = {}; | ||
const data = {}; | ||
if (p.user) data.user = p.user; | ||
@@ -326,3 +323,3 @@ if (p.company) data.company = p.company; | ||
function doctypeList(me, p) { | ||
var data = {}; | ||
const data = {}; | ||
if (p.user) data.user = p.user; | ||
@@ -334,3 +331,3 @@ if (p.company) data.company = p.company; | ||
function doctypeInfo(me, p) { | ||
var data = {}; | ||
const data = {}; | ||
if (p.user) data.user = p.user; | ||
@@ -337,0 +334,0 @@ if (p.company) data.company = p.company; |
{ | ||
"name": "tdoc-api", | ||
"version": "0.4.2", | ||
"version": "0.4.3", | ||
"description": "Wrapper around tDoc REST-ful APIs", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
18791
355