Skip to content

Commit 7d9317d

Browse files
authoredJan 25, 2021
Updated documentation (elastic#81)
1 parent 2f57e16 commit 7d9317d

File tree

3 files changed

+123
-14
lines changed

3 files changed

+123
-14
lines changed
 

‎README.md

+10-14
Original file line numberDiff line numberDiff line change
@@ -184,13 +184,6 @@ Namespaced APIs can be validated in the same way, for example:
184184
./run-validations.sh --api cat.health --request
185185
```
186186

187-
### Where do I see the test?
188-
189-
Everytime you run the `run-validations` script, a series of test will be generated and dumped on disk.
190-
You can find them in `clients-flight-recorder/scripts/types-validator/workbench`.
191-
The content of this folder is a series of recorded responses from Elasticsearch wrapped inside an helper
192-
that verifies if the type definiton is correct.
193-
194187
## Custom types
195188

196189
The goal of the specification is to be used by different languages, from dynamically typed to statically typed.
@@ -345,9 +338,7 @@ propertyOptional?: string
345338

346339
### A definition is missing, how do I add it?
347340

348-
All the definitons are in the [`specifications/specs`](./specifications/specs) folder, you should explore its content and find the
349-
most approriate place where to add the new defintion. You can either create a new file or update an existing one.
350-
If possible, try to reuse existing type definitions (eg `Indices` instead of `string`).
341+
See [here](./docs/add-new-api.md).
351342

352343
### A definition is not correct, how do I fix it?
353344

@@ -356,9 +347,7 @@ you can find above how to run the validation of the spec.
356347

357348
### An endpoint is missing, how do I add it?
358349

359-
All the endpoint definitons are inside `specifications/specs/_json_spec` folder, which contains a series of
360-
JSON files taken directly from the Elasticsearch rest-api-spec.
361-
You should copy from there the missing endpoint and add it here.
350+
See [here](./docs/add-new-api.md).
362351

363352
### An endpoint definition is not correct, how do I fix it?
364353

@@ -382,6 +371,13 @@ cd client-flight-recorder
382371
git pull
383372
```
384373

374+
### Where do I find the generated test?
375+
376+
Everytime you run the `run-validations` script, a series of test will be generated and dumped on disk.
377+
You can find them in `clients-flight-recorder/scripts/types-validator/workbench`.
378+
The content of this folder is a series of recorded responses from Elasticsearch wrapped inside an helper
379+
that verifies if the type definiton is correct.
380+
385381
### Which editor should I use?
386382

387383
Any editor is fine, but to have a better development experience it should be configured
@@ -390,7 +386,7 @@ to work with TypeScript. [Visual Studio Code](https://code.visualstudio.com/) an
390386

391387
### Is there a complete example of the process?
392388

393-
Yes, take a look [here](./validation-example.md).
389+
Yes, take a look [here](./docs/validation-example.md).
394390

395391
### realpath: command not found
396392

‎docs/add-new-api.md

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# How to add a new API
2+
3+
It might happen that a new API in Elasticsearch is not yet defined
4+
in this repository, or we do have an endpoint definition in [`/specification/specs/_json_spec`](../specification/specs/_json_spec)
5+
but we don't have a type definition for it.
6+
In this document you will see how to add a new endpopint and how to add a new endpoint definition.
7+
8+
**NOTE:** Currenlty we are following the work on the `7.x` release line.
9+
10+
## How to add a new endpoint
11+
12+
Add a new endpoint is straightforward, you only need to copy-paste the json rest-api-spec defintion
13+
from the Elasticsearch repository inside [`/specification/specs/_json_spec`](../specification/specs/_json_spec)
14+
and you are good to go.
15+
16+
You can find the rest-api-spec definitions [here](https://github.com/elastic/elasticsearch/tree/7.x/rest-api-spec/src/main/resources/rest-api-spec/api)
17+
or [here](https://github.com/elastic/elasticsearch/tree/7.x/x-pack/plugin/src/test/resources/rest-api-spec/api).
18+
19+
## How to add the definition of an endpoint
20+
21+
Once you have added a new endpoint definition, the next step is to add its type definition.
22+
First of all, you should find the most approariate place inside [`/specification/specs`](../specification/specs)
23+
where to put the new definition. The content of [`/specification/specs`](../specification/specs)
24+
tryied to mimic the Elasticsearch online documentation, so you can use it as inspiration.
25+
For example, the index document defintion can be found in [`/specification/specs/document/single/index`](../specification/specs/document/single/index).
26+
27+
Once you have found the best place for the new definition, you should create a new file for it.
28+
The filename should be the same of the type definition you are writing, for example:
29+
30+
```ts
31+
// IndexRequest.ts
32+
class IndexRequest {}
33+
```
34+
35+
```ts
36+
// IndexResponse.ts
37+
class IndexResponse {}
38+
```
39+
40+
Try to use less files as possible, for example there is no need to create a custom file for an enum,
41+
you can define it in the same file where it's used, unless is a commonly used type.
42+
43+
### Add the endpoint request definition
44+
45+
Request definitions are slighly different from other definitions.
46+
A request definition should contains three top level keys:
47+
48+
- `path_parts`: the path parameters (eg: `indices`, `id`...)
49+
- `query_parameters`: the query parameters (eg: `timeout`, `pipeline`...)
50+
- `body`: the body parameters (eg: `query` or user defined entities)
51+
52+
Finally there should be a decorator to inform the compiler that this is a endpoint request definition.
53+
The value of the decorator should be the endpoint name as it's defined in the json spec (eg: `search`, `indices.create`...).
54+
Following you can find a template valid for any request definition.
55+
56+
```ts
57+
@rest_spec_name("endpoint.name")
58+
class EndpointRequest extends RequestBase {
59+
path_parts?: {
60+
61+
};
62+
query_parameters?: {
63+
64+
};
65+
body?: {
66+
67+
};
68+
}
69+
```
70+
71+
In some cases, the request could take one or more generics, in such case the definition will be:
72+
```ts
73+
@rest_spec_name("endpoint.name")
74+
class EndpointRequest<Generic> extends RequestBase {
75+
path_parts?: {
76+
77+
};
78+
query_parameters?: {
79+
80+
};
81+
body?: {
82+
83+
};
84+
}
85+
```
86+
And the generic will be used somewhere inside the definition.
87+
There are cases where the generic might be the entire body, see [`IndexRequest`](../specification/specs/document/single/index/IndexRequest.ts).
88+
89+
### Add the endpoint response definition
90+
91+
Responses definitions should always be defined **after** a request definition,
92+
otherwise the compiler will not pick them up. It is required that the response
93+
definition has the same name as the request definition aside from the `Response` suffix.
94+
95+
For example the response definition for `ClusterHealthRequest` will be named `ClusterHealthResponse`.
96+
Following you can find a template valid for any response definition.
97+
98+
```ts
99+
class EndpointResponse extends ResponseBase {
100+
101+
}
102+
```
103+
104+
As you can see, for responses there are no custom top level keys, as the
105+
response definition represents the body of a succesful response.
106+
107+
In some cases, the response could take one or more generics, in such case the definition will be:
108+
```ts
109+
class EndpointResponse<Generic> extends ResponseBase {
110+
111+
}
112+
```
113+
And the generic will be used somewhere inside the definition.
File renamed without changes.

0 commit comments

Comments
 (0)