Skip to content

Commit d2a0e7d

Browse files
committed
Handle central Google discovery overrides
1 parent e91270a commit d2a0e7d

2 files changed

Lines changed: 24 additions & 5 deletions

File tree

packages/plugins/google/src/sdk/discovery.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,17 @@ it("accepts only supported HTTPS Google Discovery endpoints", () => {
5050
expect(
5151
normalizeGoogleDiscoveryUrl("https://www.googleapis.com/discovery/v1/apis/gmail/v1/rest/"),
5252
).toBe("https://www.googleapis.com/discovery/v1/apis/gmail/v1/rest");
53+
expect(
54+
normalizeGoogleDiscoveryUrl(
55+
"https://www.googleapis.com/discovery/v1/apis/photospicker/v1/rest",
56+
),
57+
).toBe("https://photospicker.googleapis.com/$discovery/rest?version=v1");
58+
expect(
59+
normalizeGoogleDiscoveryUrl("https://www.googleapis.com/discovery/v1/apis/forms/v1/rest"),
60+
).toBe("https://forms.googleapis.com/$discovery/rest?version=v1");
61+
expect(
62+
normalizeGoogleDiscoveryUrl("https://www.googleapis.com/discovery/v1/apis/keep/v1/rest"),
63+
).toBe("https://keep.googleapis.com/$discovery/rest?version=v1");
5364
expect(
5465
normalizeGoogleDiscoveryUrl("https://chat.googleapis.com/$discovery/rest?version=v1"),
5566
).toBe("https://www.googleapis.com/discovery/v1/apis/chat/v1/rest");

packages/plugins/google/src/sdk/discovery.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,17 @@ const GOOGLE_DISCOVERY_SERVICE_OVERRIDES: Record<string, GoogleDiscoveryServiceO
5050
},
5151
};
5252

53+
const googleDiscoveryUrlForService = (
54+
service: string,
55+
version: string,
56+
host = `${service}.googleapis.com`,
57+
): string => {
58+
const override = GOOGLE_DISCOVERY_SERVICE_OVERRIDES[service];
59+
return override?.preserveServiceHostedUrl === true
60+
? `https://${host}/$discovery/rest?version=${version}`
61+
: `${DISCOVERY_SERVICE_HOST}/${service}/${version}/rest`;
62+
};
63+
5364
type JsonPrimitive = string | number | boolean | null;
5465
type JsonValue = JsonPrimitive | readonly JsonValue[] | { readonly [key: string]: JsonValue };
5566

@@ -288,7 +299,7 @@ export const normalizeGoogleDiscoveryUrl = (discoveryUrl: string): string | null
288299
const match = parsed.pathname.match(DISCOVERY_SERVICE_PATH_RE);
289300
const service = match?.[1];
290301
const version = match?.[2];
291-
return service && version ? `${DISCOVERY_SERVICE_HOST}/${service}/${version}/rest` : null;
302+
return service && version ? googleDiscoveryUrlForService(service, version) : null;
292303
}
293304

294305
const service = serviceFromGoogleApisHost(host);
@@ -305,10 +316,7 @@ export const normalizeGoogleDiscoveryUrl = (discoveryUrl: string): string | null
305316
) {
306317
return null;
307318
}
308-
const override = GOOGLE_DISCOVERY_SERVICE_OVERRIDES[service];
309-
return override?.preserveServiceHostedUrl === true
310-
? `https://${host}/$discovery/rest?version=${version}`
311-
: `${DISCOVERY_SERVICE_HOST}/${service}/${version}/rest`;
319+
return googleDiscoveryUrlForService(service, version, host);
312320
};
313321

314322
const normalizeDiscoveryUrl = (discoveryUrl: string): string => {

0 commit comments

Comments
 (0)