1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const hal_1 = require("./representor/hal");
|
4 | const html_1 = require("./representor/html");
|
5 | const jsonapi_1 = require("./representor/jsonapi");
|
6 | const resource_1 = require("./resource");
|
7 | const fetch_helper_1 = require("./utils/fetch-helper");
|
8 | require("./utils/fetch-polyfill");
|
9 | const url_1 = require("./utils/url");
|
10 |
|
11 |
|
12 |
|
13 | class 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 |
|
46 |
|
47 | follow(rel, variables) {
|
48 | return this.getResource().follow(rel, variables);
|
49 | }
|
50 | |
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
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 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 | getResource(uri) {
|
76 | return this.go(uri);
|
77 | }
|
78 | |
79 |
|
80 |
|
81 |
|
82 |
|
83 | fetch(input, init) {
|
84 | return this.fetchHelper.fetch(input, init);
|
85 | }
|
86 | |
87 |
|
88 |
|
89 |
|
90 |
|
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 |
|
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 |
|
135 | this.resourceCache[request.url].clearCache();
|
136 | }
|
137 | }
|
138 | }
|
139 | exports.default = Ketting;
|
140 |
|
\ | No newline at end of file |