/
http-get-store.ts
36 lines (32 loc) · 1.04 KB
/
http-get-store.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
27
28
29
30
31
32
33
34
35
36
import {
BaseResourceStore,
BasicRepresentation,
Representation,
ResourceIdentifier,
} from "@solid/community-server";
import fetch from "node-fetch";
import { RepresentationPreferences } from "@solid/community-server/dist/ldp/representation/RepresentationPreferences";
const parseContentType = require("content-type").parse;
/**
* Fetches the resource at an URL
*/
export class HttpGetStore extends BaseResourceStore {
private readonly url: string;
public constructor(options: { url: string }) {
super();
this.url = options.url;
}
/**
* Retrieves a JSON representation of events in the calender.
*/
public async getRepresentation(
identifier: ResourceIdentifier,
preferences: RepresentationPreferences
): Promise<Representation> {
const response = await fetch(this.url);
const text = await response.text();
let contentType = response.headers.get("content-type") || "text/plain";
contentType = parseContentType(contentType).type;
return new BasicRepresentation(text, identifier, contentType);
}
}