@curi/helpers

GitHub logoGitHub RepoNPM logoNPM Package

About

The @curi/helpers package provides functions that may be useful in a Curi application.

Installation

npm install @curi/helpers

UMD scripts script are also available through Unpkg. You can access the package's exports using window.CuriHelpers.

API

once

once is a simple caching function. It takes a function as its argument and returns a new function. The first time the returned function is called, it will call the function passed to it and return its result. Every call after that will re-use the result from the first call.

The once function is useful for any async functions that only need to be called once.

import { once } from "@curi/helpers";

let cachedGetItems = once(() => api.getItems());

let routes = prepareRoutes([
  {
    name: "Menu",
    path: "menu",
    resolve() {
      // this function will be called every time the user
      // navigates to the "Menu" route
      let nonCached = api.getItems();

      // this function is only called the first time the
      // user navigates to the "Menu" route
      let cached = cachedGetItems();
      return Promise.all([nonCached, cached]);
    }
  }
]);

preferDefault

When using dynamic import syntax (import("someModule")), the resolved module is a module object containing all of the exports from that module. If the module has a default export (export default ...), that will be the module's default property. The preferDefault function will resolve with the default property of the module if it exists and with the module if it does not.

import { preferDefault } from "@curi/helpers";

let routes = prepareRoutes([
  {
    name: "Menu",
    path: "menu",
    resolve() {
      return import("./components/Menu")
        .then(preferDefault);
    },
    respond({ resolved }) {
      return { body: resolved }
    }
  }
]);