/* * Licensed to Elasticsearch B.V. under one or more contributor * license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright * ownership. Elasticsearch B.V. licenses this file to you under * the Apache License, Version 2.0 (the "License"); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ import { expectType, expectError, expectAssignable } from 'tsd' import { Client } from '../../' import { BulkHelper, BulkStats, BulkHelperOptions, ScrollSearchResponse, OnDropDocument, MsearchHelper } from '../../lib/Helpers' import { ApiResponse, ApiError, Context } from '../../lib/Transport' const client = new Client({ node: 'http://localhost:9200' }) /// .helpers.bulk const b = client.helpers.bulk>({ datasource: [], onDocument (doc) { expectType>(doc) return { index: { _index: 'test' } } }, flushBytes: 5000000, flushInterval: 30000, concurrency: 5, retries: 3, wait: 5000, onDrop (doc) { expectType>>(doc) }, refreshOnCompletion: true, pipeline: 'my-pipeline' }) expectType>(b) expectType>(b.abort()) b.then(stats => expectType(stats)) // body can't be provided expectError( client.helpers.bulk({ datasource: [], onDocument (doc) { return { index: { _index: 'test' } } }, body: [] }) ) // test onDocument actions // index { const options = { datasource: [], onDocument (doc: Record) { return { index: { _index: 'test' } } } } expectAssignable>>(options) } // create { const options = { datasource: [], onDocument (doc: Record) { return { create: { _index: 'test' } } } } expectAssignable>>(options) } // update { // without `:BulkHelperOptions` this test cannot pass // but if we write these options inline inside // a `.helper.bulk`, it works as expected const options: BulkHelperOptions> = { datasource: [], onDocument (doc: Record) { return [{ update: { _index: 'test' } }, doc] } } expectAssignable>>(options) } // delete { const options = { datasource: [], onDocument (doc: Record) { return { delete: { _index: 'test' } } } } expectAssignable>>(options) } /// .helpers.scrollSearch // just search params { async function test () { const scrollSearch = client.helpers.scrollSearch({ index: 'test', body: { query: { match: { foo: 'bar' } } } }) for await (const response of scrollSearch) { expectAssignable(response) } } } // search params and options { async function test () { const scrollSearch = client.helpers.scrollSearch({ index: 'test', body: { query: { match: { foo: 'bar' } } } }, { ignore: [404] }) for await (const response of scrollSearch) { expectAssignable(response) expectType>(response.body) expectType(response.documents) expectType(response.meta.context) } } } // with type defs { interface ShardsResponse { total: number; successful: number; failed: number; skipped: number; } interface Explanation { value: number; description: string; details: Explanation[]; } interface SearchResponse { took: number; timed_out: boolean; _scroll_id?: string; _shards: ShardsResponse; hits: { total: number; max_score: number; hits: Array<{ _index: string; _type: string; _id: string; _score: number; _source: T; _version?: number; _explanation?: Explanation; fields?: any; highlight?: any; inner_hits?: any; matched_queries?: string[]; sort?: string[]; }>; }; aggregations?: any; } interface Source { foo: string } async function test () { const scrollSearch = client.helpers.scrollSearch>({ index: 'test', body: { query: { match: { foo: 'bar' } } } }) for await (const response of scrollSearch) { expectAssignable(response) expectType>(response.body) expectType(response.documents) expectType(response.meta.context) } } } { interface SearchBody { query: { match: { foo: string } } } interface ShardsResponse { total: number; successful: number; failed: number; skipped: number; } interface Explanation { value: number; description: string; details: Explanation[]; } interface SearchResponse { took: number; timed_out: boolean; _scroll_id?: string; _shards: ShardsResponse; hits: { total: number; max_score: number; hits: Array<{ _index: string; _type: string; _id: string; _score: number; _source: T; _version?: number; _explanation?: Explanation; fields?: any; highlight?: any; inner_hits?: any; matched_queries?: string[]; sort?: string[]; }>; }; aggregations?: any; } interface Source { foo: string } async function test () { const scrollSearch = client.helpers.scrollSearch, SearchBody, Record>({ index: 'test', body: { query: { match: { foo: 'bar' } } } }) for await (const response of scrollSearch) { expectAssignable(response) expectType>(response.body) expectType(response.documents) expectType>(response.meta.context) } } } /// .helpers.scrollDocuments // just search params { async function test () { const scrollDocuments = client.helpers.scrollDocuments({ index: 'test', body: { query: { match: { foo: 'bar' } } } }) for await (const document of scrollDocuments) { expectType(document) } } } // search params and options { async function test () { const scrollDocuments = client.helpers.scrollDocuments({ index: 'test', body: { query: { match: { foo: 'bar' } } } }, { ignore: [404] }) for await (const document of scrollDocuments) { expectType(document) } } } // with type defs { interface Source { foo: string } async function test () { const scrollDocuments = client.helpers.scrollDocuments({ index: 'test', body: { query: { match: { foo: 'bar' } } } }) for await (const document of scrollDocuments) { expectType(document) } } } { interface SearchBody { query: { match: { foo: string } } } interface Source { foo: string } async function test () { const scrollDocuments = client.helpers.scrollDocuments({ index: 'test', body: { query: { match: { foo: 'bar' } } } }) for await (const document of scrollDocuments) { expectType(document) } } } /// .helpers.search // just search params { const p = client.helpers.search({ index: 'test', body: { query: { match: { foo: 'bar' } } } }) expectType>(p) expectType(await p) } // search params and options { const p = client.helpers.search({ index: 'test', body: { query: { match: { foo: 'bar' } } } }, { ignore: [404] }) expectType>(p) expectType(await p) } // with type defs { interface Source { foo: string } const p = client.helpers.search({ index: 'test', body: { query: { match: { foo: 'bar' } } } }) expectType>(p) expectType(await p) } { interface SearchBody { query: { match: { foo: string } } } interface Source { foo: string } const p = client.helpers.search({ index: 'test', body: { query: { match: { foo: 'bar' } } } }) expectType>(p) expectType(await p) } /// .helpers.msearch const s = client.helpers.msearch({ operations: 5, flushInterval: 500, concurrency: 5, retries: 5, wait: 5000 }) expectType(s) expectType(s.stop()) expectType(s.stop(new Error('kaboom'))) expectType, unknown>>>(s.search({ index: 'foo'}, { query: {} })) expectType>>(s.search, string>({ index: 'foo'}, { query: {} })) expectType(s.search({ index: 'foo'}, { query: {} }, (err, result) => { expectType(err) expectType(result) })) expectType(s.search, string>({ index: 'foo'}, { query: {} }, (err, result) => { expectType(err) expectType>(result) }))