-
Notifications
You must be signed in to change notification settings - Fork 120
Add ApiOperation component #416
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
svenkonings
wants to merge
2
commits into
rohit-gohri:main
Choose a base branch
from
svenkonings:feature/api-operation-component
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'docusaurus-theme-redoc': minor | ||
--- | ||
|
||
Add ApiOperation component to import and display the definitions of API operations within your Docusaurus docs |
63 changes: 63 additions & 0 deletions
63
packages/docusaurus-theme-redoc/src/theme/ApiOperation/ApiOperation.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import React, { useEffect } from 'react'; | ||
import clsx from 'clsx'; | ||
import { ThemeProvider } from 'styled-components'; | ||
import '../../global'; | ||
import { Operation, OperationModel, Section } from 'redoc'; | ||
import { useSpec } from '../../utils/useSpec'; | ||
import { useSpecData } from '../useSpecData'; | ||
import type { ApiOperationProps as Props } from '../../types/common'; | ||
import '../Redoc/styles.css'; | ||
import './styles.css'; | ||
|
||
const ApiOperation: React.FC<Props> = ({ | ||
id, | ||
example, | ||
pointer, | ||
...rest | ||
}: Props): JSX.Element => { | ||
const specProps = useSpecData(id); | ||
const { store } = useSpec(specProps); | ||
|
||
// The # at the start is not included | ||
const operationPointer = | ||
pointer.charAt(0) === '#' ? pointer.substring(1) : pointer; | ||
|
||
// The menu contains a flattened list of spec items for easy searching | ||
const model = store.menu.flatItems.find( | ||
(item) => | ||
item instanceof OperationModel && item.pointer === operationPointer, | ||
) as OperationModel | undefined; | ||
|
||
if (!model) { | ||
throw new Error(`Failed to resolve reference "${pointer}"`); | ||
} | ||
|
||
useEffect(() => { | ||
/** | ||
* @see https://github.com/Redocly/redoc/blob/823be24b313c3a2445df7e0801a0cc79c20bacd1/src/services/MenuStore.ts#L273-L276 | ||
*/ | ||
store.menu.dispose(); | ||
}, [store]); | ||
|
||
return ( | ||
<ThemeProvider theme={store.options.theme}> | ||
<div | ||
className={clsx([ | ||
'redocusaurus', | ||
'redocusaurus-operation', | ||
example ? null : 'hide-example', | ||
])} | ||
> | ||
<Section id={model.id} $underlined={true}> | ||
<Operation operation={model} {...rest} /> | ||
</Section> | ||
</div> | ||
</ThemeProvider> | ||
); | ||
}; | ||
|
||
ApiOperation.defaultProps = { | ||
example: false, | ||
}; | ||
|
||
export default ApiOperation; |
3 changes: 3 additions & 0 deletions
3
packages/docusaurus-theme-redoc/src/theme/ApiOperation/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import ApiOperation from './ApiOperation'; | ||
|
||
export default ApiOperation; |
14 changes: 14 additions & 0 deletions
14
packages/docusaurus-theme-redoc/src/theme/ApiOperation/styles.css
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
.redocusaurus-operation.hide-example > div > div > div:nth-child(2) { | ||
display: none; | ||
} | ||
.redocusaurus-operation.hide-example > div > div > div:nth-child(1) { | ||
width: 100%; | ||
} | ||
|
||
.redocusaurus-operation h3 { | ||
color: var(--ifm-color-white); | ||
} | ||
|
||
.redocusaurus-operation > div:last-child { | ||
min-height: 0; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
--- | ||
title: Migrating to V1 | ||
sidebar_position: 4 | ||
sidebar_position: 5 | ||
--- | ||
|
||
## Options Changed | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
--- | ||
title: Showing Multiple APIs | ||
sidebar_position: 2 | ||
sidebar_position: 3 | ||
--- | ||
|
||
## Nested View with MDX | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
--- | ||
title: Operation Imports | ||
sidebar_position: 2 | ||
--- | ||
|
||
import ApiOperation from '@theme/ApiOperation'; | ||
|
||
# Operation Imports | ||
|
||
You can import operation definitions from your API schema and render them in your Docusaurus Docs. You'll need to create an `.mdx` file and import the React Component. Read more [here about MDX in Docusaurus](https://docusaurus.io/docs/markdown-features/react). | ||
|
||
# Import Operation Model in Docs | ||
|
||
The `pointer` prop is passed on to [Redoc](https://redoc.ly/docs/resources/ref-guide/#pointer). | ||
|
||
Note that `/` is escaped as `~1`. | ||
|
||
```tsx | ||
import ApiOperation from '@theme/ApiOperation'; | ||
|
||
<ApiOperation pointer="#/paths/~1pet/post" />; | ||
``` | ||
|
||
### Results | ||
|
||
<ApiOperation pointer="#/paths/~1pet/post" /> | ||
|
||
## Import Operation Model (with example) in Docs | ||
|
||
```tsx | ||
import ApiOperation from '@theme/ApiOperation'; | ||
|
||
<ApiOperation example pointer="#/paths/~1pet/post" />; | ||
``` | ||
|
||
### Results | ||
|
||
<ApiOperation example pointer="#/paths/~1pet/post" /> | ||
|
||
## Importing Operation Model with multiple OpenAPI schemas | ||
|
||
If you have multiple APIs loaded with redocusaurus, then it is recommended to add `id`s to the config so that you can refer them when loading operation models. | ||
|
||
```js title="docusaurus.config.js" | ||
const config = { | ||
presets: [ | ||
'@docusaurus/preset-classic', | ||
[ | ||
'redocusaurus', | ||
{ | ||
specs: [ | ||
{ | ||
id: 'using-single-yaml', | ||
spec: 'openapi/single-file/openapi.yaml', | ||
route: '/examples/using-single-yaml/', | ||
}, | ||
{ | ||
id: 'using-remote-url', | ||
spec: 'https://redocly.github.io/redoc/openapi.yaml', | ||
route: '/examples/using-remote-url/', | ||
}, | ||
], | ||
theme: { | ||
/** | ||
* Highlight color for docs | ||
*/ | ||
primaryColor: '#1890ff', | ||
/** | ||
* Options to pass to redoc | ||
* @see https://github.com/redocly/redoc#redoc-options-object | ||
*/ | ||
options: { disableSearch: true }, | ||
}, | ||
}, | ||
], | ||
], | ||
title: 'Redocusaurus', | ||
}; | ||
``` | ||
|
||
```tsx | ||
import ApiOperation from '@theme/ApiOperation'; | ||
|
||
<ApiOperation id="using-single-yaml" pointer="#/paths/~1pet/post" /> | ||
<ApiOperation id="using-remote-url" pointer="#/paths/~1pet/post" /> | ||
``` | ||
|
||
### Results | ||
|
||
#### For ID `id="using-single-yaml"` | ||
|
||
<ApiOperation id="using-single-yaml" pointer="#/paths/~1pet/post" /> | ||
|
||
#### For ID `id="using-remote-url"` | ||
|
||
<ApiOperation id="using-remote-url" pointer="#/paths/~1pet/post" /> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this a Redoc convention?