Skip to main content

ContextStrategy

sdlc-cdk-lib v1.0.0


sdlc-cdk-lib / src/contextStrategy/ContextStrategy

src/contextStrategy/ContextStrategy

Classes

ContextStrategy

Defined in: src/contextStrategy/ContextStrategy.ts:68

Manages SDLC (Software Development Life Cycle) context and environment configuration.

Remarks

This class extracts and normalizes stage information from CDK context, providing type-safe access to environment configuration. It supports six SDLC stages (dev, test, staging, qa, preprod, prod) mapped to three core environments.

The stage can be provided in multiple formats:

  • Simple: dev, staging, prod
  • With context: dev-andrew, staging-feature-123

The class normalizes these formats and provides both the full stage string and the normalized SDLC value.

Examples

// In CDK App
const app = new App();
const strategy = new ContextStrategy(app);

console.log(strategy.sdlcStage); // 'dev-andrew' (full context)
console.log(strategy.sdlc); // 'dev' (normalized)
console.log(strategy.sdlcCore); // 'dev' (core environment)
// In CDK Stack
export class MyStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);

// Access via Stack's contextStrategy property
const sdlc = this.contextStrategy.sdlc;
const stage = this.contextStrategy.sdlcStage;
}
}

Constructors

Constructor

new ContextStrategy(scope): ContextStrategy

Defined in: src/contextStrategy/ContextStrategy.ts:162

Creates a new ContextStrategy instance.

Parameters
scope

Construct

CDK construct to extract context from

Returns

ContextStrategy

Throws

Error if stage context is not provided or is not a string

Remarks

The stage must be provided via CDK context:

cdk deploy --context stage=dev
cdk deploy --context stage=dev-andrew
Example
const app = new App();
const strategy = new ContextStrategy(app);

Properties

sdlc

readonly sdlc: Sdlc

Defined in: src/contextStrategy/ContextStrategy.ts:76

Normalized SDLC stage (e.g., 'dev', 'staging', 'prod').

Remarks

This is the normalized stage value extracted from sdlcStage. It removes any additional context like feature branch names.

sdlcCore

readonly sdlcCore: SdlcCore

Defined in: src/contextStrategy/ContextStrategy.ts:97

Core SDLC environment ('dev', 'staging', or 'prod').

Remarks

Maps multiple SDLC stages to core environments:

  • 'dev', 'test' → 'dev'
  • 'staging', 'qa' → 'staging'
  • 'preprod', 'prod' → 'prod'
sdlcStage

readonly sdlcStage: string

Defined in: src/contextStrategy/ContextStrategy.ts:86

Full stage string including context (e.g., 'dev-andrew', 'staging-feature-123').

Remarks

This is the raw stage value provided via CDK context with --context stage=<value>. Use this for creating unique resource names that include developer or feature context.

Methods

getSdlc()

getSdlc(): Sdlc

Defined in: src/contextStrategy/ContextStrategy.ts:184

Gets the normalized SDLC stage.

Returns

Sdlc

Normalized SDLC value (e.g., 'dev', 'staging', 'prod')

Example
const sdlc = strategy.getSdlc();  // 'dev'
getSdlcCore()

getSdlcCore(): SdlcCore

Defined in: src/contextStrategy/ContextStrategy.ts:228

Gets the core SDLC environment.

Returns

SdlcCore

Core environment ('dev', 'staging', or 'prod')

Remarks

Maps SDLC stages to core environments:

  • 'dev', 'test' → 'dev'
  • 'staging', 'qa' → 'staging'
  • 'preprod', 'prod' → 'prod'
Example
const coreEnv = strategy.getSdlcCore();  // 'dev'
getSdlcStage()

getSdlcStage(): string

Defined in: src/contextStrategy/ContextStrategy.ts:198

Gets the full stage string including context.

Returns

string

Full stage string (e.g., 'dev-andrew', 'staging-feature-123')

Example
const stage = strategy.getSdlcStage();  // 'dev-andrew'
getStageType()

getStageType(): Sdlc

Defined in: src/contextStrategy/ContextStrategy.ts:208

Gets the stage type (alias for getSdlc).

Returns

Sdlc

Normalized SDLC value

Deprecated

Use getSdlc() instead

getContextStrategySdlcCore()

static getContextStrategySdlcCore(construct): SdlcCore

Defined in: src/contextStrategy/ContextStrategy.ts:139

Gets the core SDLC environment for a construct.

Parameters
construct

Construct

CDK construct to extract context from

Returns

SdlcCore

Core SDLC environment ('dev', 'staging', or 'prod')

Remarks

This is a convenience method for getting the core environment without creating a full ContextStrategy instance.

Example
const coreEnv = ContextStrategy.getContextStrategySdlcCore(app);
console.log(coreEnv); // 'dev', 'staging', or 'prod'
validateSDLCStage()

static validateSDLCStage(stage): Sdlc

Defined in: src/contextStrategy/ContextStrategy.ts:112

Validates and normalizes a stage string.

Parameters
stage

string

Stage string to validate

Returns

Sdlc

Validated Sdlc enum value

Throws

ContextStrategyValidationError If stage is not a valid SDLC value

Example
const validStage = ContextStrategy.validateSDLCStage('dev');  // 'dev'
const invalidStage = ContextStrategy.validateSDLCStage('invalid'); // throws error

ContextStrategyValidationError

Defined in: src/contextStrategy/ContextStrategy.ts:13

Error thrown when an invalid stage is provided.

Remarks

This error is thrown when a stage value cannot be normalized or validated against the SDLC enum values.

Extends

  • Error

Constructors

Constructor

new ContextStrategyValidationError(invalidStage, allowedStages): ContextStrategyValidationError

Defined in: src/contextStrategy/ContextStrategy.ts:20

Creates a new StageValidationError.

Parameters
invalidStage

string

The invalid stage value that was provided

allowedStages

readonly string[]

List of valid stage values

Returns

ContextStrategyValidationError

Overrides

Error.constructor