Skip to content
This repository has been archived by the owner on May 14, 2020. It is now read-only.

Use namespace builder library #260

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
"@koa/cors": "^3.0.0",
"@koa/router": "^8.0.8",
"@rdfjs/data-model": "^1.1.2",
"@rdfjs/namespace": "^1.1.0",
"@rdfjs/parser-jsonld": "^1.2.0",
"@rdfjs/serializer-jsonld-ext": "^2.0.0",
"@tpluscode/rdf-ns-builders": "^0.1.0",
"clownface": "^0.12.1",
"content-type": "^1.0.4",
"format-link-header": "^3.0.0",
Expand Down Expand Up @@ -45,7 +45,6 @@
"@types/rdf-dataset-ext": "^1.0.0",
"@types/rdf-dataset-indexed": "^0.4.3",
"@types/rdf-js": "^2.0.11",
"@types/rdfjs__namespace": "^1.1.1",
"@types/rdfjs__parser-jsonld": "^1.2.2",
"@types/rdfjs__serializer-jsonld-ext": "^2.0.2",
"@types/supertest": "^2.0.8",
Expand Down
2 changes: 1 addition & 1 deletion src/adaptors/in-memory-articles.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { rdf, schema } from '@tpluscode/rdf-ns-builders';
import {
DatasetCore, NamedNode, Quad, Quad_Object as QuadObject,
} from 'rdf-js';
import Articles from '../articles';
import ArticleNotFound from '../errors/article-not-found';
import NotAnArticle from '../errors/not-an-article';
import { rdf, schema } from '../namespaces';

