Browse Source

basic client outline

Kieran Gibb 2 years ago
parent
commit
a61685eec4
No known key found for this signature in database
6 changed files with 49 additions and 8 deletions
  1. 4
    3
      index.js
  2. 6
    2
      lib/groups.js
  3. 12
    0
      lib/replicators.js
  4. 1
    3
      lib/request.js
  5. 9
    0
      test/groups.test.js
  6. 17
    0
      test/index.test.js

+ 4
- 3
index.js View File

@@ -2,9 +2,10 @@ const axios = require('axios')
2 2
 const debug = require('debug')('cobox-client')
3 3
 const assert = require('assert')
4 4
 
5
-const crypto = require('crypto')
5
+const crypto = require('cobox-crypto')
6 6
 const { isObject } = require('util')
7 7
 
8
+const constants = require('./constants')
8 9
 const Groups = require('./lib/groups')
9 10
 const Replicators = require('./lib/replicators')
10 11
 
@@ -12,8 +13,8 @@ module.exports = class Client {
12 13
   constructor (endpoint, opts = {}) {
13 14
     if (isObject(endpoint)) return new Client(null, endpoint)
14 15
 
15
-    this.endpoint = endpoint || BASE_URL
16
-    this._id = opts._id || crypto.randombytes(2).toString('hex')
16
+    this.endpoint = endpoint || constants.BASE_URL
17
+    this._id = opts._id || crypto.randomBytes(2).toString('hex')
17 18
     debug(this._id, 'client aimed at', this.endpoint, opts)
18 19
 
19 20
     this.groups = new Groups(endpoint, opts)

+ 6
- 2
lib/groups.js View File

@@ -53,10 +53,14 @@ module.exports = class Groups {
53 53
   }
54 54
 
55 55
   async mount (data) {
56
-
56
+    var data = await this.find(params.id)
57
+    if (data.errors) return data
58
+    return await this.mounts.create(data.address)
57 59
   }
58 60
 
59 61
   async unmount (data) {
60
-
62
+    var data = await this.find(params.id)
63
+    if (data.errors) return data
64
+    return await this.mounts.destroy(data.address)
61 65
   }
62 66
 }

+ 12
- 0
lib/replicators.js View File

@@ -0,0 +1,12 @@
1
+const path = require('path')
2
+const request = require('./request')
3
+const debug = require('debug')('cobox-client')
4
+const constants = require('../constants')
5
+const Connections = require('./connections')
6
+
7
+module.exports = class Replicators {
8
+  constructor (endpoint = constants.BASE_URL, opts = {}) {
9
+    this.endpoint = path.join(endpoint, 'replicators')
10
+    this.connections = new Connections(this.endpoint, opts)
11
+  }
12
+}

+ 1
- 3
lib/request.js View File

@@ -4,9 +4,7 @@ const debug = require('debug')('cobox-client')
4 4
 module.exports = async (opts) => {
5 5
   Object.assign(opts, {
6 6
     maxRedirects: 0,
7
-    headers: { 'Content-Type': 'application/json' },
8
-    data: opts.data || {},
9
-    responseType: opts.responseType
7
+    headers: { 'Content-Type': 'application/json' }
10 8
   })
11 9
 
12 10
   debug('request', opts)

+ 9
- 0
test/groups.test.js View File

@@ -2,10 +2,19 @@ const { describe } = require('tape-plus')
2 2
 const sinon = require('sinon')
3 3
 const Groups = require('../lib/groups')
4 4
 const constants = require('../constants')
5
+const path = require('path')
5 6
 
6 7
 const data = require('./fixtures/groups.json')
8
+const ConnectionClient = require('../lib/connections')
7 9
 
8 10
 describe('groups', (context) => {
11
+  context('basic', (assert, next) => {
12
+    var client = new Groups()
13
+    assert.same(client.endpoint, path.join(constants.BASE_URL, 'groups'))
14
+    assert.ok(client.connections instanceof ConnectionClient, 'has connections resource')
15
+    next()
16
+  })
17
+
9 18
   context('all()', async (assert, next) => {
10 19
     var client = new Groups()
11 20
 

+ 17
- 0
test/index.test.js View File

@@ -0,0 +1,17 @@
1
+const { describe } = require('tape-plus')
2
+
3
+const Client = require('../')
4
+const GroupClient = require('../lib/groups')
5
+const ReplicatorClient = require('../lib/replicators')
6
+const constants = require('../constants')
7
+
8
+describe('client', (context) => {
9
+  context('basic', async (assert, next) => {
10
+    const client = new Client()
11
+    assert.ok(client._id, 'has an object id')
12
+    assert.same(client.endpoint, constants.BASE_URL, 'defaults to base URL')
13
+    assert.ok(client.groups instanceof GroupClient, 'has group resource')
14
+    assert.ok(client.replicators instanceof ReplicatorClient, 'has replicator resource')
15
+    next()
16
+  })
17
+})