...
 
Commits (2)
......@@ -116,10 +116,12 @@ var Path = function () {
var param = _step.value;
if (param.in === 'path') {
if (typeof data[param.name] === 'undefined') {
throw 'Path parameter "' + param.name + '" is missing.';
if (uri.includes(param.name)) {
if (typeof data[param.name] === 'undefined') {
throw 'Path parameter "' + param.name + '" is missing.';
}
uri = uri.replace('{' + param.name + '}', data[param.name]);
}
uri = uri.replace('{' + param.name + '}', data[param.name]);
delete data[param.name];
} else if (param.required) {
throw 'Required parameter "' + param.name + '" is missing.';
......@@ -235,25 +237,7 @@ var Resource = function () {
return _this.definition.delete(target);
};
} else if (propKey in target.relationships) {
// Get Relations.
if (!(propKey in this._relationship_promises)) {
var definition_id = this.definition.relationshipDefinition(propKey);
var prop_definition = this.client.on(definition_id);
if (!prop_definition) {
console.log(definition_id);
return Promise.reject(new Error('Unable to load definition for property `' + propKey + '`'));
}
if (Array.isArray(target.relationships[propKey].data)) {
var uuids = target.relationships[propKey].data.map(function (stub) {
return stub.id;
});
this._relationship_promises[propKey] = prop_definition.getMultiple(uuids);
} else {
this._relationship_promises[propKey] = prop_definition.get(data.id);
}
}
return this._relationship_promises[propKey];
return this.definition.getRelated(target, propKey);
} else if (propKey in target.attributes) {
// Return property if it exists.
return new Promise(function (resolve, reject) {
......@@ -382,7 +366,7 @@ var Definition = function () {
for (var _iterator = response.data[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var item = _step.value;
resources.push(_this2.constructResource(response.data));
resources.push(_this2.constructResource(item));
}
} catch (err) {
_didIteratorError = true;
......@@ -411,22 +395,44 @@ var Definition = function () {
*/
}, {
key: 'related',
value: function (_related) {
function related(_x, _x2) {
return _related.apply(this, arguments);
key: 'getRelated',
value: function getRelated(parent, property) {
if (parent._relationship_promises === undefined) {
parent._relationship_promises = [];
}
// Get Relations.
if (!(property in parent._relationship_promises)) {
related.toString = function () {
return _related.toString();
};
var relationship_schema = this.relationshipDefinition(property);
return related;
}(function (parent, related_url) {
return this.client.fetch(related_url).then(function (results) {
console.log(related);
});
})
var definition_id = "";
if (relationship_schema.type === "array") {
definition_id = relationship_schema.items.properties.type.enum[0];
} else {
definition_id = relationship_schema.properties.type.enum[0];
}
var prop_definition = this.client.on(definition_id);
if (!prop_definition) {
return Promise.reject(new Error('Unable to load definition for property `' + property + '`'));
}
var related_url = parent.relationships[property].links.related;
parent._relationship_promises[property] = this.client.fetch(related_url).then(function (results) {
if (relationship_schema.type === "array") {
var resources = results.data.map(function (data) {
return prop_definition.constructResource(data);
});
return Promise.resolve(resources);
} else {
var resource = prop_definition.constructResource(parent.relationships[property].data);
return Promise.resolve(resource);
}
});
}
return parent._relationship_promises[property];
}
/**
* Determine the definition type of an relationship property.
......@@ -436,7 +442,7 @@ var Definition = function () {
key: 'relationshipDefinition',
value: function relationshipDefinition(property) {
if (property in this.schema.properties.relationships.properties) {
return this.schema.properties.relationships.properties[property].properties.data.properties.type.enum[0];
return this.schema.properties.relationships.properties[property].properties.data;
}
return undefined;
}
......@@ -814,7 +820,6 @@ var OpenJacClient = function () {
}, {
key: 'on',
value: function on(type) {
console.log("ON" + type);
return this.definitions[type];
}
}]);
......
This diff is collapsed.
......@@ -217,7 +217,6 @@ class OpenJacClient {
* Get the definition type.
*/
on(type) {
console.log("ON" + type);
return this.definitions[type];
}
......
......@@ -30,23 +30,7 @@ class Resource {
}
}
else if (propKey in target.relationships) {
// Get Relations.
if (!(propKey in this._relationship_promises)) {
var definition_id = this.definition.relationshipDefinition(propKey);
var prop_definition = this.client.on(definition_id);
if (!prop_definition) {
console.log(definition_id);
return Promise.reject(new Error('Unable to load definition for property `' + propKey + '`'));
}
if (Array.isArray(target.relationships[propKey].data)) {
var uuids = target.relationships[propKey].data.map(stub => stub.id);
this._relationship_promises[propKey] = prop_definition.getMultiple(uuids);
} else {
this._relationship_promises[propKey] = prop_definition.get(data.id);
}
}
return this._relationship_promises[propKey];
return this.definition.getRelated(target, propKey);
}
else if (propKey in target.attributes) {
// Return property if it exists.
......
......@@ -45,13 +45,13 @@ class Definition {
};
return this.getAll(or_filter);
}
save(object) {
console.log("save");
}
new(object) {
}
/**
......@@ -62,7 +62,7 @@ class Definition {
return this.paths.collection.execute('GET', filters).then(response => {
var resources = [];
for (let item of response.data) {
resources.push(this.constructResource(response.data));
resources.push(this.constructResource(item));
}
return resources;
});
......@@ -75,10 +75,40 @@ class Definition {
/**
* Fetch the set of related resources using the related url.
*/
related(parent, related_url) {
return this.client.fetch(related_url).then(results => {
console.log(related);
});
getRelated(parent, property) {
if (parent._relationship_promises === undefined) {
parent._relationship_promises = [];
}
// Get Relations.
if (!(property in parent._relationship_promises)) {
var relationship_schema = this.relationshipDefinition(property);
var definition_id = "";
if (relationship_schema.type === "array") {
definition_id = relationship_schema.items.properties.type.enum[0];
} else {
definition_id = relationship_schema.properties.type.enum[0];
}
var prop_definition = this.client.on(definition_id);
if (!prop_definition) {
return Promise.reject(new Error('Unable to load definition for property `' + property + '`'));
}
var related_url = parent.relationships[property].links.related;
parent._relationship_promises[property] = this.client.fetch(related_url).then(results => {
if (relationship_schema.type === "array") {
var resources = results.data.map(data => prop_definition.constructResource(data));
return Promise.resolve(resources);
} else {
var resource = prop_definition.constructResource(parent.relationships[property].data);
return Promise.resolve(resource);
}
});
}
return parent._relationship_promises[property];
}
/**
......@@ -86,11 +116,11 @@ class Definition {
*/
relationshipDefinition(property) {
if (property in this.schema.properties.relationships.properties) {
return this.schema.properties.relationships.properties[property].properties.data.properties.type.enum[0];
return this.schema.properties.relationships.properties[property].properties.data;
}
return undefined;
}
/**
* Given a data blob, construct a resource.
*/
......
......@@ -21,10 +21,12 @@ class Path {
var schema = this.methods[method];
for (let param of schema.parameters) {
if (param.in === 'path') {
if (typeof data[param.name] === 'undefined') {
throw 'Path parameter "' + param.name + '" is missing.';
if (uri.includes(param.name)) {
if (typeof data[param.name] === 'undefined') {
throw 'Path parameter "' + param.name + '" is missing.';
}
uri = uri.replace('{' + param.name + '}', data[param.name]);
}
uri = uri.replace('{' + param.name + '}', data[param.name]);
delete data[param.name];
} else if (param.required) {
throw 'Required parameter "' + param.name +'" is missing.';
......