@fedify/fedify
Version:
An ActivityPub server framework
48 lines (45 loc) • 1.35 kB
JavaScript
import { Temporal } from "@js-temporal/polyfill";
import { URLPattern } from "urlpattern-polyfill";
//#region x/hono.ts
/**
* Create a Hono middleware to integrate with the {@link Federation} object.
*
* @typeParam TContextData A type of the context data for the {@link Federation}
* object.
* @typeParam THonoContext A type of the Hono context.
* @param federation A {@link Federation} object to integrate with Hono.
* @param contextDataFactory A function to create a context data for the
* {@link Federation} object.
* @returns A Hono middleware.
*/
function federation(federation$1, contextDataFactory) {
return async (ctx, next) => {
let contextData = contextDataFactory(ctx);
if (contextData instanceof Promise) contextData = await contextData;
return await federation$1.fetch(ctx.req.raw, {
contextData,
...integrateFetchOptions(ctx, next)
});
};
}
function integrateFetchOptions(ctx, next) {
return {
async onNotFound(_req) {
await next();
return ctx.res;
},
async onNotAcceptable(_req) {
await next();
if (ctx.res.status !== 404) return ctx.res;
return new Response("Not acceptable", {
status: 406,
headers: {
"Content-Type": "text/plain",
Vary: "Accept"
}
});
}
};
}
//#endregion
export { federation };