From c771941cf143bda663e11c114bfc26661a1855a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivar=20Conradi=20=C3=98sthus?= Date: Thu, 30 May 2024 09:41:17 +0200 Subject: [PATCH] fix: allow clients to send metrics when using all path (#176) --- src/test/__snapshots__/openapi.test.ts.snap | 73 +++++++++++++++++++++ src/test/unleash-proxy.test.ts | 24 +++++++ src/unleash-proxy.ts | 13 ++++ 3 files changed, 110 insertions(+) diff --git a/src/test/__snapshots__/openapi.test.ts.snap b/src/test/__snapshots__/openapi.test.ts.snap index dddfd3e..5928e6b 100644 --- a/src/test/__snapshots__/openapi.test.ts.snap +++ b/src/test/__snapshots__/openapi.test.ts.snap @@ -1093,6 +1093,79 @@ If you don't provide the \`toggles\` property, then this operation functions exa ], }, }, + "/proxy/all/client/metrics": Object { + "post": Object { + "description": "This endpoint lets you register usage metrics with Unleash. Accepts either one of the proxy's configured \`serverSideTokens\` or one of its \`clientKeys\` for authorization.", + "requestBody": Object { + "content": Object { + "application/json": Object { + "schema": Object { + "$ref": "#/components/schemas/registerMetricsSchema", + }, + }, + }, + }, + "responses": Object { + "200": Object { + "content": Object { + "text/plain": Object { + "schema": Object { + "example": "ok", + "type": "string", + }, + }, + }, + "description": "The request was successful.", + }, + "400": Object { + "content": Object { + "application/json": Object { + "schema": Object { + "example": Object { + "error": "Request validation failed", + "validation": Array [ + Object { + "dataPath": ".body", + "keyword": "required", + "message": "should have required property 'appName'", + "params": Object { + "missingProperty": "appName", + }, + "schemaPath": "#/components/schemas/registerMetricsSchema/required", + }, + ], + }, + "properties": Object { + "error": Object { + "type": "string", + }, + "validation": Object { + "items": Object { + "type": "object", + }, + "type": "array", + }, + }, + "required": Array [ + "error", + ], + "type": "object", + }, + }, + }, + "description": "The provided request data is invalid.", + }, + "401": Object { + "description": "Authorization information is missing or invalid.", + }, + }, + "summary": "Send usage metrics to Unleash.", + "tags": Array [ + "Operational", + "Server-side client", + ], + }, + }, "/proxy/client/features": Object { "get": Object { "description": "Returns the toggle configuration from the proxy's internal Unleash SDK. Use this to bootstrap other proxies and server-side SDKs. Requires you to provide one of the proxy's configured \`serverSideTokens\` for authorization.", diff --git a/src/test/unleash-proxy.test.ts b/src/test/unleash-proxy.test.ts index 2814373..fa79943 100644 --- a/src/test/unleash-proxy.test.ts +++ b/src/test/unleash-proxy.test.ts @@ -390,6 +390,30 @@ test('Should register metrics', () => { .expect(200); }); +test('Should register metrics an /all path as well', () => { + const toggles = [ + { + name: 'test', + enabled: true, + impressionData: true, + }, + ]; + const client = new MockClient(toggles); + + const proxySecrets = ['sdf']; + const app = createApp( + { unleashUrl, unleashApiToken, proxySecrets }, + client, + ); + client.emit('ready'); + + return request(app) + .post('/proxy/all/client/metrics') + .send(metrics) + .set('Authorization', 'sdf') + .expect(200); +}); + test('Should require metrics to have correct format', () => { const client = new MockClient(); diff --git a/src/unleash-proxy.ts b/src/unleash-proxy.ts index 36c6969..eaffe79 100644 --- a/src/unleash-proxy.ts +++ b/src/unleash-proxy.ts @@ -173,6 +173,19 @@ If you don't provide the \`toggles\` property, then this operation functions exa this.getAllTogglesPOST.bind(this), ); + router.post( + '/all/client/metrics', + openApiService.validPath({ + requestBody: registerMetricsRequest, + responses: standardResponses(200, 400, 401), + description: + "This endpoint lets you register usage metrics with Unleash. Accepts either one of the proxy's configured `serverSideTokens` or one of its `clientKeys` for authorization.", + summary: 'Send usage metrics to Unleash.', + tags: ['Operational', 'Server-side client'], + }), + this.registerMetrics.bind(this), + ); + router.post( '', openApiService.validPath({