Repository navigation
feat: add the API docs page #1643
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
Draft
jubianchi
wants to merge
2
commits into
dev-null-sh:production
Choose a base branch
from
jubianchi:feat/api-docs-shell
base: production
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.
Draft
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
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 |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Enums; | ||
|
|
||
| /** | ||
| * A page of the API docs: a guide to how the API works, in reading order. | ||
| */ | ||
| enum ApiDocsSection: string | ||
| { | ||
| case Introduction = 'introduction'; | ||
|
|
||
| public function title(): string | ||
| { | ||
| return $this->name; | ||
| } | ||
|
|
||
| public function description(): string | ||
| { | ||
| return match ($this) { | ||
| self::Introduction => 'What the API is for, and where it lives', | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * @return array{slug: string, title: string, description: string} | ||
| */ | ||
| public function toArray(): array | ||
| { | ||
| return [ | ||
| 'slug' => $this->value, | ||
| 'title' => $this->title(), | ||
| 'description' => $this->description(), | ||
| ]; | ||
| } | ||
| } |
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,30 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Http\Controllers; | ||
|
|
||
| use App\Enums\ApiDocsSection; | ||
| use App\Services\ApiGuideSnippets; | ||
| use App\Services\PageMetadata; | ||
| use Inertia\Inertia; | ||
| use Inertia\Response; | ||
|
|
||
| final readonly class ApiDocsController | ||
| { | ||
| /** | ||
| * A page of the API docs, readable by everyone, with every section for the sidebar. | ||
| */ | ||
| public function show(ApiDocsSection $section, PageMetadata $metadata, ApiGuideSnippets $snippets): Response | ||
| { | ||
| return Inertia::render('api-docs/show', [ | ||
| 'sections' => array_map(fn (ApiDocsSection $section): array => $section->toArray(), ApiDocsSection::cases()), | ||
| 'current' => $section->value, | ||
| 'snippets' => $snippets->for($section), | ||
| ])->withViewData(['preview' => $metadata->page( | ||
| $section->title().' - API docs - /dev/null', | ||
| $section->description().'. The JSON API the /dev/null apps use, open to anything you build.', | ||
| route('api-docs.show', $section), | ||
| )]); | ||
| } | ||
| } |
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,39 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Services; | ||
|
|
||
| use App\Enums\ApiDocsSection; | ||
| use Tempest\Highlight\Highlighter; | ||
|
|
||
| /** | ||
| * The code the API docs' guides show, highlighted on the server, so a guide shows it in the same code block as | ||
| * everywhere else on the site. | ||
| */ | ||
| final readonly class ApiGuideSnippets | ||
| { | ||
| public const string BaseUrl = 'https://dev-null.sh/api/v1'; | ||
|
|
||
| public function __construct(private Highlighter $highlighter) | ||
| { | ||
| // | ||
| } | ||
|
|
||
| /** | ||
| * @return array<string, array{language: string, html: string}> | ||
| */ | ||
| public function for(ApiDocsSection $section): array | ||
| { | ||
| $snippets = match ($section) { | ||
| ApiDocsSection::Introduction => [ | ||
| 'firstRequest' => ['bash', 'curl '.self::BaseUrl."/users/nunomaduro \\\n -H 'Accept: application/json'"], | ||
| ], | ||
| }; | ||
|
|
||
| return array_map(fn (array $snippet): array => [ | ||
| 'language' => $snippet[0], | ||
| 'html' => $this->highlighter->parse($snippet[1], $snippet[0]), | ||
| ], $snippets); | ||
| } | ||
| } |
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,119 @@ | ||
| import type { ReactNode } from 'react'; | ||
| import { CodeBlock } from '@/components/code-block'; | ||
| import { cn } from '@/lib/utils'; | ||
| import type { ApiDocsSnippet } from '@/types'; | ||
|
|
||
| /** | ||
| * How wide the docs' content is, so moving between their pages never changes the page's width. | ||
| */ | ||
| export const DOCS_WIDTH = 'max-w-[1160px]'; | ||
|
|
||
| /** | ||
| * A guide of the API docs, read like the site's other long pages: one Feed White sheet on the console's shell, its | ||
| * sections one after the other with a hairline between them, never a card each. | ||
| */ | ||
| export function Guide({ children }: { children: ReactNode }) { | ||
| return ( | ||
| <article | ||
| className={cn( | ||
| 'mx-auto divide-y rounded-lg border bg-card text-[15px] leading-relaxed', | ||
| DOCS_WIDTH, | ||
| )} | ||
| > | ||
| {children} | ||
| </article> | ||
| ); | ||
| } | ||
|
|
||
| function anchor(title: string): string { | ||
| return title | ||
| .toLowerCase() | ||
| .replace(/[^a-z0-9]+/g, '-') | ||
| .replace(/^-|-$/g, ''); | ||
| } | ||
|
|
||
| /** | ||
| * A section of a guide, its title a link to itself, so a reader can point someone at it. | ||
| */ | ||
| export function GuideSection({ | ||
| title, | ||
| children, | ||
| }: { | ||
| title: string; | ||
| children: ReactNode; | ||
| }) { | ||
| const id = anchor(title); | ||
|
|
||
| return ( | ||
| <section | ||
| id={id} | ||
| aria-labelledby={`${id}-title`} | ||
| className="scroll-mt-32 space-y-3 px-5 py-7 text-pretty sm:px-8 lg:scroll-mt-20 [&_a]:underline [&_a]:decoration-rail [&_a]:underline-offset-4 [&_a:hover]:decoration-current" | ||
| > | ||
| <div className="flex flex-wrap items-baseline gap-x-3"> | ||
| <h2 | ||
| id={`${id}-title`} | ||
| className="text-lg font-semibold tracking-tight" | ||
| > | ||
| <a | ||
| href={`#${id}`} | ||
| className="no-underline! hover:underline!" | ||
| > | ||
| {title} | ||
| </a> | ||
| </h2> | ||
| </div> | ||
| {children} | ||
| </section> | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * A guide's code, in the code block the rest of the site uses, highlighted by the server. | ||
| */ | ||
| export function Snippet({ snippet }: { snippet: ApiDocsSnippet | undefined }) { | ||
| if (snippet === undefined) { | ||
| return null; | ||
| } | ||
|
|
||
| return ( | ||
| <CodeBlock | ||
| html={snippet.html} | ||
| language={snippet.language} | ||
| filename={null} | ||
| /> | ||
| ); | ||
| } | ||
|
|
||
| export function Code({ children }: { children: ReactNode }) { | ||
| return ( | ||
| <code className="rounded-sm bg-muted px-1 py-0.5 font-mono text-[13px]"> | ||
| {children} | ||
| </code> | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Named values in a hairline list, keys in mono, as the settings list what they show. | ||
| */ | ||
| export function FieldList({ | ||
| fields, | ||
| }: { | ||
| fields: { name: string; children: ReactNode }[]; | ||
| }) { | ||
| return ( | ||
| <dl className="divide-y rounded-lg border"> | ||
| {fields.map((field) => ( | ||
| <div | ||
| key={field.name} | ||
| className="grid gap-x-3 gap-y-0.5 px-3 py-2.5 sm:grid-cols-[9rem_minmax(0,1fr)]" | ||
| > | ||
| <dt className="font-mono text-[13px] text-muted-foreground"> | ||
| {field.name} | ||
| </dt> | ||
| <dd className="min-w-0 text-sm">{field.children}</dd> | ||
| </div> | ||
| ))} | ||
| </dl> | ||
| ); | ||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import { usePage } from '@inertiajs/react'; | ||
| import { BookOpen } from 'lucide-react'; | ||
| import type { PropsWithChildren } from 'react'; | ||
| import ApiDocsController from '@/actions/App/Http/Controllers/ApiDocsController'; | ||
| import LogController from '@/actions/App/Http/Controllers/LogController'; | ||
| import ConsoleLayout from '@/layouts/console/layout'; | ||
| import type { ConsoleNavGroup } from '@/layouts/console/layout'; | ||
| import { home } from '@/routes'; | ||
| import type { ApiDocsProps, ApiDocsSection } from '@/types'; | ||
|
|
||
| function navItems(sections: ApiDocsSection[]) { | ||
| return sections.map((section) => ({ | ||
| key: section.slug, | ||
| title: section.title, | ||
| description: section.description, | ||
| href: ApiDocsController.show(section.slug), | ||
| icon: BookOpen, | ||
| })); | ||
| } | ||
|
|
||
| /** | ||
| * The API docs as a console beside the log, its guides in the sidebar. | ||
| */ | ||
| export default function ApiDocsLayout({ children }: PropsWithChildren) { | ||
| const { props } = usePage<ApiDocsProps>(); | ||
|
|
||
| const groups: ConsoleNavGroup[] = [ | ||
| { | ||
| label: 'Guide', | ||
| items: navItems(props.sections), | ||
| }, | ||
| ]; | ||
|
|
||
| return ( | ||
| <ConsoleLayout | ||
| chip="api" | ||
| label="/dev/null API docs, go to the introduction" | ||
| navLabel="API docs" | ||
| brandHref={ApiDocsController.show('introduction')} | ||
| prefetch | ||
| groups={groups} | ||
| current={props.current} | ||
| backHref={ | ||
| props.auth.log === null | ||
| ? home().url | ||
| : LogController.show(props.auth.log).url | ||
| } | ||
| user={props.auth.user} | ||
| headerActions={ | ||
| <span className="px-2 font-mono text-[13px] text-muted-foreground"> | ||
| v1 | ||
| </span> | ||
| } | ||
| > | ||
| {children} | ||
| </ConsoleLayout> | ||
| ); | ||
| } |
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.
Can you change this to
Developers?