|
| 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. |
0 commit comments