Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
229 additions
and
0 deletions.
There are no files selected for viewing
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,59 @@ | ||
import { Request, Response } from 'express'; | ||
import Controller from '../controller'; | ||
import { NONE } from '../../types/permissions'; | ||
import { IUnleashConfig } from '../../types/option'; | ||
import { IUnleashServices } from '../../types/services'; | ||
import { Logger } from '../../logger'; | ||
import { OpenApiService } from '../../services/openapi-service'; | ||
import ExportImportService, { | ||
IExportQuery, | ||
} from 'lib/services/export-import-service'; | ||
|
||
class ExportImportController extends Controller { | ||
private logger: Logger; | ||
|
||
private exportImportService: ExportImportService; | ||
|
||
private openApiService: OpenApiService; | ||
|
||
constructor( | ||
config: IUnleashConfig, | ||
{ | ||
exportImportService, | ||
openApiService, | ||
}: Pick<IUnleashServices, 'exportImportService' | 'openApiService'>, | ||
) { | ||
super(config); | ||
this.logger = config.getLogger('/admin-api/export-import.ts'); | ||
this.exportImportService = exportImportService; | ||
this.openApiService = openApiService; | ||
this.route({ | ||
method: 'post', | ||
path: '/export', | ||
permission: NONE, | ||
handler: this.export, | ||
// middleware: [ | ||
// this.openApiService.validPath({ | ||
// tags: ['Import/Export'], | ||
// operationId: 'export', | ||
// responses: { | ||
// 200: createResponseSchema('stateSchema'), | ||
// }, | ||
// parameters: | ||
// exportQueryParameters as unknown as OpenAPIV3.ParameterObject[], | ||
// }), | ||
// ], | ||
}); | ||
} | ||
|
||
async export( | ||
req: Request<unknown, unknown, IExportQuery, unknown>, | ||
res: Response, | ||
): Promise<void> { | ||
const query = req.body; | ||
const data = await this.exportImportService.export(query); | ||
|
||
res.json(data); | ||
} | ||
} | ||
export default ExportImportController; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { IUnleashConfig } from '../types/option'; | ||
import { FeatureToggle, ITag } from '../types/model'; | ||
import { Logger } from '../logger'; | ||
import { IFeatureTagStore } from '../types/stores/feature-tag-store'; | ||
import { IProjectStore } from '../types/stores/project-store'; | ||
import { ITagTypeStore } from '../types/stores/tag-type-store'; | ||
import { ITagStore } from '../types/stores/tag-store'; | ||
import { IEventStore } from '../types/stores/event-store'; | ||
import { IStrategyStore } from '../types/stores/strategy-store'; | ||
import { IFeatureToggleStore } from '../types/stores/feature-toggle-store'; | ||
import { IFeatureStrategiesStore } from '../types/stores/feature-strategies-store'; | ||
import { IEnvironmentStore } from '../types/stores/environment-store'; | ||
import { IFeatureEnvironmentStore } from '../types/stores/feature-environment-store'; | ||
import { IUnleashStores } from '../types/stores'; | ||
import { ISegmentStore } from '../types/stores/segment-store'; | ||
import { IFlagResolver } from 'lib/types'; | ||
import { IContextFieldDto } from '../types/stores/context-field-store'; | ||
|
||
export interface IExportQuery { | ||
features: string[]; | ||
environment: string; | ||
} | ||
|
||
export interface IExportData { | ||
features: FeatureToggle[]; | ||
tags?: ITag[]; | ||
contextFields?: IContextFieldDto[]; | ||
} | ||
|
||
export default class ExportImportService { | ||
private logger: Logger; | ||
|
||
private toggleStore: IFeatureToggleStore; | ||
|
||
private featureStrategiesStore: IFeatureStrategiesStore; | ||
|
||
private strategyStore: IStrategyStore; | ||
|
||
private eventStore: IEventStore; | ||
|
||
private tagStore: ITagStore; | ||
|
||
private tagTypeStore: ITagTypeStore; | ||
|
||
private projectStore: IProjectStore; | ||
|
||
private featureEnvironmentStore: IFeatureEnvironmentStore; | ||
|
||
private featureTagStore: IFeatureTagStore; | ||
|
||
private environmentStore: IEnvironmentStore; | ||
|
||
private segmentStore: ISegmentStore; | ||
|
||
private flagResolver: IFlagResolver; | ||
|
||
constructor( | ||
stores: IUnleashStores, | ||
{ | ||
getLogger, | ||
flagResolver, | ||
}: Pick<IUnleashConfig, 'getLogger' | 'flagResolver'>, | ||
) { | ||
this.eventStore = stores.eventStore; | ||
this.toggleStore = stores.featureToggleStore; | ||
this.strategyStore = stores.strategyStore; | ||
this.tagStore = stores.tagStore; | ||
this.featureStrategiesStore = stores.featureStrategiesStore; | ||
this.featureEnvironmentStore = stores.featureEnvironmentStore; | ||
this.tagTypeStore = stores.tagTypeStore; | ||
this.projectStore = stores.projectStore; | ||
this.featureTagStore = stores.featureTagStore; | ||
this.environmentStore = stores.environmentStore; | ||
this.segmentStore = stores.segmentStore; | ||
this.flagResolver = flagResolver; | ||
this.logger = getLogger('services/state-service.js'); | ||
} | ||
|
||
async export(query: IExportQuery): Promise<IExportData> { | ||
const features = ( | ||
await this.toggleStore.getAll({ archived: false }) | ||
).filter((toggle) => query.features.includes(toggle.name)); | ||
return { features: features }; | ||
} | ||
} | ||
|
||
module.exports = ExportImportService; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { IUnleashTest, setupApp } from '../../helpers/test-helper'; | ||
import dbInit, { ITestDb } from '../../helpers/database-init'; | ||
import getLogger from '../../../fixtures/no-logger'; | ||
import { IEventStore } from 'lib/types/stores/event-store'; | ||
import { FeatureToggleDTO, IStrategyConfig } from 'lib/types'; | ||
import { DEFAULT_ENV } from '../../../../lib/util'; | ||
|
||
let app: IUnleashTest; | ||
let db: ITestDb; | ||
let eventStore: IEventStore; | ||
|
||
const createToggle = async ( | ||
toggle: FeatureToggleDTO, | ||
strategy?: Omit<IStrategyConfig, 'id'>, | ||
projectId: string = 'default', | ||
username: string = 'test', | ||
) => { | ||
await app.services.featureToggleServiceV2.createFeatureToggle( | ||
projectId, | ||
toggle, | ||
username, | ||
); | ||
if (strategy) { | ||
await app.services.featureToggleServiceV2.createStrategy( | ||
strategy, | ||
{ projectId, featureName: toggle.name, environment: DEFAULT_ENV }, | ||
username, | ||
); | ||
} | ||
}; | ||
|
||
beforeAll(async () => { | ||
db = await dbInit('export_import_api_serial', getLogger); | ||
app = await setupApp(db.stores); | ||
eventStore = db.stores.eventStore; | ||
}); | ||
|
||
beforeEach(async () => { | ||
await eventStore.deleteAll(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await app.destroy(); | ||
await db.destroy(); | ||
}); | ||
|
||
afterEach(() => { | ||
db.stores.featureToggleStore.deleteAll(); | ||
}); | ||
|
||
test('exports features', async () => { | ||
await createToggle({ | ||
name: 'first_feature', | ||
description: 'the #1 feature', | ||
}); | ||
const { body } = await app.request | ||
.post('/api/admin/features-batch/export') | ||
.send({ | ||
features: ['first_feature'], | ||
environment: 'default', | ||
}) | ||
.set('Content-Type', 'application/json') | ||
.expect(200); | ||
|
||
expect(body).toMatchObject({ | ||
features: [ | ||
{ | ||
name: 'first_feature', | ||
}, | ||
], | ||
}); | ||
}); |