feat: rename helpers to define (#2453)
This commit is contained in:
+4
-4
@@ -390,20 +390,20 @@ export function Button(props: ButtonProps) {
|
||||
`;
|
||||
await writeFile("components/Button.tsx", COMPONENTS_BUTTON_TSX);
|
||||
|
||||
const UTILS_TS = `import { createHelpers } from "@fresh/core";
|
||||
const UTILS_TS = `import { createDefine } from "@fresh/core";
|
||||
|
||||
// deno-lint-ignore no-empty-interface
|
||||
export interface State {}
|
||||
|
||||
export const helpers = createHelpers<State>();
|
||||
export const define = createDefine<State>();
|
||||
`;
|
||||
await writeFile("utils.ts", UTILS_TS);
|
||||
|
||||
const ROUTES_HOME = `import { useSignal } from "@preact/signals";
|
||||
import { helpers } from "../utils.ts";
|
||||
import { define } from "../utils.ts";
|
||||
import Counter from "../islands/Counter.tsx";
|
||||
|
||||
export default helpers.definePage(function Home() {
|
||||
export default define.page(function Home() {
|
||||
const count = useSignal(3);
|
||||
|
||||
return (
|
||||
|
||||
@@ -4,12 +4,12 @@ import type { FreshContext } from "./context.ts";
|
||||
import type { Middleware } from "./middlewares/mod.ts";
|
||||
|
||||
/**
|
||||
* A set of helper functions that enable better type inference and code
|
||||
* A set of define functions that enable better type inference and code
|
||||
* completion when defining routes and middleware.
|
||||
*
|
||||
* To create a helpers object, call {@link createHelpers}.
|
||||
* To create a define object, call {@link createDefine}.
|
||||
*/
|
||||
export interface Helpers<State> {
|
||||
export interface Define<State> {
|
||||
/**
|
||||
* Define a {@link RouteHandler} object. This function returns the passed
|
||||
* input as-is.
|
||||
@@ -18,21 +18,21 @@ export interface Helpers<State> {
|
||||
* of your route handlers. For example:
|
||||
*
|
||||
* ```ts
|
||||
* export const handler = helpers.defineHandlers((ctx) => {
|
||||
* 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 definePage}
|
||||
* 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 definePage}.
|
||||
* 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
|
||||
* export const handler = helpers.defineHandlers<{ slug: string }>({
|
||||
* export const handler = define.handlers<{ slug: string }>({
|
||||
* async GET(ctx) {
|
||||
* const slug = ctx.params.slug; // slug is inferred to be a string
|
||||
* return { data: { slug } };
|
||||
@@ -49,7 +49,7 @@ export interface Helpers<State> {
|
||||
* @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.
|
||||
*/
|
||||
defineHandlers<
|
||||
handlers<
|
||||
Data,
|
||||
Handlers extends RouteHandler<Data, State> = RouteHandler<Data, State>,
|
||||
>(
|
||||
@@ -64,25 +64,25 @@ export interface Helpers<State> {
|
||||
* of the data that your page component receives. For example:
|
||||
*
|
||||
* ```ts
|
||||
* export default helpers.definePage((props) => {
|
||||
* export default define.page((props) => {
|
||||
* const slug = props.params.slug; // Because props is inferred to be a FreshContext object, slug is inferred to be a string
|
||||
* return <h1>{slug}</h1>;
|
||||
* });
|
||||
* ```
|
||||
*
|
||||
* This is particularly useful when combined with the {@link defineHandlers}
|
||||
* This is particularly useful when combined with the {@link handlers}
|
||||
* helper function, in which case the data type will be inferred from the
|
||||
* return type of the handler method.
|
||||
*
|
||||
* ```ts
|
||||
* export const handler = defineHandlers({
|
||||
* export const handler = define.handlers({
|
||||
* async GET(ctx) {
|
||||
* const slug = ctx.params.slug; // slug is inferred to be a string
|
||||
* return { data: { slug } };
|
||||
* },
|
||||
* });
|
||||
*
|
||||
* export default definePage<typeof handler>(({ data }) => {
|
||||
* export default define.page<typeof handler>(({ data }) => {
|
||||
* const slug = data.slug; // slug is inferred to be a string here
|
||||
* return <h1>{slug}</h1>;
|
||||
* });
|
||||
@@ -96,7 +96,7 @@ export interface Helpers<State> {
|
||||
* @typeParam Handler The type of the handler object that this page component is associated with. If this route has a handler, pass the handler object as a type argument to this function, e.g. `typeof handler`. If this route does not have a handler, omit this type argument.
|
||||
* @typeParam Data The type of data that the page component receives. This will be inferred from the handler methods if not provided. In very advanced use cases, you can specify `never` to the `Handler` type argument and provide the `Data` type explicitly.
|
||||
*/
|
||||
definePage<
|
||||
page<
|
||||
Handler extends RouteHandler<unknown, State> = never,
|
||||
Data = Handler extends HandlerByMethod<infer Data, State> ? Data : never,
|
||||
>(
|
||||
@@ -112,7 +112,7 @@ export interface Helpers<State> {
|
||||
* of the context object that your middleware receives. For example:
|
||||
*
|
||||
* ```ts
|
||||
* export const middleware = helpers.defineMiddleware((ctx) => {
|
||||
* export const middleware = define.middleware((ctx) => {
|
||||
* ctx.url; // ctx is inferred to be a FreshContext object, so this is a URL
|
||||
* return ctx.next();
|
||||
* });
|
||||
@@ -128,24 +128,24 @@ export interface Helpers<State> {
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a set of helper functions that enable better type inference and code
|
||||
* Create a set of define functions that enable better type inference and code
|
||||
* completion when defining routes and middleware.
|
||||
*
|
||||
* To use, call this function in a central file and export the result. In your
|
||||
* route and middleware files, import the {@link Helpers|helpers object} and use
|
||||
* them to define your routes and middleware using the
|
||||
* {@link Helpers.defineHandlers|defineHandlers},
|
||||
* {@link Helpers.definePage|definePage}, and
|
||||
* {@link Helpers.defineMiddleware|defineMiddleware} functions.
|
||||
* route and middleware files, import the {@link Define|define object} and use
|
||||
* it to define your routes and middleware using the
|
||||
* {@link Define.handlers|define.handlers},
|
||||
* {@link Define.page|define.page}, and
|
||||
* {@link Define.defineMiddleware|define.middleware} functions.
|
||||
*
|
||||
* @typeParam State The type of the state object that is passed to all middleware and route handlers.
|
||||
*/
|
||||
export function createHelpers<State>(): Helpers<State> {
|
||||
export function createDefine<State>(): Define<State> {
|
||||
return {
|
||||
defineHandlers(handlers) {
|
||||
handlers(handlers) {
|
||||
return handlers;
|
||||
},
|
||||
definePage(render) {
|
||||
page(render) {
|
||||
return render;
|
||||
},
|
||||
defineMiddleware(middleware) {
|
||||
+7
-7
@@ -23,7 +23,7 @@ export interface Render<T> {
|
||||
* signature.
|
||||
*
|
||||
* ```ts
|
||||
* export const handlers = defineHandlers({
|
||||
* export const handlers = define.handlers({
|
||||
* GET: (ctx) => {
|
||||
* return new Response("Hello from a GET request!");
|
||||
* },
|
||||
@@ -48,7 +48,7 @@ export interface Render<T> {
|
||||
* This function will be called for all requests, regardless of the method.
|
||||
*
|
||||
* ```ts
|
||||
* export const handlers = defineHandlers((ctx) => {
|
||||
* export const handlers = define.handlers((ctx) => {
|
||||
* return new Response(`Hello from a ${ctx.req.method} request!`);
|
||||
* });
|
||||
* ```
|
||||
@@ -79,7 +79,7 @@ export function isHandlerByMethod<D, S>(
|
||||
* to redirect the client to another URL.
|
||||
*
|
||||
* ```ts
|
||||
* export const handler = defineHandler((ctx) => {
|
||||
* export const handler = define.handlers((ctx) => {
|
||||
* return new Response("Hello, world!");
|
||||
* });
|
||||
* ```
|
||||
@@ -90,7 +90,7 @@ export function isHandlerByMethod<D, S>(
|
||||
* will be passed to the page component, where it can be rendered into HTML.
|
||||
*
|
||||
* ```ts
|
||||
* export const handler = defineHandler((ctx) => {
|
||||
* export const handler = define.handlers((ctx) => {
|
||||
* return { data: { message: "Hello, world!" } };
|
||||
* });
|
||||
*
|
||||
@@ -105,7 +105,7 @@ export function isHandlerByMethod<D, S>(
|
||||
* code, or a list of elements to include in the `<head>`.
|
||||
*
|
||||
* ```tsx
|
||||
* export const handler = defineHandler((ctx) => {
|
||||
* export const handler = define.handlers((ctx) => {
|
||||
* return {
|
||||
* data: { message: "Hello, world!" },
|
||||
* headers: { "Cache-Control": "public, max-age=3600" },
|
||||
@@ -122,7 +122,7 @@ export function isHandlerByMethod<D, S>(
|
||||
* operation before returning a response.
|
||||
*
|
||||
* ```ts
|
||||
* export const handler = defineHandler(async (ctx) => {
|
||||
* export const handler = define.handlers(async (ctx) => {
|
||||
* const resp = await fetch("https://api.example.com/data").;
|
||||
* if (!resp.ok) {
|
||||
* throw new Error("Failed to fetch data");
|
||||
@@ -138,7 +138,7 @@ export function isHandlerByMethod<D, S>(
|
||||
* multiple operations concurrently.
|
||||
*
|
||||
* ```ts
|
||||
* export const handler = defineHandler(async (ctx) => {
|
||||
* export const handler = define.handlers(async (ctx) => {
|
||||
* const [resp1, resp2] = await Promise.all([
|
||||
* fetch("https://api.example.com/data1")
|
||||
* .then((resp) => resp.json()),
|
||||
|
||||
+1
-1
@@ -14,7 +14,7 @@ export { staticFiles } from "./middlewares/static_files.ts";
|
||||
export { type Mode, type PageProps } from "./runtime/server/mod.tsx";
|
||||
export { type FreshConfig, type ResolvedFreshConfig } from "./config.ts";
|
||||
export { type FreshContext, type Island } from "./context.ts";
|
||||
export { createHelpers, type Helpers } from "./helpers.ts";
|
||||
export { createDefine, type Define } from "./define.ts";
|
||||
export { type Method } from "./router.ts";
|
||||
export { HttpError } from "./error.ts";
|
||||
|
||||
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
import { asset } from "@fresh/core/runtime";
|
||||
import { df } from "../utils/state.ts";
|
||||
|
||||
export default df.definePage(function App({ Component, state, url }) {
|
||||
export default df.page(function App({ Component, state, url }) {
|
||||
return (
|
||||
<html lang="en">
|
||||
<head>
|
||||
|
||||
@@ -14,7 +14,7 @@ import { SocialProof } from "../components/homepage/SocialProof.tsx";
|
||||
import { DenoSection } from "../components/homepage/DenoSection.tsx";
|
||||
import { df } from "../utils/state.ts";
|
||||
|
||||
export const handler = df.defineHandlers({
|
||||
export const handler = df.handlers({
|
||||
GET(ctx) {
|
||||
const { req } = ctx;
|
||||
const accept = req.headers.get("accept");
|
||||
|
||||
@@ -9,7 +9,7 @@ import { df } from "../utils/state.ts";
|
||||
const TITLE = "Showcase | Fresh";
|
||||
const DESCRIPTION = "Selection of projects that have been built with Fresh.";
|
||||
|
||||
export const handler = df.defineHandlers({
|
||||
export const handler = df.handlers({
|
||||
GET(ctx) {
|
||||
ctx.state.title = TITLE;
|
||||
ctx.state.description = DESCRIPTION;
|
||||
|
||||
@@ -2,7 +2,7 @@ import { PageSection } from "../components/PageSection.tsx";
|
||||
import { DemoBox } from "../components/homepage/DemoBox.tsx";
|
||||
import { df } from "../utils/state.ts";
|
||||
|
||||
export const handler = df.defineHandlers({
|
||||
export const handler = df.handlers({
|
||||
GET(ctx) {
|
||||
const url = new URL(ctx.req.url);
|
||||
const search = new URLSearchParams(url.search);
|
||||
@@ -11,7 +11,7 @@ export const handler = df.defineHandlers({
|
||||
},
|
||||
});
|
||||
|
||||
export default df.definePage<typeof handler>(function ThanksForSubscribing(
|
||||
export default df.page<typeof handler>(function ThanksForSubscribing(
|
||||
props,
|
||||
) {
|
||||
const vote = props?.data?.vote ? props.data.vote.replaceAll(/-/g, " ") : null;
|
||||
|
||||
+2
-2
@@ -1,4 +1,4 @@
|
||||
import { createHelpers } from "@fresh/core";
|
||||
import { createDefine } from "@fresh/core";
|
||||
|
||||
export interface State {
|
||||
title?: string;
|
||||
@@ -7,4 +7,4 @@ export interface State {
|
||||
noIndex?: boolean;
|
||||
}
|
||||
|
||||
export const df = createHelpers<State>();
|
||||
export const df = createDefine<State>();
|
||||
|
||||
Reference in New Issue
Block a user