Skip to content

Commit

Permalink
Allow executing blank node actions
Browse files Browse the repository at this point in the history
  • Loading branch information
Thom van Kalkeren committed Sep 23, 2021
1 parent ccc8efb commit 46da28a
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 23 deletions.
12 changes: 5 additions & 7 deletions src/LinkedDataAPI.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import { NamedNode, Quad } from "@ontologies/core";

import {
DataTuple,
DeltaProcessor,
Dispatcher,
PendingRequestStatus,
ResourceQueueItem,
SaveOpts,
} from "./types";
import {
DataTuple,
EmptyRequestStatus,
FulfilledRequestStatus,
LinkedActionResponse,
PendingRequestStatus,
ResourceQueueItem,
ResponseTransformer,
SaveOpts,
SomeNode,
} from "./types";

Expand All @@ -23,7 +21,7 @@ export interface APIFetchOpts {

export interface LinkedDataAPI extends Dispatcher, DeltaProcessor {

execActionByIRI(subject: NamedNode, dataTuple: DataTuple): Promise<LinkedActionResponse>;
execActionByIRI(subject: SomeNode, dataTuple: DataTuple): Promise<LinkedActionResponse>;

/** @private */
getEntities(resources: ResourceQueueItem[]): Promise<Quad[]>;
Expand Down
13 changes: 6 additions & 7 deletions src/LinkedRenderStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,12 @@ export class LinkedRenderStore<T, API extends LinkedDataAPI = DataProcessor> imp

/**
* Execute an Action by its IRI. This will result in an HTTP request being done and probably some state changes.
* @param {NamedNode} subject The resource to execute. Generally a schema:Action derivative with a
* @param subject The resource to execute. Generally a schema:Action derivative with a
* schema:EntryPoint to describe the request. Currently schema:url is used over schema:urlTemplate
* to acquire the request URL, since template processing isn't implemented (yet).
* @param {DataObject} data An object to send in the body when a non-safe method is used.
* @return {Promise<LinkedActionResponse>}
* @param data An object to send in the body when a non-safe method is used.
*/
public execActionByIRI(subject: NamedNode, data?: DataObject): Promise<LinkedActionResponse> {
public execActionByIRI(subject: SomeNode, data?: DataObject): Promise<LinkedActionResponse> {
const preparedData = dataToGraphTuple(data || {}, this.namespaces);
return this
.api
Expand All @@ -196,10 +195,10 @@ export class LinkedRenderStore<T, API extends LinkedDataAPI = DataProcessor> imp
*
* @see https://github.com/rescribet/link-lib/wiki/%5BDesign-draft%5D-Actions,-data-streams,-and-middleware
*
* @param {NamedNode} subject The resource to execute (can be either an IRI or an URI)
* @param {Object} args The arguments to the function defined by the subject.
* @param subject The resource to execute (can be either an IRI or an URI)
* @param args The arguments to the function defined by the subject.
*/
public async exec(subject: NamedNode, args?: DataObject): Promise<any> {
public async exec(subject: SomeNode, args?: DataObject): Promise<any> {
return this.dispatch(subject, args);
}

Expand Down
11 changes: 7 additions & 4 deletions src/createStore.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import { NamedNode } from "@ontologies/core";

import { LinkedDataAPI } from "./LinkedDataAPI";
import { LinkedRenderStore } from "./LinkedRenderStore";
import { linkMiddleware } from "./linkMiddleware";
import { DataProcessor } from "./processor/DataProcessor";
import { LinkedRenderStoreOptions, MiddlewareActionHandler, MiddlewareFn } from "./types";
import {
LinkedRenderStoreOptions,
MiddlewareActionHandler,
MiddlewareFn,
SomeNode,
} from "./types";

function applyMiddleware<T, API extends LinkedDataAPI = DataProcessor>(
lrs: LinkedRenderStore<T, API>,
...layers: Array<MiddlewareFn<T, API>>
): MiddlewareActionHandler {
const storeBound = layers.map((middleware) => middleware(lrs));

const finish: MiddlewareActionHandler = (a: NamedNode, _o: any): Promise<any> => Promise.resolve(a);
const finish: MiddlewareActionHandler = (a: SomeNode, _o: any): Promise<any> => Promise.resolve(a);

return storeBound.reduceRight((composed, f) => {
const next = f(composed);
Expand Down
11 changes: 7 additions & 4 deletions src/linkMiddleware.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { NamedNode } from "@ontologies/core";

import { LinkedDataAPI } from "./LinkedDataAPI";
import { LinkedRenderStore } from "./LinkedRenderStore";
import ll from "./ontology/ll";
import { DataProcessor } from "./processor/DataProcessor";
import { MiddlewareActionHandler, MiddlewareFn, MiddlewareWithBoundLRS } from "./types";
import {
MiddlewareActionHandler,
MiddlewareFn,
MiddlewareWithBoundLRS,
SomeNode,
} from "./types";

/**
* Binds various uris to link actions.
Expand All @@ -15,7 +18,7 @@ import { MiddlewareActionHandler, MiddlewareFn, MiddlewareWithBoundLRS } from ".
export const linkMiddleware = <T, API extends LinkedDataAPI = DataProcessor>(catchActions = true):
MiddlewareFn<T, API> => (lrs: LinkedRenderStore<T, API>): MiddlewareWithBoundLRS =>
(next: MiddlewareActionHandler): MiddlewareActionHandler =>
(action: NamedNode, args: any): Promise<any> => {
(action: SomeNode, args: any): Promise<any> => {

if (action.value.startsWith(ll.ns("data/rdflib/").value)) {
return Promise.resolve(lrs.touch(args[0], args[1]));
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export type MiddlewareFn<T, API extends LinkedDataAPI = DataProcessor> = (store:

export type MiddlewareWithBoundLRS = (next: MiddlewareActionHandler) => MiddlewareActionHandler;

export type MiddlewareActionHandler = (action: NamedNode, args?: any) => Promise<any>;
export type MiddlewareActionHandler = (action: SomeNode, args?: any) => Promise<any>;

export interface NamespaceMap {
[k: string]: CustomPredicateCreator;
Expand Down

0 comments on commit 46da28a

Please sign in to comment.