Skip to content

Commit

Permalink
feat: req in loadTypes
Browse files Browse the repository at this point in the history
  • Loading branch information
tpluscode committed Apr 4, 2022
1 parent 305a427 commit aae8333
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/lazy-swans-train.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"express-middleware-shacl": minor
---

Pass `Request` object to `loadTypes`
4 changes: 2 additions & 2 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { findNodes } from 'clownface-shacl-path'
RdfResource.factory.addMixin(...ShapeBundle)

interface ShaclMiddlewareOptions {
loadTypes?(resources: NamedNode[]): Promise<DatasetCore>
loadTypes?(resources: NamedNode[], req: Request): Promise<DatasetCore>
loadShapes(req: Request): Promise<DatasetCore>
errorContext?: string
}
Expand Down Expand Up @@ -105,7 +105,7 @@ export const shaclMiddleware = ({ loadShapes, loadTypes, errorContext = 'https:/
.filter(isNamedNode)

if (loadTypes && linkedInstances.length) {
const typeQuads = await loadTypes(linkedInstances)
const typeQuads = await loadTypes(linkedInstances, req)
for (const quad of typeQuads) {
req.shacl.dataGraph.dataset.add(quad)
}
Expand Down
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const app = express()

app.use(shaclMiddleware({
// Load the shapes to populate the Shapes Graph.
async loadShapes(req): Promise<DatasetCore> {
async loadShapes(req: express.Request): Promise<DatasetCore> {
// Should at least use the payload resource types but might also select more shapes,
// such as any shape annotated on the existing resource in database
const types = req.shacl.dataGraph.out(rdf.type).terms
Expand All @@ -33,7 +33,7 @@ app.use(shaclMiddleware({
*/
},
// (Optional) Load rdf:type quads of the given resources
loadTypes(resources: NamedNode[]): Promise<DatasetCore> {
loadTypes(resources: NamedNode[], req: express.Request): Promise<DatasetCore> {
// For example, could be implemented as a SPARQL query
/*
CONSTRUCT { ?resource a ?type }
Expand Down

0 comments on commit aae8333

Please sign in to comment.