-
Notifications
You must be signed in to change notification settings - Fork 4
/
linkMiddleware.ts
26 lines (22 loc) · 914 Bytes
/
linkMiddleware.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import { LinkedRenderStore } from "./LinkedRenderStore";
import ll from "./ontology/ll";
import { NamedNode } from "./rdf";
import { MiddlewareActionHandler, MiddlewareFn, MiddlewareWithBoundLRS } from "./types";
/**
* Binds various uris to link actions.
*
* @see {createStore}
* @param catchActions {boolean} Set to true to catch all left-over actions to {LinkedRenderStore#execActionByIRI}.
*/
export const linkMiddleware = <T>(catchActions = true): MiddlewareFn<T> =>
(lrs: LinkedRenderStore<T>): MiddlewareWithBoundLRS =>
(next: MiddlewareActionHandler): MiddlewareActionHandler =>
(action: NamedNode, args: any): Promise<any> => {
if (action.value.startsWith(ll.ns("data/rdflib/").value)) {
return Promise.resolve(lrs.touch(args[0], args[1]));
}
if (catchActions) {
return lrs.execActionByIRI(action, args);
}
return next(action, args);
};