Skip to content

Commit

Permalink
Merge pull request #124 from awarrenlove/new-option-add-route
Browse files Browse the repository at this point in the history
  • Loading branch information
rohit-gohri authored Feb 11, 2022
2 parents 629f140 + 4035dc7 commit c7a8475
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/tricky-toes-fly.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'docusaurus-plugin-redoc': minor
---

Add addRoute option to create a route for the generated docs, default to true
32 changes: 17 additions & 15 deletions packages/docusaurus-plugin-redoc/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,23 +75,25 @@ export default function redocPlugin(
JSON.stringify(options.layout),
);

const routePath = options.routePath.startsWith('/')
? options.routePath.slice(1)
: options.routePath;
const routeOptions = {
path: normalizeUrl([baseUrl, routePath]),
component: options.apiDocComponent,
modules: {
spec: specData,
layoutProps: layoutData,
},
exact: true,
};
if (options.addRoute) {
const routePath = options.routePath.startsWith('/')
? options.routePath.slice(1)
: options.routePath;
const routeOptions = {
path: normalizeUrl([baseUrl, routePath]),
component: options.apiDocComponent,
modules: {
spec: specData,
layoutProps: layoutData,
},
exact: true,
};

if (debug) {
console.error('[REDOCUSAURUS_PLUGIN] Route:', routeOptions);
if (debug) {
console.error('[REDOCUSAURUS_PLUGIN] Route:', routeOptions);
}
addRoute(routeOptions);
}
addRoute(routeOptions);
},
};
}
Expand Down
4 changes: 4 additions & 0 deletions packages/docusaurus-plugin-redoc/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,22 @@ export interface PluginOptions {
specUrl?: string;
layout?: LayoutProps;
debug?: boolean;
addRoute?: boolean;
routePath?: string;
apiDocComponent?: string;
}

export interface PluginOptionsWithDefault extends PluginOptions {
debug: boolean;
addRoute: boolean;
routePath: string;
apiDocComponent: string;
}

export const DEFAULT_OPTIONS: PluginOptionsWithDefault = {
layout: {},
debug: false,
addRoute: true,
routePath: '/api/', // URL Route.
apiDocComponent: '@theme/ApiDoc',
};
Expand All @@ -56,6 +59,7 @@ export const PluginOptionSchema = Joi.object({
specUrl: Joi.string().uri({ allowRelative: true }),
layout: Joi.any().default(DEFAULT_OPTIONS.layout),
debug: Joi.boolean().default(DEFAULT_OPTIONS.debug),
addRoute: Joi.boolean().default(DEFAULT_OPTIONS.addRoute),
routePath: Joi.string().default(DEFAULT_OPTIONS.routePath),
apiDocComponent: Joi.string().default(DEFAULT_OPTIONS.apiDocComponent),
});
39 changes: 38 additions & 1 deletion website/docs/getting-started/Installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,41 @@ npm i --save redocusaurus
};
```

The API Doc will be available by default at `/api/` path. To customize it see [full plugin options](#options).
The API Doc will be available by default at `/api/` path. If you wish to
override this path, you may set the `routePath` option. To skip adding a
route altogether, set the `addRoute` option to false. You will still be
able to reference schema elements manually using [Schema Imports](/docs/guides/schema-imports).

```js
// docusaurus.config.js
module.exports = {
// ...
presets: [
[
'redocusaurus',
{
specs: [
{
id: 'default-route',
// routePath: '/api/',
// addRoute: true,
},
{
id: 'route-overridden',
routePath: '/different-path/',
// addRoute: true,
},
{
id: 'no-route',
addRoute: false,
},
],
},
],
],
// ...
};
```

To customize it see [full plugin options](#options).

0 comments on commit c7a8475

Please sign in to comment.