|
1 | | -# Docusaurus Plugin OpenAPI (WIP) |
| 1 | +# Docusaurus OpenAPI (beta) |
2 | 2 |
|
3 | 3 |  |
4 | 4 |
|
5 | | -Install the plugin in your docusaurus project: |
| 5 | +> 💥 0.1.0 -> 0.2.0 [breaking changes](./CHANGELOG.md#020-dec-4-2021) |
6 | 6 |
|
7 | | -``` |
8 | | -yarn add docusaurus-plugin-openapi |
| 7 | +## Preset usage |
| 8 | + |
| 9 | +Install the preset in your docusaurus project by running: |
| 10 | + |
| 11 | +```sh |
| 12 | +// with npm |
| 13 | +npm install docusaurus-preset-openapi |
| 14 | + |
| 15 | +// with yarn |
| 16 | +yarn add docusaurus-preset-openapi |
9 | 17 | ``` |
10 | 18 |
|
11 | | -Add it as a plugin to `docusaurus.config.js`: |
| 19 | +The OpenAPI preset can be used as a drop-in replacement to `@docusaurus/preset-classic`: |
12 | 20 |
|
13 | 21 | ```js |
14 | | -plugins: [ |
15 | | - [ |
16 | | - "docusaurus-plugin-openapi", |
17 | | - { |
18 | | - openapiPath: require.resolve("./openapi.json"), |
19 | | - }, |
| 22 | +/* docusaurus.config.js */ |
| 23 | + |
| 24 | +{ |
| 25 | + presets: [ |
| 26 | + [ |
| 27 | + "docusaurus-preset-openapi", |
| 28 | + { |
| 29 | + // ... options |
| 30 | + }, |
| 31 | + ], |
20 | 32 | ], |
21 | | -]; |
| 33 | +} |
22 | 34 | ``` |
23 | 35 |
|
24 | | -Add it as a item in `docusaurus.config.js` to `themeConfig.navbar.items`: |
| 36 | +The default preset options will expose a route, `/api`, and look for an OpenAPI definition at `./openapi.json`. |
| 37 | + |
| 38 | +To explictly configure this, add an `api` stanza as follows: |
25 | 39 |
|
26 | 40 | ```js |
27 | | -{ to: "/api", label: "API", position: "left" } |
| 41 | +/* docusaurus.config.js */ |
| 42 | + |
| 43 | +{ |
| 44 | + presets: [ |
| 45 | + [ |
| 46 | + "docusaurus-preset-openapi", |
| 47 | + { |
| 48 | + api: { |
| 49 | + path: 'openapi.json', |
| 50 | + routeBasePath: 'api', |
| 51 | + }, |
| 52 | + // ... other options |
| 53 | + }, |
| 54 | + ], |
| 55 | + ], |
| 56 | +} |
28 | 57 | ``` |
29 | 58 |
|
30 | | -For more than one OpenAPI definition, add them as multiple plugins to `docusaurus.config.js`: |
| 59 | +To add a link to the API page, add a new item to the navbar by updating `themeConfig.navbar.items`: |
31 | 60 |
|
32 | 61 | ```js |
33 | | -plugins: [ |
34 | | - [ |
35 | | - "docusaurus-plugin-openapi", |
36 | | - { |
37 | | - id: "plugin-1", |
38 | | - openapiPath: require.resolve("./openapi1.json"), |
39 | | - routeBasePath: "cars", |
| 62 | +/* docusaurus.config.js */ |
| 63 | + |
| 64 | +{ |
| 65 | + themeConfig: { |
| 66 | + navbar: { |
| 67 | + items: [ |
| 68 | + // ... other items |
| 69 | + { to: "/api", label: "API", position: "left" }, |
| 70 | + // ... other items |
| 71 | + ], |
40 | 72 | }, |
| 73 | + }, |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +## Multiple OpenAPI Definitions |
| 78 | + |
| 79 | +To have more than one OpenAPI pages, add additional OpenAPI plugin instances: |
| 80 | + |
| 81 | +```js |
| 82 | +/* docusaurus.config.js */ |
| 83 | + |
| 84 | +{ |
| 85 | + presets: [ |
| 86 | + [ |
| 87 | + 'docusaurus-preset-openapi', |
| 88 | + { |
| 89 | + api: { |
| 90 | + // id: 'cars', // omitted => default instance |
| 91 | + path: 'cars/openapi.json', |
| 92 | + routeBasePath: 'cars', |
| 93 | + // ... other options |
| 94 | + }, |
| 95 | + }, |
| 96 | + ], |
41 | 97 | ], |
42 | | - [ |
43 | | - "docusaurus-plugin-openapi", |
44 | | - { |
45 | | - id: "plugin-2", |
46 | | - openapiPath: require.resolve("./openapi2.json"), |
47 | | - routeBasePath: "bikes", |
48 | | - }, |
| 98 | + plugins: [ |
| 99 | + [ |
| 100 | + 'docusaurus-plugin-openapi', |
| 101 | + { |
| 102 | + id: 'trains', |
| 103 | + path: 'trains/openapi.json', |
| 104 | + routeBasePath: 'trains', |
| 105 | + // ... other options |
| 106 | + }, |
| 107 | + ], |
| 108 | + [ |
| 109 | + 'docusaurus-plugin-openapi', |
| 110 | + { |
| 111 | + id: 'bikes', |
| 112 | + path: 'bikes/openapi.json', |
| 113 | + routeBasePath: 'bikes', |
| 114 | + // ... other options |
| 115 | + }, |
| 116 | + ], |
49 | 117 | ], |
50 | | -]; |
| 118 | +} |
51 | 119 | ``` |
52 | 120 |
|
53 | | -This will be resolved at /cars and /bikes endpoints respectively. |
| 121 | +This will create routes for `/cars`, `/trains` and `/bikes`. |
| 122 | + |
| 123 | +> **Note:** One instance of the plugin is included in the preset. All additional plugin instances will require an `id`. |
0 commit comments