Placeholder.js
1.11 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
'use strict';
const ApiError = require('../ApiError');
module.exports = class Placeholder {
constructor(defaultName, name) {
this._name = name || defaultName;
}
get name() {
return this._name;
}
get typeDefinition() {
return this._type;
}
set typeDefinition(type) {
this._type = type;
}
inject(uri, parameters) {
const placeholderText = `<${this.name}>`;
if (uri.indexOf(placeholderText) > -1) {
return uri.replace(placeholderText, this.getValue(parameters));
}
return uri;
}
getValue(parameters) {
const typeDefinition = this.typeDefinition;
if (!typeDefinition) {
throw new ApiError('No type definition has been specified for placeholder');
}
const parameter = parameters ? parameters[this.name] : null;
const value = this._getParameterValue(parameter);
return typeDefinition.getValue(value);
}
_getParameterValue(parameter) {
return parameter;
}
toString() {
const type = this.typeDefinition;
return `${this.name}: { type:${type.type}, optional:${type.optional}, defaultValue:${type.defaultValue} }`;
}
};