From db4c2487c673c15d60c7b0653f8cc94334427a23 Mon Sep 17 00:00:00 2001 From: Josh Mock Date: Wed, 1 Oct 2025 11:35:24 -0500 Subject: [PATCH] Add support for accepted parameters metadata on all requests (#3013) (cherry picked from commit 80418dffb18520bd7037958335bd1fb2e484c347) --- src/client.ts | 2 + test/unit/api.test.ts | 96 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) diff --git a/src/client.ts b/src/client.ts index 34d8db82e..069ff5326 100644 --- a/src/client.ts +++ b/src/client.ts @@ -41,6 +41,8 @@ import transportPackageJson from '@elastic/transport/package.json' const kChild = Symbol('elasticsearchjs-child') const kInitialOptions = Symbol('elasticsearchjs-initial-options') +export const kAcceptedParams = Symbol('elasticsearchjs-accepted-params') + let clientVersion: string = packageJson.version /* istanbul ignore next */ if (clientVersion.includes('-')) { diff --git a/test/unit/api.test.ts b/test/unit/api.test.ts index 452f53805..213f87da5 100644 --- a/test/unit/api.test.ts +++ b/test/unit/api.test.ts @@ -6,6 +6,7 @@ import { test } from 'tap' import { connection } from '../utils' import { Client } from '../..' +import { Transport } from '@elastic/transport' import * as T from '../../lib/api/types' test('Api with top level body', async t => { @@ -167,3 +168,98 @@ test('With generic document and aggregation', async t => { t.ok(Array.isArray(response.aggregations?.unique.buckets)) }) +test('Api request metadata', t => { + t.test('name', async t => { + class TestTransport extends Transport { + // @ts-expect-error + async request(params, options) { + t.equal(params.meta.name, 'synonyms.put_synonym_rule') + return super.request(params, options) + } + } + + const Connection = connection.buildMockConnection({ + onRequest () { + return { + statusCode: 200, + body: { took: 42 } + } + } + }) + + const client = new Client({ + node: 'http://localhost:9200', + // @ts-expect-error + Transport: TestTransport, + Connection + }) + // @ts-expect-error + await client.synonyms.putSynonymRule({ set_id: "foo", rule_id: "bar" }) + }) + + t.test('pathParts', async t => { + class TestTransport extends Transport { + // @ts-expect-error + async request(params, options) { + t.strictSame(params.meta.pathParts, { + set_id: 'foo', + rule_id: 'bar' + }) + return super.request(params, options) + } + } + + const Connection = connection.buildMockConnection({ + onRequest () { + return { + statusCode: 200, + body: { took: 42 } + } + } + }) + + const client = new Client({ + node: 'http://localhost:9200', + // @ts-expect-error + Transport: TestTransport, + Connection + }) + // @ts-expect-error + await client.synonyms.putSynonymRule({ set_id: "foo", rule_id: "bar" }) + }) + + t.test('acceptedParams', async t => { + class TestTransport extends Transport { + // @ts-expect-error + async request(params, options) { + t.strictSame(params.meta.acceptedParams, [ + 'set_id', + 'rule_id', + 'synonyms', + 'refresh', + ]) + return super.request(params, options) + } + } + + const Connection = connection.buildMockConnection({ + onRequest () { + return { + statusCode: 200, + body: { took: 42 } + } + } + }) + + const client = new Client({ + node: 'http://localhost:9200', + // @ts-expect-error + Transport: TestTransport, + Connection + }) + // @ts-expect-error + await client.synonyms.putSynonymRule({ set_id: "foo", rule_id: "bar" }) + }) + + t.end() +})