UNPKG

cl-react-graph

Version:
56 lines (48 loc) 1.49 kB
const plugins = require(`./api-runner-browser-plugins`) const { getResourcesForPathname, getResourcesForPathnameSync, getResourceURLsForPathname, } = require(`./loader`).publicLoader exports.apiRunner = (api, args = {}, defaultReturn, argTransform) => { // Hooks for cypress-gatsby's API handler if (process.env.CYPRESS_SUPPORT) { if (window.___apiHandler) { window.___apiHandler(api) } else if (window.___resolvedAPIs) { window.___resolvedAPIs.push(api) } else { window.___resolvedAPIs = [api] } } let results = plugins.map(plugin => { if (!plugin.plugin[api]) { return undefined } args.getResourcesForPathnameSync = getResourcesForPathnameSync args.getResourcesForPathname = getResourcesForPathname args.getResourceURLsForPathname = getResourceURLsForPathname const result = plugin.plugin[api](args, plugin.options) if (result && argTransform) { args = argTransform({ args, result, plugin }) } return result }) // Filter out undefined results. results = results.filter(result => typeof result !== `undefined`) if (results.length > 0) { return results } else if (defaultReturn) { return [defaultReturn] } else { return [] } } exports.apiRunnerAsync = (api, args, defaultReturn) => plugins.reduce( (previous, next) => next.plugin[api] ? previous.then(() => next.plugin[api](args, next.options)) : previous, Promise.resolve() )