11////@ts -check
22//connectionConfig
33const typedef = require ( './typedef' )
4+ const { encodeURISegment} = require ( "./utils" )
45
56/**
67 * @file Terminus DB connection configuration
@@ -83,7 +84,9 @@ ConnectionConfig.prototype.update = function(params) {
8384 //if (typeof params.credential !== 'undefined')this.setTokenParameter(params.credential)
8485 if ( typeof params . db !== 'undefined' ) this . setDB ( params . db )
8586 //this to set the author and the jwt in creation
86- if ( typeof params . jwt !== 'undefined' ) this . setLocalBasicAuth ( params . jwt , params . user , 'jwt' )
87+ if ( typeof params . token !== 'undefined' ) this . setLocalBasicAuth ( params . token , params . user , 'apikey' )
88+ //this is for jwt
89+ else if ( typeof params . jwt !== 'undefined' ) this . setLocalBasicAuth ( params . jwt , params . user , 'jwt' )
8790 //this is basic authO
8891 else if ( typeof params . key !== 'undefined' ) this . setLocalBasicAuth ( params . key , params . user )
8992 //If I set only the user I have to set the
@@ -225,7 +228,8 @@ ConnectionConfig.prototype.parseServerURL = function(str) {
225228ConnectionConfig . prototype . serverUrlEncoding = function ( str ) {
226229 const orgArr = str . split ( "/" )
227230 if ( orgArr . length > 3 ) {
228- const org = encodeURIComponent ( orgArr [ 3 ] )
231+ //const org = encodeURI(orgArr[3])
232+ const org = encodeURISegment ( orgArr [ 3 ] )
229233 return str . replace ( orgArr [ 3 ] , org )
230234 }
231235 return str
@@ -309,7 +313,7 @@ ConnectionConfig.prototype.setRemoteBasicAuth = function(remoteKey, remoteUserID
309313 * set the local database connection credential
310314 * @param {string } [userKey] - basic auth api key
311315 * @param {string } [userId] - user id
312- * @param {string } [type] - basic|jwt
316+ * @param {string } [type] - basic|jwt|apikey
313317 */
314318//to be review this is ok for the basic Auth
315319ConnectionConfig . prototype . setLocalBasicAuth = function ( userKey , userId = 'admin' , type = 'basic' ) {
@@ -358,7 +362,7 @@ ConnectionConfig.prototype.dbURL = function() {
358362
359363ConnectionConfig . prototype . userURL = function ( user ) {
360364 let url = `${ this . apiURL ( ) } user`
361- if ( user ) url += `/${ encodeURIComponent ( user ) } `
365+ if ( user ) url += `/${ encodeURISegment ( user ) } `
362366 return url
363367}
364368
@@ -368,11 +372,11 @@ ConnectionConfig.prototype.userURL = function(user) {
368372 * @param {string } [action] - the organization id
369373 * @returns {string }
370374 */
371-
375+ //encodeURIComponent
372376ConnectionConfig . prototype . organizationURL = function ( orgId , action ) {
373377 let url = `${ this . apiURL ( ) } organization`
374- if ( orgId ) url += `/${ encodeURIComponent ( orgId ) } `
375- if ( action ) url += `/${ encodeURIComponent ( action ) } `
378+ if ( orgId ) url += `/${ encodeURISegment ( orgId ) } `
379+ if ( action ) url += `/${ encodeURISegment ( action ) } `
376380 return url
377381}
378382
@@ -561,11 +565,11 @@ ConnectionConfig.prototype.commitDescriptorUrl = function(commitId) {
561565/**
562566 * Generate URL for optimizing db branch
563567 */
564-
568+ //encodeURI() will not encode: ~!@#$&*()=:/,;?+'
565569ConnectionConfig . prototype . optimizeBranchUrl = function ( branchId ) {
566570 //let o = this.optimizeBase()
567571 const dbBase = this . dbBase ( 'optimize' )
568- return `${ dbBase } ${ this . repoid } /branch/${ encodeURIComponent ( branchId ) } `
572+ return `${ dbBase } ${ this . repoid } /branch/${ encodeURISegment ( branchId ) } `
569573 //return dbBase + `${this.user()}/${this.db()}/${this.repoid}/branch/${nuid}`
570574}
571575
@@ -628,9 +632,10 @@ ConnectionConfig.prototype.branchBase = function(action) {
628632 * (unless dbid = system dbname in which case there is no organization)
629633 * @returns {string|boolean }
630634 */
635+ //encodeURIComponent
631636ConnectionConfig . prototype . dbURLFragment = function ( ) {
632637 if ( this . db ( ) === this . system_db ) return this . db ( )
633- return encodeURIComponent ( this . organization ( ) ) + '/' + encodeURIComponent ( this . db ( ) )
638+ return encodeURISegment ( this . organization ( ) ) + '/' + encodeURISegment ( this . db ( ) )
634639}
635640
636641/**
@@ -650,12 +655,13 @@ ConnectionConfig.prototype.documentURL = function(params){
650655ConnectionConfig . prototype . queryParameter = function ( params ) {
651656 if ( ! params || typeof params !== 'object' ) return ''
652657 let queryString = Object . keys ( params ) . map ( ( key ) => {
653- return key + '=' + encodeURIComponent ( params [ key ] )
658+ return key + '=' + encodeURISegment ( params [ key ] )
654659 } ) . join ( '&' ) ;
655660
656661 return `?${ queryString } ` ;
657662}
658663
664+
659665ConnectionConfig . prototype . jsonSchemaURL = function ( params ) {
660666 const paramsStr = this . queryParameter ( params )
661667 return this . branchBase ( 'schema' ) + paramsStr
0 commit comments