Skip to content

Commit 6447fc1

Browse files
authored
Drop support for body param in helpers and tests (#2521)
* Update tests and bulk helper to stop using body param * Update compatible-with content-type header for 9.0
1 parent e9c2f8b commit 6447fc1

File tree

4 files changed

+31
-150
lines changed

4 files changed

+31
-150
lines changed

src/client.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -383,9 +383,9 @@ export default class Client extends API {
383383
maxResponseSize: options.maxResponseSize,
384384
maxCompressedResponseSize: options.maxCompressedResponseSize,
385385
vendoredHeaders: {
386-
jsonContentType: 'application/vnd.elasticsearch+json; compatible-with=8',
387-
ndjsonContentType: 'application/vnd.elasticsearch+x-ndjson; compatible-with=8',
388-
accept: 'application/vnd.elasticsearch+json; compatible-with=8,text/plain'
386+
jsonContentType: 'application/vnd.elasticsearch+json; compatible-with=9',
387+
ndjsonContentType: 'application/vnd.elasticsearch+x-ndjson; compatible-with=9',
388+
accept: 'application/vnd.elasticsearch+json; compatible-with=9,text/plain'
389389
},
390390
redaction: options.redaction
391391
})

src/helpers.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -910,7 +910,7 @@ export default class Helpers {
910910

911911
function tryBulk (bulkBody: string[], callback: (err: Error | null, bulkBody: string[]) => void): void {
912912
if (shouldAbort) return callback(null, [])
913-
client.bulk(Object.assign({}, bulkOptions, { body: bulkBody }), reqOptions as TransportRequestOptionsWithMeta)
913+
client.bulk(Object.assign({}, bulkOptions, { operations: bulkBody }), reqOptions as TransportRequestOptionsWithMeta)
914914
.then(response => {
915915
const result = response.body
916916
const results = zipBulkResults(result.items, bulkBody)

test/unit/api.test.ts

+2-121
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { connection } from '../utils'
2222
import { Client } from '../..'
2323
import * as T from '../../lib/api/types'
2424

25-
test('Api without body key and top level body', async t => {
25+
test('Api with top level body', async t => {
2626
t.plan(2)
2727

2828
const Connection = connection.buildMockConnection({
@@ -50,37 +50,7 @@ test('Api without body key and top level body', async t => {
5050
t.equal(response.took, 42)
5151
})
5252

53-
test('Api with body key and top level body', async t => {
54-
t.plan(2)
55-
56-
const Connection = connection.buildMockConnection({
57-
onRequest (opts) {
58-
// @ts-expect-error
59-
t.same(JSON.parse(opts.body), { query: { match_all: {} } })
60-
return {
61-
statusCode: 200,
62-
body: { took: 42 }
63-
}
64-
}
65-
})
66-
67-
const client = new Client({
68-
node: 'http://localhost:9200',
69-
Connection
70-
})
71-
72-
const response = await client.search({
73-
index: 'test',
74-
allow_no_indices: true,
75-
body: {
76-
query: { match_all: {} }
77-
}
78-
})
79-
80-
t.equal(response.took, 42)
81-
})
82-
83-
test('Api without body key and keyed body', async t => {
53+
test('Api with keyed body', async t => {
8454
t.plan(2)
8555

8656
const Connection = connection.buildMockConnection({
@@ -108,95 +78,6 @@ test('Api without body key and keyed body', async t => {
10878
t.equal(response.result, 'created')
10979
})
11080

111-
test('Api with body key and keyed body', async t => {
112-
t.plan(2)
113-
114-
const Connection = connection.buildMockConnection({
115-
onRequest (opts) {
116-
// @ts-expect-error
117-
t.same(JSON.parse(opts.body), { foo: 'bar' })
118-
return {
119-
statusCode: 200,
120-
body: { result: 'created' }
121-
}
122-
}
123-
})
124-
125-
const client = new Client({
126-
node: 'http://localhost:9200',
127-
Connection
128-
})
129-
130-
const response = await client.create({
131-
index: 'test',
132-
id: '1',
133-
body: { foo: 'bar' }
134-
})
135-
136-
t.equal(response.result, 'created')
137-
})
138-
139-
test('Using the body key should not mutate the body', async t => {
140-
t.plan(2)
141-
142-
const Connection = connection.buildMockConnection({
143-
onRequest (opts) {
144-
// @ts-expect-error
145-
t.same(JSON.parse(opts.body), { query: { match_all: {} }, sort: 'foo' })
146-
return {
147-
statusCode: 200,
148-
body: { took: 42 }
149-
}
150-
}
151-
})
152-
153-
const client = new Client({
154-
node: 'http://localhost:9200',
155-
Connection
156-
})
157-
158-
const body = { query: { match_all: {} } }
159-
await client.search({
160-
index: 'test',
161-
sort: 'foo',
162-
body
163-
})
164-
165-
t.same(body, { query: { match_all: {} } })
166-
})
167-
168-
test('Using the body key with a string value', async t => {
169-
t.plan(2)
170-
171-
const Connection = connection.buildMockConnection({
172-
onRequest (opts) {
173-
// @ts-expect-error
174-
t.same(JSON.parse(opts.body), { query: { match_all: {} } })
175-
return {
176-
statusCode: 200,
177-
body: { took: 42 }
178-
}
179-
}
180-
})
181-
182-
const client = new Client({
183-
node: 'http://localhost:9200',
184-
Connection
185-
})
186-
187-
try {
188-
const body = { query: { match_all: {} } }
189-
await client.search({
190-
index: 'test',
191-
// @ts-expect-error
192-
body: JSON.stringify(body)
193-
})
194-
t.pass('ok!')
195-
} catch (err: any) {
196-
t.fail(err)
197-
}
198-
})
199-
20081
test('With generic document', async t => {
20182
t.plan(1)
20283

test/unit/helpers/bulk.test.ts

+25-25
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ test('bulk index', t => {
5858
onRequest (params) {
5959
t.equal(params.path, '/_bulk')
6060
t.match(params.headers, {
61-
'content-type': 'application/vnd.elasticsearch+x-ndjson; compatible-with=8',
61+
'content-type': 'application/vnd.elasticsearch+x-ndjson; compatible-with=9',
6262
'x-elastic-client-meta': `es=${clientVersion},js=${nodeVersion},t=${transportVersion},hc=${nodeVersion},h=bp`
6363
})
6464
// @ts-expect-error
@@ -104,7 +104,7 @@ test('bulk index', t => {
104104
const MockConnection = connection.buildMockConnection({
105105
onRequest (params) {
106106
t.equal(params.path, '/_bulk')
107-
t.match(params.headers, { 'content-type': 'application/vnd.elasticsearch+x-ndjson; compatible-with=8' })
107+
t.match(params.headers, { 'content-type': 'application/vnd.elasticsearch+x-ndjson; compatible-with=9' })
108108
t.notMatch(params.headers, {
109109
'x-elastic-client-meta': `es=${clientVersion},js=${nodeVersion},t=${transportVersion},hc=${nodeVersion},h=bp`
110110
})
@@ -150,7 +150,7 @@ test('bulk index', t => {
150150
const MockConnection = connection.buildMockConnection({
151151
onRequest (params) {
152152
t.equal(params.path, '/_bulk')
153-
t.match(params.headers, { 'content-type': 'application/vnd.elasticsearch+x-ndjson; compatible-with=8' })
153+
t.match(params.headers, { 'content-type': 'application/vnd.elasticsearch+x-ndjson; compatible-with=9' })
154154
// @ts-expect-error
155155
t.equal(params.body.split('\n').filter(Boolean).length, 6)
156156
return { body: { errors: false, items: new Array(3).fill({}) } }
@@ -195,7 +195,7 @@ test('bulk index', t => {
195195
return { body: { acknowledged: true } }
196196
} else {
197197
t.equal(params.path, '/_bulk')
198-
t.match(params.headers, { 'content-type': 'application/vnd.elasticsearch+x-ndjson; compatible-with=8' })
198+
t.match(params.headers, { 'content-type': 'application/vnd.elasticsearch+x-ndjson; compatible-with=9' })
199199
// @ts-expect-error
200200
const [action, payload] = params.body.split('\n')
201201
t.same(JSON.parse(action), { index: { _index: 'test' } })
@@ -241,7 +241,7 @@ test('bulk index', t => {
241241
return { body: { acknowledged: true } }
242242
} else {
243243
t.equal(params.path, '/_bulk')
244-
t.match(params.headers, { 'content-type': 'application/vnd.elasticsearch+x-ndjson; compatible-with=8' })
244+
t.match(params.headers, { 'content-type': 'application/vnd.elasticsearch+x-ndjson; compatible-with=9' })
245245
// @ts-expect-error
246246
const [action, payload] = params.body.split('\n')
247247
t.same(JSON.parse(action), { index: { _index: 'test' } })
@@ -283,7 +283,7 @@ test('bulk index', t => {
283283
const MockConnection = connection.buildMockConnection({
284284
onRequest (params) {
285285
t.equal(params.path, '/_bulk')
286-
t.match(params.headers, { 'content-type': 'application/vnd.elasticsearch+x-ndjson; compatible-with=8' })
286+
t.match(params.headers, { 'content-type': 'application/vnd.elasticsearch+x-ndjson; compatible-with=9' })
287287
// @ts-expect-error
288288
const [action, payload] = params.body.split('\n')
289289
t.same(JSON.parse(action), { index: { _index: 'test', _id: count } })
@@ -328,7 +328,7 @@ test('bulk index', t => {
328328
t.test('Should perform a bulk request (retry)', async t => {
329329
async function handler (req: http.IncomingMessage, res: http.ServerResponse) {
330330
t.equal(req.url, '/_bulk')
331-
t.match(req.headers, { 'content-type': 'application/vnd.elasticsearch+x-ndjson; compatible-with=8' })
331+
t.match(req.headers, { 'content-type': 'application/vnd.elasticsearch+x-ndjson; compatible-with=9' })
332332

333333
let body = ''
334334
req.setEncoding('utf8')
@@ -446,7 +446,7 @@ test('bulk index', t => {
446446
t.test('Should perform a bulk request (failure)', async t => {
447447
async function handler (req: http.IncomingMessage, res: http.ServerResponse) {
448448
t.equal(req.url, '/_bulk')
449-
t.match(req.headers, { 'content-type': 'application/vnd.elasticsearch+x-ndjson; compatible-with=8' })
449+
t.match(req.headers, { 'content-type': 'application/vnd.elasticsearch+x-ndjson; compatible-with=9' })
450450

451451
let body = ''
452452
req.setEncoding('utf8')
@@ -587,7 +587,7 @@ test('bulk index', t => {
587587
t.test('Should abort a bulk request', async t => {
588588
async function handler (req: http.IncomingMessage, res: http.ServerResponse) {
589589
t.equal(req.url, '/_bulk')
590-
t.match(req.headers, { 'content-type': 'application/vnd.elasticsearch+x-ndjson; compatible-with=8' })
590+
t.match(req.headers, { 'content-type': 'application/vnd.elasticsearch+x-ndjson; compatible-with=9' })
591591

592592
let body = ''
593593
req.setEncoding('utf8')
@@ -724,7 +724,7 @@ test('bulk index', t => {
724724
const MockConnection = connection.buildMockConnection({
725725
onRequest (params) {
726726
t.equal(params.path, '/_bulk')
727-
t.match(params.headers, { 'content-type': 'application/vnd.elasticsearch+x-ndjson; compatible-with=8' })
727+
t.match(params.headers, { 'content-type': 'application/vnd.elasticsearch+x-ndjson; compatible-with=9' })
728728
// @ts-expect-error
729729
const [action, payload] = params.body.split('\n')
730730
t.same(JSON.parse(action), { index: { _index: 'test', _id: count } })
@@ -815,7 +815,7 @@ test('bulk index', t => {
815815
const MockConnection = connection.buildMockConnection({
816816
onRequest (params) {
817817
t.equal(params.path, '/_bulk')
818-
t.match(params.headers, { 'content-type': 'application/vnd.elasticsearch+x-ndjson; compatible-with=8' })
818+
t.match(params.headers, { 'content-type': 'application/vnd.elasticsearch+x-ndjson; compatible-with=9' })
819819
// @ts-expect-error
820820
const [action, payload] = params.body.split('\n')
821821
t.same(JSON.parse(action), { index: { _index: 'test' } })
@@ -914,7 +914,7 @@ test('bulk index', t => {
914914
onRequest (params) {
915915
t.equal(params.path, '/_bulk')
916916
t.match(params.headers, {
917-
'content-type': 'application/vnd.elasticsearch+x-ndjson; compatible-with=8',
917+
'content-type': 'application/vnd.elasticsearch+x-ndjson; compatible-with=9',
918918
'x-elastic-client-meta': `es=${clientVersion},js=${nodeVersion},t=${transportVersion},hc=${nodeVersion},h=bp`
919919
})
920920
// @ts-expect-error
@@ -969,7 +969,7 @@ test('bulk create', t => {
969969
const MockConnection = connection.buildMockConnection({
970970
onRequest (params) {
971971
t.equal(params.path, '/_bulk')
972-
t.match(params.headers, { 'content-type': 'application/vnd.elasticsearch+x-ndjson; compatible-with=8' })
972+
t.match(params.headers, { 'content-type': 'application/vnd.elasticsearch+x-ndjson; compatible-with=9' })
973973
// @ts-expect-error
974974
const [action, payload] = params.body.split('\n')
975975
t.same(JSON.parse(action), { create: { _index: 'test', _id: count } })
@@ -1017,7 +1017,7 @@ test('bulk create', t => {
10171017
const MockConnection = connection.buildMockConnection({
10181018
onRequest (params) {
10191019
t.equal(params.path, '/_bulk')
1020-
t.match(params.headers, { 'content-type': 'application/vnd.elasticsearch+x-ndjson; compatible-with=8' })
1020+
t.match(params.headers, { 'content-type': 'application/vnd.elasticsearch+x-ndjson; compatible-with=9' })
10211021
// @ts-expect-error
10221022
const [action, payload] = params.body.split('\n')
10231023
t.same(JSON.parse(action), { create: { _index: 'test', _id: count } })
@@ -1073,7 +1073,7 @@ test('bulk update', t => {
10731073
const MockConnection = connection.buildMockConnection({
10741074
onRequest (params) {
10751075
t.equal(params.path, '/_bulk')
1076-
t.match(params.headers, { 'content-type': 'application/vnd.elasticsearch+x-ndjson; compatible-with=8' })
1076+
t.match(params.headers, { 'content-type': 'application/vnd.elasticsearch+x-ndjson; compatible-with=9' })
10771077
// @ts-expect-error
10781078
const [action, payload] = params.body.split('\n')
10791079
t.same(JSON.parse(action), { update: { _index: 'test', _id: count } })
@@ -1122,7 +1122,7 @@ test('bulk update', t => {
11221122
const MockConnection = connection.buildMockConnection({
11231123
onRequest (params) {
11241124
t.equal(params.path, '/_bulk')
1125-
t.match(params.headers, { 'content-type': 'application/vnd.elasticsearch+x-ndjson; compatible-with=8' })
1125+
t.match(params.headers, { 'content-type': 'application/vnd.elasticsearch+x-ndjson; compatible-with=9' })
11261126
// @ts-expect-error
11271127
const [action, payload] = params.body.split('\n')
11281128
t.same(JSON.parse(action), { update: { _index: 'test', _id: count } })
@@ -1169,7 +1169,7 @@ test('bulk update', t => {
11691169
const MockConnection = connection.buildMockConnection({
11701170
onRequest (params) {
11711171
t.equal(params.path, '/_bulk')
1172-
t.match(params.headers, { 'content-type': 'application/vnd.elasticsearch+x-ndjson; compatible-with=8' })
1172+
t.match(params.headers, { 'content-type': 'application/vnd.elasticsearch+x-ndjson; compatible-with=9' })
11731173
// @ts-expect-error
11741174
const [action, payload] = params.body.split('\n')
11751175
t.same(JSON.parse(action), { update: { _index: 'test', _id: count } })
@@ -1223,7 +1223,7 @@ test('bulk delete', t => {
12231223
const MockConnection = connection.buildMockConnection({
12241224
onRequest (params) {
12251225
t.equal(params.path, '/_bulk')
1226-
t.match(params.headers, { 'content-type': 'application/vnd.elasticsearch+x-ndjson; compatible-with=8' })
1226+
t.match(params.headers, { 'content-type': 'application/vnd.elasticsearch+x-ndjson; compatible-with=9' })
12271227
// @ts-expect-error
12281228
t.same(JSON.parse(params.body), { delete: { _index: 'test', _id: count++ } })
12291229
return { body: { errors: false, items: [{}] } }
@@ -1266,7 +1266,7 @@ test('bulk delete', t => {
12661266
t.test('Should perform a bulk request (failure)', async t => {
12671267
async function handler (req: http.IncomingMessage, res: http.ServerResponse) {
12681268
t.equal(req.url, '/_bulk')
1269-
t.match(req.headers, { 'content-type': 'application/vnd.elasticsearch+x-ndjson; compatible-with=8' })
1269+
t.match(req.headers, { 'content-type': 'application/vnd.elasticsearch+x-ndjson; compatible-with=9' })
12701270

12711271
let body = ''
12721272
req.setEncoding('utf8')
@@ -1469,7 +1469,7 @@ test('transport options', t => {
14691469

14701470
if (params.path === '/_bulk') {
14711471
t.match(params.headers, {
1472-
'content-type': 'application/vnd.elasticsearch+x-ndjson; compatible-with=8',
1472+
'content-type': 'application/vnd.elasticsearch+x-ndjson; compatible-with=9',
14731473
foo: 'bar'
14741474
})
14751475
return { body: { errors: false, items: [{}] } }
@@ -1618,7 +1618,7 @@ test('Flush interval', t => {
16181618
const MockConnection = connection.buildMockConnection({
16191619
onRequest (params) {
16201620
t.equal(params.path, '/_bulk')
1621-
t.match(params.headers, { 'content-type': 'application/vnd.elasticsearch+x-ndjson; compatible-with=8' })
1621+
t.match(params.headers, { 'content-type': 'application/vnd.elasticsearch+x-ndjson; compatible-with=9' })
16221622
// @ts-expect-error
16231623
const [action, payload] = params.body.split('\n')
16241624
t.same(JSON.parse(action), { index: { _index: 'test' } })
@@ -1671,7 +1671,7 @@ test('Flush interval', t => {
16711671
onRequest (params) {
16721672
t.ok(count < 2)
16731673
t.equal(params.path, '/_bulk')
1674-
t.match(params.headers, { 'content-type': 'application/vnd.elasticsearch+x-ndjson; compatible-with=8' })
1674+
t.match(params.headers, { 'content-type': 'application/vnd.elasticsearch+x-ndjson; compatible-with=9' })
16751675
// @ts-expect-error
16761676
const [action, payload] = params.body.split('\n')
16771677
t.same(JSON.parse(action), { index: { _index: 'test' } })
@@ -1730,7 +1730,7 @@ test('Flush interval', t => {
17301730
onRequest (params) {
17311731
t.equal(params.path, '/_bulk')
17321732
t.match(params.headers, {
1733-
'content-type': 'application/vnd.elasticsearch+x-ndjson; compatible-with=8',
1733+
'content-type': 'application/vnd.elasticsearch+x-ndjson; compatible-with=9',
17341734
'x-elastic-client-meta': `es=${clientVersion},js=${nodeVersion},t=${transportVersion},hc=${nodeVersion},h=bp`
17351735
})
17361736
// @ts-expect-error
@@ -1749,12 +1749,12 @@ test('Flush interval', t => {
17491749
datasource: dataset.slice(),
17501750
flushBytes: 1,
17511751
concurrency: 1,
1752-
onDocument (doc) {
1752+
onDocument (_doc) {
17531753
return {
17541754
index: { _index: 'test' }
17551755
}
17561756
},
1757-
onDrop (doc) {
1757+
onDrop (_doc) {
17581758
t.fail('This should never be called')
17591759
}
17601760
})

0 commit comments

Comments
 (0)