@@ -20,6 +20,21 @@ import {
2020} from '../../clients/mrt-b2c.js' ;
2121import { getLogger } from '../../logging/logger.js' ;
2222
23+ const B2C_API_PATH = '/api/cc/b2c' ;
24+
25+ /**
26+ * Ensures the origin has the /api/cc/b2c path suffix.
27+ * When callers pass the generic MRT origin (e.g. https://cloud.mobify.com),
28+ * we need to append the B2C API path.
29+ */
30+ function toB2COrigin ( origin : string | undefined ) : string {
31+ if ( ! origin ) return DEFAULT_MRT_B2C_ORIGIN ;
32+ // Already has the B2C path
33+ if ( origin . endsWith ( B2C_API_PATH ) ) return origin ;
34+ // Strip trailing slash before appending
35+ return origin . replace ( / \/ + $ / , '' ) + B2C_API_PATH ;
36+ }
37+
2338// Re-export types
2439export type { B2COrgInfo , B2CTargetInfo , PatchedB2CTargetInfo } ;
2540
@@ -155,7 +170,7 @@ export async function getB2COrgInfo(options: GetB2COrgInfoOptions, auth: AuthStr
155170
156171 logger . debug ( { organizationSlug} , '[MRT-B2C] Getting B2C organization info' ) ;
157172
158- const client = createMrtB2CClient ( { origin : origin || DEFAULT_MRT_B2C_ORIGIN } , auth ) ;
173+ const client = createMrtB2CClient ( { origin : toB2COrigin ( origin ) } , auth ) ;
159174
160175 const { data, error} = await client . GET ( '/b2c-organization-info/{organization_slug}/' , {
161176 params : {
@@ -208,7 +223,7 @@ export async function getB2CTargetInfo(options: GetB2CTargetInfoOptions, auth: A
208223
209224 logger . debug ( { projectSlug, targetSlug} , '[MRT-B2C] Getting B2C target info' ) ;
210225
211- const client = createMrtB2CClient ( { origin : origin || DEFAULT_MRT_B2C_ORIGIN } , auth ) ;
226+ const client = createMrtB2CClient ( { origin : toB2COrigin ( origin ) } , auth ) ;
212227
213228 const { data, error} = await client . GET ( '/projects/{project_slug}/b2c-target-info/{target_slug}/' , {
214229 params : {
@@ -260,7 +275,7 @@ export async function setB2CTargetInfo(options: SetB2CTargetInfoOptions, auth: A
260275
261276 logger . debug ( { projectSlug, targetSlug, instanceId} , '[MRT-B2C] Setting B2C target info' ) ;
262277
263- const client = createMrtB2CClient ( { origin : origin || DEFAULT_MRT_B2C_ORIGIN } , auth ) ;
278+ const client = createMrtB2CClient ( { origin : toB2COrigin ( origin ) } , auth ) ;
264279
265280 const body : B2CTargetInfo = {
266281 instance_id : instanceId ,
@@ -324,7 +339,7 @@ export async function updateB2CTargetInfo(
324339
325340 logger . debug ( { projectSlug, targetSlug, instanceId} , '[MRT-B2C] Updating B2C target info' ) ;
326341
327- const client = createMrtB2CClient ( { origin : origin || DEFAULT_MRT_B2C_ORIGIN } , auth ) ;
342+ const client = createMrtB2CClient ( { origin : toB2COrigin ( origin ) } , auth ) ;
328343
329344 const body : PatchedB2CTargetInfo = { } ;
330345
0 commit comments