UNPKG

4.41 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const hal_1 = require("./representor/hal");
4const html_1 = require("./representor/html");
5const jsonapi_1 = require("./representor/jsonapi");
6const resource_1 = require("./resource");
7const fetch_helper_1 = require("./utils/fetch-helper");
8require("./utils/fetch-polyfill");
9const url_1 = require("./utils/url");
10/**
11 * The main Ketting client object.
12 */
13class Ketting {
14 constructor(bookMark, options) {
15 if (typeof options === 'undefined') {
16 options = {};
17 }
18 this.resourceCache = {};
19 this.contentTypes = [
20 {
21 mime: 'application/hal+json',
22 representor: 'hal',
23 q: '1.0',
24 },
25 {
26 mime: 'application/vnd.api+json',
27 representor: 'jsonapi',
28 q: '0.9',
29 },
30 {
31 mime: 'application/json',
32 representor: 'hal',
33 q: '0.8',
34 },
35 {
36 mime: 'text/html',
37 representor: 'html',
38 q: '0.7',
39 }
40 ];
41 this.bookMark = bookMark;
42 this.fetchHelper = new fetch_helper_1.default(options, this.beforeRequest.bind(this));
43 }
44 /**
45 * This function is a shortcut for getResource().follow(x);
46 */
47 follow(rel, variables) {
48 return this.getResource().follow(rel, variables);
49 }
50 /**
51 * Returns a resource by its uri.
52 *
53 * This function doesn't do any HTTP requests. The uri is optional. If it's
54 * not specified, it will return the bookmark resource.
55 *
56 * If a relative uri is passed, it will be resolved based on the bookmark
57 * uri.
58 */
59 go(uri) {
60 if (typeof uri === 'undefined') {
61 uri = '';
62 }
63 uri = url_1.resolve(this.bookMark, uri);
64 if (!this.resourceCache[uri]) {
65 this.resourceCache[uri] = new resource_1.default(this, uri);
66 }
67 return this.resourceCache[uri];
68 }
69 /**
70 * Returns a resource by its uri.
71 *
72 * This function doesn't do any HTTP requests. The uri is optional. If it's
73 * not specified, it will return the bookmark resource.
74 */
75 getResource(uri) {
76 return this.go(uri);
77 }
78 /**
79 * This function does an arbitrary request using the fetch API.
80 *
81 * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/GlobalFetch}
82 */
83 fetch(input, init) {
84 return this.fetchHelper.fetch(input, init);
85 }
86 /**
87 * This function returns a representor constructor for a mime type.
88 *
89 * For example, given text/html, this function might return the constructor
90 * stored in representor/html.
91 */
92 getRepresentor(contentType) {
93 if (contentType.indexOf(';') !== -1) {
94 contentType = contentType.split(';')[0];
95 }
96 contentType = contentType.trim();
97 const result = this.contentTypes.find(item => {
98 return item.mime === contentType;
99 });
100 if (!result) {
101 throw new Error('Could not find a representor for contentType: ' + contentType);
102 }
103 switch (result.representor) {
104 case 'html':
105 return html_1.default;
106 case 'hal':
107 return hal_1.default;
108 case 'jsonapi':
109 return jsonapi_1.default;
110 default:
111 throw new Error('Unknown representor: ' + result.representor);
112 }
113 }
114 /**
115 * Generates an accept header string, based on registered Resource Types.
116 */
117 getAcceptHeader() {
118 return this.contentTypes
119 .map(contentType => {
120 let item = contentType.mime;
121 if (contentType.q) {
122 item += ';q=' + contentType.q;
123 }
124 return item;
125 })
126 .join(', ');
127 }
128 beforeRequest(request) {
129 const safeMethods = ['GET', 'HEAD', 'OPTIONS', 'PRI', 'PROPFIND', 'REPORT', 'SEARCH', 'TRACE'];
130 if (safeMethods.includes(request.method)) {
131 return;
132 }
133 if (request.url in this.resourceCache) {
134 // Clear cache
135 this.resourceCache[request.url].clearCache();
136 }
137 }
138}
139exports.default = Ketting;
140//# sourceMappingURL=ketting.js.map
\No newline at end of file