Unverified Commit 3c0cb932 authored by Hong Minhee's avatar Hong Minhee
Browse files

Replace inline type with ConstructorWithTypeId

Replace all occurrences of the inline type
`(new (...args: any[]) => TObject) & { typeId: URL }` with the
existing `ConstructorWithTypeId<TObject>` type alias throughout
the federation package.

This change:

- Reduces code duplication
- Removes 41 `// deno-lint-ignore no-explicit-any` comments
- Improves type consistency and maintainability

Fixes https://github.com/fedify-dev/fedify/issues/447



Co-Authored-By: default avatarClaude <noreply@anthropic.com>
parent b8aeaa40
Loading
Loading
Loading
Loading
+18 −39
Original line number Diff line number Diff line
@@ -53,11 +53,7 @@ export class FederationBuilderImpl<TContextData>
  nodeInfoDispatcher?: NodeInfoDispatcher<TContextData>;
  webFingerLinksDispatcher?: WebFingerLinksDispatcher<TContextData>;
  objectCallbacks: Record<string, ObjectCallbacks<TContextData, string>>;
  objectTypeIds: Record<
    string,
    // deno-lint-ignore no-explicit-any
    (new (...args: any[]) => Object) & { typeId: URL }
  >;
  objectTypeIds: Record<string, ConstructorWithTypeId<Object>>;
  inboxPath?: string;
  inboxCallbacks?: CollectionCallbacks<
    Activity,
