UNPKG

1.47 kBJavaScriptView Raw
1'use strict';
2
3const { nextify, asyncParamApiCallback } = require('../../utils');
4
5module.exports = async (
6 {
7 commandName,
8 command,
9 AggregateApi,
10 },
11 {
12 Command,
13 PreLoadCondition,
14 PreCondition,
15 validatorFunctionBuilder,
16 },
17 customApiBuilder,
18) => {
19 const commandSettings = {
20 name: commandName,
21 };
22
23 if (!Array.isArray(command))
24 command = [command];
25
26
27 const result = {
28 preLoadConditions: [],
29 preConditions: [],
30 command: null,
31 validator: null,
32 };
33
34 for (const item of command) { // eslint-disable-line no-restricted-syntax
35 // command
36 if (typeof item === 'function') {
37 result.command = new Command(commandSettings, (cmd, agg) => item(cmd, new AggregateApi(agg), customApiBuilder(cmd)));
38 continue;
39 }
40
41 if (item.schema && validatorFunctionBuilder) {
42 result.validator = nextify(await Promise.resolve(validatorFunctionBuilder(item.schema)), 'schema'); // eslint-disable-line no-await-in-loop
43 continue;
44 }
45
46 // settings ( exists ? )
47 if (item.settings) {
48 Object.assign(commandSettings, item.settings);
49 continue;
50 }
51
52 if (item.preLoadCondition) {
53 result.preLoadConditions.push(new PreLoadCondition({ name: [commandName] }, asyncParamApiCallback(item.preLoadCondition, customApiBuilder, 'cmd')));
54 continue;
55 }
56
57 if (item.preCondition)
58 result.preConditions.push(new PreCondition({ name: [commandName] }, asyncParamApiCallback(item.preCondition, customApiBuilder, 'cmd', 'agg')));
59 }
60
61 return result;
62};