Treats

Treats

  • Getting Started

โ€บAuthoring Addons

Getting Started

  • Installation

Tutorial

  • 01. Creating Your First Page
  • 02. Using Redux
  • 03. Using GraphQL
  • 04. Fetch Data for SSR
  • 05. Adding Addons

Main Concepts

  • Overview
  • Routing
  • Localization
  • Code-splitting
  • Redux
  • GraphQL Client
  • Middlewares
  • Helpers
  • Server-side Events
  • Server-side Template
  • Server-side Rendering
  • Custom Server App
  • Custom Client Initialization
  • Custom React App
  • Runtime Config
  • Build Config
  • Environment Variables
  • Code Generator
  • Scripts
  • Addons
  • Typescript
  • Workbox

API Reference

  • Overview
  • Filesystem Hooks
  • Components
  • Server
  • Client
  • Router
  • Intl
  • Locale Data
  • Helmet
  • Redux
  • Graphql

Authoring Addons

  • Overview
  • Helpers
  • Middlewares
  • Generators
  • Wrapping Up

Addons

  • Treats Addons List

Contributing

  • How To Contribute

FAQ

  • FAQs

Middlewares

In this section, we'll learn to create a middleware addon for Treats. Middleware are also modelled as object with middleware method that will always be triggered on response/request cycle. We used object instead of simple function to encapsulate other side-effect methods that might be needed by the middleware function, for example, we might need to separate data-fetching function into separate methods.

In the previous section, we've finished building a simple helper that returns fizz or buzz by turns in the previous example. We'll continue to use the same project to build our middleware now.

We'll build a simple middleware that triggers our helper and prints out foo if current state of our helper is fizz, and bar if it is the opposite.

To start building our middleware, let's first create the directory and middleware file:

src/
|-- middleware/
    |-- foobar/
        |-- index.js

Just like with helper, we'll start creating our middleware by defining a simple object:

// src/middleware/foobar/index.js

// ๐Ÿ‘‡ start by creating plain object for now
const FooBarMiddleware = {

};

export default FooBarMiddleware;

Valid middleware would have to implement middleware method that would be called on each response/request cycle.

// src/middleware/foobar/index.js
const FooBarMiddleware = {
     /* ๐Ÿ‘‡ this is our middleware method that would be called on each response/request cycle
     middleware method can be async or normal function. */
    async middleware(req, res, next) {
        const { app } = req,
            fizzBuzzHelper = app.get("fizzbuzz"),
            fizzBuzzMessage = fizzBuzzHelper.print();
        let message = "foo";

        if(fizzBuzzMessage === "buzz") {
            message = "bar"
        }

        console.info(`FOOBAR MIDDLEWARE: ${message}`);
        next(); // ๐Ÿ‘ˆ don't forget to call the next function so request would be continued to the next middleware in line
    }
};

export default FooBarMiddleware;

Now let's test our middleware from our main app. To use our middleware, we would need to import and attach it to our Treats custom server:

// src/_server/index.js
import initServer, { EVENT_TYPES } from "@treats/server";
import FizzBuzzHelper from "../helper/fizzbuzz";
import FooBarMiddleware from "../middleware/foobar";

const app = initServer({
    customHelpers: FizzBuzzHelper,
    customMiddlewares: FooBarMiddleware,
    ...
});

export default app;

Now when you run your Treats app, when it receives a request it would print your message from the middleware.

Just like helper, middleware can also have init method that would be triggered on main app initialized. For example:

// src/middleware/foobar/index.js
const FooBarMiddleware = {
    init(app) {
        console.info("[Foobar] Middleware Initialized");
    },
    async middleware(req, res, next) {
        ...
    }
};

export default FooBarMiddleware;

Middleware can also triggers custom events:

// src/middleware/foobar/index.js
const FooBarMiddleware = {
    init(app) {
        console.info("[Foobar] Middleware Initialized");
    },
    async middleware(req, res, next) {
        const { app } = req,
            fizzBuzzHelper = app.get("fizzbuzz"),
            fizzBuzzMessage = fizzBuzzHelper.print();
        let message = "foo";

        if(fizzBuzzMessage === "buzz") {
            message = "bar"
        } else {
            const eventManager = app.get("eventManager");
            eventManager.fire("FOO_MESSAGE", req, res);
        }

        console.info(`FOOBAR MIDDLEWARE: ${message}`);
        next();
    }
};

export default FooBarMiddleware;
// src/_server/index.js
import initServer, { EVENT_TYPES } from "@treats/server";
import FizzBuzzHelper from "../helper/fizzbuzz";
import FooBarMiddleware from "../middleware/foobar";

const app = initServer({
    customMiddlewares: FooBarMiddleware,
    customHelpers: FizzBuzzHelper,
    customEvents: {
        [EVENT_TYPES.BEFORE_RENDER]: (req, res) => {
            ...
        },
        "BUZZ_MESSAGE": count => {
            ...
        },
        "FOO_MESSAGE": (req,res) => {
            console.info("BAR MESSAGE EVENT!");
        }
    }
});

export default app;

Congratulations! You've successfully created a simple middleware addon for Treats. In the next section, we'll look at how we can create a code generator template.

โ† HelpersGenerators โ†’
Tokopedia Open Source
Copyright ยฉ 2019 Tokopedia OSS