Skip to content

Cheatsheet

Routing

ts
// path: ./src/routes/foo/bar.ts 

// GET /foo/bar
// path: ./src/routes/foo/bar.ts 

// GET /foo/bar
ts
// path: ./src/routes/foo/[bar].ts

// GET /foo/123 -> { bar: '123' }
// path: ./src/routes/foo/[bar].ts

// GET /foo/123 -> { bar: '123' }
ts
// path: ./src/routes/foo/[[bar]].ts

// GET /foo/123 -> { bar: '123' }
// GET /foo -> {}
// path: ./src/routes/foo/[[bar]].ts

// GET /foo/123 -> { bar: '123' }
// GET /foo -> {}
ts
// path: ./src/routes/foo/[...bar].ts

// GET /foo/bar -> { bar: 'bar' }
// GET /foo/bar/baz -> { bar: 'bar/baz' }
// GET /foo/https://github.com -> { bar: 'https://github.com' }
// path: ./src/routes/foo/[...bar].ts

// GET /foo/bar -> { bar: 'bar' }
// GET /foo/bar/baz -> { bar: 'bar/baz' }
// GET /foo/https://github.com -> { bar: 'https://github.com' }
ts
// path: ./src/routes/foo/[bar].[ext].ts

// GET /foo/123.json -> { bar: '123', ext: 'json' }

// For instance, making the extension parameter optional might be useful too.

// path: ./src/routes/foo/config.[[ext]].ts

// GET /foo/config.json -> { ext: 'json' }
// GET /foo/config -> {}
// path: ./src/routes/foo/[bar].[ext].ts

// GET /foo/123.json -> { bar: '123', ext: 'json' }

// For instance, making the extension parameter optional might be useful too.

// path: ./src/routes/foo/config.[[ext]].ts

// GET /foo/config.json -> { ext: 'json' }
// GET /foo/config -> {}
ts
// path: ./src/routes/foo/*

// GET /foo/bar -> {}
// GET /foo/bar/baz -> {}
// path: ./src/routes/foo/*

// GET /foo/bar -> {}
// GET /foo/bar/baz -> {}

Middleware

ts
// path: ./src/routes/foo/bar.ts

import type { Middleware, RouteHandler } from "rose";

export const middleware: Middleware = {
  // GET /foo/bar
  GET: (req) => {
    req.ctx["from-middleware"] = "Hello from middleware";
  },
};

// GET /foo/bar
export const GET: RouteHandler = (req) => {
  const middlewareCtx = req.ctx["from-middleware"];

  return new Response(`Middleware Context: '${middlewareCtx}'`);
};
// path: ./src/routes/foo/bar.ts

import type { Middleware, RouteHandler } from "rose";

export const middleware: Middleware = {
  // GET /foo/bar
  GET: (req) => {
    req.ctx["from-middleware"] = "Hello from middleware";
  },
};

// GET /foo/bar
export const GET: RouteHandler = (req) => {
  const middlewareCtx = req.ctx["from-middleware"];

  return new Response(`Middleware Context: '${middlewareCtx}'`);
};
ts
// path: ./src/routes/foo/foo/_middleware.ts

import type { MiddlewareRouteHandler } from "rose";

// GET /foo/*, POST /foo/*, PUT /foo/*, DELETE /foo/*, etc.
export const ALL: MiddlewareRouteHandler = (req) => {
  req.ctx["from-middleware-nested"] = "Context from nested middleware";
};
// path: ./src/routes/foo/foo/_middleware.ts

import type { MiddlewareRouteHandler } from "rose";

// GET /foo/*, POST /foo/*, PUT /foo/*, DELETE /foo/*, etc.
export const ALL: MiddlewareRouteHandler = (req) => {
  req.ctx["from-middleware-nested"] = "Context from nested middleware";
};
ts
// path: ./src/routes/foo/_middleware.ts

import type { MiddlewareRouteHandler } from "rose";

// GET /*, POST /*, PUT /*, DELETE /*, etc.
export const ALL: MiddlewareRouteHandler = (req) => {
  req.ctx["from-middleware-root"] = "Context from root middleware";
};
// path: ./src/routes/foo/_middleware.ts

import type { MiddlewareRouteHandler } from "rose";

// GET /*, POST /*, PUT /*, DELETE /*, etc.
export const ALL: MiddlewareRouteHandler = (req) => {
  req.ctx["from-middleware-root"] = "Context from root middleware";
};