Commit 4291d39b authored by ChanHaeng Lee's avatar ChanHaeng Lee
Browse files

Add getCollectionPath method to federation

parent 4ef8f221
Loading
Loading
Loading
Loading
+20 −0
Original line number Diff line number Diff line
@@ -1349,6 +1349,26 @@ export class FederationBuilderImpl<TContextData>
    return setters;
  }

  /**
   * Get the URL path for a custom collection.
   * If the collection is not registered, returns null.
   * @typeParam TParam The parameter names of the requested URL.
   * @param {string | symbol} name The name of the custom collection.
   * @param {TParam} values The values to fill in the URL parameters.
   * @returns {string | null} The URL path for the custom collection, or null if not registered.
   */
  getCollectionPath<TParam extends Record<string, string>>(
    name: string | symbol,
    values: TParam,
  ): string | null {
    // Check if it's a registered custom collection
    if (!(name in this.collectionCallbacks)) return null;
    const routeName = this.#uniqueCollectionId(name);
    const path = this.router.build(`collection:${routeName}`, values) ??
      this.router.build(`orderedCollection:${routeName}`, values);
    return path;
  }

  /**
   * Converts a name (string or symbol) to a unique string identifier.
   * For symbols, generates and caches a UUID if not already present.
+10 −24
Original line number Diff line number Diff line
@@ -1732,33 +1732,19 @@ export class ContextImpl<TContextData> implements Context<TContextData> {
    name: string | symbol,
    values: TParam,
  ): URL {
    // Check if it's a custom collection
    const customCallbacks = this.federation.collectionCallbacks[name];
    if (customCallbacks != null) {
      // For custom collections, use collection: or orderedCollection: prefix
      const collectionRouteName = `collection:${String(name)}`;
      const orderedCollectionRouteName = `orderedCollection:${String(name)}`;

      let path = this.federation.router.build(collectionRouteName, values);
      if (path == null) {
        path = this.federation.router.build(orderedCollectionRouteName, values);
      }

      if (path == null) {
    // Get a path for a collection dispatcher registered for the given name.
    const path = this.federation.getCollectionPath(name, values);
    if (path === null) {
      // If no collection dispatcher is registered for the given name,
      // throw a router error.
      throw new RouterError(
          `No collection dispatcher registered for ${String(name)}.`,
        `No collection dispatcher registered for "${String(name)}".`,
      );
    }

    // Return a URL for the collection path.
    return new URL(path, this.canonicalOrigin);
  }

    // Fall back to built-in collections (for backward compatibility)
    throw new RouterError(
      `No collection dispatcher registered for ${String(name)}.`,
    );
  }

  parseUri(uri: URL | null): ParseUriResult | null {
    if (uri == null) return null;
    if (uri.origin !== this.origin && uri.origin !== this.canonicalOrigin) {