export default class InMemoryArticles implements Articles {
private articles: { [id: string]: [NamedNode, DatasetCore] } = {};
Expand Down
2 changes: 1 addition & 1 deletion src/adaptors/postgres-articles.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import ParserJsonld from '@rdfjs/parser-jsonld';
import SerializerJsonld from '@rdfjs/serializer-jsonld-ext';
import { rdf, schema } from '@tpluscode/rdf-ns-builders';
import { JsonLdObj } from 'jsonld/jsonld-spec';
import pEvent from 'p-event';
import {
Expand All @@ -15,7 +16,6 @@ import Articles from '../articles';
import ArticleNotFound from '../errors/article-not-found';
import NotAnArticle from '../errors/not-an-article';
import { ExtendedDataFactory } from '../middleware/dataset';
import { rdf, schema } from '../namespaces';

const { QueryResultError, queryResultErrorCode: { noData } } = errors;

Expand Down
9 changes: 7 additions & 2 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import cors from '@koa/cors';
import Router, { RouterContext } from '@koa/router';
import {
hydra, owl, rdf, schema,
} from '@tpluscode/rdf-ns-builders';
import Koa, { DefaultState, Middleware } from 'koa';
import logger from 'koa-logger';
import Articles from './articles';
Expand All @@ -10,7 +13,6 @@ import emptyResponse from './middleware/empty-response';
import errorHandler from './middleware/error-handler';
import jsonld from './middleware/jsonld';
import routing from './middleware/routing';
import namespaces from './namespaces';

export type AppState = DefaultState;

Expand Down Expand Up @@ -44,7 +46,10 @@ export default (
app.use(addDatasets());
app.use(jsonld({
'@language': 'en',
...namespaces,
hydra: hydra().value,
owl: owl().value,
rdf: rdf().value,
schema: schema().value,
Comment on lines +49 to +52
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tpluscode Maybe we could add a base or root function to get the base IRI as a string?

Copy link

@tpluscode tpluscode Mar 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about this, and I'm not sure. Do you find yourself often in need of the base URI as string?

It would have to have a unique name, like schema.__base. Unless I duplicated the prefixes export and generated a default export

import prefixes from '@tpluscode/rdf-ns-builders'

app.use(jsonld({
  ...prefixes
}))

And neither is great IMO

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you find yourself often in need of the base URI as string?

Only case I think, as this is outside RDF/JS.

You're right, neither of those is great...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opened zazuko/rdf-vocabularies#55 to try and use a library rather than hard-coding them here.

}));
app.use(apiDocumentationLink(apiDocumentationPath));
app.use(errorHandler());
Expand Down
2 changes: 1 addition & 1 deletion src/errors/not-an-article.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { schema } from '@tpluscode/rdf-ns-builders';
import { Quad_Object as QuadObject } from 'rdf-js';
import { termToString } from 'rdf-string';
import { schema } from '../namespaces';

export default class NotAnArticle extends Error {
readonly types: Array<QuadObject>;
Expand Down
2 changes: 1 addition & 1 deletion src/middleware/error-handler.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { hydra, rdf } from '@tpluscode/rdf-ns-builders';
import createHttpError, { HttpError } from 'http-errors';
import {
DefaultStateExtends, ExtendableContext, Middleware, Next,
} from 'koa';
import { hydra, rdf } from '../namespaces';
import { DatasetContext } from './dataset';

const handleHttpError = (
Expand Down
15 changes: 0 additions & 15 deletions src/namespaces.ts

This file was deleted.

6 changes: 3 additions & 3 deletions src/routes/add-article.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { rdf, schema } from '@tpluscode/rdf-ns-builders';
import clownface from 'clownface';
import createHttpError from 'http-errors';
import { CREATED } from 'http-status-codes';
Expand All @@ -7,7 +8,6 @@ import { termToString } from 'rdf-string';
import uniqueString from 'unique-string';
import url from 'url';
import { AppContext, AppMiddleware } from '../app';
import { rdf, schema } from '../namespaces';
import Routes from './index';

export default (): AppMiddleware => (
Expand All @@ -24,8 +24,8 @@ export default (): AppMiddleware => (
throw new createHttpError.BadRequest(`Article must have a blank node identifier (${termToString(id)} given)`);
}

if (request.dataset.match(id, schema('name')).size === 0) {
throw new createHttpError.BadRequest(`Article must have at least one ${termToString(schema('name'))}`);
if (request.dataset.match(id, schema.name).size === 0) {
throw new createHttpError.BadRequest(`Article must have at least one ${termToString(schema.name)}`);
Comment on lines +27 to +28
Copy link
Contributor Author

@thewilkybarkid thewilkybarkid Feb 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As name is now a defined property on the builder, TypeScript now correctly recognises it as a NamedNode.

PHPStorm seems a bit confused (though doesn't complain):
Screenshot 2020-02-28 at 10 53 33

}

const newId = namedNode(url.resolve(request.origin, router.url(Routes.Article, uniqueString())));
Expand Down
10 changes: 5 additions & 5 deletions src/routes/api-documentation.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {
hydra, owl, rdf, schema,
} from '@tpluscode/rdf-ns-builders';
import clownface, { Clownface } from 'clownface';
import { CREATED, OK } from 'http-status-codes';
import { Next } from 'koa';
import { NamedNode } from 'rdf-js';
import { toRdf } from 'rdf-literal';
import url from 'url';
import { AppContext, AppMiddleware } from '../app';
import {
hydra, owl, rdf, schema,
} from '../namespaces';
import Routes from './index';

export default (): AppMiddleware => (
Expand Down Expand Up @@ -40,7 +40,7 @@ export default (): AppMiddleware => (
entryPoint.addOut(hydra.supportedProperty, (name: Clownface): void => {
name.addOut(rdf.type, hydra.SupportedProperty);
name.addOut(hydra.title, literal('Name', 'en'));
name.addOut(hydra.property, schema('name'), (property: Clownface): void => {
name.addOut(hydra.property, schema.name, (property: Clownface): void => {
property.addOut(rdf.type, rdf.Property);
});
name.addOut(hydra.required, true);
Expand All @@ -56,7 +56,7 @@ export default (): AppMiddleware => (
article.addOut(hydra.supportedProperty, (name: Clownface): void => {
name.addOut(rdf.type, hydra.SupportedProperty);
name.addOut(hydra.title, literal('Name', 'en'));
name.addOut(hydra.property, schema('name'), (property: Clownface): void => {
name.addOut(hydra.property, schema.name, (property: Clownface): void => {
property.addOut(rdf.type, rdf.Property);
});
name.addOut(hydra.required, true);
Expand Down
2 changes: 1 addition & 1 deletion src/routes/article-list.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { hydra, rdf, schema } from '@tpluscode/rdf-ns-builders';
import clownface, { Clownface } from 'clownface';
import { OK } from 'http-status-codes';
import all from 'it-all';
Expand All @@ -7,7 +8,6 @@ import { DatasetCore, NamedNode } from 'rdf-js';
import { toRdf } from 'rdf-literal';
import url from 'url';
import { AppContext, AppMiddleware } from '../app';
import { hydra, rdf, schema } from '../namespaces';
import Routes from './index';

export default (): AppMiddleware => (
Expand Down
4 changes: 2 additions & 2 deletions src/routes/entry-point.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { hydra, rdf, schema } from '@tpluscode/rdf-ns-builders';
import clownface, { Clownface } from 'clownface';
import { OK } from 'http-status-codes';
import { Next } from 'koa';
import { NamedNode } from 'rdf-js';
import url from 'url';
import { AppContext, AppMiddleware } from '../app';
import { hydra, rdf, schema } from '../namespaces';
import Routes from './index';

export default (): AppMiddleware => (
Expand All @@ -19,7 +19,7 @@ export default (): AppMiddleware => (
});

graph.addOut(rdf.type, schema.EntryPoint);
graph.addOut(schema('name'), literal('Article Store', 'en'));
graph.addOut(schema.name, literal('Article Store', 'en'));
graph.addOut(hydra.collection, createNamedNode(Routes.ArticleList), (list: Clownface): void => {
list.addOut(rdf.type, hydra.Collection);
});
Expand Down
4 changes: 2 additions & 2 deletions test/adaptors/in-memory-articles.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { literal, namedNode, quad } from '@rdfjs/data-model';
import all from 'it-all';
import 'jest-rdf';
import { DatasetCore, NamedNode } from 'rdf-js';
import { schema } from '@tpluscode/rdf-ns-builders';
import InMemoryArticles from '../../src/adaptors/in-memory-articles';
import ArticleNotFound from '../../src/errors/article-not-found';
import NotAnArticle from '../../src/errors/not-an-article';
import { schema } from '../../src/namespaces';
import createArticle from '../create-article';

describe('in-memory articles', (): void => {
Expand Down Expand Up @@ -37,7 +37,7 @@ describe('in-memory articles', (): void => {
await articles.set(id, createArticle({ id, name: literal('Original') }));
await articles.set(id, createArticle({ id, name: literal('Updated') }));

expect(await articles.get(id)).toBeRdfDatasetContaining(quad(id, schema('name'), literal('Updated')));
expect(await articles.get(id)).toBeRdfDatasetContaining(quad(id, schema.name, literal('Updated')));
});

it('throws an error if it is not an article', async (): Promise<void> => {
Expand Down
4 changes: 2 additions & 2 deletions test/adaptors/postgres-articles.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import all from 'it-all';
import 'jest-rdf';
import pgPromise, { IBaseProtocol, IMain } from 'pg-promise';
import { DatasetCore, NamedNode } from 'rdf-js';
import { schema } from '@tpluscode/rdf-ns-builders';
import PostgresArticles from '../../src/adaptors/postgres-articles';
import dataFactory from '../../src/data-factory';
import db from '../../src/db';
import ArticleNotFound from '../../src/errors/article-not-found';
import NotAnArticle from '../../src/errors/not-an-article';
import { schema } from '../../src/namespaces';
import createArticle from '../create-article';

let postgresPromise: IMain;
Expand Down Expand Up @@ -59,7 +59,7 @@ describe('postgres articles', (): void => {
await articles.set(id, createArticle({ id, name: literal('Original') }));
await articles.set(id, createArticle({ id, name: literal('Updated') }));

expect(await articles.get(id)).toBeRdfDatasetContaining(quad(id, schema('name'), literal('Updated')));
expect(await articles.get(id)).toBeRdfDatasetContaining(quad(id, schema.name, literal('Updated')));
});

it('throws an error if it is not an article', async (): Promise<void> => {
Expand Down
4 changes: 2 additions & 2 deletions test/create-article.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {
DatasetCore, Literal, NamedNode, Quad_Subject as QuadSubject,
} from 'rdf-js';
import { rdf, schema } from '@tpluscode/rdf-ns-builders';
import {
blankNode, dataset, literal, quad,
} from '../src/data-factory';
import { rdf, schema } from '../src/namespaces';

type Options = {
id?: QuadSubject;
Expand All @@ -24,7 +24,7 @@ export default ({
});

if (name) {
article.add(quad(id, schema('name'), name));
article.add(quad(id, schema.name, name));
}

return article;
Expand Down
2 changes: 1 addition & 1 deletion test/errors/not-an-article.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { literal } from '@rdfjs/data-model';
import { schema } from '@tpluscode/rdf-ns-builders';
import NotAnArticle from '../../src/errors/not-an-article';
import { schema } from '../../src/namespaces';

describe('not an article error', (): void => {
it('should be an error', async (): Promise<void> => {
Expand Down
2 changes: 1 addition & 1 deletion test/middleware/error-handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import createHttpError from 'http-errors';
import { INTERNAL_SERVER_ERROR, SERVICE_UNAVAILABLE } from 'http-status-codes';
import 'jest-rdf';
import { Response } from 'koa';
import { hydra, rdf } from '@tpluscode/rdf-ns-builders';
import { WithDataset } from '../../src/middleware/dataset';
import errorHandler from '../../src/middleware/error-handler';
import { hydra, rdf } from '../../src/namespaces';
import createContext, { ErrorListener } from '../context';
import runMiddleware, { NextMiddleware, throwingNext } from '../middleware';

Expand Down
4 changes: 2 additions & 2 deletions test/middleware/jsonld.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import 'jest-rdf';
import { Context as JsonLdContext } from 'jsonld/jsonld-spec';
import { addAll } from 'rdf-dataset-ext';
import { Quad } from 'rdf-js';
import { rdf, schema } from '@tpluscode/rdf-ns-builders';
import { AppContext } from '../../src/app';
import jsonld from '../../src/middleware/jsonld';
import { rdf, schema } from '../../src/namespaces';
import createContext, { Headers } from '../context';
import { NextMiddleware } from '../middleware';

Expand Down Expand Up @@ -44,7 +44,7 @@ const dc = namespace('http://purl.org/dc/elements/1.1/');
const id = namedNode('http://example.com/object');
const quads = [
quad(id, rdf.type, schema.Article),
quad(id, schema('name'), literal('English Name', 'en')),
quad(id, schema.name, literal('English Name', 'en')),
quad(id, dc.title, literal('English Title', 'en')),
quad(id, dc.title, literal('French Title', 'fr')),
];
Expand Down
2 changes: 1 addition & 1 deletion test/routes/add-article.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import all from 'it-all';
import 'jest-rdf';
import { Response } from 'koa';
import { DatasetCore } from 'rdf-js';
import { schema } from '@tpluscode/rdf-ns-builders';
import InMemoryArticles from '../../src/adaptors/in-memory-articles';
import Articles from '../../src/articles';
import { schema } from '../../src/namespaces';
import addArticle from '../../src/routes/add-article';
import createContext from '../context';
import createArticle from '../create-article';
Expand Down
2 changes: 1 addition & 1 deletion test/routes/api-documentation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { namedNode, quad } from '@rdfjs/data-model';
import { OK } from 'http-status-codes';
import 'jest-rdf';
import { Response } from 'koa';
import { hydra, rdf } from '@tpluscode/rdf-ns-builders';
import { WithDataset } from '../../src/middleware/dataset';
import { hydra, rdf } from '../../src/namespaces';
import apiDocumentation from '../../src/routes/api-documentation';
import createContext from '../context';
import runMiddleware, { NextMiddleware } from '../middleware';
Expand Down
2 changes: 1 addition & 1 deletion test/routes/article-list.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { OK } from 'http-status-codes';
import 'jest-rdf';
import { Response } from 'koa';
import { toRdf } from 'rdf-literal';
import { hydra, rdf } from '@tpluscode/rdf-ns-builders';
import InMemoryArticles from '../../src/adaptors/in-memory-articles';
import Articles from '../../src/articles';
import { WithDataset } from '../../src/middleware/dataset';
import { hydra, rdf } from '../../src/namespaces';
import articleList from '../../src/routes/article-list';
import createContext from '../context';
import createArticle from '../create-article';
Expand Down
4 changes: 2 additions & 2 deletions test/routes/entry-point.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { namedNode, quad } from '@rdfjs/data-model';
import { OK } from 'http-status-codes';
import 'jest-rdf';
import { Response } from 'koa';
import { hydra, rdf, schema } from '@tpluscode/rdf-ns-builders';
import { WithDataset } from '../../src/middleware/dataset';
import { hydra, rdf, schema } from '../../src/namespaces';
import entryPoint from '../../src/routes/entry-point';
import createContext from '../context';
import runMiddleware, { NextMiddleware } from '../middleware';
Expand All @@ -24,7 +24,7 @@ describe('entry-point', (): void => {
const id = namedNode('http://example.com/path-to/entry-point');

expect(dataset).toBeRdfDatasetContaining(quad(id, rdf.type, schema.EntryPoint));
expect(dataset).toBeRdfDatasetMatching({ subject: id, predicate: schema('name') });
expect(dataset).toBeRdfDatasetMatching({ subject: id, predicate: schema.name });
expect(dataset).toBeRdfDatasetMatching({ subject: id, predicate: hydra.collection });
});

Expand Down