@@ -507,51 +503,44 @@ export class FederationBuilderImpl<TContextData>
  }

  setObjectDispatcher<TObject extends Object, TParam extends string>(
    // deno-lint-ignore no-explicit-any
    cls: (new (...args: any[]) => TObject) & { typeId: URL },
    cls: ConstructorWithTypeId<TObject>,
    path:
      `${string}{${TParam}}${string}{${TParam}}${string}{${TParam}}${string}{${TParam}}${string}{${TParam}}${string}{${TParam}}${string}`,
    dispatcher: ObjectDispatcher<TContextData, TObject, TParam>,
  ): ObjectCallbackSetters<TContextData, TObject, TParam>;
  setObjectDispatcher<TObject extends Object, TParam extends string>(
    // deno-lint-ignore no-explicit-any
    cls: (new (...args: any[]) => TObject) & { typeId: URL },
    cls: ConstructorWithTypeId<TObject>,
    path:
      `${string}{${TParam}}${string}{${TParam}}${string}{${TParam}}${string}{${TParam}}${string}{${TParam}}${string}`,
    dispatcher: ObjectDispatcher<TContextData, TObject, TParam>,
  ): ObjectCallbackSetters<TContextData, TObject, TParam>;
  setObjectDispatcher<TObject extends Object, TParam extends string>(
    // deno-lint-ignore no-explicit-any
    cls: (new (...args: any[]) => TObject) & { typeId: URL },
    cls: ConstructorWithTypeId<TObject>,
    path:
      `${string}{${TParam}}${string}{${TParam}}${string}{${TParam}}${string}{${TParam}}${string}`,
    dispatcher: ObjectDispatcher<TContextData, TObject, TParam>,
  ): ObjectCallbackSetters<TContextData, TObject, TParam>;
  setObjectDispatcher<TObject extends Object, TParam extends string>(
    // deno-lint-ignore no-explicit-any
    cls: (new (...args: any[]) => TObject) & { typeId: URL },
    cls: ConstructorWithTypeId<TObject>,
    path: `${string}${Rfc6570Expression<TParam>}${string}${Rfc6570Expression<
      TParam
    >}${string}${Rfc6570Expression<TParam>}${string}`,
    dispatcher: ObjectDispatcher<TContextData, TObject, TParam>,
  ): ObjectCallbackSetters<TContextData, TObject, TParam>;
  setObjectDispatcher<TObject extends Object, TParam extends string>(
    // deno-lint-ignore no-explicit-any
    cls: (new (...args: any[]) => TObject) & { typeId: URL },
    cls: ConstructorWithTypeId<TObject>,
    path: `${string}${Rfc6570Expression<TParam>}${string}${Rfc6570Expression<
      TParam
    >}${string}`,
    dispatcher: ObjectDispatcher<TContextData, TObject, TParam>,
  ): ObjectCallbackSetters<TContextData, TObject, TParam>;
  setObjectDispatcher<TObject extends Object, TParam extends string>(
    // deno-lint-ignore no-explicit-any
    cls: (new (...args: any[]) => TObject) & { typeId: URL },
    cls: ConstructorWithTypeId<TObject>,
    path: `${string}${Rfc6570Expression<TParam>}${string}`,
    dispatcher: ObjectDispatcher<TContextData, TObject, TParam>,
  ): ObjectCallbackSetters<TContextData, TObject, TParam>;
  setObjectDispatcher<TObject extends Object, TParam extends string>(
    // deno-lint-ignore no-explicit-any
    cls: (new (...args: any[]) => TObject) & { typeId: URL },
    cls: ConstructorWithTypeId<TObject>,
    path: string,
    dispatcher: ObjectDispatcher<TContextData, TObject, TParam>,
  ): ObjectCallbackSetters<TContextData, TObject, TParam> {
@@ -1226,8 +1215,7 @@ export class FederationBuilderImpl<TContextData>
    TParam extends string,
  >(
    name: string | symbol,
    // deno-lint-ignore no-explicit-any
    itemType: (new (...args: any[]) => TObject) & { typeId: URL },
    itemType: ConstructorWithTypeId<TObject>,
    path: `${string}${Rfc6570Expression<
      TParam
    >}${string}${Rfc6570Expression<
@@ -1253,8 +1241,7 @@ export class FederationBuilderImpl<TContextData>
    TParam extends string,
  >(
    name: string | symbol,
    // deno-lint-ignore no-explicit-any
    itemType: (new (...args: any[]) => TObject) & { typeId: URL },
    itemType: ConstructorWithTypeId<TObject>,
    path: `${string}${Rfc6570Expression<
      TParam
    >}${string}${Rfc6570Expression<
@@ -1278,8 +1265,7 @@ export class FederationBuilderImpl<TContextData>
    TParam extends string,
  >(
    name: string | symbol,
    // deno-lint-ignore no-explicit-any
    itemType: (new (...args: any[]) => TObject) & { typeId: URL },
    itemType: ConstructorWithTypeId<TObject>,
    path: `${string}${Rfc6570Expression<TParam>}${string}${Rfc6570Expression<
      TParam
    >}${string}`,
@@ -1299,8 +1285,7 @@ export class FederationBuilderImpl<TContextData>
    TParam extends string,
  >(
    name: string | symbol,
    // deno-lint-ignore no-explicit-any
    itemType: (new (...args: any[]) => TObject) & { typeId: URL },
    itemType: ConstructorWithTypeId<TObject>,
    path: `${string}${Rfc6570Expression<TParam>}${string}`,
    dispatcher: CustomCollectionDispatcher<
      TObject,
@@ -1318,8 +1303,7 @@ export class FederationBuilderImpl<TContextData>
    TParam extends string,
  >(
    name: string | symbol,
    // deno-lint-ignore no-explicit-any
    itemType: (new (...args: any[]) => TObject) & { typeId: URL },
    itemType: ConstructorWithTypeId<TObject>,
    path: string,
    dispatcher: CustomCollectionDispatcher<
      TObject,
@@ -1346,8 +1330,7 @@ export class FederationBuilderImpl<TContextData>
    TParam extends string,
  >(
    name: string | symbol,
    // deno-lint-ignore no-explicit-any
    itemType: (new (...args: any[]) => TObject) & { typeId: URL },
    itemType: ConstructorWithTypeId<TObject>,
    path: `${string}${Rfc6570Expression<TParam>}${string}${Rfc6570Expression<
      TParam
    >}${string}${Rfc6570Expression<TParam>}${string}`,
@@ -1367,8 +1350,7 @@ export class FederationBuilderImpl<TContextData>
    TParam extends string,
  >(
    name: string | symbol,
    // deno-lint-ignore no-explicit-any
    itemType: (new (...args: any[]) => TObject) & { typeId: URL },
    itemType: ConstructorWithTypeId<TObject>,
    path: `${string}${Rfc6570Expression<TParam>}${string}${Rfc6570Expression<
      TParam
    >}${string}`,
@@ -1388,8 +1370,7 @@ export class FederationBuilderImpl<TContextData>
    TParam extends string,
  >(
    name: string | symbol,
    // deno-lint-ignore no-explicit-any
    itemType: (new (...args: any[]) => TObject) & { typeId: URL },
    itemType: ConstructorWithTypeId<TObject>,
    path: `${string}${Rfc6570Expression<TParam>}${string}`,
    dispatcher: CustomCollectionDispatcher<
      TObject,
@@ -1407,8 +1388,7 @@ export class FederationBuilderImpl<TContextData>
    TParam extends string,
  >(
    name: string | symbol,
    // deno-lint-ignore no-explicit-any
    itemType: (new (...args: any[]) => TObject) & { typeId: URL },
    itemType: ConstructorWithTypeId<TObject>,
    path: string,
    dispatcher: CustomCollectionDispatcher<
      TObject,
@@ -1436,8 +1416,7 @@ export class FederationBuilderImpl<TContextData>
  >(
    name: string | symbol,
    collectionType: "collection" | "orderedCollection",
    // deno-lint-ignore no-explicit-any
    itemType: (new (...args: any[]) => TObject) & { typeId: URL },
    itemType: ConstructorWithTypeId<TObject>,
    path: `${string}${Rfc6570Expression<TParam>}${string}`,
    dispatcher: CustomCollectionDispatcher<
      TObject,
+3 −6
Original line number Diff line number Diff line
@@ -120,8 +120,7 @@ export interface Context<TContextData> {
   * @since 0.7.0
   */
  getObjectUri<TObject extends Object>(
    // deno-lint-ignore no-explicit-any
    cls: (new (...args: any[]) => TObject) & { typeId: URL },
    cls: ConstructorWithTypeId<TObject>,
    values: Record<string, string>,
  ): URL;

@@ -483,8 +482,7 @@ export interface RequestContext<TContextData> extends Context<TContextData> {
   * @since 0.7.0
   */
  getObject<TObject extends Object>(
    // deno-lint-ignore no-explicit-any
    cls: (new (...args: any[]) => TObject) & { typeId: URL },
    cls: ConstructorWithTypeId<TObject>,
    values: Record<string, string>,
  ): Promise<TObject | null>;

@@ -646,8 +644,7 @@ export type ParseUriResult =
   */
  | {
    readonly type: "object";
    // deno-lint-ignore no-explicit-any
    readonly class: (new (...args: any[]) => Object) & { typeId: URL };
    readonly class: ConstructorWithTypeId<Object>;
    readonly typeId: URL;
    readonly values: Record<string, string>;
  }
+12 −24
Original line number Diff line number Diff line
@@ -133,8 +133,7 @@ export interface Federatable<TContextData> {
   * @param dispatcher An object dispatcher callback to register.
   */
  setObjectDispatcher<TObject extends Object, TParam extends string>(
    // deno-lint-ignore no-explicit-any
    cls: (new (...args: any[]) => TObject) & { typeId: URL },
    cls: ConstructorWithTypeId<TObject>,
    path:
      `${string}{${TParam}}${string}{${TParam}}${string}{${TParam}}${string}{${TParam}}${string}{${TParam}}${string}{${TParam}}${string}`,
    dispatcher: ObjectDispatcher<TContextData, TObject, TParam>,
@@ -154,8 +153,7 @@ export interface Federatable<TContextData> {
   * @param dispatcher An object dispatcher callback to register.
   */
  setObjectDispatcher<TObject extends Object, TParam extends string>(
    // deno-lint-ignore no-explicit-any
    cls: (new (...args: any[]) => TObject) & { typeId: URL },
    cls: ConstructorWithTypeId<TObject>,
    path:
      `${string}{${TParam}}${string}{${TParam}}${string}{${TParam}}${string}{${TParam}}${string}{${TParam}}${string}`,
    dispatcher: ObjectDispatcher<TContextData, TObject, TParam>,
@@ -175,8 +173,7 @@ export interface Federatable<TContextData> {
   * @param dispatcher An object dispatcher callback to register.
   */
  setObjectDispatcher<TObject extends Object, TParam extends string>(
    // deno-lint-ignore no-explicit-any
    cls: (new (...args: any[]) => TObject) & { typeId: URL },
    cls: ConstructorWithTypeId<TObject>,
    path:
      `${string}{${TParam}}${string}{${TParam}}${string}{${TParam}}${string}{${TParam}}${string}`,
    dispatcher: ObjectDispatcher<TContextData, TObject, TParam>,
@@ -196,8 +193,7 @@ export interface Federatable<TContextData> {
   * @param dispatcher An object dispatcher callback to register.
   */
  setObjectDispatcher<TObject extends Object, TParam extends string>(
    // deno-lint-ignore no-explicit-any
    cls: (new (...args: any[]) => TObject) & { typeId: URL },
    cls: ConstructorWithTypeId<TObject>,
    path: `${string}${Rfc6570Expression<TParam>}${string}${Rfc6570Expression<
      TParam
    >}${string}${Rfc6570Expression<TParam>}${string}`,
@@ -218,8 +214,7 @@ export interface Federatable<TContextData> {
   * @param dispatcher An object dispatcher callback to register.
   */
  setObjectDispatcher<TObject extends Object, TParam extends string>(
    // deno-lint-ignore no-explicit-any
    cls: (new (...args: any[]) => TObject) & { typeId: URL },
    cls: ConstructorWithTypeId<TObject>,
    path: `${string}${Rfc6570Expression<TParam>}${string}${Rfc6570Expression<
      TParam
    >}${string}`,
@@ -240,8 +235,7 @@ export interface Federatable<TContextData> {
   * @param dispatcher An object dispatcher callback to register.
   */
  setObjectDispatcher<TObject extends Object, TParam extends string>(
    // deno-lint-ignore no-explicit-any
    cls: (new (...args: any[]) => TObject) & { typeId: URL },
    cls: ConstructorWithTypeId<TObject>,
    path: `${string}${Rfc6570Expression<TParam>}${string}`,
    dispatcher: ObjectDispatcher<TContextData, TObject, TParam>,
  ): ObjectCallbackSetters<TContextData, TObject, TParam>;
@@ -495,8 +489,7 @@ export interface Federatable<TContextData> {
   */
  setCollectionDispatcher<TObject extends Object, TParam extends string>(
    name: string | symbol,
    // deno-lint-ignore no-explicit-any
    itemType: (new (...args: any[]) => TObject) & { typeId: URL },
    itemType: ConstructorWithTypeId<TObject>,
    path: `${string}${Rfc6570Expression<
      TParam
    >}${string}${Rfc6570Expression<
@@ -532,8 +525,7 @@ export interface Federatable<TContextData> {
   */
  setCollectionDispatcher<TObject extends Object, TParam extends string>(
    name: string | symbol,
    // deno-lint-ignore no-explicit-any
    itemType: (new (...args: any[]) => TObject) & { typeId: URL },
    itemType: ConstructorWithTypeId<TObject>,
    path: `${string}${Rfc6570Expression<TParam>}${string}${Rfc6570Expression<
      TParam
    >}${string}`,
@@ -565,8 +557,7 @@ export interface Federatable<TContextData> {
   */
  setCollectionDispatcher<TObject extends Object, TParam extends string>(
    name: string | symbol,
    // deno-lint-ignore no-explicit-any
    itemType: (new (...args: any[]) => TObject) & { typeId: URL },
    itemType: ConstructorWithTypeId<TObject>,
    path: `${string}${Rfc6570Expression<TParam>}${string}`,
    dispatcher: CustomCollectionDispatcher<
      TObject,
@@ -599,8 +590,7 @@ export interface Federatable<TContextData> {
    TParam extends string,
  >(
    name: string | symbol,
    // deno-lint-ignore no-explicit-any
    itemType: (new (...args: any[]) => TObject) & { typeId: URL },
    itemType: ConstructorWithTypeId<TObject>,
    path: `${string}${Rfc6570Expression<TParam>}${string}${Rfc6570Expression<
      TParam
    >}${string}${Rfc6570Expression<TParam>}${string}`,
@@ -635,8 +625,7 @@ export interface Federatable<TContextData> {
    TParam extends string,
  >(
    name: string | symbol,
    // deno-lint-ignore no-explicit-any
    itemType: (new (...args: any[]) => TObject) & { typeId: URL },
    itemType: ConstructorWithTypeId<TObject>,
    path: `${string}${Rfc6570Expression<TParam>}${string}${Rfc6570Expression<
      TParam
    >}${string}`,
@@ -671,8 +660,7 @@ export interface Federatable<TContextData> {
    TParam extends string,
  >(
    name: string | symbol,
    // deno-lint-ignore no-explicit-any
    itemType: (new (...args: any[]) => TObject) & { typeId: URL },
    itemType: ConstructorWithTypeId<TObject>,
    path: `${string}${Rfc6570Expression<TParam>}${string}`,
    dispatcher: CustomCollectionDispatcher<
      TObject,
+2 −2
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ import type {
  ObjectDispatcher,
} from "./callback.ts";
import type { RequestContext } from "./context.ts";
import type { ConstructorWithTypeId } from "./federation.ts";
import {
  acceptsJsonLd,
  type CustomCollectionCallbacks,
@@ -347,8 +348,7 @@ test("handleObject()", async () => {
    data: undefined,
    url: new URL("https://example.com/"),
    getObjectUri(
      // deno-lint-ignore no-explicit-any
      _cls: (new (...args: any[]) => Object) & { typeId: URL },
      _cls: ConstructorWithTypeId<Object>,
      values: Record<string, string>,
    ) {
      return new URL(
+7 −12
Original line number Diff line number Diff line
@@ -78,6 +78,7 @@ import type {
  SendActivityOptionsForCollection,
} from "./context.ts";
import type {
  ConstructorWithTypeId,
  Federation,
  FederationFetchOptions,
  FederationOptions,
@@ -931,8 +932,7 @@ export class FederationImpl<TContextData>
      documentLoader?: DocumentLoader;
      invokedFromActorDispatcher?: { identifier: string };
      invokedFromObjectDispatcher?: {
        // deno-lint-ignore no-explicit-any
        cls: (new (...args: any[]) => Object) & { typeId: URL };
        cls: ConstructorWithTypeId<Object>;
        values: Record<string, string>;
      };
    },
@@ -945,8 +945,7 @@ export class FederationImpl<TContextData>
      documentLoader?: DocumentLoader;
      invokedFromActorDispatcher?: { identifier: string };
      invokedFromObjectDispatcher?: {
        // deno-lint-ignore no-explicit-any
        cls: (new (...args: any[]) => Object) & { typeId: URL };
        cls: ConstructorWithTypeId<Object>;
        values: Record<string, string>;
      };
    } = {},
@@ -1621,8 +1620,7 @@ export class ContextImpl<TContextData> implements Context<TContextData> {
  }

  getObjectUri<TObject extends Object>(
    // deno-lint-ignore no-explicit-any
    cls: (new (...args: any[]) => TObject) & { typeId: URL },
    cls: ConstructorWithTypeId<TObject>,
    values: Record<string, string>,
  ): URL {
    const callbacks = this.federation.objectCallbacks[cls.typeId.href];
@@ -2564,8 +2562,7 @@ interface RequestContextOptions<TContextData>
  request: Request;
  invokedFromActorDispatcher?: { identifier: string };
  invokedFromObjectDispatcher?: {
    // deno-lint-ignore no-explicit-any
    cls: (new (...args: any[]) => Object) & { typeId: URL };
    cls: ConstructorWithTypeId<Object>;
    values: Record<string, string>;
  };
}
@@ -2574,8 +2571,7 @@ class RequestContextImpl<TContextData> extends ContextImpl<TContextData>
  implements RequestContext<TContextData> {
  readonly #invokedFromActorDispatcher?: { identifier: string };
  readonly #invokedFromObjectDispatcher?: {
    // deno-lint-ignore no-explicit-any
    cls: (new (...args: any[]) => Object) & { typeId: URL };
    cls: ConstructorWithTypeId<Object>;
    values: Record<string, string>;
  };
  readonly request: Request;
@@ -2634,8 +2630,7 @@ class RequestContextImpl<TContextData> extends ContextImpl<TContextData>
  }

  async getObject<TObject extends Object>(
    // deno-lint-ignore no-explicit-any
    cls: (new (...args: any[]) => TObject) & { typeId: URL },
    cls: ConstructorWithTypeId<TObject>,
    values: Record<string, string>,
  ): Promise<TObject | null> {
    const callbacks = this.federation.objectCallbacks[cls.typeId.href];