1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-07-31 13:47:02 +02:00

Wip: fixup param types

This commit is contained in:
Thomas Heartman 2022-07-22 11:03:27 +02:00
parent e6cbe0b9d6
commit fac6f24e31
4 changed files with 107 additions and 8 deletions

View File

@ -30,7 +30,6 @@ import { environmentSchema } from './spec/environment-schema';
import { environmentsSchema } from './spec/environments-schema';
import { eventSchema } from './spec/event-schema';
import { eventsSchema } from './spec/events-schema';
import { exportParametersSchema } from './spec/export-parameters-schema';
import { featureEnvironmentMetricsSchema } from './spec/feature-environment-metrics-schema';
import { featureEnvironmentSchema } from './spec/feature-environment-schema';
import { featureEventsSchema } from './spec/feature-events-schema';
@ -134,7 +133,6 @@ export const schemas = {
environmentsSchema,
eventSchema,
eventsSchema,
exportParametersSchema,
featureEnvironmentMetricsSchema,
featureEnvironmentSchema,
featureEventsSchema,

View File

@ -1,8 +1,9 @@
import { FromSchema } from 'json-schema-to-ts';
import { OpenAPIV3 } from 'openapi-types';
import { createQueryParameters } from '../util/query-parameters';
import { Parameters } from '../util/query-parameters';
const exportParameters: Parameters = {
const exportParameters = {
format: {
type: 'string',
enum: ['json', 'yaml'],
@ -44,10 +45,109 @@ const exportParameters: Parameters = {
description:
'Whether environments should be included in the exported data.',
},
} as const;
type ExportParams = typeof exportParameters;
type Mutable = {
[Property in keyof ExportParams]: { type: ExportParams[Property]['type'] };
};
const p = {
$id: 'th',
type: 'object',
properties: createQueryParameters(exportParameters).reduce(
(acc, next: OpenAPIV3.ParameterObject) => ({
...acc,
[next.name]: {
type: (next.schema as OpenAPIV3.SchemaObject).type,
},
}),
{} as Partial<Mutable>,
) as Mutable,
} as const;
export type ExpType = FromSchema<typeof p>;
export const exportQueryParameters = createQueryParameters(exportParameters);
const s = {
$id: '#/components/schemas/exportParametersSchema2',
parameters: [
{
name: 'format',
description:
'Desired export format. Must be either `json` or `yaml`.',
schema: {
type: 'string',
enum: ['json', 'yaml'],
default: 'json',
},
in: 'query',
},
{
name: 'download',
description:
'Whether exported data should be downloaded as a file.',
schema: {
type: 'boolean',
default: false,
},
in: 'query',
},
{
name: 'strategies',
description:
'Whether strategies should be included in the exported data.',
schema: {
type: 'boolean',
default: true,
},
in: 'query',
},
{
name: 'featureToggles',
description:
'Whether feature toggles should be included in the exported data.',
schema: {
type: 'boolean',
default: true,
},
in: 'query',
},
{
name: 'projects',
description:
'Whether projects should be included in the exported data.',
schema: {
type: 'boolean',
default: true,
},
in: 'query',
},
{
name: 'tags',
description:
'Whether tag types, tags, and feature_tags should be included in the exported data.',
schema: {
type: 'boolean',
default: true,
},
in: 'query',
},
{
name: 'environments',
description:
'Whether environments should be included in the exported data.',
schema: {
type: 'boolean',
default: true,
},
in: 'query',
},
],
} as const;
export const exportParametersSchema = {
$id: '#/components/schemas/exportParametersSchema',
type: 'object',

View File

@ -2,14 +2,14 @@ import { OpenAPIV3 } from 'openapi-types';
export type ParameterType = OpenAPIV3.NonArraySchemaObjectType;
export type ParameterDetails<U> = {
export type ParameterDetails<U> = Readonly<{
description: string;
type: ParameterType;
required?: boolean;
default?: U;
enum?: [U, ...U[]];
enum?: Readonly<[U, ...U[]]>;
example?: U;
};
}>;
export type Parameters = {
[parameterName: string]: ParameterDetails<unknown>;
@ -25,7 +25,7 @@ export const toParamObject = (
required: details.required,
schema: {
type: details.type,
enum: details.enum,
enum: details.enum as unknown as any[],
default: details.default,
},
in: 'query',

View File

@ -17,6 +17,7 @@ import { createResponseSchema } from '../../openapi/util/create-response-schema'
import {
exportQueryParameters,
ExportParametersSchema,
ExpType,
} from '../../openapi/spec/export-parameters-schema';
import { emptyResponse } from '../../openapi/util/standard-responses';
@ -113,7 +114,7 @@ class StateController extends Controller {
}
async export(
req: Request<unknown, unknown, unknown, ExportParametersSchema>,
req: Request<unknown, unknown, unknown, ExpType>,
res: Response,
): Promise<void> {
const { format } = req.query;