@oada/cli
Version:
CLI OADA client
72 lines • 2.11 kB
JavaScript
import { URL } from 'node:url';
import { connect, } from '@oada/client';
const methods = ['get', 'head', 'put', 'post', 'delete'];
export function conn(config) {
const connections = {
domains: new Map(),
};
const client = {};
for (const method of methods) {
client[method] = async ({ path: p, ...rest }) => {
let path = `${p}`;
let host;
try {
({ host, pathname: path } = new URL(p));
}
finally {
const con = await getConnection(host);
return con[method]({
path,
...rest,
});
}
};
}
client.watch = async ({ path: p, ...rest }) => {
let path = `${p}`;
let host;
try {
({ host, pathname: path } = new URL(p));
}
finally {
const con = await getConnection(host);
return con.watch({
path,
...rest,
});
}
};
return client;
async function getConnection(name) {
if (!name) {
if (!connections.connection) {
const { domain, token, ws } = config;
connections.connection = connect({
domain,
token,
connection: ws ? 'ws' : 'http',
});
}
return connections.connection;
}
const { domains } = connections;
if (domains.has(name)) {
return domains.get(name);
}
const { ws, domains: { [name]: { domain = '', token = '', connection = undefined } = {}, }, } = config;
if (connection) {
const con = Promise.resolve(connection);
domains.set(name, con);
return con;
}
const con = connect({
domain,
token,
connection: ws ? 'ws' : 'http',
});
domains.set(name, con);
return con;
}
}
export default conn;
//# sourceMappingURL=connections.js.map