GeezerVison is showing github.com — org plan check and endpoints auto update in cli config · contentstack/cli@ab0efb9
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboard +17-1Lines changed: 17 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | fileignoreconfig: | 2 | 2 | - filename: pnpm-lock.yaml | 3 | | - checksum: dd5f6ad633313054c21fa0e24645a25dca360f12f21b877472f514d848340ac6 | | 3 | + checksum: 6a2d0b32643423b7b3b5f3a5afab2db85f7cdd734c63128ddda5c7400065b83b | 4 | 4 | - filename: packages/contentstack-utilities/src/message-handler.ts | 5 | 5 | checksum: e7221e8413005b9efe3a230cd91aff130850976addc41c0acb10745a56ec3245 | 6 | 6 | - filename: packages/contentstack-auth/src/commands/auth/tokens/list.ts | 7 | 7 | checksum: cff2456db5a6eb9c056096e5b08d4737ad4507386f9998f9182d3acab8be868d | 8 | 8 | - filename: packages/contentstack/README.md | 9 | 9 | checksum: 4aec6271d09ee38452460140b1d8da338df15c6ed765544246c00417d179dba5 | | 10 | + - filename: packages/contentstack-utilities/src/feature-status/build-auth-headers.ts | | 11 | + checksum: 9360dfbce41aa9c8a62889f4821c37fc222b04b8b1c552d1dfbcd8f2854d49e0 | | 12 | + - filename: packages/contentstack/src/hooks/prerun/plan-guard.ts | | 13 | + checksum: 74eeb5c98e9ae48bf6f2918e75a7cfca23915396ffdd177b8716acba35c287da | | 14 | + - filename: packages/contentstack-config/src/utils/region-handler.ts | | 15 | + checksum: 1dcf8b8991d172db535792116999f2ea98d8d529c64fa436088dbecdb4516fb9 | | 16 | + - filename: packages/contentstack-utilities/test/unit/region-endpoints.test.ts | | 17 | + checksum: fe37fa11a72d154e6bfeef38a18039c68cf3cc4446183cb012596ad8c7ea5083 | | 18 | + - filename: packages/contentstack-config/test/unit/commands/region.test.ts | | 19 | + checksum: 334e2a8c63338137cbf38b1fbabc73638b3ea1c30996318a1bd88392bf7687ed | | 20 | + - filename: packages/contentstack-utilities/src/region-endpoints.ts | | 21 | + checksum: 78902404db884ddc9ad812c4db3f410a52de2c895c2021733edc939d273030fb | | 22 | + - filename: packages/contentstack-utilities/test/unit/resolve-auth-host.test.ts | | 23 | + checksum: 19557d2905561d3dda94ebd68af342ecc646d2b5bbce3c1552bb9aea90f4bbbc | | 24 | + - filename: packages/contentstack-utilities/test/unit/region-refresh.test.ts | | 25 | + checksum: 1edac51f260df3ce28b538146e607d77742daed2364c25b4a922853b99f6d912 | 10 | 26 | version: '1.0' |
Collapse file Copy file name to clipboardExpand all lines: packages/contentstack-config/src/commands/config/set/region.ts +12-2Lines changed: 12 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -132,7 +132,17 @@ export default class RegionSetCommand extends BaseCommand<typeof RegionSetComman | 132 | 132 | launchHubUrl, | 133 | 133 | composableStudioUrl, | 134 | 134 | csAssetsUrl, | 135 | | - authUrl, | | 135 | + endpoints: { | | 136 | + contentManagement: cma, | | 137 | + contentDelivery: cda, | | 138 | + application: uiHost, | | 139 | + developerHub: developerHubUrl, | | 140 | + launch: launchHubUrl, | | 141 | + personalizeManagement: personalizeUrl, | | 142 | + composableStudio: composableStudioUrl, | | 143 | + assetManagement: csAssetsUrl, | | 144 | + auth: authUrl, | | 145 | + }, | 136 | 146 | }; | 137 | 147 | customRegion = regionHandler.setCustomRegion(customRegion); | 138 | 148 | await authHandler.setConfigData('logout'); //Todo: Handle this logout flow well through logout command call | @@ -145,7 +155,7 @@ export default class RegionSetCommand extends BaseCommand<typeof RegionSetComman | 145 | 155 | cliux.success(`Launch URL: ${customRegion.launchHubUrl}`); | 146 | 156 | cliux.success(`Studio URL: ${customRegion.composableStudioUrl}`); | 147 | 157 | cliux.success(`Contentstack Assets URL: ${customRegion.csAssetsUrl}`); | 148 | | - cliux.success(`Auth API URL: ${customRegion.authUrl}`); | | 158 | + cliux.success(`Auth API URL: ${customRegion.endpoints?.auth}`); | 149 | 159 | } catch (error) { | 150 | 160 | handleAndLogError(error, { ...this.contextDetails, module: 'config-set-region' }); | 151 | 161 | } | |
Collapse file Copy file name to clipboardExpand all lines: packages/contentstack-config/src/interfaces/index.ts +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -20,7 +20,7 @@ export interface Region { | 20 | 20 | launchHubUrl: string; | 21 | 21 | composableStudioUrl: string; | 22 | 22 | csAssetsUrl?: string; | 23 | | - authUrl?: string; | | 23 | + endpoints?: Record<string, string>; | 24 | 24 | } | 25 | 25 |
| 26 | 26 | export interface Limit { | |
Collapse file Copy file name to clipboardExpand all lines: packages/contentstack-config/src/utils/region-handler.ts +20-37Lines changed: 20 additions & 37 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | | -import { configHandler } from '@contentstack/cli-utilities'; | 2 | | -import { getContentstackEndpoint } from '@contentstack/utils'; | | 1 | +import { configHandler, resolveCanonicalEndpoints, buildRegionFromEndpoints } from '@contentstack/cli-utilities'; | 3 | 2 | import { Region, RegionsMap } from '../interfaces'; | 4 | 3 |
| 5 | 4 | function validURL(str) { | @@ -23,29 +22,10 @@ function validURL(str) { | 23 | 22 | * @returns {object} Region object with all necessary URLs | 24 | 23 | */ | 25 | 24 | function getRegionObject(regionKey: string): Region { | 26 | | - try { | 27 | | - // getContentstackEndpoint handles all aliases defined in regions.json | 28 | | - const endpoints = getContentstackEndpoint(regionKey) as any; | 29 | | - | 30 | | - if (typeof endpoints === 'string') { | 31 | | - throw new Error('Invalid endpoint response'); | 32 | | - } | 33 | | - | 34 | | - return { | 35 | | - name: regionKey, | 36 | | - cma: endpoints.contentManagement, | 37 | | - cda: endpoints.contentDelivery, | 38 | | - uiHost: endpoints.application, | 39 | | - developerHubUrl: endpoints.developerHub, | 40 | | - launchHubUrl: endpoints.launch, | 41 | | - personalizeUrl: endpoints.personalizeManagement, | 42 | | - composableStudioUrl: endpoints.composableStudio, | 43 | | - csAssetsUrl: endpoints.assetManagement, | 44 | | - authUrl: endpoints.auth, | 45 | | - }; | 46 | | - } catch { | 47 | | - return null; | 48 | | - } | | 25 | + // resolveCanonicalEndpoints handles all aliases defined in regions.json | | 26 | + const endpoints = resolveCanonicalEndpoints(regionKey); | | 27 | + if (!endpoints) return null; | | 28 | + return buildRegionFromEndpoints(regionKey, endpoints) as Region; | 49 | 29 | } | 50 | 30 |
| 51 | 31 | /** | @@ -148,20 +128,23 @@ class UserConfig { | 148 | 128 | * @returns { object } JSON object with only valid keys for region | 149 | 129 | */ | 150 | 130 | sanitizeRegionObject(regionObject) { | 151 | | - const sanitizedRegion = { | 152 | | - cma: regionObject.cma, | 153 | | - cda: regionObject.cda, | 154 | | - uiHost: regionObject.uiHost, | 155 | | - name: regionObject.name, | 156 | | - developerHubUrl: regionObject['developerHubUrl'], | 157 | | - personalizeUrl: regionObject['personalizeUrl'], | 158 | | - launchHubUrl: regionObject['launchHubUrl'], | 159 | | - composableStudioUrl: regionObject['composableStudioUrl'], | 160 | | - csAssetsUrl: regionObject['csAssetsUrl'], | 161 | | - authUrl: regionObject['authUrl'], | | 131 | + // endpoints is the single source of truth — every friendly field (cma, cda, ..., | | 132 | + // auth, csAssetsUrl) is derived from it below via buildRegionFromEndpoints, same | | 133 | + // as named regions. Falls back to synthesizing endpoints from the individual raw | | 134 | + // fields when the caller didn't supply one directly. | | 135 | + const endpoints = regionObject['endpoints'] ?? { | | 136 | + contentManagement: regionObject.cma, | | 137 | + contentDelivery: regionObject.cda, | | 138 | + application: regionObject.uiHost, | | 139 | + developerHub: regionObject['developerHubUrl'], | | 140 | + launch: regionObject['launchHubUrl'], | | 141 | + personalizeManagement: regionObject['personalizeUrl'], | | 142 | + composableStudio: regionObject['composableStudioUrl'], | | 143 | + assetManagement: regionObject['csAssetsUrl'], | | 144 | + auth: regionObject['authUrl'], | 162 | 145 | }; | 163 | 146 |
| 164 | | - return sanitizedRegion; | | 147 | + return buildRegionFromEndpoints(regionObject.name, endpoints); | 165 | 148 | } | 166 | 149 | } | 167 | 150 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/contentstack-config/test/unit/commands/region.test.ts +81-1Lines changed: 81 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -313,7 +313,87 @@ describe('Region command', function () { | 313 | 313 | csAssetsUrl: 'https://custom-asset-management.com', | 314 | 314 | }; | 315 | 315 | const result = UserConfig.setCustomRegion(customRegion); | 316 | | - expect(result).to.deep.equal(customRegion); | | 316 | + expect(result.cma).to.equal(customRegion.cma); | | 317 | + expect(result.cda).to.equal(customRegion.cda); | | 318 | + expect(result.uiHost).to.equal(customRegion.uiHost); | | 319 | + expect(result.name).to.equal(customRegion.name); | | 320 | + expect(result.developerHubUrl).to.equal(customRegion.developerHubUrl); | | 321 | + expect(result.personalizeUrl).to.equal(customRegion.personalizeUrl); | | 322 | + expect(result.launchHubUrl).to.equal(customRegion.launchHubUrl); | | 323 | + expect(result.composableStudioUrl).to.equal(customRegion.composableStudioUrl); | | 324 | + }); | | 325 | + | | 326 | + it('should include a matching endpoints object for a custom region without an auth host', function () { | | 327 | + const customRegion = { | | 328 | + cma: 'https://custom-cma.com', | | 329 | + cda: 'https://custom-cda.com', | | 330 | + uiHost: 'https://custom-ui.com', | | 331 | + name: 'Custom Region', | | 332 | + developerHubUrl: 'https://custom-developer-hub.com', | | 333 | + personalizeUrl: 'https://custom-personalize.com', | | 334 | + launchHubUrl: 'https://custom-launch.com', | | 335 | + composableStudioUrl: 'https://custom-composable-studio.com', | | 336 | + }; | | 337 | + const result = UserConfig.setCustomRegion(customRegion); | | 338 | + expect(result.endpoints).to.deep.equal({ | | 339 | + contentManagement: customRegion.cma, | | 340 | + contentDelivery: customRegion.cda, | | 341 | + application: customRegion.uiHost, | | 342 | + developerHub: customRegion.developerHubUrl, | | 343 | + launch: customRegion.launchHubUrl, | | 344 | + personalizeManagement: customRegion.personalizeUrl, | | 345 | + composableStudio: customRegion.composableStudioUrl, | | 346 | + assetManagement: undefined, | | 347 | + auth: undefined, | | 348 | + }); | | 349 | + }); | | 350 | + | | 351 | + it('should preserve an explicitly supplied endpoints object for a custom region', function () { | | 352 | + const endpoints = { | | 353 | + contentManagement: 'https://custom-cma.com', | | 354 | + contentDelivery: 'https://custom-cda.com', | | 355 | + application: 'https://custom-ui.com', | | 356 | + auth: 'https://custom-auth.com', | | 357 | + }; | | 358 | + const customRegion = { | | 359 | + cma: 'https://custom-cma.com', | | 360 | + cda: 'https://custom-cda.com', | | 361 | + uiHost: 'https://custom-ui.com', | | 362 | + name: 'Custom Region', | | 363 | + endpoints, | | 364 | + }; | | 365 | + const result = UserConfig.setCustomRegion(customRegion); | | 366 | + expect(result.cma).to.equal(endpoints.contentManagement); | | 367 | + expect(result.cda).to.equal(endpoints.contentDelivery); | | 368 | + expect(result.uiHost).to.equal(endpoints.application); | | 369 | + expect(result.endpoints).to.deep.equal(endpoints); | | 370 | + }); | | 371 | + | | 372 | + it('should not fall back to a stale legacy authUrl input once endpoints.auth is supplied', function () { | | 373 | + // endpoints is the single source of truth: a stale/mismatched top-level | | 374 | + // authUrl on the raw input is never used — only endpoints.auth matters. | | 375 | + const customRegion = { | | 376 | + cma: 'https://custom-cma.com', | | 377 | + cda: 'https://custom-cda.com', | | 378 | + uiHost: 'https://custom-ui.com', | | 379 | + name: 'Custom Region', | | 380 | + authUrl: 'https://stale-auth.com', | | 381 | + endpoints: { | | 382 | + contentManagement: 'https://custom-cma.com', | | 383 | + contentDelivery: 'https://custom-cda.com', | | 384 | + application: 'https://custom-ui.com', | | 385 | + auth: 'https://correct-auth.com', | | 386 | + }, | | 387 | + }; | | 388 | + const result = UserConfig.setCustomRegion(customRegion); | | 389 | + expect(result.endpoints.auth).to.equal('https://correct-auth.com'); | | 390 | + }); | | 391 | + | | 392 | + it('should include the full endpoints passthrough (incl. auth) for named regions', function () { | | 393 | + const result = UserConfig.setRegion('NA'); | | 394 | + expect(result.endpoints).to.be.an('object'); | | 395 | + expect(result.endpoints.auth).to.equal('https://auth-api.contentstack.com'); | | 396 | + expect(result.endpoints.contentManagement).to.equal(result.cma); | 317 | 397 | }); | 318 | 398 |
| 319 | 399 | it('should sanitize region object to only include valid properties', function () { | |
Collapse file Copy file name to clipboardExpand all lines: packages/contentstack-utilities/package.json +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 30 | 30 | "dependencies": { | 31 | 31 | "@contentstack/management": "~1.30.3", | 32 | 32 | "@contentstack/marketplace-sdk": "^1.5.2", | | 33 | + "@contentstack/utils": "~1.9.1", | 33 | 34 | "@oclif/core": "^4.11.4", | 34 | 35 | "axios": "^1.16.1", | 35 | 36 | "chalk": "^5.6.2", | |
Collapse file Copy file name to clipboardExpand all lines: packages/contentstack-utilities/src/feature-status/resolve-auth-host.ts +23-3Lines changed: 23 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import configHandler from '../config-handler'; | 2 | 2 |
| 3 | | -export function resolveAuthHost(ctx?: { region?: { authUrl?: string } }): string { | 4 | | - const region = (ctx?.region ?? configHandler.get('region')) as { authUrl?: string } | undefined; | 5 | | - const authUrl = region?.authUrl; | | 3 | +// Mirrors config:set:region's own custom-region fallback (region.ts's transformUrl) — | | 4 | +// same heuristic, applied retroactively for a custom region that predates auth-api. | | 5 | +// Computed fresh on each call, never persisted back to config. | | 6 | +function deriveAuthFromCma(cma: string): string { | | 7 | + let transformed = cma.replace('api', 'auth-api'); | | 8 | + if (transformed.startsWith('http')) { | | 9 | + transformed = transformed.split('//')[1]; | | 10 | + } | | 11 | + transformed = transformed.replace(/^dev\d+/, 'dev'); | | 12 | + transformed = transformed.endsWith('io') ? transformed.replace('io', 'com') : transformed; | | 13 | + return `https://${transformed}`; | | 14 | +} | | 15 | + | | 16 | +export function resolveAuthHost(ctx?: { region?: { endpoints?: { auth?: string }; cma?: string } }): string { | | 17 | + const region = (ctx?.region ?? configHandler.get('region')) as | | 18 | + | { endpoints?: { auth?: string }; cma?: string } | | 19 | + | undefined; | | 20 | + | | 21 | + let authUrl = region?.endpoints?.auth; | | 22 | + if (!authUrl && region?.cma) { | | 23 | + authUrl = deriveAuthFromCma(region.cma); | | 24 | + } | | 25 | + | 6 | 26 | if (!authUrl) { | 7 | 27 | throw new Error( | 8 | 28 | 'PLAN_CHECK: Auth host is not configured for the current region. ' + | |
Collapse file Copy file name to clipboardExpand all lines: packages/contentstack-utilities/src/feature-status/types.ts +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -12,5 +12,5 @@ export interface FeatureCtx { | 12 | 12 | orgUid?: string; | 13 | 13 | managementToken?: string; | 14 | 14 | authToken?: string; | 15 | | - region?: { authUrl?: string; name?: string; cma?: string }; | | 15 | + region?: { name?: string; cma?: string; endpoints?: { auth?: string } }; | 16 | 16 | } |
Collapse file Copy file name to clipboardExpand all lines: packages/contentstack-utilities/src/index.ts +3-1Lines changed: 3 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -91,4 +91,6 @@ export { | 91 | 91 | DefaultProgressStrategy | 92 | 92 | } from './progress-summary'; | 93 | 93 |
| 94 | | -export * from './feature-status'; | | 94 | +export * from './feature-status'; | | 95 | +export * from './region-endpoints'; | | 96 | +export { refreshRegionEndpoints } from './region-refresh'; |
Collapse file Copy file name to clipboard +52Lines changed: 52 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import { getContentstackEndpoint } from '@contentstack/utils'; | | 2 | + | | 3 | +export interface RegionEndpoints { | | 4 | + [key: string]: string; | | 5 | +} | | 6 | + | | 7 | +export interface CanonicalRegion { | | 8 | + name: string; | | 9 | + cma: string; | | 10 | + cda: string; | | 11 | + uiHost: string; | | 12 | + developerHubUrl: string; | | 13 | + launchHubUrl: string; | | 14 | + personalizeUrl: string; | | 15 | + composableStudioUrl: string; | | 16 | + csAssetsUrl?: string; | | 17 | + endpoints: RegionEndpoints; | | 18 | +} | | 19 | + | | 20 | +/** | | 21 | + * Resolve the canonical endpoint set for a region name/alias via @contentstack/utils. | | 22 | + * Returns null when the name isn't a recognized region (e.g. a custom region name). | | 23 | + */ | | 24 | +export function resolveCanonicalEndpoints(name: string): RegionEndpoints | null { | | 25 | + try { | | 26 | + const endpoints = getContentstackEndpoint(name) as unknown; | | 27 | + if (!endpoints || typeof endpoints === 'string') return null; | | 28 | + return endpoints as RegionEndpoints; | | 29 | + } catch { | | 30 | + return null; | | 31 | + } | | 32 | +} | | 33 | + | | 34 | +/** | | 35 | + * Build a region object from a raw endpoints map, keeping the existing named | | 36 | + * fields (cma/cda/uiHost/...) plus a full raw `endpoints` passthrough so any | | 37 | + * future endpoint Contentstack adds is available without further code changes. | | 38 | + */ | | 39 | +export function buildRegionFromEndpoints(name: string, endpoints: RegionEndpoints): CanonicalRegion { | | 40 | + return { | | 41 | + name, | | 42 | + cma: endpoints.contentManagement, | | 43 | + cda: endpoints.contentDelivery, | | 44 | + uiHost: endpoints.application, | | 45 | + developerHubUrl: endpoints.developerHub, | | 46 | + launchHubUrl: endpoints.launch, | | 47 | + personalizeUrl: endpoints.personalizeManagement, | | 48 | + composableStudioUrl: endpoints.composableStudio, | | 49 | + csAssetsUrl: endpoints.assetManagement, | | 50 | + endpoints, | | 51 | + }; | | 52 | +} |
|