From c9f0ccdce709a8dc23b594683f522b2e79f880c6 Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Fri, 24 May 2024 11:48:32 +0200 Subject: [PATCH] feat: rename `helpers` to `define` (#2453) --- init/src/init.ts | 8 +++--- src/{helpers.ts => define.ts} | 46 +++++++++++++++++------------------ src/handlers.ts | 14 +++++------ src/mod.ts | 2 +- www/routes/_app.tsx | 2 +- www/routes/index.tsx | 2 +- www/routes/showcase.tsx | 2 +- www/routes/thanks.tsx | 4 +-- www/utils/state.ts | 4 +-- 9 files changed, 42 insertions(+), 42 deletions(-) rename src/{helpers.ts => define.ts} (79%) diff --git a/init/src/init.ts b/init/src/init.ts index a1cb1adb..baaa444e 100644 --- a/init/src/init.ts +++ b/init/src/init.ts @@ -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(); +export const define = createDefine(); `; 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 ( diff --git a/src/helpers.ts b/src/define.ts similarity index 79% rename from src/helpers.ts rename to src/define.ts index 111e327d..daba44fe 100644 --- a/src/helpers.ts +++ b/src/define.ts @@ -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 { +export interface Define { /** * Define a {@link RouteHandler} object. This function returns the passed * input as-is. @@ -18,21 +18,21 @@ export interface Helpers { * 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 { * @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 = RouteHandler, >( @@ -64,25 +64,25 @@ export interface Helpers { * 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

{slug}

; * }); * ``` * - * 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(({ data }) => { + * export default define.page(({ data }) => { * const slug = data.slug; // slug is inferred to be a string here * return

{slug}

; * }); @@ -96,7 +96,7 @@ export interface Helpers { * @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 = never, Data = Handler extends HandlerByMethod ? Data : never, >( @@ -112,7 +112,7 @@ export interface Helpers { * 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 { } /** - * 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(): Helpers { +export function createDefine(): Define { return { - defineHandlers(handlers) { + handlers(handlers) { return handlers; }, - definePage(render) { + page(render) { return render; }, defineMiddleware(middleware) { diff --git a/src/handlers.ts b/src/handlers.ts index 662d46c8..f94c391a 100644 --- a/src/handlers.ts +++ b/src/handlers.ts @@ -23,7 +23,7 @@ export interface Render { * 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 { * 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( * 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( * 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( * code, or a list of elements to include in the ``. * * ```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( * 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( * 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()), diff --git a/src/mod.ts b/src/mod.ts index e64f12d6..8aa87cb3 100644 --- a/src/mod.ts +++ b/src/mod.ts @@ -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"; diff --git a/www/routes/_app.tsx b/www/routes/_app.tsx index b336144c..37b9ceea 100644 --- a/www/routes/_app.tsx +++ b/www/routes/_app.tsx @@ -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 ( diff --git a/www/routes/index.tsx b/www/routes/index.tsx index a1be04a6..2fe89aac 100644 --- a/www/routes/index.tsx +++ b/www/routes/index.tsx @@ -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"); diff --git a/www/routes/showcase.tsx b/www/routes/showcase.tsx index 0d33f9d2..9043c7c8 100644 --- a/www/routes/showcase.tsx +++ b/www/routes/showcase.tsx @@ -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; diff --git a/www/routes/thanks.tsx b/www/routes/thanks.tsx index 93b53eae..cdd025f1 100644 --- a/www/routes/thanks.tsx +++ b/www/routes/thanks.tsx @@ -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(function ThanksForSubscribing( +export default df.page(function ThanksForSubscribing( props, ) { const vote = props?.data?.vote ? props.data.vote.replaceAll(/-/g, " ") : null; diff --git a/www/utils/state.ts b/www/utils/state.ts index 94201b84..5bf0741c 100644 --- a/www/utils/state.ts +++ b/www/utils/state.ts @@ -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(); +export const df = createDefine();