Unverified Commit 9d9b3361 authored by An Nyeong's avatar An Nyeong Committed by Hong Minhee
Browse files

Remove unnecessary async

parent aaaf8159
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -11,7 +11,7 @@ const federation = createFederation<void>({
// Add a simple actor
federation.setActorDispatcher(
  "/users/{identifier}",
  async (ctx, identifier) => {
  (ctx, identifier) => {
    return new Person({
      id: ctx.getActorUri(identifier),
      name: identifier,
@@ -23,7 +23,7 @@ federation.setActorDispatcher(
);

// Regular application routes
fastify.get("/", async () => {
fastify.get("/", () => {
  return {
    message: "Hello World! This is a Fastify server with Fedify integration.",
  };
+7 −7
Original line number Diff line number Diff line
@@ -13,13 +13,13 @@ test("Fedify should handle requests successfully", async () => {
  const fastify = Fastify({ logger: false });
  const federation = createFederation<void>({ kv: new MemoryKvStore() });

  fastify.get("/", async () => {
  fastify.get("/", () => {
    return { message: "Hello World" };
  });

  federation.setActorDispatcher(
    "/users/{identifier}",
    async (_ctx: RequestContext<void>, identifier: string) => {
    (_ctx: RequestContext<void>, identifier: string) => {
      if (identifier === "alice") {
        return new Person({
          id: new URL(`https://example.com/users/${identifier}`),
@@ -65,7 +65,7 @@ test("Fedify should delegate to Fastify on notFound", async () => {
  const federation = createFederation<void>({ kv: new MemoryKvStore() });
  federation.setActorDispatcher(
    "/users/{identifier}",
    async (_ctx: RequestContext<void>, identifier: string) => {
    (_ctx: RequestContext<void>, identifier: string) => {
      if (identifier === "alice") {
        return new Person({
          id: new URL(`https://example.com/users/${identifier}`),
@@ -79,7 +79,7 @@ test("Fedify should delegate to Fastify on notFound", async () => {

  await fastify.register(fedifyPlugin, { federation });

  fastify.get("/api/users/:id", async (request) => {
  fastify.get("/api/users/:id", (request) => {
    const params = request.params as { id: string };
    return { message: "Fastify handled this", userId: params.id };
  });
@@ -106,7 +106,7 @@ test("Fedify should handle notAcceptable and return 406", async () => {

  federation.setActorDispatcher(
    "/users/{identifier}",
    async (_ctx: RequestContext<void>, identifier: string) => {
    (_ctx: RequestContext<void>, identifier: string) => {
      return new Person({
        id: new URL(`https://example.com/users/${identifier}`),
        preferredUsername: identifier,
@@ -141,7 +141,7 @@ test("Fedify should handle notAcceptable with custom error handler", async () =>

  federation.setActorDispatcher(
    "/users/{identifier}",
    async (_ctx: RequestContext<void>, identifier: string) => {
    (_ctx: RequestContext<void>, identifier: string) => {
      return new Person({
        id: new URL(`https://example.com/users/${identifier}`),
        preferredUsername: identifier,
@@ -183,7 +183,7 @@ test("Fedify should handle notFound with custom error handler", async () => {

  federation.setActorDispatcher(
    "/users/{identifier}",
    async (_ctx: RequestContext<void>, identifier: string) => {
    (_ctx: RequestContext<void>, identifier: string) => {
      if (identifier === "alice") {
        return new Person({
          id: new URL(`https://example.com/users/${identifier}`),
+46 −32
Original line number Diff line number Diff line
@@ -39,21 +39,31 @@ export interface FedifyPluginOptions<TContextData>
 *
 * @example
 * ```typescript
 * import { createFederation, MemoryKvStore, Person } from "@fedify/fedify";
 * import fedifyPlugin from "@fedify/fastify";
 * import Fastify from "fastify";
 *
 * const fastify = Fastify();
 *
 * const federation = createFederation({ kv: new MemoryKvStore() });
 *
 * // Add federation routes
 * federation.setActorDispatcher("/users/{identifier}", ...);
 * federation.setActorDispatcher("/users/{identifier}", async (ctx, identifier) => {
 *   return new Person({
 *     id: ctx.getActorUri(identifier),
 *     preferredUsername: identifier,
 *   });
 * });
 *
 * // Register the plugin
 * await fastify.register(fedifyPlugin, {
 *   federation,
 *   contextDataFactory: (request) => ({ userId: request.user?.id }),
 *   errorHandlers: { onUnauthroized },
 *   contextDataFactory: () => undefined,
 *   errorHandlers: { onNotFound: () => new Response("Not Found", { status: 404 }) },
 * });
 * ```
 */
const fedifyPluginCore: FastifyPluginAsync<FedifyPluginOptions<unknown>> =
  async (
const fedifyPluginCore: FastifyPluginAsync<FedifyPluginOptions<unknown>> = (
  fastify: FastifyInstance,
  options: FedifyPluginOptions<unknown>,
) => {
@@ -77,13 +87,17 @@ const fedifyPluginCore: FastifyPluginAsync<FedifyPluginOptions<unknown>> =

    await reply.send(response);
  });
  return Promise.resolve();
};

// Wrap with fastify-plugin to bypass encapsulation
const fedifyPlugin = fp(fedifyPluginCore, {
const fedifyPlugin: FastifyPluginAsync<FedifyPluginOptions<unknown>> = fp(
  fedifyPluginCore,
  {
    name: "fedify-plugin",
    fastify: "5.x",
});
  },
);

const dummyNotFoundResponse = new Response("", { status: 404 });
const defaultNotAcceptableResponse = new Response("Not Acceptable", {