Ashkan

Ashkan

Add your urls and include parameters

// Menu: URL Actions "App"
// Description: Add your urls and include parameters
// Shortcut: cmd shift u
// Author: Ashkan Hosseini
let { getUrl, getUrls, addUrl, removeUrl } = await lib('urls');
let urlsChoices = () =>
getUrls().map(({ name, url, id }) => ({
name: `${name} (${url})`,
value: id,
}));
let useUrl = async () => {
let id = await arg('Use url:', urlsChoices());
let { url } = await getUrl(id);
const matches = url.match(/\{(.*?)\}/g);
if (matches && matches.length) {
const params = {};
for (let key of matches) {
key.replace('{', '').replace('}', '');
params[key] = await arg(`Enter value for param ${key}`);
}
url = Object.entries(params).reduce((acc, [key, value]) => {
return acc.replace(new RegExp(`${key}`, 'g'), value);
}, url);
}
exec(`open ${url}`);
};
let add = async () => {
const name = await arg('Enter a URL name:');
const url = await arg('Enter a URL address:');
addUrl(name, url);
await add();
};
let remove = async () => {
let id = await arg('Remove url:', urlsChoices());
removeUrl(id);
await remove();
};
onTab('Use', useUrl);
onTab('Add', add);
onTab('Remove', remove);
let urlsDb = db('urls', { urls: [] });
export let urls = urlsDb.get('urls');
export let getUrls = () => urls.value();
export let addUrl = (name, url) => {
urls.insert({ name, url }).write();
};
export let removeUrl = (id) => {
urls.remove({ id }).write();
};
export let getUrl = async (id) => {
const urls = await getUrls();
return urls.find((obj) => obj.id == id);
};