Repository navigation
feat(api): add works and systems collection endpoints (#83) #87
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
Merged
Merged
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,38 @@ | ||
| import { test } from 'node:test'; | ||
| import assert from 'node:assert/strict'; | ||
| import { collectionBody, JSONLD_CONTEXT } from './collection.js'; | ||
| import { fixtureRegistry } from './registry.fixture.js'; | ||
|
|
||
| test('collectionBody sets the shared JSON-LD context', () => { | ||
| const body = collectionBody(fixtureRegistry.systems); | ||
| assert.equal(body['@context'], 'https://textrefs.org/contexts/v1.jsonld'); | ||
| assert.equal(body['@context'], JSONLD_CONTEXT); | ||
| }); | ||
|
|
||
| test('collectionBody sorts @graph by key', () => { | ||
| const unsorted = [...fixtureRegistry.systems].reverse(); | ||
| assert.ok(unsorted.length > 1); | ||
| const body = collectionBody(unsorted); | ||
| const keys = body['@graph'].map((r) => r.key); | ||
| const expected = [...keys].sort((a, b) => a.localeCompare(b)); | ||
| assert.deepEqual(keys, expected); | ||
| }); | ||
|
|
||
| test('collectionBody does not mutate the input array', () => { | ||
| const unsorted = [...fixtureRegistry.systems].reverse(); | ||
| const before = unsorted.map((r) => r.key); | ||
| collectionBody(unsorted); | ||
| assert.deepEqual( | ||
| unsorted.map((r) => r.key), | ||
| before, | ||
| ); | ||
| }); | ||
|
|
||
| test('collectionBody passes items through unchanged with no per-item @context', () => { | ||
| const body = collectionBody(fixtureRegistry.systems); | ||
| const item = body['@graph'].find( | ||
| (r) => r.key === fixtureRegistry.systems[0].key, | ||
| ); | ||
| assert.deepEqual(item, fixtureRegistry.systems[0]); | ||
| assert.equal(item && '@context' in item, false); | ||
| }); |
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,15 @@ | ||
| // Registry-wide JSON-LD collections served under `/reg/` (works and citation | ||
| // systems). The sort order here is shared with the HTML registry browser | ||
| // (`src/pages/reg/index.astro`), so the two views cannot drift apart. | ||
|
|
||
| export const JSONLD_CONTEXT = 'https://textrefs.org/contexts/v1.jsonld'; | ||
|
|
||
| export function byKey(a: { key: string }, b: { key: string }): number { | ||
| return a.key.localeCompare(b.key); | ||
| } | ||
|
|
||
| export function collectionBody<T extends { key: string }>( | ||
| records: readonly T[], | ||
| ) { | ||
| return { '@context': JSONLD_CONTEXT, '@graph': [...records].sort(byKey) }; | ||
| } |
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,8 @@ | ||
| import type { APIRoute } from 'astro'; | ||
| import { loadSystems } from '../../lib/registry.ts'; | ||
| import { collectionBody } from '../../lib/collection.ts'; | ||
|
|
||
| export const GET: APIRoute = () => | ||
| new Response(JSON.stringify(collectionBody(loadSystems()), null, 2), { | ||
| headers: { 'Content-Type': 'application/ld+json; charset=utf-8' }, | ||
| }); |
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,8 @@ | ||
| import type { APIRoute } from 'astro'; | ||
| import { loadWorks } from '../../lib/registry.ts'; | ||
| import { collectionBody } from '../../lib/collection.ts'; | ||
|
|
||
| export const GET: APIRoute = () => | ||
| new Response(JSON.stringify(collectionBody(loadWorks()), null, 2), { | ||
| headers: { 'Content-Type': 'application/ld+json; charset=utf-8' }, | ||
| }); |
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.
Good catch on the inconsistency — fixed in 35def8e, but in the opposite direction to the one suggested.
The site is static. GitHub Pages derives every
Content-Typefrom the file extension and discards the header the AstroAPIRoutesets, so that header never reaches a client. The live site confirms it:So
/id/…jsondeclaringapplication/jsonwas already accurate to the wire. Switching those four paths toapplication/ld+jsonwould have made the contract describe a response nobody receives. The inaccurate declarations were the two this branch introduced.Changed instead:
/reg/collections →application/json/dump/JSONL paths →application/octet-stream(they were declaredapplication/x-ndjson)Every
.jsonpath in the contract is nowapplication/jsonand every.jsonlpath isapplication/octet-stream, which is both internally consistent and true.Since the declared types no longer describe the payloads,
info.description, theBulktag, andurl-layout.mdnow state the rule explicitly: a.jsonbody is JSON-LD by content, a.jsonlbody is newline-delimited JSON, and a client should parse by documented shape rather than by response header.One related thing left alone: the
Content-Type: application/ld+json; charset=utf-8header in theAPIRoutehandlers is effectively dead code under a static build. It would matter if the site ever moved to SSR, so it stays — out of scope for this PR.