Skip to content

Route factory #2

Description

@tur-nr

Example

// ./routes/profile/[name].get.ts
import { route } from "https://serva.land/serva@latest/mod.ts";

export default route(({ hook, param }, meta) => {
  console.log(meta);
  // => { path: "/profile/[name]", method: "GET", paramNames: ["name"] }

  // add hooks
  hook(async (request, next) => {
    await next();
  });

  // validate/transform parameters
  param("name", (name) => {
    return name.charAt(0).toUpperCase() + name.substring(1);
  });

  return ({ params }) => `Welcome ${params.get("name")}.`;
});

API

  • hook: add hooks to the route
  • param: validate/transform a parameter value
interface RouteFactoryApi {
  hook: (request: ServaRequest, next: () => Promise<any> | any) => Promise<any> | any;
  param: (value: string, request: ServaRequest) => Promise<any> | any;
}

Meta

  • path: the route path (NOT the request path)
  • method: the route method (NOT the request method)
  • paramNames: the route parameter names
interface RouteFactoryMeta {
  method: string;
  path: string;
  paramNames: string[];
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions