UNPKG

@monitoro/herd

Version:

Automate your browser, build AI web tools and MCP servers with Monitoro Herd

56 lines (55 loc) 1.79 kB
import Handlebars from 'handlebars'; function format(url, params) { // ensure params provided fulfill all required params from url const requiredParams = url.params; for (const key in requiredParams) { if (!params || !params.hasOwnProperty(key)) { throw new Error(`Missing required parameter: ${key}`); } } // Use Handlebars to process the template try { const template = Handlebars.compile(url.template, { noEscape: true }); const result = template(params || {}); return result; } catch (error) { console.error('Error processing Handlebars template:', error); return url.template; } } export class ResourceProvider { constructor(urls, selectors) { this.urls = urls; this.selectors = selectors; } url(id, params) { const url = this.urls.find(url => url.id === id); if (!url) { throw new Error(`URL with id ${id} not found`); } return format(url, params); } _selector(id) { const selector = this.selectors.find(selector => selector.id === id); if (!selector) { throw new Error(`Selector with id ${id} not found`); } return selector; } selector(id) { const selector = this._selector(id); return selector.value; } urlExamplesForSelector(selectorId) { const selector = this._selector(selectorId); const urls = selector.examples.map(example => { const url = this.urls.find(url => url.id === example.urlId); if (!url) { throw new Error(`URL with id ${example.urlId} not found`); } return format(url, example.urlParams); }); return urls; } }