Skip to content

Commit

Permalink
set x-state
Browse files Browse the repository at this point in the history
  • Loading branch information
jloleysens committed Oct 17, 2024
1 parent 60be4e4 commit 58d13de
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
6 changes: 5 additions & 1 deletion packages/kbn-router-to-openapispec/src/process_router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ import {
getVersionedHeaderParam,
mergeResponseContent,
prepareRoutes,
setXState,
} from './util';
import type { OperationIdCounter } from './operation_id_counter';
import type { GenerateOpenApiDocumentOptionsFilters } from './generate_oas';
import type { CustomOperationObject } from './type';

export const processRouter = (
appRouter: Router,
Expand Down Expand Up @@ -61,7 +63,7 @@ export const processRouter = (
parameters.push(...pathObjects, ...queryObjects);
}

const operation: OpenAPIV3.OperationObject = {
const operation: CustomOperationObject = {
summary: route.options.summary ?? '',
tags: route.options.tags ? extractTags(route.options.tags) : [],
...(route.options.description ? { description: route.options.description } : {}),
Expand All @@ -81,6 +83,8 @@ export const processRouter = (
operationId: getOpId(route.path),
};

setXState(route.options.availability, operation);

const path: OpenAPIV3.PathItemObject = {
[route.method]: operation,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
extractTags,
mergeResponseContent,
getXsrfHeaderForMethod,
setXState,
} from './util';

export const processVersionedRouter = (
Expand Down Expand Up @@ -112,6 +113,9 @@ export const processVersionedRouter = (
parameters,
operationId: getOpId(route.path),
};

setXState(route.options.options?.availability, operation);

const path: OpenAPIV3.PathItemObject = {
[route.method]: operation,
};
Expand Down
5 changes: 5 additions & 0 deletions packages/kbn-router-to-openapispec/src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,8 @@ export interface OpenAPIConverter {

is(type: unknown): boolean;
}

export type CustomOperationObject = OpenAPIV3.OperationObject<{
// Custom OpenAPI from ES API spec based on @availability
'x-state'?: 'Technical Preview' | 'Beta';
}>;
16 changes: 15 additions & 1 deletion packages/kbn-router-to-openapispec/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
type RouterRoute,
type RouteValidatorConfig,
} from '@kbn/core-http-server';
import { KnownParameters } from './type';
import { CustomOperationObject, KnownParameters } from './type';
import type { GenerateOpenApiDocumentOptionsFilters } from './generate_oas';

const tagPrefix = 'oas-tag:';
Expand Down Expand Up @@ -165,3 +165,17 @@ export const getXsrfHeaderForMethod = (
},
];
};

export function setXState(
availability: RouteConfigOptions<RouteMethod>['availability'],
operation: CustomOperationObject
): void {
if (availability) {
if (availability.stability === 'experimental') {
operation['x-state'] = 'Technical Preview';
}
if (availability.stability === 'beta') {
operation['x-state'] = 'Beta';
}
}
}

0 comments on commit 58d13de

Please sign in to comment.