Repository navigation
feat: add the API docs page #1533
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
jubianchi
wants to merge
5
commits into
dev-null-sh:production
Choose a base branch
from
jubianchi:feat/api-docs
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.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fc2238b
refactor: extract the console layout from the operations layout
jubianchi 41fc48d
feat: describe the v1 API as an OpenAPI spec, and serve it
jubianchi 0725c56
feat: add the API docs page
jubianchi e7eeef2
feat: link the API docs from the aside and the user menu
jubianchi aba5c0b
docs: document the API docs page
jubianchi 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 |
|---|---|---|
|
|
@@ -5,3 +5,5 @@ | |
| *.html diff=html | ||
| *.md diff=markdown | ||
| *.php diff=php | ||
|
|
||
| resources/api/openapi.json linguist-generated=true | ||
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 |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Enums; | ||
|
|
||
| /** | ||
| * A page of the API docs: a guide to how the API works, or a group of its reference, named after the first segment of | ||
| * the paths it lists, as the OpenAPI spec groups them. | ||
| */ | ||
| enum ApiDocsSection: string | ||
| { | ||
| case Introduction = 'introduction'; | ||
| case Authentication = 'authentication'; | ||
| case RateLimits = 'rate-limits'; | ||
| case Pagination = 'pagination'; | ||
| case Errors = 'errors'; | ||
| case Users = 'users'; | ||
| case Commits = 'commits'; | ||
| case Organizations = 'organizations'; | ||
| case Me = 'me'; | ||
| case Search = 'search'; | ||
| case ScheduledCommits = 'scheduled-commits'; | ||
| case Remotes = 'remotes'; | ||
| case Notifications = 'notifications'; | ||
| case Headlines = 'headlines'; | ||
| case Issues = 'issues'; | ||
|
|
||
| /** | ||
| * The guides in reading order, then the groups of the reference. | ||
| * | ||
| * @return list<self> | ||
| */ | ||
| public static function ordered(): array | ||
| { | ||
| return [...array_values(array_filter(self::cases(), fn (self $section): bool => $section->isGuide())), ...self::reference()]; | ||
| } | ||
|
|
||
| /** | ||
| * The groups of the reference alphabetically, so a reader finds one by its name, wherever they are listed. | ||
| * | ||
| * @return list<self> | ||
| */ | ||
| public static function reference(): array | ||
| { | ||
| $groups = array_values(array_filter(self::cases(), fn (self $section): bool => ! $section->isGuide())); | ||
| usort($groups, fn (self $a, self $b): int => strcmp($a->title(), $b->title())); | ||
|
|
||
| return $groups; | ||
| } | ||
|
|
||
| public function isGuide(): bool | ||
| { | ||
| return in_array($this, [self::Introduction, self::Authentication, self::RateLimits, self::Pagination, self::Errors], true); | ||
| } | ||
|
|
||
| public function title(): string | ||
| { | ||
| return match ($this) { | ||
| self::RateLimits => 'Rate limits', | ||
| self::ScheduledCommits => 'Scheduled commits', | ||
| default => $this->name, | ||
| }; | ||
| } | ||
|
|
||
| public function description(): string | ||
| { | ||
| return match ($this) { | ||
| self::Introduction => 'What the API is for, and where it lives', | ||
| self::Authentication => 'Reading as anyone, or signing requests as someone', | ||
| self::RateLimits => 'How many requests a minute, and what happens past that', | ||
| self::Pagination => 'Following a list from one page to the next', | ||
| self::Errors => 'What went wrong, and the code that says which error it is', | ||
| self::Users => 'Profiles, what they committed, and who follows whom', | ||
| self::Commits => 'Reading, writing, starring and replying to commits', | ||
| self::Organizations => 'GitHub organizations, their commits and their mentions', | ||
| self::Me => 'The signed-in user: their account, settings and devices', | ||
| self::Search => 'People, organizations and commits, by name or meaning', | ||
| self::ScheduledCommits => 'Commits waiting for their time to be posted', | ||
| self::Remotes => 'Conversations, orgs, messages and calls', | ||
| self::Notifications => 'What happened to the user, grouped as the inbox shows it', | ||
| self::Headlines => 'Hot topics: the headlines the aside shows, each with its recap', | ||
| self::Issues => 'Feature requests and bug reports, opened on GitHub', | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * @return array{slug: string, title: string, description: string, kind: 'guide'|'reference'} | ||
| */ | ||
| public function toArray(): array | ||
| { | ||
| return [ | ||
| 'slug' => $this->value, | ||
| 'title' => $this->title(), | ||
| 'description' => $this->description(), | ||
| 'kind' => $this->isGuide() ? 'guide' : 'reference', | ||
| ]; | ||
| } | ||
| } |
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,20 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Http\Controllers\Api; | ||
|
|
||
| use Illuminate\Http\Response; | ||
| use Illuminate\Support\Facades\File; | ||
|
|
||
| final readonly class OpenApiController | ||
| { | ||
| /** | ||
| * The OpenAPI spec of this API, as committed from what the code describes, for anyone building on it. Public and | ||
| * unsigned, like the docs page that reads it. It is sent whole rather than streamed, so its ETag can be computed. | ||
| */ | ||
| public function show(): Response | ||
| { | ||
| return response(File::get(resource_path('api/openapi.json')), headers: ['Content-Type' => 'application/json']); | ||
| } | ||
| } |
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,40 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Http\Controllers; | ||
|
|
||
| use App\Enums\ApiDocsSection; | ||
| use App\Enums\ApiErrorCode; | ||
| use App\Queries\ApiDocsQuery; | ||
| use App\Queries\ApiRateLimitsQuery; | ||
| use App\Services\ApiExamples; | ||
| 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, and the endpoints of a group | ||
| * of the reference. The search's index of every endpoint is sent once, and kept by the page across the visit. | ||
| */ | ||
| public function show(ApiDocsSection $section, PageMetadata $metadata, ApiDocsQuery $endpoints, ApiExamples $examples, ApiGuideSnippets $snippets, ApiRateLimitsQuery $rateLimits): Response | ||
| { | ||
| return Inertia::render('api-docs/show', [ | ||
| 'sections' => array_map(fn (ApiDocsSection $section): array => $section->toArray(), ApiDocsSection::ordered()), | ||
| 'current' => $section->value, | ||
| 'endpoints' => $section->isGuide() ? [] : $endpoints->get($section), | ||
| 'index' => Inertia::once(fn (): array => $endpoints->index()), | ||
| 'signing' => $section === ApiDocsSection::Authentication ? $examples->signing() : [], | ||
| 'rateLimits' => $section === ApiDocsSection::RateLimits ? $rateLimits->get() : [], | ||
| 'snippets' => $snippets->for($section), | ||
| 'errorCodes' => $section !== ApiDocsSection::Errors ? [] : array_map(fn (ApiErrorCode $code): array => ['code' => $code->value, 'status' => $code->status(), 'meaning' => $code->meaning()], ApiErrorCode::cases()), | ||
| ])->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,26 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Http\Controllers; | ||
|
|
||
| use App\Enums\ApiDocsSection; | ||
| use App\Queries\ApiDocsQuery; | ||
| use Illuminate\Http\JsonResponse; | ||
|
|
||
| final readonly class ApiDocsSchemaController | ||
| { | ||
| /** | ||
| * The JSON schemas of an endpoint of the API docs, which the page asks for when a reader picks the schema view, so | ||
| * they never weigh on the page itself. The route only lets a section through, unbound since the route skips the | ||
| * web group. | ||
| */ | ||
| public function show(string $section, string $endpoint, ApiDocsQuery $query): JsonResponse | ||
| { | ||
| $schemas = $query->schemas(ApiDocsSection::from($section), $endpoint); | ||
|
|
||
| return $schemas === null | ||
| ? response()->json(['message' => 'Not found.'], 404) | ||
| : response()->json($schemas); | ||
| } | ||
| } |
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
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.
Moved the limit to
CreateIssue::MAX_PER_DAYso the rate limits guide shows the number the middleware actually uses.