Switched request and response generics position (#1132)
* Updated code generation * Switched request and response generics position * Updated test * API generation * Removed unused generics * Test type definitions for callback style API as well * Fix comments * Fix conflict * API generation * Updated type def
This commit is contained in:
committed by
GitHub
parent
e67b55d163
commit
6779f3b11a
@ -3,8 +3,9 @@
|
||||
// See the LICENSE file in the project root for more information
|
||||
|
||||
import { expectType, expectError } from 'tsd'
|
||||
import { ResponseBody } from '../../lib/Transport'
|
||||
import { Client } from '../../'
|
||||
import { Readable as ReadableStream } from 'stream';
|
||||
import { TransportRequestCallback } from '../../lib/Transport'
|
||||
import { Client, ApiError } from '../../'
|
||||
|
||||
const client = new Client({
|
||||
node: 'http://localhost:9200'
|
||||
@ -59,7 +60,7 @@ interface Source {
|
||||
foo: string
|
||||
}
|
||||
|
||||
// Use a bad body
|
||||
// body that does not respect the RequestBody constraint
|
||||
expectError(
|
||||
client.search({
|
||||
index: 'hello',
|
||||
@ -67,7 +68,7 @@ expectError(
|
||||
}).then(console.log)
|
||||
)
|
||||
|
||||
// No generics
|
||||
// No generics (promise style)
|
||||
{
|
||||
const response = await client.search({
|
||||
index: 'test',
|
||||
@ -78,28 +79,13 @@ expectError(
|
||||
}
|
||||
})
|
||||
|
||||
expectType<ResponseBody>(response.body)
|
||||
expectType<Record<string, any>>(response.body)
|
||||
expectType<unknown>(response.meta.context)
|
||||
}
|
||||
|
||||
// Define only the request body
|
||||
// Define only the response body (promise style)
|
||||
{
|
||||
const response = await client.search<SearchBody>({
|
||||
index: 'test',
|
||||
body: {
|
||||
query: {
|
||||
match: { foo: 'bar' }
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
expectType<ResponseBody>(response.body)
|
||||
expectType<unknown>(response.meta.context)
|
||||
}
|
||||
|
||||
// Define request body and response body
|
||||
{
|
||||
const response = await client.search<SearchBody, SearchResponse<Source>>({
|
||||
const response = await client.search<SearchResponse<Source>>({
|
||||
index: 'test',
|
||||
body: {
|
||||
query: {
|
||||
@ -112,9 +98,24 @@ expectError(
|
||||
expectType<unknown>(response.meta.context)
|
||||
}
|
||||
|
||||
// Define request body, response body and the context
|
||||
// Define response body and request body (promise style)
|
||||
{
|
||||
const response = await client.search<SearchBody, SearchResponse<Source>, string>({
|
||||
const response = await client.search<SearchResponse<Source>, SearchBody>({
|
||||
index: 'test',
|
||||
body: {
|
||||
query: {
|
||||
match: { foo: 'bar' }
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
expectType<SearchResponse<Source>>(response.body)
|
||||
expectType<unknown>(response.meta.context)
|
||||
}
|
||||
|
||||
// Define response body, request body and the context (promise style)
|
||||
{
|
||||
const response = await client.search<SearchResponse<Source>, SearchBody, string>({
|
||||
index: 'test',
|
||||
body: {
|
||||
query: {
|
||||
@ -126,3 +127,143 @@ expectError(
|
||||
expectType<SearchResponse<Source>>(response.body)
|
||||
expectType<string>(response.meta.context)
|
||||
}
|
||||
|
||||
// Send request body as string (promise style)
|
||||
{
|
||||
const response = await client.search({
|
||||
index: 'test',
|
||||
body: 'hello world'
|
||||
})
|
||||
|
||||
expectType<Record<string, any>>(response.body)
|
||||
expectType<unknown>(response.meta.context)
|
||||
}
|
||||
|
||||
// Send request body as buffer (promise style)
|
||||
{
|
||||
const response = await client.search({
|
||||
index: 'test',
|
||||
body: Buffer.from('hello world')
|
||||
})
|
||||
|
||||
expectType<Record<string, any>>(response.body)
|
||||
expectType<unknown>(response.meta.context)
|
||||
}
|
||||
|
||||
// Send request body as readable stream (promise style)
|
||||
{
|
||||
const response = await client.search({
|
||||
index: 'test',
|
||||
body: new ReadableStream()
|
||||
})
|
||||
|
||||
expectType<Record<string, any>>(response.body)
|
||||
expectType<unknown>(response.meta.context)
|
||||
}
|
||||
|
||||
// No generics (callback style)
|
||||
{
|
||||
const result = client.search({
|
||||
index: 'test',
|
||||
body: {
|
||||
query: {
|
||||
match: { foo: 'bar' }
|
||||
}
|
||||
}
|
||||
}, (err, response) => {
|
||||
expectType<ApiError>(err)
|
||||
expectType<Record<string, any>>(response.body)
|
||||
expectType<unknown>(response.meta.context)
|
||||
})
|
||||
expectType<TransportRequestCallback>(result)
|
||||
}
|
||||
|
||||
// Define only the response body (callback style)
|
||||
{
|
||||
const result = client.search<SearchResponse<Source>>({
|
||||
index: 'test',
|
||||
body: {
|
||||
query: {
|
||||
match: { foo: 'bar' }
|
||||
}
|
||||
}
|
||||
}, (err, response) => {
|
||||
expectType<ApiError>(err)
|
||||
expectType<SearchResponse<Source>>(response.body)
|
||||
expectType<unknown>(response.meta.context)
|
||||
})
|
||||
expectType<TransportRequestCallback>(result)
|
||||
}
|
||||
|
||||
// Define response body and request body (callback style)
|
||||
{
|
||||
const result = client.search<SearchResponse<Source>, SearchBody>({
|
||||
index: 'test',
|
||||
body: {
|
||||
query: {
|
||||
match: { foo: 'bar' }
|
||||
}
|
||||
}
|
||||
}, (err, response) => {
|
||||
expectType<ApiError>(err)
|
||||
expectType<SearchResponse<Source>>(response.body)
|
||||
expectType<unknown>(response.meta.context)
|
||||
})
|
||||
expectType<TransportRequestCallback>(result)
|
||||
}
|
||||
|
||||
// Define response body, request body and the context (callback style)
|
||||
{
|
||||
const result = client.search<SearchResponse<Source>, SearchBody, string>({
|
||||
index: 'test',
|
||||
body: {
|
||||
query: {
|
||||
match: { foo: 'bar' }
|
||||
}
|
||||
}
|
||||
}, (err, response) => {
|
||||
expectType<ApiError>(err)
|
||||
expectType<SearchResponse<Source>>(response.body)
|
||||
expectType<string>(response.meta.context)
|
||||
})
|
||||
expectType<TransportRequestCallback>(result)
|
||||
}
|
||||
|
||||
// Send request body as string (callback style)
|
||||
{
|
||||
const result = client.search({
|
||||
index: 'test',
|
||||
body: 'hello world'
|
||||
}, (err, response) => {
|
||||
expectType<ApiError>(err)
|
||||
expectType<Record<string, any>>(response.body)
|
||||
expectType<unknown>(response.meta.context)
|
||||
})
|
||||
expectType<TransportRequestCallback>(result)
|
||||
}
|
||||
|
||||
// Send request body as buffer (callback style)
|
||||
{
|
||||
const result = client.search({
|
||||
index: 'test',
|
||||
body: Buffer.from('hello world')
|
||||
}, (err, response) => {
|
||||
expectType<ApiError>(err)
|
||||
expectType<Record<string, any>>(response.body)
|
||||
expectType<unknown>(response.meta.context)
|
||||
})
|
||||
expectType<TransportRequestCallback>(result)
|
||||
}
|
||||
|
||||
// Send request body as readable stream (callback style)
|
||||
{
|
||||
const result = client.search({
|
||||
index: 'test',
|
||||
body: new ReadableStream()
|
||||
}, (err, response) => {
|
||||
expectType<ApiError>(err)
|
||||
expectType<Record<string, any>>(response.body)
|
||||
expectType<unknown>(response.meta.context)
|
||||
})
|
||||
expectType<TransportRequestCallback>(result)
|
||||
}
|
||||
@ -3,22 +3,22 @@
|
||||
// See the LICENSE file in the project root for more information
|
||||
|
||||
import { expectType } from 'tsd'
|
||||
import { ResponseBody } from '../../lib/Transport'
|
||||
import { Client } from '../../'
|
||||
import { TransportRequestCallback } from '../../lib/Transport'
|
||||
import { Client, ApiError } from '../../'
|
||||
|
||||
const client = new Client({
|
||||
node: 'http://localhost:9200'
|
||||
})
|
||||
|
||||
// No generics
|
||||
// No generics (promise style)
|
||||
{
|
||||
const response = await client.cat.count({ index: 'test' })
|
||||
|
||||
expectType<ResponseBody>(response.body)
|
||||
expectType<Record<string, any>>(response.body)
|
||||
expectType<unknown>(response.meta.context)
|
||||
}
|
||||
|
||||
// Define only the request body
|
||||
// Define only the response body (promise style)
|
||||
{
|
||||
const response = await client.cat.count<string>({ index: 'test' })
|
||||
|
||||
@ -26,10 +26,40 @@ const client = new Client({
|
||||
expectType<unknown>(response.meta.context)
|
||||
}
|
||||
|
||||
// Define request body and the context
|
||||
// Define response body and the context (promise style)
|
||||
{
|
||||
const response = await client.cat.count<string, string>({ index: 'test' })
|
||||
|
||||
expectType<string>(response.body)
|
||||
expectType<string>(response.meta.context)
|
||||
}
|
||||
|
||||
// No generics (callback style)
|
||||
{
|
||||
const result = client.cat.count({ index: 'test' }, (err, response) => {
|
||||
expectType<ApiError>(err)
|
||||
expectType<Record<string, any>>(response.body)
|
||||
expectType<unknown>(response.meta.context)
|
||||
})
|
||||
expectType<TransportRequestCallback>(result)
|
||||
}
|
||||
|
||||
// Define only the response body (callback style)
|
||||
{
|
||||
const result = client.cat.count<string>({ index: 'test' }, (err, response) => {
|
||||
expectType<ApiError>(err)
|
||||
expectType<string>(response.body)
|
||||
expectType<unknown>(response.meta.context)
|
||||
})
|
||||
expectType<TransportRequestCallback>(result)
|
||||
}
|
||||
|
||||
// Define response body and the context (callback style)
|
||||
{
|
||||
const result = client.cat.count<string, string>({ index: 'test' }, (err, response) => {
|
||||
expectType<ApiError>(err)
|
||||
expectType<string>(response.body)
|
||||
expectType<string>(response.meta.context)
|
||||
})
|
||||
expectType<TransportRequestCallback>(result)
|
||||
}
|
||||
|
||||
@ -4,7 +4,6 @@
|
||||
|
||||
import { expectType } from 'tsd'
|
||||
import { errors, ApiResponse, Connection } from '../../'
|
||||
import { ResponseBody } from '../../lib/Transport'
|
||||
|
||||
const response = {
|
||||
body: {},
|
||||
@ -77,7 +76,7 @@ const response = {
|
||||
expectType<string>(err.name)
|
||||
expectType<string>(err.message)
|
||||
expectType<ApiResponse>(err.meta)
|
||||
expectType<ResponseBody>(err.body)
|
||||
expectType<Record<string, any>>(err.body)
|
||||
expectType<number>(err.statusCode)
|
||||
expectType<Record<string, any>>(err.headers)
|
||||
}
|
||||
|
||||
@ -4,7 +4,6 @@
|
||||
|
||||
import { expectType, expectError, expectAssignable } from 'tsd'
|
||||
import { Client } from '../../'
|
||||
import { RequestBody, ResponseBody } from '../../lib/Transport'
|
||||
import {
|
||||
BulkHelper,
|
||||
BulkStats,
|
||||
@ -130,7 +129,7 @@ expectError(
|
||||
|
||||
for await (const response of scrollSearch) {
|
||||
expectAssignable<ScrollSearchResponse>(response)
|
||||
expectType<ResponseBody<Record<string, any>>>(response.body)
|
||||
expectType<Record<string, any>>(response.body)
|
||||
expectType<unknown[]>(response.documents)
|
||||
expectType<unknown>(response.meta.context)
|
||||
}
|
||||
@ -138,6 +137,69 @@ expectError(
|
||||
}
|
||||
|
||||
// with type defs
|
||||
{
|
||||
interface ShardsResponse {
|
||||
total: number;
|
||||
successful: number;
|
||||
failed: number;
|
||||
skipped: number;
|
||||
}
|
||||
|
||||
interface Explanation {
|
||||
value: number;
|
||||
description: string;
|
||||
details: Explanation[];
|
||||
}
|
||||
|
||||
interface SearchResponse<T> {
|
||||
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<Source, SearchResponse<Source>>({
|
||||
index: 'test',
|
||||
body: {
|
||||
query: {
|
||||
match: { foo: 'bar' }
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
for await (const response of scrollSearch) {
|
||||
expectAssignable<ScrollSearchResponse>(response)
|
||||
expectType<SearchResponse<Source>>(response.body)
|
||||
expectType<Source[]>(response.documents)
|
||||
expectType<unknown>(response.meta.context)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
interface SearchBody {
|
||||
query: {
|
||||
@ -189,7 +251,7 @@ expectError(
|
||||
}
|
||||
|
||||
async function test () {
|
||||
const scrollSearch = client.helpers.scrollSearch<SearchBody, Source, SearchResponse<Source>, string>({
|
||||
const scrollSearch = client.helpers.scrollSearch<Source, SearchResponse<Source>, SearchBody, string>({
|
||||
index: 'test',
|
||||
body: {
|
||||
query: {
|
||||
@ -246,6 +308,27 @@ expectError(
|
||||
}
|
||||
|
||||
// with type defs
|
||||
{
|
||||
interface Source {
|
||||
foo: string
|
||||
}
|
||||
|
||||
async function test () {
|
||||
const scrollDocuments = client.helpers.scrollDocuments<Source>({
|
||||
index: 'test',
|
||||
body: {
|
||||
query: {
|
||||
match: { foo: 'bar' }
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
for await (const document of scrollDocuments) {
|
||||
expectType<Source>(document)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
interface SearchBody {
|
||||
query: {
|
||||
@ -258,7 +341,7 @@ expectError(
|
||||
}
|
||||
|
||||
async function test () {
|
||||
const scrollDocuments = client.helpers.scrollDocuments<SearchBody, Source>({
|
||||
const scrollDocuments = client.helpers.scrollDocuments<Source, SearchBody>({
|
||||
index: 'test',
|
||||
body: {
|
||||
query: {
|
||||
@ -306,6 +389,24 @@ expectError(
|
||||
}
|
||||
|
||||
// with type defs
|
||||
{
|
||||
interface Source {
|
||||
foo: string
|
||||
}
|
||||
|
||||
const p = client.helpers.search<Source>({
|
||||
index: 'test',
|
||||
body: {
|
||||
query: {
|
||||
match: { foo: 'bar' }
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
expectType<Promise<Source[]>>(p)
|
||||
expectType<Source[]>(await p)
|
||||
}
|
||||
|
||||
{
|
||||
interface SearchBody {
|
||||
query: {
|
||||
@ -317,7 +418,7 @@ expectError(
|
||||
foo: string
|
||||
}
|
||||
|
||||
const p = client.helpers.search<SearchBody, Source>({
|
||||
const p = client.helpers.search<Source, SearchBody>({
|
||||
index: 'test',
|
||||
body: {
|
||||
query: {
|
||||
|
||||
@ -16,10 +16,9 @@ import {
|
||||
TransportRequestCallback,
|
||||
RequestEvent,
|
||||
ApiError,
|
||||
ApiResponse,
|
||||
RequestBody,
|
||||
RequestNDBody,
|
||||
ResponseBody
|
||||
ApiResponse
|
||||
} from '../../lib/Transport'
|
||||
|
||||
const params = {
|
||||
@ -85,14 +84,6 @@ expectAssignable<RequestNDBody>(['string'])
|
||||
expectAssignable<RequestNDBody>(Buffer.from('hello world'))
|
||||
expectAssignable<RequestNDBody>(new ReadableStream())
|
||||
|
||||
expectAssignable<ResponseBody>({ foo: 'bar' })
|
||||
expectAssignable<ResponseBody<TestBody>>({ hello: 'world' })
|
||||
expectError<ResponseBody<TestBody>>({ foo: 'bar' })
|
||||
expectAssignable<ResponseBody>('string')
|
||||
expectAssignable<ResponseBody<TestBody>>('string')
|
||||
expectAssignable<ResponseBody>(true)
|
||||
expectAssignable<ResponseBody>(new ReadableStream())
|
||||
|
||||
const transport = new Transport({
|
||||
emit: (event, ...args) => true,
|
||||
serializer: new Serializer(),
|
||||
@ -110,16 +101,52 @@ const transport = new Transport({
|
||||
expectType<Transport>(transport)
|
||||
|
||||
expectType<TransportRequestCallback>(transport.request(params, options, (err, result) => {}))
|
||||
|
||||
// body as object
|
||||
transport.request(params, options, (err, result) => {
|
||||
expectType<ApiError>(err)
|
||||
expectType<ApiResponse>(result)
|
||||
})
|
||||
|
||||
// body as string
|
||||
transport.request({
|
||||
method: 'POST',
|
||||
path: '/search',
|
||||
body: 'hello world',
|
||||
querystring: { baz: 'faz' }
|
||||
}, options, (err, result) => {
|
||||
expectType<ApiError>(err)
|
||||
expectType<ApiResponse>(result)
|
||||
})
|
||||
|
||||
// body as Buffer
|
||||
transport.request({
|
||||
method: 'POST',
|
||||
path: '/search',
|
||||
body: Buffer.from('hello world'),
|
||||
querystring: { baz: 'faz' }
|
||||
}, options, (err, result) => {
|
||||
expectType<ApiError>(err)
|
||||
expectType<ApiResponse>(result)
|
||||
})
|
||||
|
||||
// body as ReadableStream
|
||||
transport.request({
|
||||
method: 'POST',
|
||||
path: '/search',
|
||||
body: new ReadableStream(),
|
||||
querystring: { baz: 'faz' }
|
||||
}, options, (err, result) => {
|
||||
expectType<ApiError>(err)
|
||||
expectType<ApiResponse>(result)
|
||||
})
|
||||
|
||||
const promise = transport.request(params, options)
|
||||
expectType<Promise<ApiResponse>>(promise)
|
||||
promise.then(result => expectType<ApiResponse>(result))
|
||||
expectType<ApiResponse>(await promise)
|
||||
|
||||
// body that does not respect the RequestBody constraint
|
||||
expectError(
|
||||
transport.request({
|
||||
method: 'POST',
|
||||
|
||||
Reference in New Issue
Block a user