Unverified Commit a7c2b2f5 authored by Hong Minhee's avatar Hong Minhee
Browse files

Make onNotFound/onNotAcceptable optional

parent 82c374f5
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -38,6 +38,11 @@ To be released.

 -  Added `SendActivityOptions` interface.

 -  Now `onNotFound`/`onNotAcceptable` options are optional for
    `Federation.handle()` method.  [[#9]]

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


Version 0.2.0
-------------
+14 −3
Original line number Diff line number Diff line
@@ -689,6 +689,8 @@ export class Federation<TContextData> {
      contextData,
    }: FederationHandlerParameters<TContextData>,
  ): Promise<Response> {
    onNotFound ??= notFound;
    onNotAcceptable ??= notAcceptable;
    const url = new URL(request.url);
    const route = this.#router.route(url.pathname);
    if (route == null) {
@@ -775,18 +777,19 @@ export interface FederationHandlerParameters<TContextData> {

  /**
   * A callback to handle a request when the route is not found.
   * If not provided, a 404 response is returned.
   * @param request The request object.
   * @returns The response to the request.
   */
  onNotFound(request: Request): Response | Promise<Response>;
  onNotFound?: (request: Request) => Response | Promise<Response>;

  /**
   * A callback to handle a request when the request's `Accept` header is not
   * acceptable.
   * acceptable.  If not provided, a 406 response is returned.
   * @param request The request object.
   * @returns The response to the request.
   */
  onNotAcceptable(request: Request): Response | Promise<Response>;
  onNotAcceptable?: (request: Request) => Response | Promise<Response>;
}

interface ActorCallbacks<TContextData> {
@@ -863,3 +866,11 @@ export interface InboxListenerSetter<TContextData> {
    handler: InboxErrorHandler<TContextData>,
  ): InboxListenerSetter<TContextData>;
}

function notFound(_request: Request): Response {
  return new Response("Not Found", { status: 404 });
}

function notAcceptable(_request: Request): Response {
  return new Response("Not Acceptable", { status: 406 });
}