Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions lib/bus.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,43 @@ const { metarhia } = require('./deps.js');

const prepare = (unit, application) => {
const namespaces = application.schemas ? [application.schemas.model] : [];
const { parameters, returns } = unit;
const { parameters, query = {}, returns } = unit;
const { Schema } = metarhia.metaschema;
const validation = {
parameters: parameters ? Schema.from(parameters, namespaces) : null,
query: query.params ? Schema.from(query.params, namespaces) : null,
returns: returns ? Schema.from(returns, namespaces) : null,
};
const method = async (args) => {
const { parameters, returns } = validation;
const method = async (args = {}) => {
const { parameters, query, returns } = validation;
if (parameters) {
const { valid, errors } = parameters.check(args);
const problems = errors.join('; ');
if (!valid) return new Error('Invalid parameters type: ' + problems);
}
if (unit.query.params) {
const { valid, errors } = query.check(args);
const problems = errors.join('; ');
if (!valid) return new Error('Invalid query type: ' + problems);
}
const service = method.parent['.service'];
const verb = unit.method.get ? 'get' : 'post';
const target = [service.url, unit.method[verb]];
if (unit.method.path) {
target.push(...unit.method.path.map((arg) => args[arg]));
}
let url = target.join('/');
if (unit.query) {
const params = [];
const { prefix = '', suffix = '' } = unit.query;
for (const param of Object.keys(unit.query.params)) {
if (!args[param]) continue;
params.push([prefix + param + suffix, args[param]]);
}
const parsedParams = Object.fromEntries(params);
const stringParams = new URLSearchParams(parsedParams).toString();
url = params.length ? url + '?' + stringParams : url;
}
const body = metarhia.metautil.serializeArguments(unit.method.body, args);
const url = target.join('/');
const options = { method: verb.toUpperCase(), headers: unit.headers, body };
Expand Down
2 changes: 1 addition & 1 deletion test/bus.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
assert.deepStrictEqual(bus.tree, {});

await bus.load();
assert.deepStrictEqual(Object.keys(bus.tree), ['math', 'worldTime']);
assert.deepStrictEqual(Object.keys(bus.tree), ['fakerapi', 'math', 'worldTime']);

Check failure on line 28 in test/bus.js

View workflow job for this annotation

GitHub Actions / build (18, macos-latest)

This line has a length of 83. Maximum allowed is 80

Check failure on line 28 in test/bus.js

View workflow job for this annotation

GitHub Actions / build (22, ubuntu-latest)

This line has a length of 83. Maximum allowed is 80

Check failure on line 28 in test/bus.js

View workflow job for this annotation

GitHub Actions / build (20, macos-latest)

This line has a length of 83. Maximum allowed is 80

Check failure on line 28 in test/bus.js

View workflow job for this annotation

GitHub Actions / build (23, ubuntu-latest)

This line has a length of 83. Maximum allowed is 80

Check failure on line 28 in test/bus.js

View workflow job for this annotation

GitHub Actions / build (22, macos-latest)

This line has a length of 83. Maximum allowed is 80

Check failure on line 28 in test/bus.js

View workflow job for this annotation

GitHub Actions / build (21, ubuntu-latest)

This line has a length of 83. Maximum allowed is 80
assert.strictEqual(bus.tree.math.parent, bus.tree);
assert.strictEqual(typeof bus.tree.math, 'object');
assert.strictEqual(typeof bus.tree.math.eval, 'function');
Expand Down
7 changes: 7 additions & 0 deletions test/bus/fakerapi/.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
({
url: 'https://fakerapi.it',
limits: [
{ calls: 10, per: '1m' },
{ calls: 10000, per: '1d' },
],
});
32 changes: 32 additions & 0 deletions test/bus/fakerapi/books.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
({
method: {
get: 'api/v1/books',
},

query: {
prefix: '_',
params: {
quantity: '?number',
},
},

returns: {
status: 'string',
code: 'number',
total: 'number',
data: {
type: 'array',
value: {
id: 'number',
title: 'string',
author: 'string',
genre: 'string',
description: 'string',
isbn: 'string',
image: 'string',
published: 'string',
publisher: 'string',
},
},
},
});
Loading