-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathurl.js.html
More file actions
305 lines (263 loc) · 11.2 KB
/
url.js.html
File metadata and controls
305 lines (263 loc) · 11.2 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: url.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: url.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>import parse from 'url-parse';
import { getElements, getAttributeNS, find } from './utils';
import { OpenSearchParameter } from './parameter';
/**
* @module opensearch/url
*/
/**
* Class to parse a single URL of an OpenSearchDescription XML document and
* to create HTTP requests for searches.
* @property {string} type The mime-type for the content the URL is referring to
* @property {string} url The URL template or base URL
* @property {array} parameters The template/request parameters of the URL
* @property {string} method The HTTP method
* @property {string} enctype The encoding type
* @property {Number} indexOffset the index offset of this URL
* @property {Number} pageOffset the page offset of this URL
*/
export class OpenSearchUrl {
/**
* Create an OpenSearchUrl object
* @param {string} type The mime-type for the content the URL is referring to
* @param {string} url The URL template or base URL
* @param {OpenSearchParameter[]} parameters The template/request parameters of the URL
* @param {string} [method='GET'] The HTTP method
* @param {string} [enctype='application/x-www-form-urlencoded'] The encoding type
* @param {Number} [indexOffset=1] The index offset of this URL
* @param {Number} [pageOffset=1] The page offset of this URL
* @param {string[]} [relations=['results']] The relations of this URL.
*/
constructor(type, url, parameters = [], method = 'GET',
enctype = 'application/x-www-form-urlencoded',
indexOffset = 1, pageOffset = 1, relations = ['results']) {
this._type = type;
this._url = url;
this._method = method;
this._enctype = enctype;
this._indexOffset = indexOffset;
this._pageOffset = pageOffset;
this._relations = relations;
this._parameters = parameters;
this._parametersByName = {};
this._parametersByType = {};
parameters.forEach((param) => {
this._parametersByType[param.type] = param;
this._parametersByName[param.name] = param;
});
}
/**
* The mime-type for the content the URL is referring to
* @readonly
*/
get type() {
return this._type;
}
/**
* The URL template or base URL
* @readonly
*/
get url() {
return this._url;
}
/**
* The HTTP method
* @readonly
*/
get method() {
return this._method;
}
/**
* The encoding type
* @readonly
*/
get enctype() {
return this._enctype;
}
/**
* The index offset of this URL
* @readonly
*/
get indexOffset() {
return this._indexOffset;
}
/**
* The page offset of this URL
* @readonly
*/
get pageOffset() {
return this._pageOffset;
}
/**
* The page offset of this URL
* @readonly
*/
get relations() {
return this._relations;
}
/**
* The template/request parameters of the URL
* @readonly
*/
get parameters() {
return this._parameters;
}
/**
* Returns whether the URL has a template parameter of the given type
* @param {string} type The parameter type to check
* @returns {boolean} Whether the URL has a parameter of that type
*/
hasParameter(type) {
return Object.prototype.hasOwnProperty.call(this._parametersByType, type);
}
/**
* Get the parameter of the specified type, if available
* @param {string} type The parameter type to check
* @returns {OpenSearchParameter} The parameter of the given type or null
*/
getParameter(type) {
return this._parametersByType[type];
}
/**
* Checks whether this URL is compatible with the given parameters
* @param {object} parameters An object mapping the name or type to the value
* @returns {boolean} Whether or not the URL is compatible with the given parameters
*/
isCompatible(parameters) {
let compatible = true;
Object.keys(parameters).forEach((key) => {
if (!Object.prototype.hasOwnProperty.call(this._parametersByType, key)
&& !Object.prototype.hasOwnProperty.call(this._parametersByName, key)) {
compatible = false;
}
});
if (!compatible) {
return false;
}
const missingMandatoryParameters = this.parameters.filter(
parameter => parameter.mandatory
&& !Object.prototype.hasOwnProperty.call(parameters, parameter.name)
&& !Object.prototype.hasOwnProperty.call(parameters, parameter.type)
);
if (missingMandatoryParameters.length) {
return false;
}
return true;
}
/**
* Construct a {@link OpenSearchUrl} from a DOMNode
* @param {DOMNode} node The DOM node from the OpenSearchDescription XML document
* @returns {OpenSearchUrl} The constructed OpenSearchUrl object
*/
static fromNode(node) {
const parameterNodes = getElements(node, 'parameters', 'Parameter');
const method = getAttributeNS(node, 'parameters', 'method');
const enctype = getAttributeNS(node, 'parameters', 'enctype');
const indexOffset = node.hasAttribute('indexOffset') ?
parseInt(node.getAttribute('indexOffset'), 10) : 1;
const pageOffset = node.hasAttribute('pageOffset') ?
parseInt(node.getAttribute('pageOffset'), 10) : 1;
const rel = node.getAttribute('rel');
const relations = (!rel || rel === '') ? undefined : rel.split(' ');
const parsed = parse(node.getAttribute('template'), true);
const parametersFromTemplate = Object.keys(parsed.query)
.map(name => OpenSearchParameter.fromKeyValuePair(name, parsed.query[name]))
.filter(parameter => parameter);
const parametersFromNode = parameterNodes.map(OpenSearchParameter.fromNode);
const parametersNotInTemplate = parametersFromNode.filter(
p1 => !find(parametersFromTemplate, p2 => p1.name === p2.name)
).map((param) => {
// eslint-disable-next-line no-underscore-dangle, no-param-reassign
param._mandatory = (typeof param.mandatory === 'undefined') ? true : param.mandatory;
return param;
});
// merge parameters from node and template
const parameters = parametersFromTemplate.map((p1) => {
const p2 = find(parametersFromNode, p => p1.name === p.name);
if (p2) {
return p1.combined(p2);
}
return p1;
}).concat(parametersNotInTemplate);
return new OpenSearchUrl(
node.getAttribute('type'), node.getAttribute('template'),
parameters, method, enctype, indexOffset, pageOffset, relations
);
}
/**
* Construct a {@link OpenSearchUrl} from a template URL
* @param {string} type The mime-type
* @param {string} templateUrl The template URL string.
* @param {string} [method='GET'] The HTTP method
* @param {string} [enctype='application/x-www-form-urlencoded'] The encoding type
* @returns {OpenSearchUrl} The constructed OpenSearchUrl object
*/
static fromTemplateUrl(type, templateUrl, method = 'GET',
enctype = 'application/x-www-form-urlencoded') {
const parsed = parse(templateUrl, true);
const parameters = Object.keys(parsed.query)
.map(name => OpenSearchParameter.fromKeyValuePair(name, parsed.query[name]))
.filter(parameter => parameter);
return new OpenSearchUrl(type, templateUrl, parameters, method, enctype);
}
/**
* Serialize the URL to a simple object.
* @returns {object} The serialized URL
*/
serialize() {
return {
type: this._type,
url: this._url,
method: this._method,
enctype: this._enctype,
indexOffset: this._indexOffset,
pageOffset: this._pageOffset,
relations: this._relations,
parameters: this._parameters.map(parameter => parameter.serialize()),
};
}
/**
* Deserialize a parameter from a simple object.
* @param {object} values The serialized URL
* @returns {OpenSearchUrl} The deserialized URL
*/
static deserialize(values) {
return new OpenSearchUrl(
values.type, values.url,
values.parameters.map(parameterDesc => OpenSearchParameter.deserialize(parameterDesc)),
values.method, values.enctype, values.indexOffset, values.pageOffset, values.relations
);
}
}
</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-opensearch.html">opensearch</a></li><li><a href="module-opensearch_config.html">opensearch/config</a></li><li><a href="module-opensearch_error.html">opensearch/error</a></li><li><a href="module-opensearch_formats.html">opensearch/formats</a></li><li><a href="module-opensearch_formats_atom.html">opensearch/formats/atom</a></li><li><a href="module-opensearch_formats_geojson.html">opensearch/formats/geojson</a></li><li><a href="module-opensearch_formats_rss.html">opensearch/formats/rss</a></li><li><a href="module-opensearch_formats_suggestions-json.html">opensearch/formats/suggestions-json</a></li><li><a href="module-opensearch_paginator.html">opensearch/paginator</a></li><li><a href="module-opensearch_parameter.html">opensearch/parameter</a></li><li><a href="module-opensearch_search.html">opensearch/search</a></li><li><a href="module-opensearch_url.html">opensearch/url</a></li><li><a href="module-opensearch_utils.html">opensearch/utils</a></li></ul><h3>Classes</h3><ul><li><a href="module-opensearch_formats_atom-AtomFormat.html">AtomFormat</a></li><li><a href="module-opensearch_formats_geojson-GeoJSONFormat.html">GeoJSONFormat</a></li><li><a href="module-opensearch_formats_rss-RSSFormat.html">RSSFormat</a></li><li><a href="module-opensearch_formats_suggestions-json-SuggestionsJSONFormat.html">SuggestionsJSONFormat</a></li><li><a href="module-opensearch_paginator.OpenSearchPaginator.html">OpenSearchPaginator</a></li><li><a href="module-opensearch_paginator-PagedSearchProgressEmitter.html">PagedSearchProgressEmitter</a></li><li><a href="module-opensearch_parameter.OpenSearchParameter.html">OpenSearchParameter</a></li><li><a href="module-opensearch_url.OpenSearchUrl.html">OpenSearchUrl</a></li><li><a href="OpenSearchDescription.html">OpenSearchDescription</a></li><li><a href="OpenSearchService.html">OpenSearchService</a></li></ul><h3>Events</h3><ul><li><a href="module-opensearch_paginator-PagedSearchProgressEmitter.html#event:error">error</a></li><li><a href="module-opensearch_paginator-PagedSearchProgressEmitter.html#event:page">page</a></li><li><a href="module-opensearch_paginator-PagedSearchProgressEmitter.html#event:success">success</a></li></ul><h3>Interfaces</h3><ul><li><a href="module-opensearch_formats.FormatInterface.html">FormatInterface</a></li></ul>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Wed Jan 10 2018 13:14:22 GMT+0100 (CET)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>