Skip to content

Commit

Permalink
feat: getCoreRepresentation handlers (#213)
Browse files Browse the repository at this point in the history
* feat: getCoreRepresentation handlers

* test: getCoreRepresentation

* fix spelling
  • Loading branch information
tpluscode authored Jan 28, 2025
1 parent aef55d4 commit dbc9de5
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/perfect-humans-own.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@kopflos-cms/core": patch
---

Added `handlers.js` module with `getCoreRepresentation` handler
12 changes: 12 additions & 0 deletions packages/core/handlers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { log, type SubjectHandler } from './index.js'

export function getCoreRepresentation(): SubjectHandler {
log.warn('Using @kopflos-cms/core/handlers.js#getCoreRepresentation is not equivalent to skipping the handler altogether because it materiliazes the dataset. This is not recommended for large resources.')

return ({ subject }) => {
return {
status: 200,
body: subject.dataset.toStream(),
}
}
}
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
},
"exports": {
".": "./index.js",
"./handlers.js": "./handlers.js",
"./env.js": "./lib/env/index.js",
"./resourceLoaders.js": "./resourceLoaders.js",
"./plugins.js": "./plugins.js",
Expand Down
6 changes: 6 additions & 0 deletions packages/core/test/__snapshots__/handlers.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`@kopflos-cms/handlers.js getCoreRepresentation forwards core representation 1`] = `
"<http://example.org/foo> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://schema.org/Thing> .
"
`;
50 changes: 50 additions & 0 deletions packages/core/test/handlers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import type { Stream } from '@rdfjs/types'
import { expect, use } from 'chai'
import rdf from '@zazuko/env-node'
import snapshots from 'mocha-chai-rdf/snapshots.js'
import { createStore } from 'mocha-chai-rdf/store.js'
import { getCoreRepresentation } from '../handlers.js'
import type { Body, KopflosConfig } from '../lib/Kopflos.js'
import { ex } from '../../testing-helpers/ns.js'
import Kopflos from '../lib/Kopflos.js'

describe('@kopflos-cms/handlers.js', () => {
use(snapshots)

before(createStore(import.meta.url, { format: 'trig', includeDefaultGraph: true }))

const config: KopflosConfig = {
baseIri: 'http://example.com/',
sparql: {
default: 'http://localhost:8080/sparql',
},
}
describe('getCoreRepresentation', () => {
it('forwards core representation', async function () {
// given
const kopflos = new Kopflos(config, {
dataset: this.rdf.dataset,
resourceShapeLookup: async () => [{
api: ex.api,
resourceShape: ex.FooShape,
subject: ex.foo,
}],
handlerLookup: () => [getCoreRepresentation()],
})

// when
const response = await kopflos.handleRequest({
iri: ex.foo,
method: 'GET',
headers: {},
body: {} as Body,
query: {},
}) as unknown as { status: number; body: Stream }

// then
expect(response).to.have.property('status', 200)
const dataset = await rdf.dataset().import(response.body)
expect(dataset).canonical.toMatchSnapshot()
})
})
})
27 changes: 27 additions & 0 deletions packages/core/test/handlers.test.ts.trig
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
PREFIX ex: <http://example.org/>
PREFIX code: <https://code.described.at/>
prefix kopflos: <https://kopflos.described.at/>

ex:config
a kopflos:Config ;
kopflos:api ex:api1 ;
kopflos:api ex:api2 ;
.

ex:api1 a kopflos:Api .

ex:api2 a kopflos:Api .

ex:FooShape
a kopflos:ResourceShape ;
kopflos:api ex:api1 ;
kopflos:resourceLoader """
function(iri, { env }) {
return env.clownface()
.namedNode(iri)
.addOut(env.ns.rdf.type, env.ns.schema.Thing)
.dataset
.toStream()
}
"""^^code:EcmaScript ;
.

0 comments on commit dbc9de5

Please sign in to comment.