@forge/cli-shared

9.8.0-next.9-experimental-e03cc859.8.0-next.10
out/graphql/path-based-graphql-client-resolver.js
+out/graphql/path-based-graphql-client-resolver.jsNew file
+29
Index: package/out/graphql/path-based-graphql-client-resolver.js
===================================================================
--- package/out/graphql/path-based-graphql-client-resolver.js
+++ package/out/graphql/path-based-graphql-client-resolver.js
@@ -0,0 +1,29 @@
+"use strict";
+Object.defineProperty(exports, "__esModule", { value: true });
+exports.PathBasedGraphQLClientResolver = void 0;
+const gateway_1 = require("./gateway");
+const SITE_GRAPHQL_GATEWAY_PATH = '/gateway/api/graphql';
+const HOSTNAME_PATTERN = /^(?=.{1,253}$)(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$/i;
+class PathBasedGraphQLClientResolver {
+    defaultClient;
+    createGraphQLClient;
+    siteClients = new Map();
+    constructor(defaultClient, createGraphQLClient) {
+        this.defaultClient = defaultClient;
+        this.createGraphQLClient = createGraphQLClient;
+    }
+    resolve(site) {
+        const hostname = site?.trim().toLowerCase();
+        if (!hostname || !HOSTNAME_PATTERN.test(hostname) || !(0, gateway_1.isPathBasedRoutingEnabled)()) {
+            return this.defaultClient;
+        }
+        const endpoint = `https://${hostname}${SITE_GRAPHQL_GATEWAY_PATH}`;
+        let client = this.siteClients.get(endpoint);
+        if (!client) {
+            client = this.createGraphQLClient(endpoint);
+            this.siteClients.set(endpoint, client);
+        }
+        return client;
+    }
+}
+exports.PathBasedGraphQLClientResolver = PathBasedGraphQLClientResolver;