-
-
Notifications
You must be signed in to change notification settings - Fork 723
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: feature oriented architecture for feature dependencies (#4771)
- Loading branch information
Showing
8 changed files
with
158 additions
and
92 deletions.
There are no files selected for viewing
102 changes: 102 additions & 0 deletions
102
src/lib/features/dependent-features/dependent-features-controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import { Response } from 'express'; | ||
import Controller from '../../routes/controller'; | ||
import { OpenApiService } from '../../services'; | ||
import { | ||
CREATE_FEATURE, | ||
IFlagResolver, | ||
IUnleashConfig, | ||
IUnleashServices, | ||
} from '../../types'; | ||
import { Logger } from '../../logger'; | ||
import { | ||
CreateDependentFeatureSchema, | ||
createRequestSchema, | ||
emptyResponse, | ||
getStandardResponses, | ||
} from '../../openapi'; | ||
import { IAuthRequest } from '../../routes/unleash-types'; | ||
import { InvalidOperationError } from '../../error'; | ||
import { DependentFeaturesService } from './dependent-features-service'; | ||
|
||
interface FeatureParams { | ||
featureName: string; | ||
} | ||
|
||
const PATH = '/:projectId/features'; | ||
const PATH_FEATURE = `${PATH}/:featureName`; | ||
const PATH_DEPENDENCIES = `${PATH_FEATURE}/dependencies`; | ||
|
||
type DependentFeaturesServices = Pick< | ||
IUnleashServices, | ||
'dependentFeaturesService' | 'openApiService' | ||
>; | ||
|
||
export default class DependentFeaturesController extends Controller { | ||
private dependentFeaturesService: DependentFeaturesService; | ||
|
||
private openApiService: OpenApiService; | ||
|
||
private flagResolver: IFlagResolver; | ||
|
||
private readonly logger: Logger; | ||
|
||
constructor( | ||
config: IUnleashConfig, | ||
{ dependentFeaturesService, openApiService }: DependentFeaturesServices, | ||
) { | ||
super(config); | ||
this.dependentFeaturesService = dependentFeaturesService; | ||
this.openApiService = openApiService; | ||
this.flagResolver = config.flagResolver; | ||
this.logger = config.getLogger( | ||
'/dependent-features/dependent-feature-service.ts', | ||
); | ||
|
||
this.route({ | ||
method: 'post', | ||
path: PATH_DEPENDENCIES, | ||
handler: this.addFeatureDependency, | ||
permission: CREATE_FEATURE, | ||
middleware: [ | ||
openApiService.validPath({ | ||
tags: ['Features'], | ||
summary: 'Add a feature dependency.', | ||
description: | ||
'Add a dependency to a parent feature. Each environment will resolve corresponding dependency independently.', | ||
operationId: 'addFeatureDependency', | ||
requestBody: createRequestSchema( | ||
'createDependentFeatureSchema', | ||
), | ||
responses: { | ||
200: emptyResponse, | ||
...getStandardResponses(401, 403, 404), | ||
}, | ||
}), | ||
], | ||
}); | ||
} | ||
|
||
async addFeatureDependency( | ||
req: IAuthRequest<FeatureParams, any, CreateDependentFeatureSchema>, | ||
res: Response, | ||
): Promise<void> { | ||
const { featureName } = req.params; | ||
const { variants, enabled, feature } = req.body; | ||
|
||
if (this.config.flagResolver.isEnabled('dependentFeatures')) { | ||
await this.dependentFeaturesService.upsertFeatureDependency( | ||
featureName, | ||
{ | ||
variants, | ||
enabled, | ||
feature, | ||
}, | ||
); | ||
res.status(200).end(); | ||
} else { | ||
throw new InvalidOperationError( | ||
'Dependent features are not enabled', | ||
); | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/lib/features/dependent-features/dependent-features-service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { CreateDependentFeatureSchema } from '../../openapi'; | ||
|
||
export type FeatureDependency = | ||
| { | ||
parent: string; | ||
child: string; | ||
enabled: true; | ||
variants?: string[]; | ||
} | ||
| { parent: string; child: string; enabled: false }; | ||
export class DependentFeaturesService { | ||
async upsertFeatureDependency( | ||
parentFeature: string, | ||
dependentFeature: CreateDependentFeatureSchema, | ||
): Promise<void> { | ||
const { enabled, feature, variants } = dependentFeature; | ||
const featureDependency: FeatureDependency = | ||
enabled === false | ||
? { | ||
parent: parentFeature, | ||
child: feature, | ||
enabled, | ||
} | ||
: { | ||
parent: parentFeature, | ||
child: feature, | ||
enabled: true, | ||
variants, | ||
}; | ||
console.log(featureDependency); | ||
} | ||
} |
8 changes: 4 additions & 4 deletions
8
...project/features.dependencies.e2e.test.ts → ...eatures/features.dependencies.e2e.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters