Loading examples/fastify/index.ts +2 −2 Original line number Diff line number Diff line Loading @@ -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, Loading @@ -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.", }; Loading packages/fastify/src/index.test.ts +7 −7 Original line number Diff line number Diff line Loading @@ -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}`), Loading Loading @@ -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}`), Loading @@ -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 }; }); Loading @@ -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, Loading Loading @@ -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, Loading Loading @@ -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}`), Loading packages/fastify/src/index.ts +46 −32 Original line number Diff line number Diff line Loading @@ -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>, ) => { Loading @@ -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", { Loading Loading
examples/fastify/index.ts +2 −2 Original line number Diff line number Diff line Loading @@ -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, Loading @@ -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.", }; Loading
packages/fastify/src/index.test.ts +7 −7 Original line number Diff line number Diff line Loading @@ -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}`), Loading Loading @@ -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}`), Loading @@ -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 }; }); Loading @@ -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, Loading Loading @@ -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, Loading Loading @@ -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}`), Loading
packages/fastify/src/index.ts +46 −32 Original line number Diff line number Diff line Loading @@ -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>, ) => { Loading @@ -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", { Loading