This commit is contained in:
Max Peintner
2025-02-03 09:34:09 +01:00
parent c683613633
commit 152d24f076

View File

@@ -50,11 +50,20 @@ export async function createServiceForHost<T extends ServiceClass>(
return createClientFor<T>(service)(transport); return createClientFor<T>(service)(transport);
} }
/**
* Extracts the service url and region from the headers if used in a multitenant context (x-zitadel-forward-host, x-zitade-region header)
* or falls back to the ZITADEL_API_URL for a self hosting deployment
* or falls back to the host header for a self hosting deployment using custom domains
* @param headers
* @returns the service url and region from the headers
* @throws if the service url could not be determined
*
*/
export function getServiceUrlFromHeaders(headers: ReadonlyHeaders): { export function getServiceUrlFromHeaders(headers: ReadonlyHeaders): {
serviceUrl: string; serviceUrl: string;
serviceRegion: string; serviceRegion: string;
} { } {
let instanceUrl: string = process.env.ZITADEL_API_URL; let instanceUrl;
const forwardedHost = headers.get("x-zitadel-forward-host"); const forwardedHost = headers.get("x-zitadel-forward-host");
// use the forwarded host if available (multitenant), otherwise fall back to the host of the deployment itself // use the forwarded host if available (multitenant), otherwise fall back to the host of the deployment itself
@@ -63,6 +72,8 @@ export function getServiceUrlFromHeaders(headers: ReadonlyHeaders): {
instanceUrl = instanceUrl.startsWith("https://") instanceUrl = instanceUrl.startsWith("https://")
? instanceUrl ? instanceUrl
: `https://${instanceUrl}`; : `https://${instanceUrl}`;
} else if (process.env.ZITADEL_API_URL) {
instanceUrl = process.env.ZITADEL_API_URL;
} else { } else {
const host = headers.get("host"); const host = headers.get("host");
@@ -74,6 +85,10 @@ export function getServiceUrlFromHeaders(headers: ReadonlyHeaders): {
} }
} }
if (!instanceUrl) {
throw new Error("Service URL could not be determined");
}
return { return {
serviceUrl: instanceUrl, serviceUrl: instanceUrl,
serviceRegion: headers.get("x-zitadel-region") || "", serviceRegion: headers.get("x-zitadel-region") || "",