Skip to content

gaman@0.0.21

Choose a tag to compare

@angga7togk angga7togk released this 25 Jun 13:50

Release Notes: v0.0.21

Patch Changes

In this release, significant updates and enhancements have been implemented:

Key Features

  1. WebSocket Block

    • Now you can define WebSocket functionality directly in a block using the new websocket property.
    • Example:
      import { defineBlock } from "gaman";
      
      export default defineBlock({
        path: "/ws",
        websocket: (ctx) => {
          return {
            onMessage: (message) => {
              ctx.send(`Received: ${message}`);
            },
          };
        },
      });
    • Supports events like onOpen, onClose, onMessage, onError, and more.
  2. Middleware Enhancements

    • Middleware can now be included at the block level using the includes property.
    • Example:
      import { defineBlock, cors } from "gaman";
      
      export default defineBlock({
        path: "/api",
        includes: [cors({ origin: "*" })],
        routes: {
          "/example": (ctx) => {
            return Response.json({ message: "CORS enabled!" });
          },
        },
      });
    • Custom middleware is supported via defineMiddleware.
      import { defineMiddleware, next } from "gaman";
      
      export default defineMiddleware((ctx) => {
        console.log("Custom middleware executed");
        return next();
      });
  3. Logging Improvements

    • Enhanced logging with detailed information such as:
      • Response time in milliseconds.
      • Status codes.
      • Path accessed.
    • This makes debugging and performance tracking easier.

image

  1. New Built-in Packages
    • Added built-in middleware packages:
      • cors: For Cross-Origin Resource Sharing configuration.
      • basic-auth: For HTTP Basic Authentication.

Example Usage

WebSocket

Define WebSocket in a block:

import { defineBlock } from "gaman";

export default defineBlock({
  path: "/chat",
  websocket: (ctx) => {
    return {
      onMessage: (message) => {
        ctx.send(`You said: ${message}`);
      },
    };
  },
});