gell-dispatch 0.0.0-stage → 0.1.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/.github/workflows/publish.yml +45 -0
- package/.mocharc.json +4 -0
- package/README.md +4 -2
- package/bus.js +169 -0
- package/docs/superpowers/plans/2026-11-25-registry.md +215 -0
- package/docs/superpowers/specs/2026-11-25-registry-design.md +87 -0
- package/kind/action.js +6 -0
- package/kind/event.js +38 -0
- package/kind/snapshot.js +6 -0
- package/kind/type.js +21 -0
- package/middleware/arguments.js +50 -0
- package/middleware/attributes.js +38 -0
- package/middleware/roles.js +81 -0
- package/package.json +29 -4
- package/registry.js +97 -0
- package/stream/sink.js +35 -0
- package/test/bootstrap.js +25 -0
- package/test/kind/test-action.js +81 -0
- package/test/kind/test-snapshot.js +81 -0
- package/test/kind/test-type.js +54 -0
- package/test/middleware/test-arguments.js +565 -0
- package/test/middleware/test-attributes.js +79 -0
- package/test/middleware/test-roles.js +176 -0
- package/test/resolver.js +26 -0
- package/test/stream/test-sink.js +112 -0
- package/test/test-bus.js +383 -0
- package/test/test-registry.js +151 -0
- package/test/util/test-chain.js +164 -0
- package/test/util/test-error.js +33 -0
- package/test/util/test-parameters.js +30 -0
- package/test/util/test-restrict.js +73 -0
- package/util/chain.js +9 -0
- package/util/error.js +15 -0
- package/util/parameters.js +22 -0
- package/util/restrict.js +27 -0
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
const assert = require('assert');
|
|
2
|
+
|
|
3
|
+
const Registry = require('../registry');
|
|
4
|
+
|
|
5
|
+
const KIND = {
|
|
6
|
+
kind: 'query',
|
|
7
|
+
nameOf: d => d && d.name,
|
|
8
|
+
describe: d => ({ description: d.description }),
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
const ONE = { name: 'q.one', description: 'one' };
|
|
12
|
+
const TWO = { name: 'q.two', description: 'two' };
|
|
13
|
+
|
|
14
|
+
describe('registry', function() {
|
|
15
|
+
let registry;
|
|
16
|
+
|
|
17
|
+
beforeEach(function() {
|
|
18
|
+
registry = new Registry(KIND);
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
it('requires a kind with nameOf', function() {
|
|
22
|
+
assert.throws(() => new Registry(), { name: 'AssertionError' });
|
|
23
|
+
assert.throws(() => new Registry({ kind: 'query' }), { name: 'AssertionError' });
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
describe('register', function() {
|
|
27
|
+
it('registers and returns the original declaration', function() {
|
|
28
|
+
assert.strictEqual(registry.register(ONE), ONE);
|
|
29
|
+
assert.strictEqual(registry.get('q.one'), ONE);
|
|
30
|
+
assert.strictEqual(registry.has('q.one'), true);
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
it('returns undefined / false for unknown names', function() {
|
|
34
|
+
assert.strictEqual(registry.get('q.none'), undefined);
|
|
35
|
+
assert.strictEqual(registry.has('q.none'), false);
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
it('rejects a declaration that is not an object', function() {
|
|
39
|
+
assert.throws(() => registry.register(null), { name: 'AssertionError' });
|
|
40
|
+
assert.throws(() => registry.register('q.one'), { name: 'AssertionError' });
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
it('rejects a declaration without a name', function() {
|
|
44
|
+
assert.throws(() => registry.register({}), { name: 'AssertionError', message: 'query must define a name to be registered' });
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
it('rejects a duplicate name', function() {
|
|
48
|
+
registry.register(ONE);
|
|
49
|
+
|
|
50
|
+
const message = 'query (name=q.one) is already registered';
|
|
51
|
+
|
|
52
|
+
assert.throws(() => registry.register({ name: 'q.one' }), { name: 'AssertionError', message });
|
|
53
|
+
assert.throws(() => registry.register(ONE), { name: 'AssertionError', message });
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
it('validates with the kind', function() {
|
|
57
|
+
registry = new Registry({ ...KIND, validate: d => assert(d.description, 'description required') });
|
|
58
|
+
|
|
59
|
+
assert.throws(() => registry.register({ name: 'q.one' }), { message: 'description required' });
|
|
60
|
+
assert.strictEqual(registry.has('q.one'), false);
|
|
61
|
+
})
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
describe('names', function() {
|
|
65
|
+
it('lists names in registration order', function() {
|
|
66
|
+
registry.register(TWO);
|
|
67
|
+
registry.register(ONE);
|
|
68
|
+
|
|
69
|
+
assert.deepStrictEqual(registry.names(), ['q.two', 'q.one']);
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
it('returns a copy of names', function() {
|
|
73
|
+
registry.register(ONE);
|
|
74
|
+
|
|
75
|
+
registry.names().push('q.other');
|
|
76
|
+
|
|
77
|
+
assert.deepStrictEqual(registry.names(), ['q.one']);
|
|
78
|
+
})
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
describe('registerAll', function() {
|
|
82
|
+
it('registers every value of an object', function() {
|
|
83
|
+
const registered = registry.registerAll({ a: ONE, b: TWO });
|
|
84
|
+
|
|
85
|
+
assert.deepStrictEqual(registry.names(), ['q.one', 'q.two']);
|
|
86
|
+
assert.strictEqual(registered[0], ONE);
|
|
87
|
+
assert.strictEqual(registered[1], TWO);
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
it('registers every item of an iterable', function() {
|
|
91
|
+
registry.registerAll([ONE]);
|
|
92
|
+
registry.registerAll(new Set([TWO]));
|
|
93
|
+
|
|
94
|
+
assert.deepStrictEqual(registry.names(), ['q.one', 'q.two']);
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
it('registers nothing if a later declaration is invalid', function() {
|
|
98
|
+
assert.throws(() => registry.registerAll([ONE, {}]), { name: 'AssertionError' });
|
|
99
|
+
|
|
100
|
+
assert.strictEqual(registry.has('q.one'), false);
|
|
101
|
+
})
|
|
102
|
+
|
|
103
|
+
it('registers nothing if a batch repeats a name', function() {
|
|
104
|
+
assert.throws(() => registry.registerAll([ONE, { ...ONE }]), { name: 'AssertionError', message: 'query (name=q.one) is already registered' });
|
|
105
|
+
|
|
106
|
+
assert.deepStrictEqual(registry.names(), []);
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
it('registers nothing if a batch repeats a registered name', function() {
|
|
110
|
+
registry.register(ONE);
|
|
111
|
+
|
|
112
|
+
assert.throws(() => registry.registerAll([TWO, ONE]), { name: 'AssertionError' });
|
|
113
|
+
|
|
114
|
+
assert.deepStrictEqual(registry.names(), ['q.one']);
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
it('rejects null, undefined or a string', function() {
|
|
118
|
+
assert.throws(() => registry.registerAll(null), { name: 'AssertionError' });
|
|
119
|
+
assert.throws(() => registry.registerAll(undefined), { name: 'AssertionError' });
|
|
120
|
+
assert.throws(() => registry.registerAll('q.one'), { name: 'AssertionError' });
|
|
121
|
+
})
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
describe('describe', function() {
|
|
125
|
+
beforeEach(function() {
|
|
126
|
+
registry.registerAll([ONE, TWO]);
|
|
127
|
+
})
|
|
128
|
+
|
|
129
|
+
it('describes a declaration with its name', function() {
|
|
130
|
+
assert.deepStrictEqual(registry.describe('q.one'), { description: 'one', name: 'q.one' });
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
it('describes with only the name if the kind has no describe', function() {
|
|
134
|
+
registry = new Registry({ kind: 'query', nameOf: d => d.name });
|
|
135
|
+
registry.register(ONE);
|
|
136
|
+
|
|
137
|
+
assert.deepStrictEqual(registry.describe('q.one'), { name: 'q.one' });
|
|
138
|
+
})
|
|
139
|
+
|
|
140
|
+
it('returns undefined for unknown names', function() {
|
|
141
|
+
assert.strictEqual(registry.describe('q.none'), undefined);
|
|
142
|
+
})
|
|
143
|
+
|
|
144
|
+
it('describes all declarations in order', function() {
|
|
145
|
+
assert.deepStrictEqual(registry.describeAll(), [
|
|
146
|
+
{ description: 'one', name: 'q.one' },
|
|
147
|
+
{ description: 'two', name: 'q.two' },
|
|
148
|
+
]);
|
|
149
|
+
})
|
|
150
|
+
})
|
|
151
|
+
})
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
const assert = require('assert');
|
|
2
|
+
const _ = require('lodash');
|
|
3
|
+
|
|
4
|
+
const build = require('../../util/chain');
|
|
5
|
+
|
|
6
|
+
describe('function chain', function() {
|
|
7
|
+
|
|
8
|
+
describe('no functions', function() {
|
|
9
|
+
it('no args returns undefined', function() {
|
|
10
|
+
const chain = build();
|
|
11
|
+
|
|
12
|
+
const result = chain();
|
|
13
|
+
|
|
14
|
+
assert(_.isUndefined(result));
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
it('single arg returns undefined', function() {
|
|
18
|
+
const chain = build();
|
|
19
|
+
|
|
20
|
+
const result = chain('gello!');
|
|
21
|
+
|
|
22
|
+
assert(_.isUndefined(result));
|
|
23
|
+
})
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
describe('single function', function() {
|
|
27
|
+
let chain;
|
|
28
|
+
|
|
29
|
+
describe('noop function', function() {
|
|
30
|
+
beforeEach(function() {
|
|
31
|
+
chain = build(z => {});
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
it('no args returns undefined', function() {
|
|
35
|
+
const result = chain();
|
|
36
|
+
|
|
37
|
+
assert(_.isUndefined(result));
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
it('single arg returns undefined', function() {
|
|
41
|
+
const result = chain('gello!');
|
|
42
|
+
|
|
43
|
+
assert(_.isUndefined(result));
|
|
44
|
+
})
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
describe('simple function', function() {
|
|
48
|
+
beforeEach(function() {
|
|
49
|
+
chain = build(z => 'world!');
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
it('no args returns last result', function() {
|
|
53
|
+
const result = chain();
|
|
54
|
+
|
|
55
|
+
assert.strictEqual(result, 'world!');
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
it('single arg returns last result', function() {
|
|
59
|
+
const result = chain('gello!');
|
|
60
|
+
|
|
61
|
+
assert.strictEqual(result, 'world!');
|
|
62
|
+
})
|
|
63
|
+
})
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
describe('two functions', function() {
|
|
67
|
+
let chain, firstExecuted;
|
|
68
|
+
|
|
69
|
+
describe('noop function', function() {
|
|
70
|
+
beforeEach(function() {
|
|
71
|
+
chain = build(
|
|
72
|
+
z => { firstExecuted = true },
|
|
73
|
+
z => {}
|
|
74
|
+
);
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
it('chain invoked', async function() {
|
|
78
|
+
chain();
|
|
79
|
+
|
|
80
|
+
assert(firstExecuted);
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
it('no args returns undefined', function() {
|
|
84
|
+
const result = chain();
|
|
85
|
+
|
|
86
|
+
assert(_.isUndefined(result));
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
it('single arg returns undefined', function() {
|
|
90
|
+
const result = chain('gello!');
|
|
91
|
+
|
|
92
|
+
assert(_.isUndefined(result));
|
|
93
|
+
})
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
describe('simple function', function() {
|
|
97
|
+
beforeEach(function() {
|
|
98
|
+
chain = build(
|
|
99
|
+
(z, resume) => { firstExecuted = true; return resume(z); },
|
|
100
|
+
z => 'world!'
|
|
101
|
+
);
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
it('chain invoked', async function() {
|
|
105
|
+
chain();
|
|
106
|
+
|
|
107
|
+
assert(firstExecuted);
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
it('no args returns last result', function() {
|
|
111
|
+
const result = chain();
|
|
112
|
+
|
|
113
|
+
assert.strictEqual(result, 'world!');
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
it('single arg returns last result', function() {
|
|
117
|
+
const result = chain('gello!');
|
|
118
|
+
|
|
119
|
+
assert.strictEqual(result, 'world!');
|
|
120
|
+
})
|
|
121
|
+
})
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
it('building call chain', async function() {
|
|
125
|
+
var result = "";
|
|
126
|
+
|
|
127
|
+
async function e(arg) {
|
|
128
|
+
// console.debug('e', arg);
|
|
129
|
+
|
|
130
|
+
return arg + "4";
|
|
131
|
+
}
|
|
132
|
+
async function one(arg, resume) {
|
|
133
|
+
// console.debug('one-before', arg);
|
|
134
|
+
|
|
135
|
+
const a = arg + '1';
|
|
136
|
+
|
|
137
|
+
const b = await resume(a);
|
|
138
|
+
|
|
139
|
+
// console.debug('one-after', b);
|
|
140
|
+
|
|
141
|
+
return b + "6";
|
|
142
|
+
}
|
|
143
|
+
function two(arg, resume) {
|
|
144
|
+
// console.debug('two-before', arg);
|
|
145
|
+
|
|
146
|
+
result += "2";
|
|
147
|
+
|
|
148
|
+
return resume(arg + '2');
|
|
149
|
+
}
|
|
150
|
+
async function three(arg, resume) {
|
|
151
|
+
const a = await resume(arg);
|
|
152
|
+
|
|
153
|
+
return a + "5";
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
assert.strictEqual(await build(e)('0'), '04');
|
|
157
|
+
// assert.strictEqual(await build(one)('0'), '016');
|
|
158
|
+
assert.strictEqual(await build(two, e)('0'), '024');
|
|
159
|
+
assert.strictEqual(await build(one, e)('0'), '0146');
|
|
160
|
+
assert.strictEqual(await build(one, two, e)('0'), '01246');
|
|
161
|
+
assert.strictEqual(await build(one, two, three, e)('0'), '012456');
|
|
162
|
+
})
|
|
163
|
+
|
|
164
|
+
})
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
const assert = require('assert');
|
|
2
|
+
|
|
3
|
+
const { error } = require('../../util/error');
|
|
4
|
+
|
|
5
|
+
describe('error', function() {
|
|
6
|
+
it('creates an Error with a name and message', function() {
|
|
7
|
+
const e = error('RequiredParameterError', 'gello!');
|
|
8
|
+
|
|
9
|
+
assert(e instanceof Error);
|
|
10
|
+
assert.strictEqual(e.name, 'RequiredParameterError');
|
|
11
|
+
assert.strictEqual(e.message, 'gello!');
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
it('formats the message with arguments', function() {
|
|
15
|
+
const e = error('RequiredParameterError', 'route (name="%s") argument (name="%s")', {}, 'my.event#action', 'message');
|
|
16
|
+
|
|
17
|
+
assert.strictEqual(e.message, 'route (name="my.event#action") argument (name="message")');
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
it('assigns attributes to the error', function() {
|
|
21
|
+
const e = error('ForbiddenError', 'gello!', { status: 403, route: 'my.event#action' });
|
|
22
|
+
|
|
23
|
+
assert.strictEqual(e.status, 403);
|
|
24
|
+
assert.strictEqual(e.route, 'my.event#action');
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
it('does not let attributes replace the name or message', function() {
|
|
28
|
+
const e = error('ForbiddenError', 'gello!', { name: 'Other', message: 'other' });
|
|
29
|
+
|
|
30
|
+
assert.strictEqual(e.name, 'ForbiddenError');
|
|
31
|
+
assert.strictEqual(e.message, 'gello!');
|
|
32
|
+
})
|
|
33
|
+
})
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
const assert = require('assert');
|
|
2
|
+
|
|
3
|
+
const { normalizeParameters } = require('../../util/parameters');
|
|
4
|
+
|
|
5
|
+
describe('normalizeParameters', function() {
|
|
6
|
+
it('normalizes a type name', function() {
|
|
7
|
+
assert.deepStrictEqual(normalizeParameters({ parentId: 'uuid' }), [{ name: 'parentId', type: 'uuid', optional: false }]);
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
it('normalizes a spec object', function() {
|
|
11
|
+
assert.deepStrictEqual(normalizeParameters({ type: { type: 'string', optional: true } }), [{ name: 'type', type: 'string', optional: true }]);
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
it('defaults optional to false', function() {
|
|
15
|
+
assert.deepStrictEqual(normalizeParameters({ count: { type: 'number' } }), [{ name: 'count', type: 'number', optional: false }]);
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
it('keeps declaration order', function() {
|
|
19
|
+
assert.deepStrictEqual(normalizeParameters({ b: 'string', a: 'string' }).map(p => p.name), ['b', 'a']);
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
it('returns an empty array for missing parameters', function() {
|
|
23
|
+
assert.deepStrictEqual(normalizeParameters(), []);
|
|
24
|
+
assert.deepStrictEqual(normalizeParameters(null), []);
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
it('does not copy other spec attributes', function() {
|
|
28
|
+
assert.deepStrictEqual(normalizeParameters({ m: { type: 'string', maxLength: 10 } }), [{ name: 'm', type: 'string', optional: false }]);
|
|
29
|
+
})
|
|
30
|
+
})
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
const assert = require('assert');
|
|
2
|
+
|
|
3
|
+
const restrict = require('../../util/restrict');
|
|
4
|
+
|
|
5
|
+
describe('restrict', function() {
|
|
6
|
+
let module;
|
|
7
|
+
|
|
8
|
+
beforeEach(function() {
|
|
9
|
+
module = {
|
|
10
|
+
__metadata: {
|
|
11
|
+
name: 'unittest.event',
|
|
12
|
+
triggers: {
|
|
13
|
+
effect: {},
|
|
14
|
+
action: {
|
|
15
|
+
params: { id: 'string' },
|
|
16
|
+
transaction: 'NEW'
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
|
|
21
|
+
effect: z => 'effect',
|
|
22
|
+
action: z => 'action'
|
|
23
|
+
};
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
it('defines roles on action trigger', function() {
|
|
27
|
+
const restricted = restrict(module, ['administrator']);
|
|
28
|
+
|
|
29
|
+
assert.deepStrictEqual(restricted.__metadata.triggers.action, {
|
|
30
|
+
params: { id: 'string' },
|
|
31
|
+
transaction: 'NEW',
|
|
32
|
+
roles: ['administrator']
|
|
33
|
+
});
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
it('does not restrict effect trigger', function() {
|
|
37
|
+
const restricted = restrict(module, ['administrator']);
|
|
38
|
+
|
|
39
|
+
assert.deepStrictEqual(restricted.__metadata.triggers.effect, {});
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
it('does not add triggers the module does not implement', function() {
|
|
43
|
+
const restricted = restrict(module, ['administrator']);
|
|
44
|
+
|
|
45
|
+
assert(!restricted.__metadata.triggers.api);
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
it('defines trigger metadata if missing', function() {
|
|
49
|
+
delete module.__metadata.triggers;
|
|
50
|
+
|
|
51
|
+
const restricted = restrict(module, ['administrator']);
|
|
52
|
+
|
|
53
|
+
assert.deepStrictEqual(restricted.__metadata.triggers.action, { roles: ['administrator'] });
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
it('keeps trigger functions', function() {
|
|
57
|
+
const restricted = restrict(module, ['administrator']);
|
|
58
|
+
|
|
59
|
+
assert.strictEqual(restricted.action, module.action);
|
|
60
|
+
assert.strictEqual(restricted.effect, module.effect);
|
|
61
|
+
assert.strictEqual(restricted.__metadata.name, 'unittest.event');
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
it('does not modify the original module', function() {
|
|
65
|
+
restrict(module, ['administrator']);
|
|
66
|
+
|
|
67
|
+
assert(!module.__metadata.triggers.action.roles);
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
it('requires roles', function() {
|
|
71
|
+
assert.throws(z => restrict(module, []));
|
|
72
|
+
})
|
|
73
|
+
})
|
package/util/chain.js
ADDED
package/util/error.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
const { format } = require('util');
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Creates an Error describing a failed event invocation
|
|
5
|
+
*
|
|
6
|
+
* @param {*} name - error name (e.g. RequiredParameterError)
|
|
7
|
+
* @param {*} message - printf style message (see util.format)
|
|
8
|
+
* @param {*} attributes - additional properties assigned to the error (e.g. route, status)
|
|
9
|
+
* @param {...any} args - message arguments
|
|
10
|
+
*/
|
|
11
|
+
function error(name, message, attributes={}, ...args) {
|
|
12
|
+
return Object.assign(new Error(), attributes, { name, message: format(message, ...args) });
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
module.exports = { error };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
const _ = require('lodash');
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Normalizes parameter specs (e.g. trigger params, named query parameters) into a uniform description
|
|
5
|
+
* - a spec is either a type name ('uuid') or an object ({ type, optional })
|
|
6
|
+
* - other spec attributes (e.g. maxLength) are not included
|
|
7
|
+
*
|
|
8
|
+
* @param {*} params map of parameter name to spec
|
|
9
|
+
* @returns array of { name, type, optional }, in declaration order
|
|
10
|
+
*/
|
|
11
|
+
function normalizeParameters(params) {
|
|
12
|
+
if (!params) return [];
|
|
13
|
+
|
|
14
|
+
return Object.keys(params).map(name => {
|
|
15
|
+
const spec = params[name];
|
|
16
|
+
const { type, optional=false } = _.isString(spec) ? { type: spec } : (spec || {});
|
|
17
|
+
|
|
18
|
+
return { name, type, optional: Boolean(optional) };
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
module.exports = { normalizeParameters };
|
package/util/restrict.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
const assert = require('assert');
|
|
2
|
+
const _ = require('lodash');
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Returns a copy of an event/snapshot module with allowed roles defined on its externally reachable triggers
|
|
6
|
+
* - used to restrict framework (gell) modules, which don't define application roles, when registering them with an application
|
|
7
|
+
* - the original module is not modified
|
|
8
|
+
*
|
|
9
|
+
* @param {*} module event/snapshot module with __metadata
|
|
10
|
+
* @param {*} roles allowed roles (see gell-dispatch/middleware/roles)
|
|
11
|
+
* @param {*} triggers names of triggers to restrict (only those defined by the module are restricted)
|
|
12
|
+
*/
|
|
13
|
+
module.exports = function restrict(module, roles, triggers=['action', 'api']) {
|
|
14
|
+
assert(module && module.__metadata, 'module with __metadata is required');
|
|
15
|
+
assert(_.isArray(roles) && roles.length > 0, 'roles are required');
|
|
16
|
+
|
|
17
|
+
const __metadata = _.cloneDeep(module.__metadata);
|
|
18
|
+
const definedTriggers = __metadata.triggers || {};
|
|
19
|
+
|
|
20
|
+
triggers.forEach(name => {
|
|
21
|
+
if (_.isFunction(module[name])) definedTriggers[name] = { ...definedTriggers[name], roles: [...roles] };
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
__metadata.triggers = definedTriggers;
|
|
25
|
+
|
|
26
|
+
return { ...module, __metadata };
|
|
27
|
+
}
|