Skip to content

Commit d8a75c9

Browse files
author
Fletcher91
committed
[REF] Extract DataProcessor into an interface
1 parent ffc04bd commit d8a75c9

File tree

6 files changed

+39
-135
lines changed

6 files changed

+39
-135
lines changed

src/LinkedDataAPI.ts

+15-40
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@ import {
55
} from "rdflib";
66

77
import {
8-
DataProcessor,
98
DataProcessorOpts,
10-
emptyRequest,
11-
} from "./processor/DataProcessor";
9+
} from "./types";
1210
import {
1311
DataTuple,
1412
EmptyRequestStatus,
@@ -18,31 +16,16 @@ import {
1816
SomeNode,
1917
} from "./types";
2018

21-
export interface LinkedDataAPIOpts {
22-
dataProcessorOpts?: DataProcessorOpts;
23-
processor?: DataProcessor;
24-
}
25-
26-
export class LinkedDataAPI {
27-
private processor: DataProcessor;
28-
29-
public constructor(opts: LinkedDataAPIOpts) {
30-
this.processor = opts.processor || new DataProcessor(opts.dataProcessorOpts);
31-
}
32-
33-
public execActionByIRI(subject: NamedNode, dataTuple: DataTuple): Promise<LinkedActionResponse> {
34-
return this.processor.execActionByIRI(subject, dataTuple);
35-
}
19+
export interface LinkedDataAPI {
20+
execActionByIRI(subject: NamedNode, dataTuple: DataTuple): Promise<LinkedActionResponse>;
3621

3722
/**
3823
* Loads a resource from the {iri}.
3924
* @param iri The SomeNode of the resource
4025
* @return The response from the server, or an response object from
4126
* the extension
4227
*/
43-
public fetchResource(iri: NamedNode): Promise<Response | object> {
44-
return this.processor.fetchResource(iri);
45-
}
28+
fetchResource(iri: NamedNode): Promise<Response | object>;
4629

4730
/**
4831
* Gets an entity by its SomeNode.
@@ -53,9 +36,7 @@ export class LinkedDataAPI {
5336
* @param opts The options for fetch-/processing the resource.
5437
* @return A promise with the resulting entity
5538
*/
56-
public getEntity(iri: NamedNode, opts?: FetchOpts): Promise<Statement[]> {
57-
return this.processor.getEntity(iri, opts);
58-
}
39+
getEntity(iri: NamedNode, opts?: FetchOpts): Promise<Statement[]>;
5940

6041
/**
6142
* Retrieve the (network) status for a resource.
@@ -74,28 +55,22 @@ export class LinkedDataAPI {
7455
* @param {SomeNode} iri The resource to get the status on.
7556
* @return {EmptyRequestStatus | FulfilledRequestStatus}
7657
*/
77-
public getStatus(iri: SomeNode): EmptyRequestStatus | FulfilledRequestStatus {
78-
if (iri.termType === "BlankNode") {
79-
return emptyRequest as EmptyRequestStatus;
80-
}
81-
82-
return this.processor.getStatus(iri);
83-
}
58+
getStatus(iri: SomeNode): EmptyRequestStatus | FulfilledRequestStatus;
8459

8560
/** Register a transformer so it can be used to interact with API's. */
86-
public registerTransformer(processor: ResponseTransformer,
87-
mediaType: string | string[],
88-
acceptValue: number): void {
89-
const mediaTypes: string[] = Array.isArray(mediaType) ? mediaType : [mediaType];
90-
this.processor.registerTransformer(processor, mediaTypes, acceptValue);
91-
}
61+
registerTransformer(processor: ResponseTransformer,
62+
mediaType: string | string[],
63+
acceptValue: number): void;
9264

9365
/**
9466
* Overrides the `Accept` value for when a certain host doesn't respond well to multiple values.
9567
* @param origin The iri of the origin for the requests.
9668
* @param acceptValue The value to use for the `Accept` header.
9769
*/
98-
public setAcceptForHost(origin: string, acceptValue: string): void {
99-
this.processor.setAcceptForHost(origin, acceptValue);
100-
}
70+
setAcceptForHost(origin: string, acceptValue: string): void;
10171
}
72+
73+
declare var LinkedDataAPI: {
74+
new (opts?: DataProcessorOpts): any;
75+
(): any;
76+
};

src/LinkedRenderStore.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010

1111
import { ComponentStore } from "./ComponentStore";
1212
import { LinkedDataAPI } from "./LinkedDataAPI";
13+
import { DataProcessor, emptyRequest } from "./processor/DataProcessor";
1314
import { dataToGraphTuple } from "./processor/DataToGraph";
1415
import { RDFStore } from "./RDFStore";
1516
import { Schema } from "./Schema";
@@ -72,11 +73,9 @@ export class LinkedRenderStore<T> {
7273
this.store = opts.store;
7374
}
7475

75-
this.api = opts.api || new LinkedDataAPI({
76-
dataProcessorOpts: {
77-
requestNotifier: this.touch.bind(this),
78-
store: this.store,
79-
},
76+
this.api = opts.api || new DataProcessor({
77+
requestNotifier: this.touch.bind(this),
78+
store: this.store,
8079
});
8180
this.defaultType = opts.defaultType || defaultNS.schema("Thing");
8281
this.namespaces = opts.namespaces || {...defaultNS};
@@ -239,6 +238,10 @@ export class LinkedRenderStore<T> {
239238
}
240239

241240
public getStatus(iri: SomeNode): EmptyRequestStatus | FulfilledRequestStatus {
241+
if (iri.termType === "BlankNode") {
242+
return emptyRequest as EmptyRequestStatus;
243+
}
244+
242245
return this.api.getStatus(iri);
243246
}
244247

src/__tests__/LinkedDataAPI.spec.ts

-79
This file was deleted.

src/processor/DataProcessor.ts

+3-10
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ import {
1515
Statement,
1616
uri as Uri,
1717
} from "rdflib";
18+
import { LinkedDataAPI } from "../LinkedDataAPI";
1819

1920
import { RDFStore } from "../RDFStore";
2021
import {
22+
DataProcessorOpts,
2123
DataTuple,
2224
EmptyRequestStatus,
2325
FailedResponse,
@@ -114,15 +116,6 @@ function processResponse(iri: string | NamedNode, res: Response): Statement[] {
114116
return [];
115117
}
116118

117-
export interface DataProcessorOpts {
118-
accept?: { [k: string]: string };
119-
requestInitGenerator?: RequestInitGenerator;
120-
fetcher?: Fetcher;
121-
mapping?: { [k: string]: ResponseTransformer[] };
122-
requestNotifier?: RequestCallbackHandler;
123-
store: RDFStore;
124-
}
125-
126119
export const emptyRequest = Object.freeze({
127120
lastRequested: null,
128121
requested: false,
@@ -147,7 +140,7 @@ const timedOutRequest = (totalRequested: number): FulfilledRequestStatus => Obje
147140
timesRequested: totalRequested,
148141
}) as FulfilledRequestStatus;
149142

150-
export class DataProcessor {
143+
export class DataProcessor implements LinkedDataAPI {
151144
public accept: { [k: string]: string };
152145
public timeout: number = 30000;
153146

src/testUtilities.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export type GetBasicStoreOpts = Partial<ExplodedLRS<BasicComponent>>;
3232
export const getBasicStore = (opts: GetBasicStoreOpts = {}): ExplodedLRS<BasicComponent> => {
3333
const store = opts.store || new RDFStore();
3434
const processor = opts.processor || new DataProcessor({ store });
35-
const api = opts.api || new LinkedDataAPI({ processor });
35+
const api = opts.api || processor;
3636
const schema = opts.schema || new Schema(store);
3737
const mapping = opts.mapping || new ComponentStoreTestProxy<BasicComponent>(schema);
3838

src/types.ts

+12
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
import {
22
BlankNode,
3+
Fetcher,
34
IndexedFormula,
45
Literal,
56
NamedNamespace,
67
NamedNode,
8+
RequestCallbackHandler,
79
SomeTerm,
810
Statement,
911
} from "rdflib";
1012

1113
import { ComponentStore } from "./ComponentStore";
1214
import { LinkedDataAPI } from "./LinkedDataAPI";
1315
import { LinkedRenderStore } from "./LinkedRenderStore";
16+
import { RequestInitGenerator } from "./processor/RequestInitGenerator";
1417
import { RDFStore } from "./RDFStore";
1518
import { Schema } from "./Schema";
1619
import { DisjointSet } from "./utilities/DisjointSet";
@@ -161,3 +164,12 @@ export interface VocabularyProcessor {
161164
*/
162165
processType: (type: NamedNode, ctx: VocabularyProcessingContext) => boolean;
163166
}
167+
168+
export interface DataProcessorOpts {
169+
accept?: { [k: string]: string };
170+
requestInitGenerator?: RequestInitGenerator;
171+
fetcher?: Fetcher;
172+
mapping?: { [k: string]: ResponseTransformer[] };
173+
requestNotifier?: RequestCallbackHandler;
174+
store: RDFStore;
175+
}

0 commit comments

Comments
 (0)