Skip to content

Commit 7e0adf5

Browse files
authored
docs: improve server replace guide (#2770)
1 parent 077148d commit 7e0adf5

1 file changed

Lines changed: 15 additions & 35 deletions

File tree

docs/@v2/guides/replace-servers-url.md

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,13 @@ This page describes how to replace the server URL with a decorator for a given e
88

99
## Prerequisites
1010

11-
- [Install @redocly/cli](../installation.md). We use version 1.12.0 in this tutorial.
12-
13-
- [Install @redocly/cli](../installation.md) with version 1.0.0-beta.111 or later (we use 1.0.0-beta.111 in this tutorial).
14-
- Save the following OpenAPI file as `original.yaml` into a new directory named `replace-servers-demo`.
15-
```yaml
16-
openapi: 3.1.0
17-
info:
18-
version: 1.0.0
19-
title: Custom decorators demo
20-
description: The servers URL is replaced by the decorator during the `bundle` process.
21-
servers:
22-
- url: 'https://example.com/api/v1'
23-
paths:
24-
/status:
25-
get:
26-
summary: Get status
27-
operationId: getStatus
28-
security: []
29-
responses:
30-
'204':
31-
description: Status OK
32-
'400':
33-
description: Status not OK
34-
```
35-
- Use your favorite IDE for editing files (we use VS Code and have the [Redocly extension](https://redocly.com/docs/redocly-openapi/) installed).
11+
- [Install Redocly CLI](../installation.md) version 2.x.
12+
- Use an editor for working with YAML and JavaScript files.
13+
We use VS Code with the [Redocly extension](https://redocly.com/docs/vscode) installed.
3614

3715
## Create a demo folder and description file
3816

39-
Before you start, create a demo folder and a sample OpenAPI description file to later test your plugin and decorators.
17+
Create a working folder and a sample OpenAPI file that the decorator in the following sections uses as its input.
4018

4119
1. Create a new folder and name it `replace-servers-demo`.
4220

@@ -67,7 +45,8 @@ paths:
6745
6846
## Create a custom plugin
6947
70-
In this step, create a custom plugin and define the decorator dependency.
48+
A plugin is the container that registers a decorator with Redocly CLI.
49+
Create the plugin file first and wire up the decorator it depends on; the decorator itself is added in the next section.
7150
7251
1. In the `replace-servers-demo` folder, create a folder called `plugins`.
7352

@@ -94,12 +73,14 @@ export default function replaceServersUrlPlugin() {
9473
3. Save the file.
9574

9675
{% admonition type="info" %}
97-
If you change the names of the plugins directory or the files, make sure to change them also in the Redocly configuration file [when registering your plugins and decorators](#add-a-decorator-and-associate-it-with-an-environment-variable).
76+
The `plugins` folder and the `plugin.js` file can be renamed.
77+
In that case, the import path above, and the `plugins` entry in the Redocly configuration file, must [match the new names](#configure-the-plugin-for-use).
9878
{% /admonition %}
9979

100-
## Add a decorator and associate it with an environment variable
80+
## Add a decorator
10181

102-
In this step, add a decorator and define the environment variable associated with it.
82+
The decorator is the function that modifies the API description.
83+
It accepts a `serverUrl` parameter from the Redocly configuration file and overwrites the URL on every `Server` node in the document.
10384

10485
1. Inside the `plugins` folder, create a `decorators` folder.
10586

@@ -125,14 +106,13 @@ export default function ReplaceServersURL({serverUrl}) {
125106
3. Save the file.
126107

127108
{% admonition type="info" %}
128-
If you change the name of the decorators directory, make sure to also change it in line 1 of the [`plugin.js` file](#create-a-custom-plugin).
109+
The `decorators` folder can be renamed.In that case, keep the import path in the [`plugin.js` file](#create-a-custom-plugin) in sync with the new name.
129110
{% /admonition %}
130111

131112
## Configure the plugin for use
132113

133-
To use the decorators, register your `plugins` and `decorators` in the [Redocly configuration file](../configuration/index.md).
134-
135-
- In your Redocly configuration file, register your plugins and decorators:
114+
Register the plugin in the [Redocly configuration file](../configuration/index.md) and create one API entry per environment.
115+
Each API entry passes a different `serverUrl` value to the same decorator, producing multiple outputs from a single source file.
136116

137117
```yaml
138118
apis:
@@ -177,7 +157,7 @@ servers:
177157

178158
### Check the configuration for the "proxy" server
179159

180-
1. Run the following command bundles the `original.yaml` API with the "proxy" server URL.
160+
1. Run the following command to bundle the `original.yaml` API with the "proxy" server URL.
181161

182162
```shell
183163
npx @redocly/cli@latest bundle sample@v1-proxy

0 commit comments

Comments
 (0)