From 282f452ac2c4b0205466c58c0ca95c845e36f78f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gast=C3=B3n=20Fournier?= Date: Fri, 3 Mar 2023 13:07:24 +0100 Subject: [PATCH 1/2] chore: re-use the same client schema for proxy --- src/lib/openapi/spec/client-metrics-schema.ts | 30 ++------ src/lib/openapi/spec/proxy-metrics-schema.ts | 51 ++----------- .../__snapshots__/openapi.e2e.test.ts.snap | 74 +------------------ 3 files changed, 15 insertions(+), 140 deletions(-) diff --git a/src/lib/openapi/spec/client-metrics-schema.ts b/src/lib/openapi/spec/client-metrics-schema.ts index 7d615b604e3..c194c9a4d9e 100644 --- a/src/lib/openapi/spec/client-metrics-schema.ts +++ b/src/lib/openapi/spec/client-metrics-schema.ts @@ -6,25 +6,15 @@ export const clientMetricsSchema = { type: 'object', required: ['appName', 'bucket'], properties: { - appName: { - type: 'string', - }, - instanceId: { - type: 'string', - }, - environment: { - type: 'string', - }, + appName: { type: 'string' }, + instanceId: { type: 'string' }, + environment: { type: 'string' }, bucket: { type: 'object', required: ['start', 'stop', 'toggles'], properties: { - start: { - $ref: '#/components/schemas/dateSchema', - }, - stop: { - $ref: '#/components/schemas/dateSchema', - }, + start: { $ref: '#/components/schemas/dateSchema' }, + stop: { $ref: '#/components/schemas/dateSchema' }, toggles: { type: 'object', example: { @@ -45,14 +35,8 @@ export const clientMetricsSchema = { additionalProperties: { type: 'object', properties: { - yes: { - type: 'integer', - minimum: 0, - }, - no: { - type: 'integer', - minimum: 0, - }, + yes: { type: 'integer', minimum: 0 }, + no: { type: 'integer', minimum: 0 }, variants: { type: 'object', additionalProperties: { diff --git a/src/lib/openapi/spec/proxy-metrics-schema.ts b/src/lib/openapi/spec/proxy-metrics-schema.ts index 3fd80079858..83e378d4c15 100644 --- a/src/lib/openapi/spec/proxy-metrics-schema.ts +++ b/src/lib/openapi/spec/proxy-metrics-schema.ts @@ -1,55 +1,14 @@ import { FromSchema } from 'json-schema-to-ts'; +import { clientMetricsSchema } from './client-metrics-schema'; export const proxyMetricsSchema = { $id: '#/components/schemas/proxyMetricsSchema', - type: 'object', - required: ['appName', 'instanceId', 'bucket'], - properties: { - appName: { type: 'string' }, - instanceId: { type: 'string' }, - environment: { type: 'string' }, - bucket: { - type: 'object', - required: ['start', 'stop', 'toggles'], - properties: { - start: { type: 'string', format: 'date-time' }, - stop: { type: 'string', format: 'date-time' }, - toggles: { - type: 'object', - example: { - myCoolToggle: { - yes: 25, - no: 42, - variants: { - blue: 6, - green: 15, - red: 46, - }, - }, - myOtherToggle: { - yes: 0, - no: 100, - }, - }, - additionalProperties: { - type: 'object', - properties: { - yes: { type: 'integer', minimum: 0 }, - no: { type: 'integer', minimum: 0 }, - variants: { - type: 'object', - additionalProperties: { - type: 'integer', - minimum: 0, - }, - }, - }, - }, - }, - }, + allOf: [{ $ref: '#/components/schemas/clientMetricsSchema' }], + components: { + schemas: { + clientMetricsSchema, }, }, - components: {}, } as const; export type ProxyMetricsSchema = FromSchema; diff --git a/src/test/e2e/api/openapi/__snapshots__/openapi.e2e.test.ts.snap b/src/test/e2e/api/openapi/__snapshots__/openapi.e2e.test.ts.snap index 54fad762ac4..e7e450e8857 100644 --- a/src/test/e2e/api/openapi/__snapshots__/openapi.e2e.test.ts.snap +++ b/src/test/e2e/api/openapi/__snapshots__/openapi.e2e.test.ts.snap @@ -3028,79 +3028,11 @@ Stats are divided into current and previous **windows**. "type": "object", }, "proxyMetricsSchema": { - "properties": { - "appName": { - "type": "string", - }, - "bucket": { - "properties": { - "start": { - "format": "date-time", - "type": "string", - }, - "stop": { - "format": "date-time", - "type": "string", - }, - "toggles": { - "additionalProperties": { - "properties": { - "no": { - "minimum": 0, - "type": "integer", - }, - "variants": { - "additionalProperties": { - "minimum": 0, - "type": "integer", - }, - "type": "object", - }, - "yes": { - "minimum": 0, - "type": "integer", - }, - }, - "type": "object", - }, - "example": { - "myCoolToggle": { - "no": 42, - "variants": { - "blue": 6, - "green": 15, - "red": 46, - }, - "yes": 25, - }, - "myOtherToggle": { - "no": 100, - "yes": 0, - }, - }, - "type": "object", - }, - }, - "required": [ - "start", - "stop", - "toggles", - ], - "type": "object", - }, - "environment": { - "type": "string", - }, - "instanceId": { - "type": "string", + "allOf": [ + { + "$ref": "#/components/schemas/clientMetricsSchema", }, - }, - "required": [ - "appName", - "instanceId", - "bucket", ], - "type": "object", }, "publicSignupTokenCreateSchema": { "additionalProperties": false, From 20b03d55543e3e780f445faf33bc2cc968d91969 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gast=C3=B3n=20Fournier?= Date: Fri, 3 Mar 2023 13:21:07 +0100 Subject: [PATCH 2/2] Remove proxy-metrics-schema in favor of client-metrics-schema --- src/lib/openapi/index.ts | 2 -- src/lib/openapi/spec/index.ts | 1 - src/lib/openapi/spec/proxy-metrics-schema.ts | 14 -------------- src/lib/routes/proxy-api/index.ts | 6 +++--- src/lib/services/proxy-service.ts | 4 ++-- .../openapi/__snapshots__/openapi.e2e.test.ts.snap | 7 ------- 6 files changed, 5 insertions(+), 29 deletions(-) delete mode 100644 src/lib/openapi/spec/proxy-metrics-schema.ts diff --git a/src/lib/openapi/index.ts b/src/lib/openapi/index.ts index b7c0aa221ac..b1dc56e14d2 100644 --- a/src/lib/openapi/index.ts +++ b/src/lib/openapi/index.ts @@ -82,7 +82,6 @@ import { proxyClientSchema, proxyFeatureSchema, proxyFeaturesSchema, - proxyMetricsSchema, publicSignupTokenCreateSchema, projectStatsSchema, publicSignupTokenSchema, @@ -230,7 +229,6 @@ export const schemas = { proxyClientSchema, proxyFeatureSchema, proxyFeaturesSchema, - proxyMetricsSchema, publicSignupTokenCreateSchema, publicSignupTokenSchema, publicSignupTokensSchema, diff --git a/src/lib/openapi/spec/index.ts b/src/lib/openapi/spec/index.ts index d08c8da12ff..0677a1e2a19 100644 --- a/src/lib/openapi/spec/index.ts +++ b/src/lib/openapi/spec/index.ts @@ -66,7 +66,6 @@ export * from './feature-types-schema'; export * from './feature-usage-schema'; export * from './health-report-schema'; export * from './proxy-feature-schema'; -export * from './proxy-metrics-schema'; export * from './search-events-schema'; export * from './set-ui-config-schema'; export * from './client-feature-schema'; diff --git a/src/lib/openapi/spec/proxy-metrics-schema.ts b/src/lib/openapi/spec/proxy-metrics-schema.ts deleted file mode 100644 index 83e378d4c15..00000000000 --- a/src/lib/openapi/spec/proxy-metrics-schema.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { FromSchema } from 'json-schema-to-ts'; -import { clientMetricsSchema } from './client-metrics-schema'; - -export const proxyMetricsSchema = { - $id: '#/components/schemas/proxyMetricsSchema', - allOf: [{ $ref: '#/components/schemas/clientMetricsSchema' }], - components: { - schemas: { - clientMetricsSchema, - }, - }, -} as const; - -export type ProxyMetricsSchema = FromSchema; diff --git a/src/lib/routes/proxy-api/index.ts b/src/lib/routes/proxy-api/index.ts index c5fc5325914..24e9383f1e0 100644 --- a/src/lib/routes/proxy-api/index.ts +++ b/src/lib/routes/proxy-api/index.ts @@ -9,13 +9,13 @@ import { import { Logger } from '../../logger'; import ApiUser from '../../types/api-user'; import { + ClientMetricsSchema, createRequestSchema, createResponseSchema, emptyResponse, ProxyClientSchema, proxyFeaturesSchema, ProxyFeaturesSchema, - ProxyMetricsSchema, } from '../../openapi'; import { Context } from 'unleash-client'; import { enrichContextWithIp } from '../../proxy'; @@ -95,7 +95,7 @@ export default class ProxyController extends Controller { this.services.openApiService.validPath({ tags: ['Frontend API'], operationId: 'registerFrontendMetrics', - requestBody: createRequestSchema('proxyMetricsSchema'), + requestBody: createRequestSchema('clientMetricsSchema'), responses: { 200: emptyResponse }, }), ], @@ -168,7 +168,7 @@ export default class ProxyController extends Controller { } private async registerProxyMetrics( - req: ApiUserRequest, + req: ApiUserRequest, res: Response, ) { await this.services.proxyService.registerProxyMetrics( diff --git a/src/lib/services/proxy-service.ts b/src/lib/services/proxy-service.ts index b8329903196..ad1314395bd 100644 --- a/src/lib/services/proxy-service.ts +++ b/src/lib/services/proxy-service.ts @@ -1,6 +1,6 @@ import { IUnleashConfig, IUnleashServices, IUnleashStores } from '../types'; import { Logger } from '../logger'; -import { ProxyFeatureSchema, ProxyMetricsSchema } from '../openapi'; +import { ClientMetricsSchema, ProxyFeatureSchema } from '../openapi'; import ApiUser from '../types/api-user'; import { Context, @@ -94,7 +94,7 @@ export class ProxyService { async registerProxyMetrics( token: ApiUser, - metrics: ProxyMetricsSchema, + metrics: ClientMetricsSchema, ip: string, ): Promise { ProxyService.assertExpectedTokenType(token); diff --git a/src/test/e2e/api/openapi/__snapshots__/openapi.e2e.test.ts.snap b/src/test/e2e/api/openapi/__snapshots__/openapi.e2e.test.ts.snap index e7e450e8857..70c5232ace8 100644 --- a/src/test/e2e/api/openapi/__snapshots__/openapi.e2e.test.ts.snap +++ b/src/test/e2e/api/openapi/__snapshots__/openapi.e2e.test.ts.snap @@ -3027,13 +3027,6 @@ Stats are divided into current and previous **windows**. ], "type": "object", }, - "proxyMetricsSchema": { - "allOf": [ - { - "$ref": "#/components/schemas/clientMetricsSchema", - }, - ], - }, "publicSignupTokenCreateSchema": { "additionalProperties": false, "properties": {