Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bus: custom URL params (queries) was added #1957

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
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',
},
},
},
});
Loading