@podium/fastify-layout

    2.2.27 • Public • Published

    @podium/fastify-layout

    Fastify plugin for @podium/layout.

    Dependencies GitHub Actions status Known Vulnerabilities

    Module for building @podium/layout servers with fastify. For writing layouts, please see the Podium documentation.

    Installation

    $ npm install @podium/fastify-layout

    Requirements

    This module require Fastify v2.0.0 or newer.

    Simple usage

    Build a simple layout server including a single podlet:

    const FastifyLayout = require('@podium/fastify-layout');
    const Layout = require('@podium/layout');
    const fastify = require('fastify');
    
    const app = fastify({ logger: true });
    
    const layout = new Layout({
        pathname: '/',
        logger: console,
        name: 'layout',
    
    });
    
    const podlet = layout.client.register({
        name: 'myPodlet',
        uri: 'http://localhost:7100/manifest.json'
    });
    
    app.register(FastifyLayout, layout);
    
    app.get(layout.pathname(), async (request, reply) => {
        const incoming = reply.app.podium;
        const result = await podlet.fetch(incoming);
        reply.podiumSend(result.content);
    });
    
    const start = async () => {
        try {
            await app.listen(7000);
            app.log.info(`server listening on ${app.server.address().port}`);
        } catch (err) {
            app.log.error(err);
            process.exit(1);
        }
    }
    start();

    Register plugin

    The plugin is registered by passing an instance of this plugin to the fastify server .register() method together with an instance of the @podium/layout class.

    app.register(FastifyLayout, layout);

    Request params

    On each request @podium/layout will run a set of operations on the request and create an [incoming] object. The [incoming] object is stored at reply.app.podium which is accessible inside request handlers.

    app.get('/', async (request, reply) => {
        console.log(reply.app.podium.context);
        [ ... ]
    });

    Its possible to pass request bound parameters to @podium/context parsers by setting an object at reply.app.params.

    Example: To pass a value to the @podium/context locale parser it should be set on reply.app.params by a extension executed previously of this extension.

    reply.podiumSend(fragment)

    This method will wrap the given fragment in a default document template before dispatching.

    See document template for further information.

    License

    Copyright (c) 2019 FINN.no

    Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

    Install

    npm i @podium/fastify-layout

    DownloadsWeekly Downloads

    16

    Version

    2.2.27

    License

    MIT

    Unpacked Size

    16.7 kB

    Total Files

    5

    Last publish

    Collaborators

    • avatar
    • avatar
    • avatar
    • avatar
    • avatar
    • avatar