Repository: denoland/fresh
File: src/define.ts (TypeScript)

Code before:
   * of your route handlers. For example:
   *
   * ```ts
   * import { define } from "../utils.ts";
   *
   * export const handler = define.handlers((ctx) => {
   *   ctx.url; // ctx is inferred to be a FreshContext object, so this is a URL
   *   return new Response("Hello, world!");
   * });
   * ```
   *
   * This is particularly useful when combined with the {@link Define.page}
   * helper function, which can infer the data type from the handler function.
   * For more information, see {@link Define.page}.
   *
   * You can also pass an explicit type argument to ensure that all data
   * returned from the render function is of the correct type:
   *
   * ```ts
   * import { render } from "@fresh/core";
   * import { define } from "../utils.ts";
   *
   * export const handler = define.handlers<{ slug: string }>({
   *   async GET(ctx) {
   *     const slug = ctx.params.slug; // slug is inferred to be a string
   *     return render({ slug });
   *   },
   *
   *   // This method will cause a type error because the data object is missing
   *   // the required `slug` property.
   *   async POST(ctx) {
   *     return render({ });
   *   },
   * });
   * ```
   *
   * @typeParam Data The type of data that the handler returns. This will be inferred from the handler methods if not provided.
   * @typeParam Handlers This will always be inferred from the input object. Do not manually specify this type.
   */
  handlers<
    Data,
    Handlers extends RouteHandler<Data, State> = RouteHandler<Data, State>,
  >(
    handlers: Handlers,
  ): typeof handlers;

  /**
   * Define a page component that will be rendered when a route handler returns
   * data. This function returns the passed input as-is.
   *
   * You can use this function to help the TypeScript compiler infer the types

Review comment (suggestion):
```suggestion
   *     return page({ slug });
```

Code after:
   * of your route handlers. For example:
   *
   * ```ts
   * import { define } from "../utils.ts";
   *
   * export const handler = define.handlers((ctx) => {
   *   ctx.url; // ctx is inferred to be a FreshContext object, so this is a URL
   *   return new Response("Hello, world!");
   * });
   * ```
   *
   * This is particularly useful when combined with the {@link Define.page}
   * helper function, which can infer the data type from the handler function.
   * For more information, see {@link Define.page}.
   *
   * You can also pass an explicit type argument to ensure that all data
   * returned from the handler is of the correct type:
   *
   * ```ts
   * import { page } from "@fresh/core";
   * import { define } from "../utils.ts";
   *
   * export const handler = define.handlers<{ slug: string }>({
   *   async GET(ctx) {
   *     const slug = ctx.params.slug; // slug is inferred to be a string
   *     return page({ slug });
   *   },
   *
   *   // This method will cause a type error because the data object is missing
   *   // the required `slug` property.
   *   async POST(ctx) {
   *     return page({ });
   *   },
   * });
   * ```
   *
   * @typeParam Data The type of data that the handler returns. This will be inferred from the handler methods if not provided.
   * @typeParam Handlers This will always be inferred from the input object. Do not manually specify this type.
   */
  handlers<
    Data,
    Handlers extends RouteHandler<Data, State> = RouteHandler<Data, State>,
  >(
    handlers: Handlers,
  ): typeof handlers;

  /**
   * Define a page component that will be rendered when a route handler returns
   * data. This function returns the passed input as-is.
   *
   * You can use this function to help the TypeScript compiler infer the types