You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/@v2/guides/replace-servers-url.md
+15-35Lines changed: 15 additions & 35 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,35 +8,13 @@ This page describes how to replace the server URL with a decorator for a given e
8
8
9
9
## Prerequisites
10
10
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.
36
14
37
15
## Create a demo folder and description file
38
16
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.
40
18
41
19
1. Create a new folder and name it `replace-servers-demo`.
42
20
@@ -67,7 +45,8 @@ paths:
67
45
68
46
## Create a custom plugin
69
47
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.
71
50
72
51
1. In the `replace-servers-demo` folder, create a folder called `plugins`.
73
52
@@ -94,12 +73,14 @@ export default function replaceServersUrlPlugin() {
94
73
3. Save the file.
95
74
96
75
{% 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).
98
78
{% /admonition %}
99
79
100
-
## Add a decorator and associate it with an environment variable
80
+
## Add a decorator
101
81
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.
103
84
104
85
1. Inside the `plugins` folder, create a `decorators` folder.
105
86
@@ -125,14 +106,13 @@ export default function ReplaceServersURL({serverUrl}) {
125
106
3. Save the file.
126
107
127
108
{% 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.
129
110
{% /admonition %}
130
111
131
112
## Configure the plugin for use
132
113
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.
136
116
137
117
```yaml
138
118
apis:
@@ -177,7 +157,7 @@ servers:
177
157
178
158
### Check the configuration for the "proxy" server
179
159
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.
0 commit comments