Skip to content

Commit

Permalink
fix: add open-api spec for json apis
Browse files Browse the repository at this point in the history
  • Loading branch information
ivarconr committed Oct 18, 2022
1 parent ed38cc3 commit e4958fc
Show file tree
Hide file tree
Showing 7 changed files with 187 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/lib/openapi/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ import { validateTagTypeSchema } from './spec/validate-tag-type-schema';
import { variantSchema } from './spec/variant-schema';
import { variantsSchema } from './spec/variants-schema';
import { versionSchema } from './spec/version-schema';
import { instanceAdminStatsSchema } from './spec/instance-admin-stats-schema';
import apiVersion from '../util/version';

// All schemas in `openapi/spec` should be listed here.
Expand Down Expand Up @@ -176,6 +177,7 @@ export const schemas = {
healthOverviewSchema,
healthReportSchema,
idSchema,
instanceAdminStatsSchema,
legalValueSchema,
loginSchema,
meSchema,
Expand Down
13 changes: 13 additions & 0 deletions src/lib/openapi/spec/instance-admin-stats-schema.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { validateSchema } from '../validate';
import { InstanceAdminStatsSchema } from './instance-admin-stats-schema';

test('instanceAdminStatsSchema', () => {
const data: InstanceAdminStatsSchema = {
instanceId: '123',
users: 0,
};

expect(
validateSchema('#/components/schemas/instanceAdminStatsSchema', data),
).toBeUndefined();
});
62 changes: 62 additions & 0 deletions src/lib/openapi/spec/instance-admin-stats-schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { FromSchema } from 'json-schema-to-ts';

export const instanceAdminStatsSchema = {
$id: '#/components/schemas/instanceAdminStatsSchema',
type: 'object',
additionalProperties: false,
required: ['instanceId'],
properties: {
instanceId: {
type: 'string',
},
timestamp: {
type: 'string',
format: 'date-time',
nullable: true,
},
versionOSS: {
type: 'string',
},
versionEnterprise: {
type: 'string',
},
users: {
type: 'number',
},
featureToggles: {
type: 'number',
},
projects: {
type: 'number',
},
contextFields: {
type: 'number',
},
roles: {
type: 'number',
},
groups: {
type: 'number',
},
environments: {
type: 'number',
},
segments: {
type: 'number',
},
strategies: {
type: 'number',
},
SAMLenabled: {
type: 'number',
},
OIDCenabled: {
type: 'number',
},
},
components: {},
} as const;

export type InstanceAdminStatsSchema = FromSchema<
typeof instanceAdminStatsSchema
>;
5 changes: 5 additions & 0 deletions src/lib/openapi/util/openapi-tags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ const OPENAPI_TAGS = [
description:
'[Import and export](https://docs.getunleash.io/deploy/import_export) the state of your Unleash instance.',
},
{
name: 'Instance Admin',
description:
'Instance admin endpoints used to manage the Unleash instance itself.',
},
{
name: 'Metrics',
description: 'Register, read, or delete metrics recorded by Unleash.',
Expand Down
30 changes: 27 additions & 3 deletions src/lib/routes/admin-api/instance-admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,52 @@ import Controller from '../controller';
import { NONE } from '../../types/permissions';
import { UiConfigSchema } from '../../openapi/spec/ui-config-schema';
import { InstanceStatsService } from '../../services/instance-stats-service';
import { OpenApiService } from '../../services/openapi-service';
import { createResponseSchema } from '../../openapi/util/create-response-schema';

class InstanceAdminController extends Controller {
private instanceStatsService: InstanceStatsService;

private openApiService: OpenApiService;

constructor(
config: IUnleashConfig,
{
instanceStatsService,
}: Pick<IUnleashServices, 'instanceStatsService'>,
openApiService,
}: Pick<IUnleashServices, 'instanceStatsService' | 'openApiService'>,
) {
super(config);

this.openApiService = openApiService;
this.instanceStatsService = instanceStatsService;

this.route({
method: 'get',
path: '/statistics/csv',
handler: this.getStatistics,
handler: this.getStatisticsCSV,
permission: NONE,
});

this.route({
method: 'get',
path: '/statistics',
handler: this.getStatisticsCSV,
permission: NONE,
middleware: [
openApiService.validPath({
tags: ['Instance Admin'],
operationId: 'getInstanceAdminStats',
responses: {
200: createResponseSchema('instanceAdminStatsSchema'),
},
deprecated: true,
}),
],
});
}

async getStatistics(
async getStatisticsCSV(
req: AuthedRequest,
res: Response<UiConfigSchema>,
): Promise<void> {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/services/instance-stats-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import { ISettingStore } from '../types/stores/settings-store';

// eslint-disable-next-line @typescript-eslint/no-unused-vars
interface InstanceStats {
timestamp: Date;
instanceId: string;
timestamp: Date;
versionOSS: string;
versionEnterprise?: string;
users: number;
Expand Down
77 changes: 77 additions & 0 deletions src/test/e2e/api/openapi/__snapshots__/openapi.e2e.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1560,6 +1560,62 @@ exports[`should serve the OpenAPI spec 1`] = `
],
"type": "object",
},
"instanceAdminStatsSchema": {
"additionalProperties": false,
"properties": {
"OIDCenabled": {
"type": "number",
},
"SAMLenabled": {
"type": "number",
},
"contextFields": {
"type": "number",
},
"environments": {
"type": "number",
},
"featureToggles": {
"type": "number",
},
"groups": {
"type": "number",
},
"instanceId": {
"type": "string",
},
"projects": {
"type": "number",
},
"roles": {
"type": "number",
},
"segments": {
"type": "number",
},
"strategies": {
"type": "number",
},
"timestamp": {
"format": "date-time",
"nullable": true,
"type": "string",
},
"users": {
"type": "number",
},
"versionEnterprise": {
"type": "string",
},
"versionOSS": {
"type": "string",
},
},
"required": [
"instanceId",
],
"type": "object",
},
"legalValueSchema": {
"additionalProperties": false,
"properties": {
Expand Down Expand Up @@ -4561,6 +4617,27 @@ If the provided project does not exist, the list of events will be empty.",
],
},
},
"/api/admin/instance-admin/statistics": {
"get": {
"deprecated": true,
"operationId": "getInstanceAdminStats",
"responses": {
"200": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/instanceAdminStatsSchema",
},
},
},
"description": "instanceAdminStatsSchema",
},
},
"tags": [
"Features",
],
},
},
"/api/admin/invite-link/tokens": {
"get": {
"operationId": "getAllPublicSignupTokens",
Expand Down

0 comments on commit e4958fc

Please sign in to comment.