1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-11-01 19:07:38 +01:00
unleash.unleash/src/lib/openapi/index.ts
andreas-unleash 32e1ad44ed
Feat/add cypress tests for project scoped stickiness (#3340)
<!-- Thanks for creating a PR! To make it easier for reviewers and
everyone else to understand what your changes relate to, please add some
relevant content to the headings below. Feel free to ignore or delete
sections that you don't think are relevant. Thank you! ❤️ -->


## About the changes
<!-- Describe the changes introduced. What are they and why are they
being introduced? Feel free to also add screenshots or steps to view the
changes if they're visual. -->

<!-- Does it close an issue? Multiple? -->
Closes #

<!-- (For internal contributors): Does it relate to an issue on public
roadmap? -->
<!--
Relates to [roadmap](https://github.com/orgs/Unleash/projects/10) item:
#
-->

### Important files
<!-- PRs can contain a lot of changes, but not all changes are equally
important. Where should a reviewer start looking to get an overview of
the changes? Are any files particularly important? -->


## Discussion points
<!-- Anything about the PR you'd like to discuss before it gets merged?
Got any questions or doubts? -->

---------

Signed-off-by: andreas-unleash <andreas@getunleash.ai>
2023-03-17 14:41:59 +02:00

357 lines
8.9 KiB
TypeScript

import { OpenAPIV3 } from 'openapi-types';
import {
adminFeaturesQuerySchema,
addonParameterSchema,
addonSchema,
addonsSchema,
addonTypeSchema,
apiTokenSchema,
apiTokensSchema,
applicationSchema,
applicationsSchema,
batchFeaturesSchema,
changePasswordSchema,
clientApplicationSchema,
clientFeatureSchema,
clientFeaturesQuerySchema,
clientFeaturesSchema,
clientMetricsSchema,
cloneFeatureSchema,
constraintSchema,
contextFieldSchema,
contextFieldsSchema,
createApiTokenSchema,
createFeatureSchema,
createFeatureStrategySchema,
createInvitedUserSchema,
createUserSchema,
dateSchema,
edgeTokenSchema,
emailSchema,
environmentProjectSchema,
environmentSchema,
environmentsProjectSchema,
environmentsSchema,
eventSchema,
eventsSchema,
exportResultSchema,
exportQuerySchema,
featureEnvironmentMetricsSchema,
featureEnvironmentSchema,
featureEventsSchema,
featureMetricsSchema,
featureSchema,
featuresSchema,
featureStrategySchema,
featureStrategySegmentSchema,
featureTagSchema,
featureTypeSchema,
featureTypesSchema,
featureUsageSchema,
featureVariantsSchema,
feedbackSchema,
groupSchema,
groupsSchema,
groupUserModelSchema,
healthCheckSchema,
healthOverviewSchema,
healthReportSchema,
idSchema,
instanceAdminStatsSchema,
legalValueSchema,
loginSchema,
meSchema,
nameSchema,
overrideSchema,
parametersSchema,
passwordSchema,
patchesSchema,
patchSchema,
patSchema,
patsSchema,
permissionSchema,
playgroundConstraintSchema,
playgroundFeatureSchema,
playgroundRequestSchema,
playgroundResponseSchema,
playgroundSegmentSchema,
playgroundStrategySchema,
profileSchema,
projectEnvironmentSchema,
projectSchema,
projectsSchema,
proxyClientSchema,
proxyFeatureSchema,
proxyFeaturesSchema,
publicSignupTokenCreateSchema,
projectStatsSchema,
publicSignupTokenSchema,
publicSignupTokensSchema,
publicSignupTokenUpdateSchema,
pushVariantsSchema,
resetPasswordSchema,
requestsPerSecondSchema,
requestsPerSecondSegmentedSchema,
roleSchema,
sdkContextSchema,
searchEventsSchema,
segmentSchema,
setStrategySortOrderSchema,
setUiConfigSchema,
sortOrderSchema,
splashSchema,
stateSchema,
strategiesSchema,
strategySchema,
tagsBulkAddSchema,
tagSchema,
tagsSchema,
tagTypeSchema,
tagTypesSchema,
tagWithVersionSchema,
tokenUserSchema,
uiConfigSchema,
updateApiTokenSchema,
updateFeatureSchema,
updateFeatureStrategySchema,
updateTagTypeSchema,
updateUserSchema,
upsertContextFieldSchema,
upsertSegmentSchema,
upsertStrategySchema,
userSchema,
usersGroupsBaseSchema,
usersSchema,
usersSearchSchema,
validateEdgeTokensSchema,
validatePasswordSchema,
validateTagTypeSchema,
variantSchema,
variantsSchema,
versionSchema,
projectOverviewSchema,
importTogglesSchema,
importTogglesValidateSchema,
importTogglesValidateItemSchema,
} from './spec';
import { IServerOption } from '../types';
import { mapValues, omitKeys } from '../util';
import { openApiTags } from './util';
import { URL } from 'url';
import apiVersion from '../util/version';
import { maintenanceSchema } from './spec/maintenance-schema';
import { bulkRegistrationSchema } from './spec/bulk-registration-schema';
import { bulkMetricsSchema } from './spec/bulk-metrics-schema';
import { clientMetricsEnvSchema } from './spec/client-metrics-env-schema';
import { updateTagsSchema } from './spec/update-tags-schema';
import { batchStaleSchema } from './spec/batch-stale-schema';
// All schemas in `openapi/spec` should be listed here.
export const schemas = {
adminFeaturesQuerySchema,
addonParameterSchema,
addonSchema,
addonsSchema,
addonTypeSchema,
apiTokenSchema,
apiTokensSchema,
applicationSchema,
applicationsSchema,
batchFeaturesSchema,
batchStaleSchema,
bulkRegistrationSchema,
bulkMetricsSchema,
changePasswordSchema,
clientApplicationSchema,
clientFeatureSchema,
clientFeaturesQuerySchema,
clientFeaturesSchema,
clientMetricsSchema,
clientMetricsEnvSchema,
cloneFeatureSchema,
constraintSchema,
contextFieldSchema,
contextFieldsSchema,
createApiTokenSchema,
createFeatureSchema,
createFeatureStrategySchema,
createInvitedUserSchema,
createUserSchema,
dateSchema,
edgeTokenSchema,
emailSchema,
environmentSchema,
environmentProjectSchema,
environmentsSchema,
environmentsProjectSchema,
eventSchema,
eventsSchema,
exportResultSchema,
exportQuerySchema,
featureEnvironmentMetricsSchema,
featureEnvironmentSchema,
featureEventsSchema,
featureMetricsSchema,
featureSchema,
featuresSchema,
featureStrategySchema,
featureStrategySegmentSchema,
featureTagSchema,
featureTypeSchema,
featureTypesSchema,
featureUsageSchema,
featureVariantsSchema,
feedbackSchema,
groupSchema,
groupsSchema,
groupUserModelSchema,
healthCheckSchema,
healthOverviewSchema,
healthReportSchema,
idSchema,
instanceAdminStatsSchema,
legalValueSchema,
loginSchema,
maintenanceSchema,
meSchema,
nameSchema,
overrideSchema,
parametersSchema,
passwordSchema,
patchesSchema,
patchSchema,
patSchema,
patsSchema,
permissionSchema,
playgroundConstraintSchema,
playgroundFeatureSchema,
playgroundRequestSchema,
playgroundResponseSchema,
playgroundSegmentSchema,
playgroundStrategySchema,
profileSchema,
projectEnvironmentSchema,
projectSchema,
projectsSchema,
proxyClientSchema,
proxyFeatureSchema,
proxyFeaturesSchema,
publicSignupTokenCreateSchema,
publicSignupTokenSchema,
publicSignupTokensSchema,
publicSignupTokenUpdateSchema,
pushVariantsSchema,
projectStatsSchema,
resetPasswordSchema,
requestsPerSecondSchema,
requestsPerSecondSegmentedSchema,
roleSchema,
sdkContextSchema,
searchEventsSchema,
segmentSchema,
setStrategySortOrderSchema,
setUiConfigSchema,
sortOrderSchema,
splashSchema,
stateSchema,
strategiesSchema,
strategySchema,
tagsBulkAddSchema,
tagSchema,
tagsSchema,
tagTypeSchema,
tagTypesSchema,
tagWithVersionSchema,
tokenUserSchema,
uiConfigSchema,
updateApiTokenSchema,
updateFeatureSchema,
updateFeatureStrategySchema,
updateTagTypeSchema,
updateUserSchema,
updateTagsSchema,
upsertContextFieldSchema,
upsertSegmentSchema,
upsertStrategySchema,
userSchema,
usersGroupsBaseSchema,
usersSchema,
usersSearchSchema,
validateEdgeTokensSchema,
validatePasswordSchema,
validateTagTypeSchema,
variantSchema,
variantsSchema,
versionSchema,
projectOverviewSchema,
importTogglesSchema,
importTogglesValidateSchema,
importTogglesValidateItemSchema,
};
// Schemas must have an $id property on the form "#/components/schemas/mySchema".
export type SchemaId = typeof schemas[keyof typeof schemas]['$id'];
// Schemas must list all their $refs in `components`, including nested schemas.
export type SchemaRef = typeof schemas[keyof typeof schemas]['components'];
// JSON schema properties that should not be included in the OpenAPI spec.
export interface JsonSchemaProps {
$id: string;
components: object;
}
// Remove JSONSchema keys that would result in an invalid OpenAPI spec.
export const removeJsonSchemaProps = <T extends JsonSchemaProps>(
schema: T,
): OpenAPIV3.SchemaObject => {
return omitKeys(schema, '$id', 'components');
};
const findRootUrl: (unleashUrl: string, baseUriPath: string) => string = (
unleashUrl: string,
baseUriPath?: string,
) => {
if (!baseUriPath) {
return unleashUrl;
}
const baseUrl = new URL(unleashUrl);
if (baseUrl.pathname.indexOf(baseUriPath) >= 0) {
return `${baseUrl.protocol}//${baseUrl.host}`;
}
return baseUrl.toString();
};
export const createOpenApiSchema = ({
unleashUrl,
baseUriPath,
}: Pick<IServerOption, 'unleashUrl' | 'baseUriPath'>): Omit<
OpenAPIV3.Document,
'paths'
> => {
const url = findRootUrl(unleashUrl, baseUriPath);
return {
openapi: '3.0.3',
servers: url ? [{ url }] : [],
info: {
title: 'Unleash API',
version: apiVersion,
},
security: [{ apiKey: [] }],
components: {
securitySchemes: {
apiKey: {
type: 'apiKey',
in: 'header',
name: 'Authorization',
},
},
schemas: mapValues(schemas, removeJsonSchemaProps),
},
tags: openApiTags,
};
};
export * from './util';
export * from './spec';