[HTTP] Add example of using version header public side (#156833)

## Summary

Added an example to the HTTP versioning tutorial of how a version can be
sent using `http.fetch`.

Also changed the example to use a more realistic version like
`2023-10-31`.

<img width="996" alt="Screenshot 2023-05-05 at 13 56 08"
src="https://user-images.githubusercontent.com/8155004/236451506-cc1838e3-6406-4890-932e-6c700a9cf681.png">
This commit is contained in:
Jean-Louis Leysens 2023-05-05 14:15:08 +02:00 committed by GitHub
parent cab27d3e21
commit f86913dbea
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -238,7 +238,7 @@ Core exposes a versioned router that ensures your endpoint's behaviour and forma
})
.addVersion(
{
version: '2023-01-01', // The public version of this API
version: '2023-10-31', // The public version of this API
validate: {
request: {
query: schema.object({
@ -264,7 +264,7 @@ Core exposes a versioned router that ensures your endpoint's behaviour and forma
// BREAKING CHANGE: { foo: string } => { fooString: string } in response body
.addVersion(
{
version: '2023-02-01',
version: '2024-10-31',
validate: {
request: {
query: schema.object({
@ -290,7 +290,7 @@ Core exposes a versioned router that ensures your endpoint's behaviour and forma
// BREAKING CHANGES: Enforce min/max length on fooString
.addVersion(
{
version: '2023-03-01',
version: '2025-03-01',
validate: {
request: {
query: schema.object({
@ -314,5 +314,25 @@ Core exposes a versioned router that ensures your endpoint's behaviour and forma
}
```
#### Use `http.fetch` to send a version
Core's `http.fetch` (and helpers like `http.get`) accept an optional `version` parameter. The `version` parameter can be used to send your request to the corresponding handler of the route.
```ts
import type { CoreSetup, Plugin } from '@kbn/core/public';
export class MyPlugin implements Plugin {
setup(core: CoreSetup): FilesSetup {
// Example call using core's http.fetch
core.http.post('/api/my-app/foo/1', {
version: '2023-10-31',
headers: { 'content-type': 'application/json' },
query: { name: 'example' },
body: JSON.stringify({ fooString: 'string' }),
});
}
}
```
#### Additional reading
For a more details on the versioning specification see [this document](https://docs.google.com/document/d/1YpF6hXIHZaHvwNaQAxWFzexUF1nbqACTtH2IfDu0ldA/edit?usp=sharing).
For more details on the versioning specification see [this document](https://docs.google.com/document/d/1YpF6hXIHZaHvwNaQAxWFzexUF1nbqACTtH2IfDu0ldA/edit?usp=sharing).