Skip to content

Commit 6e72173

Browse files
committed
Merge branch 'dev' of https://github.com/terminusdb/terminus-client into dev
2 parents e2a66ce + b8a6917 commit 6e72173

File tree

5 files changed

+72
-34
lines changed

5 files changed

+72
-34
lines changed

lib/connectionConfig.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ ConnectionConfig.prototype.update = function(params) {
7676
const orgID = params.organization || params.user
7777
console.log("orgID",orgID,params)
7878
this.setOrganization(orgID)
79+
if (typeof params.credential !== 'undefined')this.setTokenParameter(params.credential)
7980
if (typeof params.db !== 'undefined') this.setDB(params.db)
8081
if (typeof params.jwt !== 'undefined') this.setRemoteBasicAuth(params.jwt, params.jwt_user)
8182
if (typeof params.key !== 'undefined') this.setLocalBasicAuth(params.key, params.user)
@@ -84,6 +85,10 @@ ConnectionConfig.prototype.update = function(params) {
8485
if (typeof params.repo !== 'undefined') this.setRepo(params.repo)
8586
}
8687

88+
ConnectionConfig.prototype.setTokenParameter = function(param){
89+
this.tokenParameter = param
90+
}
91+
8792
/**
8893
* Simple gets to retrieve current connection status
8994
* Gets the current server url
@@ -278,13 +283,14 @@ ConnectionConfig.prototype.setRemoteBasicAuth = function(remoteKey, remoteUserID
278283
* set the local database connection credential
279284
* @param {string} [userKey] - basic auth api key
280285
* @param {string} [userId] - user id
286+
* @param {string} [type] - basic - jwt - token
281287
*/
282-
ConnectionConfig.prototype.setLocalBasicAuth = function(userKey, userId = 'admin') {
288+
ConnectionConfig.prototype.setLocalBasicAuth = function(userKey, userId = 'admin', type= 'basic') {
283289
if (!userKey) {
284290
this.local_auth = undefined
285291
} else {
286292
const uid = userId
287-
this.local_auth = {type: 'basic', user: uid, key: userKey}
293+
this.local_auth = {type:type, user: uid, key: userKey}
288294
}
289295
}
290296

@@ -574,6 +580,10 @@ ConnectionConfig.prototype.branchBase = function(action) {
574580
return b
575581
}
576582
//_commits branch is magic - stores all commits for repo
583+
584+
/*
585+
*https://127.0.0.1:6363/api/db/admin/profiles01/local/_commits
586+
*/
577587
if (this.branch() === '_commits') {
578588
return b + `/${this.branch()}`
579589
} else if (this.ref()) {

lib/const.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module.exports = Object.freeze({
88
POST:'POST',
99
GET:'GET',
1010
DELETE:'DELETE',
11+
PUT:'PUT',
1112
SQUASH_BRANCH: 'SQUASH_BRANCH',
1213
UPDATE_SCHEMA: 'UPDATE_SCHEMA',
1314
CONNECT: 'connect',

lib/dispatchRequest.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ function DispatchRequest(url, action, payload, local_auth, remote_auth = null, c
3434
mode: 'cors', // no-cors, cors, *same-origin
3535
redirect: 'follow', // manual, *follow, error
3636
referrer: 'client',
37+
headers:{}
3738
// url:url,
3839
// no-referrer, *client
3940
}
@@ -51,22 +52,22 @@ function DispatchRequest(url, action, payload, local_auth, remote_auth = null, c
5152
* I can call the local database using the local authorization key or
5253
* a jwt token
5354
*/
54-
if (local_auth && local_auth.type == 'basic') {
55+
if (local_auth && local_auth.type === 'basic') {
5556
let encoded = btoaImplementation(local_auth.user + ':' + local_auth.key)
5657
options.headers = {Authorization: `Basic ${encoded}`}
57-
} else if (local_auth && local_auth.type == 'jwt') {
58+
} else if (local_auth && local_auth.type === 'jwt') {
5859
options.headers = {Authorization: `Bearer ${local_auth.key}`}
60+
} else if (local_auth && local_auth.type === 'cloud') {
61+
5962
}
6063
/*
6164
* pass the Authorization information of another
6265
* terminusdb server to the local terminusdb
6366
*/
64-
if (remote_auth && remote_auth.type == 'jwt') {
65-
if (!options.headers) options.headers = {}
67+
if (remote_auth && remote_auth.type == 'jwt') {
6668
options.headers['Authorization-Remote'] = `Bearer ${remote_auth.key}`
6769
} else if (remote_auth && remote_auth.type == 'basic') {
6870
let rencoded = btoaImplementation(remote_auth.user + ':' + remote_auth.key)
69-
if (!options.headers) options.headers = {}
7071
options.headers['Authorization-Remote'] = `Basic ${rencoded}`
7172
}
7273

@@ -117,6 +118,17 @@ function DispatchRequest(url, action, payload, local_auth, remote_auth = null, c
117118
if (err.response && err.response.data) e.data = err.response.data
118119
throw e
119120
})
121+
case CONST.PUT:
122+
options.headers = options.headers ? options.headers : {}
123+
options.headers['Content-Type'] = 'application/json'
124+
return axiosInstance
125+
.put(url, payload, options)
126+
.then(response => response.data)
127+
.catch(err => {
128+
const e = new Error(ErrorMessage.getAPIErrorMessage(url, options, err))
129+
if (err.response && err.response.data) e.data = err.response.data
130+
throw e
131+
})
120132
default:
121133
options.headers = options.headers ? options.headers : {}
122134
options.headers['Content-Type'] = 'application/json'

lib/query/woqlLibrary.js

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -344,20 +344,20 @@ WOQLLibrary.prototype.graphs = function(values, variables, cresource) {
344344
* General Pattern 4: Retrieves Branches, Their ID, Head Commit ID, Head Commit Time
345345
* (if present, new branches have no commits)
346346
*/
347-
/*WOQLLibrary.prototype.branches = function(values, variables, cresource) {
348-
cresource = cresource || this.default_commit_resource
349-
this.default_variables = ['Branch ID', 'Time', 'Commit ID', 'Branch IRI', 'Commit IRI']
350-
if (variables) this._set_user_variables(variables)
351-
let woql = new WOQLQuery()
352-
.triple(this._varn('Branch IRI'), 'ref:branch_name', this._varn('Branch ID'))
353-
.opt()
354-
.triple(this._varn('Branch IRI'), 'ref:ref_commit', this._varn('Commit IRI'))
355-
.triple(this._varn('Commit IRI'), 'ref:commit_id', this._varn('Commit ID'))
356-
.triple(this._varn('Commit IRI'), 'ref:commit_timestamp', this._varn('Time'))
357-
358-
let compiled = this._add_constraints(woql, values)
359-
return new WOQLQuery().using(cresource, compiled)
360-
}*/
347+
WOQLLibrary.prototype.branches = function(){//values, variables, cresource) {
348+
//cresource = cresource || this.default_commit_resource
349+
//this.default_variables = ['Branch ID', 'Time', 'Commit ID', 'Branch IRI', 'Commit IRI']
350+
//if (variables) this._set_user_variables(variables)
351+
let woql = new WOQLQuery().triple("v:Branch","rdf:type","@schema:Branch").
352+
opt().triple("v:Branch","@schema:head","v:Head").
353+
triple("v:Branch","@schema:name","v:Name").
354+
opt().triple("v:Head","@schema:timestamp","v:Timestamp")
355+
356+
//to be review add constaints
357+
//let compiled = this._add_constraints(woql, values)
358+
//there is a problem with using
359+
return woql//new WOQLQuery().using('_commits', woql)
360+
}
361361

362362
/**
363363
* General Pattern 5: Objects - just a list of object ids and their types
@@ -917,6 +917,7 @@ WOQLLibrary.prototype.next_commits = function(commit_id, branch, count) {
917917
WOQLLibrary.prototype.active_commit_id = function(branch, ts, commit_var) {
918918
commit_var = commit_var || 'Commit ID'
919919
ts = ts || Date.now() / 1000
920+
return new WOQLQuery()
920921
let [biri, hiri, ciri, cpath, cts, tiri, tts, cid, hts] = this.vars(
921922
'My Branch IRI',
922923
'My Head IRI',
@@ -959,6 +960,7 @@ WOQLLibrary.prototype.active_commit_id = function(branch, ts, commit_var) {
959960

960961
//includes passed commit id in list
961962
WOQLLibrary.prototype.history_ids = function(commit_id, count, commit_var) {
963+
return new WOQLQuery()
962964
commit_var = commit_var || 'Commit ID'
963965
let [commit_iri, cpath, tail_iri, cid] = this.vars(
964966
'My Head IRI',
@@ -981,6 +983,7 @@ WOQLLibrary.prototype.history_ids = function(commit_id, count, commit_var) {
981983

982984
//includes passed commit id in list
983985
WOQLLibrary.prototype.future_ids = function(commit_id, branch, count, commit_var) {
986+
return new WOQLQuery()
984987
count = count || 10
985988
commit_var = commit_var || 'Commit ID'
986989
let [branch_iri, head_iri, commit_iri, branch_path, cpath, tail_iri, cid] = this.vars(
@@ -1015,6 +1018,7 @@ WOQLLibrary.prototype.future_ids = function(commit_id, branch, count, commit_var
10151018
}
10161019

10171020
WOQLLibrary.prototype.next_commit_ids = function(commit_id, branch, count, commit_var) {
1021+
return new WOQLQuery()
10181022
count = count || 1
10191023
commit_var = commit_var || 'Commit ID'
10201024
let [branch_iri, head_iri, commit_iri, branch_path, cpath, tail_iri, cid] = this.vars(
@@ -1050,6 +1054,7 @@ lib().future_ids(cid, "main", 8)
10501054

10511055
//does not include passed commit id in list
10521056
WOQLLibrary.prototype.previous_commit_ids = function(commit_id, count, commit_var) {
1057+
return new WOQLQuery()
10531058
commit_var = commit_var || 'Commit ID'
10541059
count = count || 1
10551060
let [commit_iri, cpath, tail_iri, cid] = this.vars(

lib/woqlClient.js

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const ErrorMessage = require('./errorMessage')
77
const ConnectionConfig = require('./connectionConfig')
88
const WOQL = require('./woql')
99
const WOQLQuery = require('./query/woqlBuilder')
10+
const { default: axios } = require('axios')
1011

1112
/**
1213
* @license Apache Version 2
@@ -164,6 +165,7 @@ WOQLClient.prototype.databaseInfo = function(dbId) {
164165
WOQLClient.prototype.db = function(dbId) {
165166
if (typeof dbId !== 'undefined') {
166167
this.connectionConfig.setDB(dbId)
168+
//this.checkout("_commits")
167169
}
168170
return this.connectionConfig.db()
169171
}
@@ -674,7 +676,7 @@ WOQLClient.prototype.query = function(woql, commitMsg, allWitnesses) {
674676
* @example
675677
* client.branch("dev")
676678
*/
677-
WOQLClient.prototype.branch = function(newBranchId, sourceFree) {
679+
WOQLClient.prototype.branch = function(newBranchId, sourceFree) {
678680
if (newBranchId) {
679681
let source = this.ref()
680682
? {origin: `${this.organization()}/${this.db()}/${this.repo()}/commit/${this.ref()}`}
@@ -858,7 +860,7 @@ WOQLClient.prototype.clonedb = function(cloneSource, newDbId, orgId) {
858860
* @property {object} [payload] - the post body
859861
* @returns {Promise} A promise that returns the call response object, or an Error if rejected.
860862
*/
861-
WOQLClient.prototype.dispatch = function(action, apiUrl, payload) {
863+
WOQLClient.prototype.dispatch = async function(action, apiUrl, payload) {
862864
if (!apiUrl) {
863865
return Promise.reject(
864866
new Error(
@@ -868,15 +870,24 @@ WOQLClient.prototype.dispatch = function(action, apiUrl, payload) {
868870
),
869871
),
870872
)
873+
}//I have to do a call to get the jwt token
874+
if(this.connectionConfig.tokenParameter){
875+
const param = this.connectionConfig.tokenParameter
876+
await axios.post(param.url,param.options).then(result=>result.data).then(data=>{
877+
if(data.access_token){
878+
this.localAuth({"key":data.access_token,"type":"jwt"})
879+
}
880+
})
871881
}
872882
return DispatchRequest(
873-
apiUrl,
874-
action,
875-
payload,
876-
this.localAuth(),
877-
this.remoteAuth(),
878-
this.customHeaders(),
879-
)
883+
apiUrl,
884+
action,
885+
payload,
886+
this.localAuth(),
887+
this.remoteAuth(),
888+
this.customHeaders(),
889+
)
890+
880891
}
881892

882893
/**
@@ -1020,7 +1031,7 @@ WOQLClient.prototype.action_permitted = function(action, resource) {
10201031

10211032
WOQLClient.prototype.createUser = function(userId, userDoc) {
10221033
if (userId) {
1023-
return this.dispatch(CONST.CREATE_USER, this.connectionConfig.userURL(), userDoc)
1034+
return this.dispatch(CONST.POST, this.connectionConfig.userURL(), userDoc)
10241035
}
10251036
let errmsg = `Create user parameter error - you must specify a valid user id`
10261037
return Promise.reject(
@@ -1241,17 +1252,16 @@ WOQLClient.prototype.getDocument = function(params,dbId,branch){
12411252

12421253
WOQLClient.prototype.updateDocument = function(json,params,dbId, message="update document"){
12431254
const docParams = params || {}
1244-
docParams['overwrite']=true
12451255
docParams['author']=this.author()
12461256
docParams['message'] = message
12471257
if (dbId) {
12481258
this.db(dbId)
12491259
}
1250-
return this.dispatch(CONST.POST, this.connectionConfig.documentURL(docParams),json)
1260+
return this.dispatch(CONST.PUT, this.connectionConfig.documentURL(docParams),json)
12511261
}
12521262

12531263

1254-
WOQLClient.prototype.deleteDocument = function(params,message="delete document"){
1264+
WOQLClient.prototype.deleteDocument = function(params,dbId,message="delete document"){
12551265
const docParams = params || {}
12561266
let payload = null
12571267
if(typeof params.id === 'object'){

0 commit comments

Comments
 (0)