@toa.io/bridges.node 0.7.2-dev.0 → 0.8.0-dev.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +5 -5
- package/src/shortcuts/.test/mock.aspect.js +9 -0
- package/src/shortcuts/amqp.js +16 -0
- package/src/shortcuts/amqp.test.js +38 -0
- package/src/shortcuts/{origins.js → http.js} +3 -3
- package/src/shortcuts/index.js +4 -2
- package/test/{context.origins.fixtures.js → context.http.fixtures.js} +1 -1
- package/test/{context.origins.test.js → context.http.test.js} +7 -7
- package/types/context.d.ts +2 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@toa.io/bridges.node",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0-dev.0",
|
|
4
4
|
"description": "Toa Node Bridge (inproc)",
|
|
5
5
|
"homepage": "https://toa.io",
|
|
6
6
|
"author": {
|
|
@@ -26,13 +26,13 @@
|
|
|
26
26
|
"test": "echo \"Error: run tests from root\" && exit 1"
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@toa.io/core": "0.
|
|
30
|
-
"@toa.io/filesystem": "0.7.0",
|
|
31
|
-
"@toa.io/generic": "0.
|
|
29
|
+
"@toa.io/core": "0.8.0-dev.0",
|
|
30
|
+
"@toa.io/filesystem": "0.7.1-dev.0",
|
|
31
|
+
"@toa.io/generic": "0.8.0-dev.0",
|
|
32
32
|
"fast-glob": "3.2.7"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
35
|
"clone-deep": "4.0.1"
|
|
36
36
|
},
|
|
37
|
-
"gitHead": "
|
|
37
|
+
"gitHead": "5f1bfd167bb7919492d249f78bbcc6bd9fc645a4"
|
|
38
38
|
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { underlay } = require('@toa.io/generic')
|
|
4
|
+
|
|
5
|
+
/** @type {toa.node.shortcut} */
|
|
6
|
+
const amqp = (context, aspect) => {
|
|
7
|
+
context.amqp = underlay(async (segs, args) => {
|
|
8
|
+
if (segs.length !== 2) throw new Error(`AMQP aspect call should have 2 segments [${segs.join(', ')}] given`)
|
|
9
|
+
|
|
10
|
+
const [origin, method] = segs
|
|
11
|
+
|
|
12
|
+
return aspect.invoke(origin, method, ...args)
|
|
13
|
+
})
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
exports.amqp = amqp
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { generate } = require('randomstring')
|
|
4
|
+
const { random } = require('@toa.io/generic')
|
|
5
|
+
|
|
6
|
+
const { aspect } = require('./.test/mock.aspect')
|
|
7
|
+
const { amqp } = require('./amqp')
|
|
8
|
+
|
|
9
|
+
it('should be', async () => {
|
|
10
|
+
expect(amqp).toBeInstanceOf(Function)
|
|
11
|
+
})
|
|
12
|
+
|
|
13
|
+
/** @type {toa.node.Context} */
|
|
14
|
+
let context
|
|
15
|
+
|
|
16
|
+
beforeEach(() => {
|
|
17
|
+
jest.clearAllMocks()
|
|
18
|
+
|
|
19
|
+
context = /** @type {toa.node.Context} */ {}
|
|
20
|
+
|
|
21
|
+
amqp(context, aspect)
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
it('should define shortcut', async () => {
|
|
25
|
+
expect(context.amqp).toBeDefined()
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
it('should call invoke', async () => {
|
|
29
|
+
const args = Array.from({ length: random(5) + 2 }, generate)
|
|
30
|
+
|
|
31
|
+
await context.amqp.test.emit(...args)
|
|
32
|
+
|
|
33
|
+
expect(aspect.invoke).toHaveBeenCalledWith('test', 'emit', ...args)
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
it('should throw if wrong amount of segments', async () => {
|
|
37
|
+
await expect(context.amqp.one.two.emit()).rejects.toThrow('AMQP aspect call should have 2 segments [one, two, emit] given')
|
|
38
|
+
})
|
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
const { underlay } = require('@toa.io/generic')
|
|
4
4
|
|
|
5
5
|
/** @type {toa.node.shortcut} */
|
|
6
|
-
const
|
|
7
|
-
context.
|
|
6
|
+
const http = (context, aspect) => {
|
|
7
|
+
context.http = underlay(async (segs, args) => {
|
|
8
8
|
if (segs.length < 2) throw new Error(`Origins call requires at least 2 arguments, ${segs.length} given`)
|
|
9
9
|
|
|
10
10
|
const name = segs.shift()
|
|
@@ -17,4 +17,4 @@ const origins = (context, aspect) => {
|
|
|
17
17
|
})
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
exports.
|
|
20
|
+
exports.http = http
|
package/src/shortcuts/index.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const {
|
|
3
|
+
const { http } = require('./http')
|
|
4
|
+
const { amqp } = require('./amqp')
|
|
4
5
|
const { configuration } = require('./configuration')
|
|
5
6
|
const { state } = require('./state')
|
|
6
7
|
|
|
7
|
-
exports.
|
|
8
|
+
exports.http = http
|
|
9
|
+
exports.amqp = amqp
|
|
8
10
|
exports.configuration = configuration
|
|
9
11
|
exports.state = state
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
const { generate } = require('randomstring')
|
|
4
4
|
|
|
5
|
-
const fixtures = require('./context.
|
|
5
|
+
const fixtures = require('./context.http.fixtures')
|
|
6
6
|
const { Context } = require('../src/context')
|
|
7
7
|
|
|
8
8
|
const origins = fixtures.context.aspects[0]
|
|
@@ -18,14 +18,14 @@ beforeEach(async () => {
|
|
|
18
18
|
})
|
|
19
19
|
|
|
20
20
|
it('should expose aspect', async () => {
|
|
21
|
-
expect(context.aspects.
|
|
21
|
+
expect(context.aspects.http).toBeDefined()
|
|
22
22
|
})
|
|
23
23
|
|
|
24
24
|
it('should invoke', async () => {
|
|
25
25
|
const name = generate()
|
|
26
26
|
const arg = { [generate()]: generate() }
|
|
27
27
|
|
|
28
|
-
await context.
|
|
28
|
+
await context.http[name].baz.quu.get(arg)
|
|
29
29
|
|
|
30
30
|
expect(origins.invoke).toHaveBeenCalled()
|
|
31
31
|
expect(origins.invoke).toHaveBeenCalledWith(name, 'baz/quu', expect.objectContaining(arg), undefined)
|
|
@@ -34,20 +34,20 @@ it('should invoke', async () => {
|
|
|
34
34
|
it('should define request method', async () => {
|
|
35
35
|
const arg = { [generate()]: generate() }
|
|
36
36
|
|
|
37
|
-
await context.
|
|
38
|
-
await context.
|
|
37
|
+
await context.http.foo.post(arg)
|
|
38
|
+
await context.http.bar.baz.put()
|
|
39
39
|
|
|
40
40
|
expect(origins.invoke).toHaveBeenNthCalledWith(1, 'foo', '', { method: 'POST', ...arg }, undefined)
|
|
41
41
|
expect(origins.invoke).toHaveBeenNthCalledWith(2, 'bar', 'baz', { method: 'PUT' }, undefined)
|
|
42
42
|
})
|
|
43
43
|
|
|
44
44
|
it('should throw if no origin name specified', async () => {
|
|
45
|
-
await expect(context.
|
|
45
|
+
await expect(context.http.get()).rejects.toThrow(/at least 2 arguments/)
|
|
46
46
|
})
|
|
47
47
|
|
|
48
48
|
it('should pass options', async () => {
|
|
49
49
|
const options = { [generate()]: generate() }
|
|
50
|
-
await context.
|
|
50
|
+
await context.http.foo.post({}, options)
|
|
51
51
|
|
|
52
52
|
expect(origins.invoke.mock.calls[0][3]).toStrictEqual(options)
|
|
53
53
|
})
|