@curi/helpers

Installation

The package can be installed through npm (you need to have Node & NPM installed).

npm install @curi/helpers

Prefer inline scripts? A full (.umd.js) and minified (.min.js) script is available for every version through Unpkg. You can access the package's exports through window.CuriHelpers.

About

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

API

once()

once() 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 route resolve functions that only need to be called once.

Note: This will not work for functions whose result depends on variables that will change for a route (i.e. loading data based on route params).
import { once } from "@curi/helpers";
            
const routes = prepareRoutes([
  {
    name: "Menu",
    path: "menu",
    resolve: {
      // this function will be called every time the user
      // navigates to the "Menu" route
      nonCached: () => api.getItems(),
      // this function is only called the first time the
      // user navigates to the "Menu" route
      cached: once(() => api.getItems)
    }
  }
]);

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";

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