Unverified Commit 46d75e2f authored by Hong Minhee's avatar Hong Minhee
Browse files

Rename Federation.handle() to Federation.fetch()

parent 1b6dd6d4
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -22,6 +22,14 @@ To be released.
     -  The Activity Vocabulary object passed to `InboxListener` now implicitly
        loads remote object with an authenticated `DocumentLoader`.

 -  Added `Federation.fetch()` method.

     -  Deprecated `Federation.handle()` method.  Use `Federation.fetch()`
        method instead.
     -  Renamed `FederationHandlerParameters` type to `FederationFetchOptions`.
     -  Added `integrateFetchOptions()` function.
     -  Deprecated `integrateHandlerOptions()` function.

[#27]: https://github.com/dahlia/fedify/issues/27


+5 −5
Original line number Diff line number Diff line
@@ -23,10 +23,10 @@ You can create a `Federation` object by calling the constructor function
with an optional configuration object:

~~~~ typescript
import { Federation } from "@fedify/fedify";
import { Federation, MemoryKvStore } from "@fedify/fedify";

const federation = new Federation<void>({
  kv: await Deno.openKv(),
  kv: new MemoryKvStore(),
  // Omitted for brevity; see the following sections for details.
});
~~~~
@@ -151,7 +151,7 @@ import { FreshContext } from "$fresh/server.ts";
import { federation } from "../federation.ts"; // Import the `Federation` object

export async function handler(request: Request, context: FreshContext) {
  return await federation.handle(request, {
  return await federation.fetch(request, {
    // Wonder what is `contextData`?  See the next section for details.
    contextData: undefined,

@@ -202,7 +202,7 @@ object can handle.
> ~~~~

> [!TIP]
> In theory, you can directly pass `Federation.handle()` to the [`Deno.serve()`]
> In theory, you can directly pass `Federation.fetch()` to the [`Deno.serve()`]
> function, but you probably wouldn't want to do that because you want to handle
> other requests with the web framework.

@@ -233,7 +233,7 @@ import { federation } from "../federation.ts"; // Import the `Federation` object
import { DatabasePool, getPool } from "./database.ts";

export async function handler(request: Request, context: FreshContext) {
  return federation.handle(request, {
  return federation.fetch(request, {
    contextData: getPool(),
    onNotFound: context.next.bind(context),
    onNotAcceptable: async (request: Request) => {
+4 −4
Original line number Diff line number Diff line
@@ -166,15 +166,15 @@ a key-value store.
> Since `MemoryKvStore` is for testing and development purposes, you should
> use a persistent key-value store like `DenoKvStore` for production use.

Then, we pass the incoming `Request` to the `federation.handle()` method:
Then, we pass the incoming `Request` to the `Federation.fetch()` method:

~~~~ typescript
Deno.serve(
  request => federation.handle(request, { contextData: undefined })
  request => federation.fetch(request, { contextData: undefined })
);
~~~~

The `federation.handle()` method takes the incoming `Request` and few options.
The `Federation.fetch()` method takes the incoming `Request` and few options.
In this case, we pass `undefined` as the `contextData` because we don't
need to share any context data here.

@@ -227,7 +227,7 @@ federation.setActorDispatcher("/users/{handle}", async (ctx, handle) => {
});

Deno.serve(
  request => federation.handle(request, { contextData: undefined })
  request => federation.fetch(request, { contextData: undefined })
);
~~~~

+25 −4
Original line number Diff line number Diff line
@@ -732,14 +732,34 @@ export class Federation<TContextData> {
   * @param request The request object.
   * @param parameters The parameters for handling the request.
   * @returns The response to the request.
   * @deprecated Use {@link Federation.fetch} instead.
   */
  async handle(
  handle(
    request: Request,
    options: FederationFetchOptions<TContextData>,
  ): Promise<Response> {
    return this.fetch(request, options);
  }

  /**
   * Handles a request related to federation.  If a request is not related to
   * federation, the `onNotFound` or `onNotAcceptable` callback is called.
   *
   * Usually, this method is called from a server's request handler or
   * a web framework's middleware.
   *
   * @param request The request object.
   * @param parameters The parameters for handling the request.
   * @returns The response to the request.
   * @since 0.6.0
   */
  async fetch(
    request: Request,
    {
      onNotFound,
      onNotAcceptable,
      contextData,
    }: FederationHandlerParameters<TContextData>,
    }: FederationFetchOptions<TContextData>,
  ): Promise<Response> {
    onNotFound ??= notFound;
    onNotAcceptable ??= notAcceptable;
@@ -824,11 +844,12 @@ export class Federation<TContextData> {
}

/**
 * Parameters of {@link Federation.handle} method.
 * Parameters of {@link Federation.fetch} method.
 *
 * @typeParam TContextData The context data to pass to the {@link Context}.
 * @since 0.6.0
 */
export interface FederationHandlerParameters<TContextData> {
export interface FederationFetchOptions<TContextData> {
  /**
   * The context data to pass to the {@link Context}.
   */
+15 −5
Original line number Diff line number Diff line
@@ -12,7 +12,7 @@
 */
import type {
  Federation,
  FederationHandlerParameters,
  FederationFetchOptions,
} from "../federation/middleware.ts";

interface FreshContext {
@@ -37,10 +37,11 @@ interface FreshContext {
 *
 * @param context A Fresh context.
 * @returns Options for the {@link Federation.handle} method.
 * @since 0.6.0
 */
export function integrateHandlerOptions(
export function integrateFetchOptions(
  context: FreshContext,
): Omit<FederationHandlerParameters<void>, "contextData"> {
): Omit<FederationFetchOptions<void>, "contextData"> {
  return {
    // If the `federation` object finds a request not responsible for it
    // (i.e., not a federation-related request), it will call the `next`
@@ -69,6 +70,15 @@ export function integrateHandlerOptions(
  };
}

/**
 * Create options for the `federation` object to integrate with Fresh.
 *
 * @param context A Fresh context.
 * @returns Options for the {@link Federation.handle} method.
 * @deprecated
 */
export const integrateHandlerOptions = integrateFetchOptions;

/**
 * Create a Fresh middleware handler to integrate with the {@link Federation}
 * object.
@@ -104,9 +114,9 @@ export function integrateHandler<
  ): Promise<Response> => {
    let contextData = createContextData(request, context);
    if (contextData instanceof Promise) contextData = await contextData;
    return await federation.handle(request, {
    return await federation.fetch(request, {
      contextData,
      ...integrateHandlerOptions(context),
      ...integrateFetchOptions(context),
    });
  };
}