@plattar/plattar-api
Advanced tools
Comparing version 1.90.14-dev to 1.90.15-dev
{ | ||
"name": "@plattar/plattar-api", | ||
"version": "1.90.14-dev", | ||
"version": "1.90.15-dev", | ||
"description": "Module for interfacing with the Plattar API (https://www.plattar.com)", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -5,36 +5,2 @@ const fetch = require('node-fetch'); | ||
class PlattarQuery { | ||
/** | ||
* Plattar uses GUID for all object ids. This means | ||
* that the GUID will not be shared between different | ||
* object instances. This allows us to create a global | ||
* static cache to optimise fetch operations for all | ||
* objects. | ||
* | ||
* WARNING: These are for internal uses only! | ||
*/ | ||
static _GlobalObjectCache = {}; | ||
static _InvalidateGlobalCache() { | ||
PlattarQuery._GlobalObjectCache = {}; | ||
} | ||
static _HasGlobalCachedObject(obj) { | ||
return PlattarQuery._GlobalObjectCache.hasOwnProperty(obj.id); | ||
} | ||
static _GetGlobalCachedObject(obj) { | ||
return PlattarQuery._HasGlobalCachedObject(obj) ? PlattarQuery._GlobalObjectCache[obj.id] : undefined; | ||
} | ||
static _SetGlobalCachedObject(obj) { | ||
PlattarQuery._GlobalObjectCache[obj.id] = obj; | ||
} | ||
static _DeleteGlobalCachedObject(obj) { | ||
if (PlattarQuery._HasGlobalCachedObject(obj)) { | ||
delete PlattarQuery._GlobalObjectCache[obj.id]; | ||
} | ||
} | ||
constructor(target, server) { | ||
@@ -199,3 +165,35 @@ if (!target) { | ||
/** | ||
* Plattar uses GUID for all object ids. This means | ||
* that the GUID will not be shared between different | ||
* object instances. This allows us to create a global | ||
* static cache to optimise fetch operations for all | ||
* objects. | ||
* | ||
* WARNING: These are for internal uses only! | ||
*/ | ||
PlattarQuery._GlobalObjectCache = {}; | ||
PlattarQuery._InvalidateGlobalCache = () => { | ||
PlattarQuery._GlobalObjectCache = {}; | ||
}; | ||
PlattarQuery._HasGlobalCachedObject = (obj) => { | ||
return PlattarQuery._GlobalObjectCache.hasOwnProperty(obj.id); | ||
}; | ||
PlattarQuery._GetGlobalCachedObject = (obj) => { | ||
return PlattarQuery._HasGlobalCachedObject(obj) ? PlattarQuery._GlobalObjectCache[obj.id] : undefined; | ||
}; | ||
PlattarQuery._SetGlobalCachedObject = (obj) => { | ||
PlattarQuery._GlobalObjectCache[obj.id] = obj; | ||
}; | ||
PlattarQuery._DeleteGlobalCachedObject = (obj) => { | ||
if (PlattarQuery._HasGlobalCachedObject(obj)) { | ||
delete PlattarQuery._GlobalObjectCache[obj.id]; | ||
} | ||
}; | ||
module.exports = PlattarQuery; |
@@ -5,29 +5,2 @@ const fetch = require('node-fetch'); | ||
class PlattarServer { | ||
/** | ||
* We keep and maintain a default server | ||
* for quick access | ||
*/ | ||
static _default = undefined; | ||
static create(origin, auth) { | ||
const newServer = new PlattarServer(); | ||
if (origin) { | ||
newServer.origin(origin); | ||
} | ||
if (auth) { | ||
newServer.auth(auth); | ||
} | ||
PlattarServer._default = newServer; | ||
return newServer; | ||
} | ||
static default() { | ||
return PlattarServer._default; | ||
} | ||
constructor() { | ||
@@ -152,2 +125,24 @@ this._authToken = {}; | ||
PlattarServer._default = undefined; | ||
PlattarServer.create = (origin, auth) => { | ||
const newServer = new PlattarServer(); | ||
if (origin) { | ||
newServer.origin(origin); | ||
} | ||
if (auth) { | ||
newServer.auth(auth); | ||
} | ||
PlattarServer._default = newServer; | ||
return newServer; | ||
}; | ||
PlattarServer.default = () => { | ||
return PlattarServer._default; | ||
}; | ||
module.exports = PlattarServer; |
'use strict'; | ||
const PlattarBase = require('./interfaces/plattar-base.js'); | ||
class Application extends PlattarBase { | ||
static type() { | ||
return 'application'; | ||
} | ||
} | ||
class Application extends PlattarBase { } | ||
Application.type = () => { | ||
return 'application'; | ||
}; | ||
module.exports = Application; |
@@ -50,145 +50,145 @@ 'use strict'; | ||
class PlattarUtil { | ||
class PlattarUtil { } | ||
/** | ||
* Checks if the provided Object is a Plattar Object | ||
* | ||
* @param {*} obj The object instance to check | ||
*/ | ||
static isPlattarObject(obj) { | ||
const PlattarObject = require('../types/interfaces/plattar-object.js'); | ||
/** | ||
* Checks if the provided Object is a Plattar Object | ||
* | ||
* @param {*} obj The object instance to check | ||
*/ | ||
PlattarUtil.isPlattarObject = (obj) => { | ||
const PlattarObject = require('../types/interfaces/plattar-object.js'); | ||
if (obj && obj.prototype && obj.prototype instanceof PlattarObject) { | ||
return true; | ||
} | ||
if (obj && obj.prototype && obj.prototype instanceof PlattarObject) { | ||
return true; | ||
} | ||
if (obj && obj instanceof PlattarObject) { | ||
return true; | ||
} | ||
return false; | ||
if (obj && obj instanceof PlattarObject) { | ||
return true; | ||
} | ||
/** | ||
* This function will recursively reconstruct an object | ||
* and its required hierarchy from a provided response JSON | ||
* from the PLATTAR Server. | ||
* | ||
* This is an internal use function and should not be used directly | ||
* | ||
* @param {*} parent The parent object of the request | ||
* @param {*} json The parsed JSON data from the PLATTAR Server | ||
* @param {*} options The cache/process options | ||
*/ | ||
static reconstruct(parent, json, options) { | ||
parent._attributes = json.data.attributes; | ||
return false; | ||
}; | ||
// cache the current object in the global cache | ||
if (options.cache == true) { | ||
parent._cache(); | ||
} | ||
/** | ||
* This function will recursively reconstruct an object | ||
* and its required hierarchy from a provided response JSON | ||
* from the PLATTAR Server. | ||
* | ||
* This is an internal use function and should not be used directly | ||
* | ||
* @param {*} parent The parent object of the request | ||
* @param {*} json The parsed JSON data from the PLATTAR Server | ||
* @param {*} options The cache/process options | ||
*/ | ||
PlattarUtil.reconstruct = (parent, json, options) => { | ||
parent._attributes = json.data.attributes; | ||
const server = parent._query.server; | ||
// cache the current object in the global cache | ||
if (options.cache == true) { | ||
parent._cache(); | ||
} | ||
// fill the relationships for the object | ||
if (json.data.relationships) { | ||
for (const [key, value] of Object.entries(json.data.relationships)) { | ||
const data = value.data; | ||
const server = parent._query.server; | ||
if (Array.isArray(data)) { | ||
data.forEach((item) => { | ||
const construct = PlattarUtil.create(key, item.id, server); | ||
construct._attributes = item.attributes || {}; | ||
// fill the relationships for the object | ||
if (json.data.relationships) { | ||
for (const [key, value] of Object.entries(json.data.relationships)) { | ||
const data = value.data; | ||
parent.relationships._put(construct); | ||
}); | ||
} | ||
else { | ||
const construct = PlattarUtil.create(key, data.id, server); | ||
construct._attributes = data.attributes || {}; | ||
if (Array.isArray(data)) { | ||
data.forEach((item) => { | ||
const construct = PlattarUtil.create(key, item.id, server); | ||
construct._attributes = item.attributes || {}; | ||
parent.relationships._put(construct); | ||
} | ||
}); | ||
} | ||
} | ||
else { | ||
const construct = PlattarUtil.create(key, data.id, server); | ||
construct._attributes = data.attributes || {}; | ||
// loop through the includes and populate as required | ||
if (json.included) { | ||
json.included.forEach((item) => { | ||
const existing = parent.relationships.find(PlattarUtil.match(item.type), item.id); | ||
if (existing) { | ||
PlattarUtil.reconstruct(existing, { | ||
data: item, | ||
included: json.included | ||
}, options); | ||
} | ||
}); | ||
parent.relationships._put(construct); | ||
} | ||
} | ||
} | ||
/** | ||
* Used to dynamically match types from the Plattar API into class objects | ||
* Throws an Error if the provided type does not exit. | ||
* | ||
* @param {*} type (string) the type of object to create | ||
* @param {*} id (string) the id of the object | ||
* @param {*} server (optional) the server this object belongs in | ||
*/ | ||
static create(type, id, server) { | ||
// dynamic class matching from a string type | ||
const clazz = PlattarUtil.match(type); | ||
// loop through the includes and populate as required | ||
if (json.included) { | ||
json.included.forEach((item) => { | ||
const existing = parent.relationships.find(PlattarUtil.match(item.type), item.id); | ||
return new clazz(id, server); | ||
if (existing) { | ||
PlattarUtil.reconstruct(existing, { | ||
data: item, | ||
included: json.included | ||
}, options); | ||
} | ||
}); | ||
} | ||
}; | ||
/** | ||
* Dynamic class matching provided an object type as a string | ||
* | ||
* @param {*} type The type of class to construct | ||
*/ | ||
static match(type) { | ||
switch (type) { | ||
case Application.type(): return Application; | ||
case Scene.type(): return Scene; | ||
case SceneAnnotation.type(): return SceneAnnotation; | ||
case SceneAudio.type(): return SceneAudio; | ||
case SceneButton.type(): return SceneButton; | ||
case SceneCamera.type(): return SceneCamera; | ||
case SceneCarousel.type(): return SceneCarousel; | ||
case SceneImage.type(): return SceneImage; | ||
case SceneModel.type(): return SceneModel; | ||
case ScenePanorama.type(): return ScenePanorama; | ||
case ScenePoller.type(): return ScenePoller; | ||
case SceneProduct.type(): return SceneProduct; | ||
case SceneShadow.type(): return SceneShadow; | ||
case SceneVideo.type(): return SceneVideo; | ||
case SceneVolumetric.type(): return SceneVolumetric; | ||
case SceneYoutube.type(): return SceneYoutube; | ||
case Page.type(): return Page; | ||
case CardButton.type(): return CardButton; | ||
case CardHTML.type(): return CardHTML; | ||
case CardIFrame.type(): return CardIFrame; | ||
case Product.type(): return Product; | ||
case ProductVariation.type(): return ProductVariation; | ||
case ProductAnnotation.type(): return ProductAnnotation; | ||
case FileAudio.type(): return FileAudio; | ||
case FileVideo.type(): return FileVideo; | ||
case FileModel.type(): return FileModel; | ||
case FileImage.type(): return FileImage; | ||
case CardMap.type(): return CardMap; | ||
case CardParagraph.type(): return CardParagraph; | ||
case CardRow.type(): return CardRow; | ||
case CardSlider.type(): return CardSlider; | ||
case CardTitle.type(): return CardTitle; | ||
case CardVideo.type(): return CardVideo; | ||
case CardYoutube.type(): return CardYoutube; | ||
case CardImage.type(): return CardImage; | ||
case ScriptEvent.type(): return ScriptEvent; | ||
case Tag.type(): return Tag; | ||
default: throw new Error('PlattarUtil.match(type) - provided type of \'' + type + '\' does not exist and cannot be created'); | ||
} | ||
/** | ||
* Used to dynamically match types from the Plattar API into class objects | ||
* Throws an Error if the provided type does not exit. | ||
* | ||
* @param {*} type (string) the type of object to create | ||
* @param {*} id (string) the id of the object | ||
* @param {*} server (optional) the server this object belongs in | ||
*/ | ||
PlattarUtil.create = (type, id, server) => { | ||
// dynamic class matching from a string type | ||
const clazz = PlattarUtil.match(type); | ||
return new clazz(id, server); | ||
}; | ||
/** | ||
* Dynamic class matching provided an object type as a string | ||
* | ||
* @param {*} type The type of class to construct | ||
*/ | ||
PlattarUtil.match = (type) => { | ||
switch (type) { | ||
case Application.type(): return Application; | ||
case Scene.type(): return Scene; | ||
case SceneAnnotation.type(): return SceneAnnotation; | ||
case SceneAudio.type(): return SceneAudio; | ||
case SceneButton.type(): return SceneButton; | ||
case SceneCamera.type(): return SceneCamera; | ||
case SceneCarousel.type(): return SceneCarousel; | ||
case SceneImage.type(): return SceneImage; | ||
case SceneModel.type(): return SceneModel; | ||
case ScenePanorama.type(): return ScenePanorama; | ||
case ScenePoller.type(): return ScenePoller; | ||
case SceneProduct.type(): return SceneProduct; | ||
case SceneShadow.type(): return SceneShadow; | ||
case SceneVideo.type(): return SceneVideo; | ||
case SceneVolumetric.type(): return SceneVolumetric; | ||
case SceneYoutube.type(): return SceneYoutube; | ||
case Page.type(): return Page; | ||
case CardButton.type(): return CardButton; | ||
case CardHTML.type(): return CardHTML; | ||
case CardIFrame.type(): return CardIFrame; | ||
case Product.type(): return Product; | ||
case ProductVariation.type(): return ProductVariation; | ||
case ProductAnnotation.type(): return ProductAnnotation; | ||
case FileAudio.type(): return FileAudio; | ||
case FileVideo.type(): return FileVideo; | ||
case FileModel.type(): return FileModel; | ||
case FileImage.type(): return FileImage; | ||
case CardMap.type(): return CardMap; | ||
case CardParagraph.type(): return CardParagraph; | ||
case CardRow.type(): return CardRow; | ||
case CardSlider.type(): return CardSlider; | ||
case CardTitle.type(): return CardTitle; | ||
case CardVideo.type(): return CardVideo; | ||
case CardYoutube.type(): return CardYoutube; | ||
case CardImage.type(): return CardImage; | ||
case ScriptEvent.type(): return ScriptEvent; | ||
case Tag.type(): return Tag; | ||
default: throw new Error('PlattarUtil.match(type) - provided type of \'' + type + '\' does not exist and cannot be created'); | ||
} | ||
} | ||
}; | ||
module.exports = PlattarUtil; |
41270
1050