1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-10-13 11:17:26 +02:00
unleash.unleash/frontend/src/component/playground/Playground/playground.utils.ts
andreas-unleash 8e3863a27e
feat: add input for api token in playground (#5130)
Adds a token input in playground.

In the case of tokens that span multiple projects ie
`[]:development.etc` it will look into the token definitions to find the
token and get the projects

Otherwise it will try to parse the project and environment from the
token itself (without checking for it being a valid token)

Also, does not support admin tokens `*:*.etc`

Closes #
[1-1507](https://linear.app/unleash/issue/1-1507/create-a-token-input-in-the-playground-form)
<img width="1661" alt="Screenshot 2023-10-23 at 16 38 11"
src="https://github.com/Unleash/unleash/assets/104830839/f2d4fb6e-962f-4cc1-b5e4-817fd2de18ff">
<img width="1673" alt="Screenshot 2023-10-23 at 16 38 33"
src="https://github.com/Unleash/unleash/assets/104830839/27645955-d651-41e6-be02-4381c4f00551">

<img width="1377" alt="Screenshot 2023-10-25 at 17 06 43"
src="https://github.com/Unleash/unleash/assets/104830839/c7638366-3634-4521-af65-4f68a4f3b330">

---------

Signed-off-by: andreas-unleash <andreas@getunleash.ai>
2023-10-25 17:55:33 +03:00

145 lines
3.4 KiB
TypeScript

import {
PlaygroundResponseSchema,
AdvancedPlaygroundResponseSchema,
} from 'openapi';
import { IEnvironment } from 'interfaces/environments';
import { ensureArray } from '@server/util/ensureArray';
export const resolveProjects = (
projects: string[] | string,
): string[] | '*' => {
if (
!projects ||
projects.length === 0 ||
(projects.length === 1 && projects[0] === '*')
) {
return '*';
}
return ensureArray(projects);
};
export const resolveEnvironments = (
envrironments: string[] | string,
): string[] => {
return ensureArray(envrironments);
};
export const resolveDefaultEnvironment = (
environmentOptions: IEnvironment[],
) => {
const options = getEnvironmentOptions(environmentOptions);
if (options.length > 0) {
return options[0];
}
return '';
};
export const getEnvironmentOptions = (environments: IEnvironment[]) => {
return environments
.filter(({ enabled }) => Boolean(enabled))
.sort((a, b) => a.sortOrder - b.sortOrder)
.map(({ name }) => name);
};
export const resolveResultsWidth = (
matches: boolean,
results:
| PlaygroundResponseSchema
| AdvancedPlaygroundResponseSchema
| undefined,
) => {
if (matches) {
return '100%';
}
if (results && !matches) {
return '60%';
}
return '50%';
};
export const isStringOrStringArray = (
value: unknown,
): value is string | string[] => {
if (typeof value === 'string') {
return true;
}
if (Array.isArray(value)) {
return value.every((item) => typeof item === 'string');
}
return false;
};
type InputContextProperties = {
appName?: string;
environment?: string;
userId?: string;
sessionId?: string;
remoteAddress?: string;
currentTime?: string;
properties?: { [key: string]: any };
[key: string]: any;
};
export type NormalizedContextProperties = Omit<
InputContextProperties,
'properties'
> & {
properties?: { [key: string]: any };
};
export const normalizeCustomContextProperties = (
input: InputContextProperties,
): NormalizedContextProperties => {
const standardProps = new Set([
'appName',
'environment',
'userId',
'sessionId',
'remoteAddress',
'currentTime',
'properties',
]);
const output: InputContextProperties = { ...input };
let hasCustomProperties = false;
for (const key in input) {
if (!standardProps.has(key)) {
if (!output.properties) {
output.properties = {};
}
output.properties[key] = input[key];
delete output[key];
hasCustomProperties = true;
}
}
if (!hasCustomProperties && !input.properties) {
delete output.properties;
}
return output;
};
export const validateTokenFormat = (token: string): void => {
const [projectEnvAccess] = token.split('.');
const [project, environment] = projectEnvAccess.split(':');
if (!project || !environment) {
throw new Error('Invalid token format');
}
if (environment === '*') {
throw new Error('Admin tokens are not supported in the playground');
}
};
export const extractProjectEnvironmentFromToken = (token: string) => {
const [projectEnvAccess] = token.split('.');
return projectEnvAccess.split(':');
};