Introduction

    Context Management

    Optimize request handling and configurations in Pylon with context management. Integrate seamlessly with Hono, access context within service functions, and elevate your web services' performance.



    In Pylon, context management plays a crucial role in handling incoming requests, accessing request-specific data, and configuring your application. This document outlines how context management works in Pylon, including accessing the underlying Hono framework and utilizing context within service functions.

    Hono Integration

    Pylon leverages Hono, a fast and lightweight web framework, to power its underlying infrastructure. Understanding how to interact with the Hono framework is essential for customizing routes, middleware, and other configurations within your Pylon application.

    configureApp Function

    The configureApp function is a key entry point for configuring your Pylon application. It allows you to access the Hono app instance and define custom routes and middleware.

    import { defineService, PylonAPI } from "@getcronit/pylon";
    
    export default defineService({
      Query: {
        sum: (a: number, b: number) => a + b,
      },
      Mutation: {
        divide: (a: number, b: number) => a / b,
      },
    });
    
    export const configureApp: PylonAPI["configureApp"] = (app) => {
      app.get("/hello", (ctx, next) => {
        return new Response("Hello, world!");
      });
    };
    

    Custom Routes and Middleware

    Within the configureApp function, you can define custom routes and middleware using the Hono app instance. This provides flexibility in handling different types of requests and implementing additional functionality.

    export const configureApp: PylonAPI["configureApp"] = (app) => {
      app.get("/hello", (ctx, next) => {
        return new Response("Hello, world!");
      });
    };
    

    For more detailed information on utilizing the Hono app instance, refer to the Hono documentation.

    Accessing Context in Service Functions

    Service functions in Pylon often require access to request-specific data or context information. Pylon provides mechanisms to access this context within your service functions, allowing you to implement custom logic based on request attributes.

    getContext Function

    The getContext function from the @getcronit/pylon package enables service functions to access the request context. This includes request headers, parameters, and other relevant information necessary for processing incoming requests.

    import { defineService, getContext } from "@getcronit/pylon";
    
    export default defineService({
      Query: {
        protected: () => {
          const ctx = getContext();
          const header = ctx.req.header("X-API-Key");
    
          if (header !== "secret") {
            return new Response("Unauthorized", { status: 401 });
          }
    
          return new Response("The secret is safe with me!");
        },
      },
    });
    

    Handling Authorization Logic

    By accessing the request context within service functions, you can implement custom authorization logic based on incoming request attributes. This allows you to enforce access control policies and protect sensitive resources within your application.

    Conclusion

    Understanding context management in Pylon is essential for building robust and secure web services. By leveraging the Hono framework and accessing context within service functions, developers can customize application behavior and implement sophisticated logic tailored to their specific use cases.