Skip to content

Commit f8bbf47

Browse files
committed
API Docs: implement custom OpenAPI component
Previously, there was no easy way to reference docs for our REST API endpoints. This PR adds a new `<ApiOperation/>` React component that we can use in MDX files to embed docs for a single API operation. The component renders the code examples, request schema, and response schema using existing components like code blocks and HTML tables avoiding the need for custom CSS styling.
1 parent f52265f commit f8bbf47

File tree

13 files changed

+1399
-2
lines changed

13 files changed

+1399
-2
lines changed

docs/api/cody.mdx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Sourcegraph Cody API
2+
3+
## `POST /.api/cody/context`
4+
<ApiOperation operation="POST /.api/cody/context" />
5+
6+
## `POST /.api/llm/chat/completions`
7+
<ApiOperation operation="POST /.api/llm/chat/completions" />
8+
9+
## `GET /.api/llm/models`
10+
<ApiOperation operation="GET /.api/llm/models" />
11+
12+
## `GET /.api/llm/models/{modelId}`
13+
<ApiOperation operation="GET /.api/llm/models/{modelId}" />

docs/api/index.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
Sourcegraph exposes the following APIs:
44

55
- [Sourcegraph GraphQL API](/api/graphql/), for accessing data stored or computed by Sourcegraph
6+
- [Sourcegraph Cody API](/api/cody/), for interacting with Cody.
67
- [Sourcegraph Stream API](/api/stream_api/), for consuming search results as a stream of events

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"dev": "next dev",
77
"build": "next build",
88
"start": "next start",
9-
"lint": "next lint"
9+
"lint": "next lint",
10+
"openapi:watch": "./scripts/openapi-watch.sh"
1011
},
1112
"browserslist": "defaults, not ie <= 11",
1213
"dependencies": {
@@ -46,6 +47,7 @@
4647
"react": "18.3.1",
4748
"react-dom": "18.3.1",
4849
"react-highlight-words": "^0.20.0",
50+
"react-markdown": "^9.0.1",
4951
"react-syntax-highlighter": "^15.5.0",
5052
"rehype-autolink-headings": "^7.1.0",
5153
"rehype-pretty-code": "^0.10.2",

pnpm-lock.yaml

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/openapi-compile.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/bash
2+
set -eux
3+
4+
# Check if yq is installed, if not, install it using Homebrew
5+
if ! command -v yq &> /dev/null
6+
then
7+
echo "yq could not be found, installing via Homebrew..."
8+
if ! command -v brew &> /dev/null
9+
then
10+
echo "Homebrew is not installed. Please install Homebrew first."
11+
exit 1
12+
fi
13+
brew install yq
14+
fi
15+
16+
17+
SOURCEGRAPH_DIR=$1
18+
DOCS_DIR=$2
19+
cd "$SOURCEGRAPH_DIR"
20+
pnpm -C internal/openapi compile
21+
yq eval -o=json internal/openapi/tsp-output/@typespec/openapi3/openapi.Sourcegraph.Latest.yaml > "$DOCS_DIR"/src/components/openapi/openapi.Sourcegraph.Latest.json

scripts/openapi-watch.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/bash
2+
set -eux
3+
4+
5+
# Check if entr is installed
6+
if ! command -v entr &> /dev/null
7+
then
8+
echo "entr could not be found, installing with Homebrew..."
9+
brew install entr
10+
fi
11+
12+
COMPILE_SCRIPT=$(dirname "${BASH_SOURCE[0]}")/openapi-compile.sh
13+
DOCS_DIR=$(realpath $(dirname "${BASH_SOURCE[0]}")/../)
14+
SOURCEGRAPH_DIR=${SOURCEGRAPH_DIR:-$(dirname "${BASH_SOURCE[0]}")/../../sourcegraph}
15+
SOURCEGRAPH_DIR=$(realpath $SOURCEGRAPH_DIR)
16+
echo $SOURCEGRAPH_DIR/internal/openapi/public.tsp | entr $COMPILE_SCRIPT $SOURCEGRAPH_DIR $DOCS_DIR

src/components/MdxComponents.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import AWSOneClickLaunchForm from './AWSOneClickLaunchForm';
22
import { ContentTab, ContentTabs } from './ContentTabs';
33
import FeatureParity from './FeatureParity';
4+
import { PreCode, PreCodeBlock } from './PreCodeBlock';
45
import Accordion from './mdx/Accordion';
56
import { Callout } from './mdx/Callout';
67
import { CustomLink } from './mdx/CustomLink';
@@ -9,12 +10,13 @@ import { LinkCard, LinkCards } from './mdx/LinkCards';
910
import { ProductCard, ProductCards } from './mdx/ProductCards';
1011
import { QuickLink, QuickLinks } from './mdx/QuickLinks';
1112
import { Tab, Tabs } from './mdx/Tabs';
12-
import { PreCodeBlock, PreCode } from './PreCodeBlock';
13+
import { ApiOperation } from './openapi/ApiOperation';
1314
import ResourceEstimator from './resource-estimator/ResourceEstimator';
1415
import { Badge } from './ui/badge';
1516

1617
const MdxComponents = (version?: string) => {
1718
return {
19+
ApiOperation,
1820
FeatureParity,
1921
ResourceEstimator,
2022
AWSOneClickLaunchForm,

src/components/mdx/Tabs.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ export function Tabs({children}: TabsProps) {
6868
}
6969

7070
interface TabProps {
71+
title?: string;
7172
children: ReactNode;
7273
}
7374

0 commit comments

Comments
 (0)