Server

A base datasource layer for reading, transforming and writing structured server payloads.

Import
the javascript logo
import { Server } from "@schukai/monster/source/data/datasource/server.mjs";
Source
the git logo
Package
the npm logo
Since
3.4.0

Server is the shared datasource base for transports that read and write structured backend payloads. It owns the reusable parts of the server workflow: mapping incoming data, reducing outgoing data and wrapping payloads before they leave the datasource.

Use it when you implement a transport on top of the datasource contract instead of talking directly to HTTP or sockets. Application code usually consumes a derived datasource such as RestAPI or WebConnect, while this base class keeps their payload-shaping rules consistent.

The class becomes most useful when server payloads do not match the local state shape exactly. Read paths, transformers, callbacks, partial writes and sheathing all live here so concrete transports can focus on delivery.

Transform And Prepare Server Payloads

import { Server } from "@schukai/monster/source/data/datasource/server.mjs";

class DemoServer extends Server {}

const output = document.getElementById("server-output");
const datasource = new DemoServer({
    read: {
        path: "payload",
    },
});

document.getElementById("server-run").addEventListener("click", () => {
    const transformed = datasource.transformServerPayload({
        payload: {
            id: 7,
            label: "demo",
        },
    });

    output.textContent = JSON.stringify(transformed, null, 2);
});
Open in playground

Combine Transformer Expressions With Mapping Callbacks

import { Server } from "@schukai/monster/source/data/datasource/server.mjs";

class DemoServer extends Server {}

const datasource = new DemoServer({
    read: {
        mapping: {
            transformer: "call:normalize",
            callbacks: {
                normalize(payload) {
                    return {
                        id: payload.item.id,
                        label: payload.item.label.toUpperCase(),
                        status: payload.item.state,
                    };
                },
            },
        },
    },
});

const output = document.getElementById("server-transformer-output");

document.getElementById("server-transformer-run").addEventListener("click", () => {
    const transformed = datasource.transformServerPayload({
        item: {
            id: 7,
            label: "release",
            state: "ready",
        },
    });

    output.textContent = JSON.stringify(transformed, null, 2);
});
Open in playground

Reduce Write Payloads Through The Partial Diff Callback

import { Server } from "@schukai/monster/source/data/datasource/server.mjs";

class DemoServer extends Server {}

const datasource = new DemoServer({
    write: {
        partial: {
            callback(source, changes) {
                return {
                    patch: changes.map((entry) => ({
                        path: entry.path,
                        actual: entry.actual,
                    })),
                };
            },
        },
    },
});

const output = document.getElementById("server-diff-output");

datasource.transformServerPayload({
    id: 7,
    state: "draft",
    title: "Release notes",
});

datasource.set({
    id: 7,
    state: "published",
    title: "Release notes",
});

document.getElementById("server-diff-run").addEventListener("click", () => {
    output.textContent = JSON.stringify(
        datasource.prepareServerPayload(datasource.get()),
        null,
        2,
    );
});
Open in playground

Wrap Outgoing Payloads Into A Server Side Envelope

import { Server } from "@schukai/monster/source/data/datasource/server.mjs";

class DemoServer extends Server {}

const datasource = new DemoServer({
    write: {
        sheathing: {
            object: {
                meta: {
                    source: "docs",
                },
            },
            path: "payload.record",
        },
    },
});

datasource.set({
    id: 9,
    title: "Customer note",
});

const output = document.getElementById("server-sheathing-output");

document.getElementById("server-sheathing-run").addEventListener("click", () => {
    output.textContent = JSON.stringify(
        datasource.prepareServerPayload(datasource.get()),
        null,
        2,
    );
});
Open in playground

Exported

Server

Derived from

Datasource

Options

The Options listed in this section are defined directly within the class. This class is derived from several parent classes. Therefore, it inherits Options from these parent classes. If you cannot find a specific Options in this list, we recommend consulting the documentation of the Datasource.

Option
Type
Default
Description
-/-

Properties

The Properties listed in this section are defined directly within the class. This class is derived from several parent classes. Therefore, it inherits Properties from these parent classes. If you cannot find a specific Properties in this list, we recommend consulting the documentation of the Datasource.

Methods

The methods listed in this section are defined directly within the class. This class is derived from several parent classes. Therefore, it inherits methods from these parent classes. If you cannot find a specific method in this list, we recommend consulting the documentation of the Datasource.

Static methods

[instanceSymbol]()
Returns
  • {symbol}
This method is called by the instanceof operator.

Other methods

prepareServerPayload(payload)
Parameters
  • payload {object}: payload
Returns
  • {Object}
This prepares the data for writing and should not be called directly.
transformServerPayload(payload)
Parameters
  • payload {object}: payload
Returns
  • {Object}
This prepares the data that comes from the server. Should not be called directly.

Events

This component does not fire any public events. It may fire events that are inherited from its parent classes.

The current width of the area is too small to display the content correctly.