saucier-core

2.0.1 • Public • Published
                       _           
                      (_)          
  ___  __ _ _   _  ___ _  ___ _ __ 
 / __|/ _` | | | |/ __| |/ _ \ '__|
 \__ \ (_| | |_| | (__| |  __/ |   
 |___/\__,_|\__,_|\___|_|\___|_|   
                                   
                                   

Saucier

Saucier is a Node.JS framework for helping you build headless Drupal websites.

Usage

Setup

There are two additional companion modules you can use when starting out, saucier-get and saucier-cache. These modules provide functionality that was previously baked directly into Saucier.

var env = require('./_config/env.json'),
    https = require('https'),
    saucierGet = require('saucier-get')( new https.Agent({ keepAlive: true}) ),
    saucierCache = require('saucier-cache')(),
    templates = require('./public/templates/views'),
    saucier = require('saucier-core')(saucierGet, saucierCache, templates, {
      logFormat: '\033[1;32m:remote-addr ->\033[m [:date] \033[1;35m:method :url\033[m :http-version \033[1;34m:status\033[m :res[Content-Length]',
      staticDir: 'public',
      maxAge: '4d',
      envConfig: env
    });

In this example we are using the default saucier-get and saucier-cache modules.

Saucier has four required options to function correctly, and API compatible saucier-get, saucier-cache, a templates function, and an options object. Saucier Get/Cache are explained in their own repositories.

In this instance templates is a series of compiled Dust.JS templates. You can technically use any template engine/object as long as it supports a render() function.

The following properties are expected when passing in an options object:

  • logFormat : Log Format is any Morgan compatible log string. Default is dev.

  • staticDir : This is where your static assets will be served from. Default is public.

  • maxAge : Length of time in which to cache static assets with the client. Default is 4d.

  • envConfig : REQUIRED This is an environmental configuration object.

{
  "local": {
    "environment": "local",
    "api": "https://fakeapi.ssl.dev",
    "ttl": "800",
    "maxAttempts" : 2,
    "retryDelay": 100
  }
}

Each primary object should match the environment variable set at NODE_ENV. This defaults to local.

  • api : The backend API path.
  • ttl : The TTL attached to cache keys in Redis.
  • maxAttempts : The number of attempts to try when requesting data from the backend API. Default is 2.
  • retryDelay : Delay between retrying requests to the backend API in milliseconds. Default is 100.

Routes

When creating your routes, Saucier V1 routes are compatible. The only difference is how route processors are handled. Pass the completely created Saucier object to your routes.

Routes are essentially node modules.

module.exports = function attach(app) {
  app.handleGet('/', 'example.dust', {
    resource: ['sample'],
    processors: {
      sample: function (data) {
        data.cheese = 'cake';
        return data;
      }
    }
  });
}

The function app.handleGet() is a wrapper for app.get() which supports some additional options.

  • / : The route pattern you wish to match on. Routes are processed sequentially within the file, and then sequentially in the order that they are required.
  • example.dust : This is the name of the template. That will be rendered for this route.
  • An options object:
    • resource : An array of resources to request data from. These names will be merged with the value of api from your env.json, https://fakeapi.ssl.dev/sample. These resources are are processed asynchronously and will return an object containing JSON from the backend resource.
    • processors : An object of functions that will modify the data. The data object is the response from the previously collected resources.

Extras

  • SAUCIERRENDER : An environment variable that can be set to return JSON instead of a rendered route. False is the only accepted value, SAUCIERRENDER=false
  • DEBUG=saucier:* : Saucier and each of it's core modules has a debug name space, saucier:*. Enable this to get wonderful debug output when developing your application.

Readme

Keywords

none

Package Sidebar

Install

npm i saucier-core

Weekly Downloads

0

Version

2.0.1

License

MIT

Last publish

Collaborators

  • alwaysworking