Skip to content

Commit

Permalink
Bus: custom URL params (queries) was added
Browse files Browse the repository at this point in the history
  • Loading branch information
stazeworks committed Feb 4, 2024
1 parent 1fd89f6 commit 4ca8ed0
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 5 deletions.
25 changes: 21 additions & 4 deletions lib/bus.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,44 @@ 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(), body };
const result = await metarhia.metautil.httpApiCall(url, options);
if (returns) {
Expand Down
2 changes: 1 addition & 1 deletion test/bus.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ metatests.testAsync('lib/bus', async (test) => {
test.strictSame(typeof bus.application, 'object');
test.strictSame(bus.tree, {});
await bus.load();
test.strictSame(Object.keys(bus.tree), ['math', 'worldTime']);
test.strictSame(Object.keys(bus.tree), ['fakerapi', 'math', 'worldTime']);
test.strictSame(bus.tree.math.parent, bus.tree);
test.strictSame(typeof bus.tree.math, 'object');
test.strictSame(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',
},
},
},
});

0 comments on commit 4ca8ed0

Please sign in to comment.