Skip to content

Commit eeb4f10

Browse files
authored
Bus: custom URL params (queries) was added
1 parent 5aa66da commit eeb4f10

4 files changed

Lines changed: 61 additions & 4 deletions

File tree

lib/bus.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,43 @@ const { metarhia } = require('./deps.js');
44

55
const prepare = (unit, application) => {
66
const namespaces = application.schemas ? [application.schemas.model] : [];
7-
const { parameters, returns } = unit;
7+
const { parameters, query = {}, returns } = unit;
88
const { Schema } = metarhia.metaschema;
99
const validation = {
1010
parameters: parameters ? Schema.from(parameters, namespaces) : null,
11+
query: query.params ? Schema.from(query.params, namespaces) : null,
1112
returns: returns ? Schema.from(returns, namespaces) : null,
1213
};
13-
const method = async (args) => {
14-
const { parameters, returns } = validation;
14+
const method = async (args = {}) => {
15+
const { parameters, query, returns } = validation;
1516
if (parameters) {
1617
const { valid, errors } = parameters.check(args);
1718
const problems = errors.join('; ');
1819
if (!valid) return new Error('Invalid parameters type: ' + problems);
1920
}
21+
if (unit.query.params) {
22+
const { valid, errors } = query.check(args);
23+
const problems = errors.join('; ');
24+
if (!valid) return new Error('Invalid query type: ' + problems);
25+
}
2026
const service = method.parent['.service'];
2127
const verb = unit.method.get ? 'get' : 'post';
2228
const target = [service.url, unit.method[verb]];
2329
if (unit.method.path) {
2430
target.push(...unit.method.path.map((arg) => args[arg]));
2531
}
32+
let url = target.join('/');
33+
if (unit.query) {
34+
const params = [];
35+
const { prefix = '', suffix = '' } = unit.query;
36+
for (const param of Object.keys(unit.query.params)) {
37+
if (!args[param]) continue;
38+
params.push([prefix + param + suffix, args[param]]);
39+
}
40+
const parsedParams = Object.fromEntries(params);
41+
const stringParams = new URLSearchParams(parsedParams).toString();
42+
url = params.length ? url + '?' + stringParams : url;
43+
}
2644
const body = metarhia.metautil.serializeArguments(unit.method.body, args);
2745
const url = target.join('/');
2846
const options = { method: verb.toUpperCase(), headers: unit.headers, body };

test/bus.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ test('lib/bus - should load bus correctly', async () => {
2525
assert.deepStrictEqual(bus.tree, {});
2626

2727
await bus.load();
28-
assert.deepStrictEqual(Object.keys(bus.tree), ['math', 'worldTime']);
28+
assert.deepStrictEqual(Object.keys(bus.tree), ['fakerapi', 'math', 'worldTime']);
2929
assert.strictEqual(bus.tree.math.parent, bus.tree);
3030
assert.strictEqual(typeof bus.tree.math, 'object');
3131
assert.strictEqual(typeof bus.tree.math.eval, 'function');

test/bus/fakerapi/.service.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
({
2+
url: 'https://fakerapi.it',
3+
limits: [
4+
{ calls: 10, per: '1m' },
5+
{ calls: 10000, per: '1d' },
6+
],
7+
});

test/bus/fakerapi/books.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
({
2+
method: {
3+
get: 'api/v1/books',
4+
},
5+
6+
query: {
7+
prefix: '_',
8+
params: {
9+
quantity: '?number',
10+
},
11+
},
12+
13+
returns: {
14+
status: 'string',
15+
code: 'number',
16+
total: 'number',
17+
data: {
18+
type: 'array',
19+
value: {
20+
id: 'number',
21+
title: 'string',
22+
author: 'string',
23+
genre: 'string',
24+
description: 'string',
25+
isbn: 'string',
26+
image: 'string',
27+
published: 'string',
28+
publisher: 'string',
29+
},
30+
},
31+
},
32+
});

0 commit comments

Comments
 (0)