mirror of
https://github.com/Unleash/unleash.git
synced 2025-08-18 13:48:58 +02:00
feat: add warnings to the api response
This commit is contained in:
parent
0fb3f9460d
commit
6363a3265f
@ -76,3 +76,30 @@ test('returns the input context exactly as it came in, even if invalid values ha
|
|||||||
|
|
||||||
expect(body.input.context).toMatchObject(inputContext);
|
expect(body.input.context).toMatchObject(inputContext);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('adds all removed top-level context properties to the list of warnings', async () => {
|
||||||
|
const invalidData = {
|
||||||
|
invalid1: {},
|
||||||
|
invalid2: {},
|
||||||
|
};
|
||||||
|
|
||||||
|
const inputContext = {
|
||||||
|
...invalidData,
|
||||||
|
appName: 'test',
|
||||||
|
};
|
||||||
|
|
||||||
|
const { body } = await app.request
|
||||||
|
.post('/api/admin/playground/advanced')
|
||||||
|
.send({
|
||||||
|
context: inputContext,
|
||||||
|
environments: ['production'],
|
||||||
|
projects: '*',
|
||||||
|
})
|
||||||
|
.expect(200);
|
||||||
|
|
||||||
|
const warned = body.warnings.invalidContextProperties;
|
||||||
|
const invalidKeys = Object.keys(invalidData);
|
||||||
|
|
||||||
|
expect(warned).toEqual(expect.arrayContaining(invalidKeys));
|
||||||
|
expect(invalidKeys).toEqual(expect.arrayContaining(warned));
|
||||||
|
});
|
||||||
|
@ -103,7 +103,10 @@ export class PlaygroundService {
|
|||||||
context: SdkContextSchema,
|
context: SdkContextSchema,
|
||||||
limit: number,
|
limit: number,
|
||||||
userId: number,
|
userId: number,
|
||||||
): Promise<AdvancedPlaygroundFeatureEvaluationResult[]> {
|
): Promise<{
|
||||||
|
result: AdvancedPlaygroundFeatureEvaluationResult[];
|
||||||
|
invalidContextProperties: string[];
|
||||||
|
}> {
|
||||||
const segments = await this.segmentReadModel.getActive();
|
const segments = await this.segmentReadModel.getActive();
|
||||||
|
|
||||||
let filteredProjects: typeof projects = projects;
|
let filteredProjects: typeof projects = projects;
|
||||||
@ -126,7 +129,8 @@ export class PlaygroundService {
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
const { context: cleanedContext } = cleanContext(context);
|
const { context: cleanedContext, removedProperties } =
|
||||||
|
cleanContext(context);
|
||||||
const contexts = generateObjectCombinations(cleanedContext);
|
const contexts = generateObjectCombinations(cleanedContext);
|
||||||
|
|
||||||
validateQueryComplexity(
|
validateQueryComplexity(
|
||||||
@ -152,7 +156,7 @@ export class PlaygroundService {
|
|||||||
);
|
);
|
||||||
const items = results.flat();
|
const items = results.flat();
|
||||||
const itemsByName = groupBy(items, (item) => item.name);
|
const itemsByName = groupBy(items, (item) => item.name);
|
||||||
return Object.values(itemsByName).map((entries) => {
|
const result = Object.values(itemsByName).map((entries) => {
|
||||||
const groupedEnvironments = groupBy(
|
const groupedEnvironments = groupBy(
|
||||||
entries,
|
entries,
|
||||||
(entry) => entry.environment,
|
(entry) => entry.environment,
|
||||||
@ -163,6 +167,11 @@ export class PlaygroundService {
|
|||||||
environments: groupedEnvironments,
|
environments: groupedEnvironments,
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
result,
|
||||||
|
invalidContextProperties: removedProperties,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private async evaluate({
|
private async evaluate({
|
||||||
|
@ -40,6 +40,7 @@ const addStrategyEditLink = (
|
|||||||
export const advancedPlaygroundViewModel = (
|
export const advancedPlaygroundViewModel = (
|
||||||
input: AdvancedPlaygroundRequestSchema,
|
input: AdvancedPlaygroundRequestSchema,
|
||||||
playgroundResult: AdvancedPlaygroundFeatureEvaluationResult[],
|
playgroundResult: AdvancedPlaygroundFeatureEvaluationResult[],
|
||||||
|
invalidContextProperties: string[],
|
||||||
): AdvancedPlaygroundResponseSchema => {
|
): AdvancedPlaygroundResponseSchema => {
|
||||||
const features = playgroundResult.map(({ environments, ...rest }) => {
|
const features = playgroundResult.map(({ environments, ...rest }) => {
|
||||||
const transformedEnvironments = Object.entries(environments).map(
|
const transformedEnvironments = Object.entries(environments).map(
|
||||||
@ -79,6 +80,10 @@ export const advancedPlaygroundViewModel = (
|
|||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (invalidContextProperties.length > 0) {
|
||||||
|
return { features, input, warnings: { invalidContextProperties } };
|
||||||
|
}
|
||||||
|
|
||||||
return { features, input };
|
return { features, input };
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -125,7 +125,8 @@ export default class PlaygroundController extends Controller {
|
|||||||
? Number.parseInt(payload?.value)
|
? Number.parseInt(payload?.value)
|
||||||
: 15000;
|
: 15000;
|
||||||
|
|
||||||
const result = await this.playgroundService.evaluateAdvancedQuery(
|
const { result, invalidContextProperties } =
|
||||||
|
await this.playgroundService.evaluateAdvancedQuery(
|
||||||
req.body.projects || '*',
|
req.body.projects || '*',
|
||||||
req.body.environments,
|
req.body.environments,
|
||||||
req.body.context,
|
req.body.context,
|
||||||
@ -134,7 +135,11 @@ export default class PlaygroundController extends Controller {
|
|||||||
);
|
);
|
||||||
|
|
||||||
const response: AdvancedPlaygroundResponseSchema =
|
const response: AdvancedPlaygroundResponseSchema =
|
||||||
advancedPlaygroundViewModel(req.body, result);
|
advancedPlaygroundViewModel(
|
||||||
|
req.body,
|
||||||
|
result,
|
||||||
|
invalidContextProperties,
|
||||||
|
);
|
||||||
|
|
||||||
res.json(response);
|
res.json(response);
|
||||||
}
|
}
|
||||||
|
@ -30,6 +30,20 @@ export const advancedPlaygroundResponseSchema = {
|
|||||||
$ref: advancedPlaygroundFeatureSchema.$id,
|
$ref: advancedPlaygroundFeatureSchema.$id,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
warnings: {
|
||||||
|
type: 'object',
|
||||||
|
description: 'Warnings that occurred during evaluation.',
|
||||||
|
properties: {
|
||||||
|
invalidContextProperties: {
|
||||||
|
type: 'array',
|
||||||
|
description:
|
||||||
|
'A list of top-level context properties that were provided as input that are not valid due to being the wrong type.',
|
||||||
|
items: {
|
||||||
|
type: 'string',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
components: {
|
components: {
|
||||||
schemas: {
|
schemas: {
|
||||||
|
Loading…
Reference in New Issue
Block a user