import { OpenAPIV3 } from 'openapi-types'; import { adminFeaturesQuerySchema, addonParameterSchema, addonSchema, addonsSchema, addonTypeSchema, apiTokenSchema, apiTokensSchema, applicationSchema, applicationsSchema, 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, proxyMetricsSchema, publicSignupTokenCreateSchema, publicSignupTokenSchema, publicSignupTokensSchema, publicSignupTokenUpdateSchema, pushVariantsSchema, resetPasswordSchema, requestsPerSecondSchema, requestsPerSecondSegmentedSchema, roleSchema, sdkContextSchema, searchEventsSchema, segmentSchema, setStrategySortOrderSchema, setUiConfigSchema, sortOrderSchema, splashSchema, stateSchema, strategiesSchema, strategySchema, tagSchema, tagsSchema, tagTypeSchema, tagTypesSchema, tagWithVersionSchema, tokenUserSchema, uiConfigSchema, updateApiTokenSchema, updateFeatureSchema, updateFeatureStrategySchema, updateTagTypeSchema, updateUserSchema, upsertContextFieldSchema, upsertStrategySchema, userSchema, usersGroupsBaseSchema, usersSchema, usersSearchSchema, validateEdgeTokensSchema, validatePasswordSchema, validateTagTypeSchema, variantSchema, variantsSchema, versionSchema, } 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'; // All schemas in `openapi/spec` should be listed here. export const schemas = { adminFeaturesQuerySchema, addonParameterSchema, addonSchema, addonsSchema, addonTypeSchema, apiTokenSchema, apiTokensSchema, applicationSchema, applicationsSchema, changePasswordSchema, clientApplicationSchema, clientFeatureSchema, clientFeaturesQuerySchema, clientFeaturesSchema, clientMetricsSchema, 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, proxyMetricsSchema, publicSignupTokenCreateSchema, publicSignupTokenSchema, publicSignupTokensSchema, publicSignupTokenUpdateSchema, pushVariantsSchema, resetPasswordSchema, requestsPerSecondSchema, requestsPerSecondSegmentedSchema, roleSchema, sdkContextSchema, searchEventsSchema, segmentSchema, setStrategySortOrderSchema, setUiConfigSchema, sortOrderSchema, splashSchema, stateSchema, strategiesSchema, strategySchema, tagSchema, tagsSchema, tagTypeSchema, tagTypesSchema, tagWithVersionSchema, tokenUserSchema, uiConfigSchema, updateApiTokenSchema, updateFeatureSchema, updateFeatureStrategySchema, updateTagTypeSchema, updateUserSchema, upsertContextFieldSchema, upsertStrategySchema, userSchema, usersGroupsBaseSchema, usersSchema, usersSearchSchema, validateEdgeTokensSchema, validatePasswordSchema, validateTagTypeSchema, variantSchema, variantsSchema, versionSchema, }; // 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 = ( 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): 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';