@plattar/plattar-api
Advanced tools
Comparing version 1.90.7-dev to 1.90.8-dev
49
index.js
@@ -8,2 +8,16 @@ 'use strict'; | ||
const Scene = require('./types/scene/scene.js'); | ||
const SceneAnnotation = require('./types/scene/scene-annotation.js'); | ||
const SceneAudio = require('./types/scene/scene-audio.js'); | ||
const SceneButton = require('./types/scene/scene-button.js'); | ||
const SceneCamera = require('./types/scene/scene-camera.js'); | ||
const SceneCarousel = require('./types/scene/scene-carousel.js'); | ||
const SceneImage = require('./types/scene/scene-image.js'); | ||
const SceneModel = require('./types/scene/scene-model.js'); | ||
const ScenePanorama = require('./types/scene/scene-panorama.js'); | ||
const ScenePoller = require('./types/scene/scene-poller.js'); | ||
const SceneProduct = require('./types/scene/scene-product.js'); | ||
const SceneShadow = require('./types/scene/scene-shadow.js'); | ||
const SceneVideo = require('./types/scene/scene-video.js'); | ||
const SceneVolumetric = require('./types/scene/scene-volumetric.js'); | ||
const SceneYoutube = require('./types/scene/scene-youtube.js'); | ||
@@ -15,2 +29,10 @@ // import Page and its types | ||
const CardIFrame = require('./types/page/card-iframe.js'); | ||
const CardImage = require('./types/page/card-image.js'); | ||
const CardMap = require('./types/page/card-map.js'); | ||
const CardParagraph = require('./types/page/card-paragraph.js'); | ||
const CardRow = require('./types/page/card-row.js'); | ||
const CardSlider = require('./types/page/card-slider.js'); | ||
const CardTitle = require('./types/page/card-title.js'); | ||
const CardVideo = require('./types/page/card-video.js'); | ||
const CardYoutube = require('./types/page/card-youtube.js'); | ||
@@ -32,6 +54,23 @@ // import Product and its types | ||
module.exports = { | ||
// core types | ||
Server: PlattarServer, | ||
Util: PlattarUtil, | ||
Project: Application, | ||
// scene and types | ||
Scene: Scene, | ||
SceneAnnotation: SceneAnnotation, | ||
SceneAudio: SceneAudio, | ||
SceneButton: SceneButton, | ||
SceneCamera: SceneCamera, | ||
SceneCarousel: SceneCarousel, | ||
SceneImage: SceneImage, | ||
SceneModel: SceneModel, | ||
ScenePanorama: ScenePanorama, | ||
ScenePoller: ScenePoller, | ||
SceneProduct, SceneProduct, | ||
SceneShadow: SceneShadow, | ||
SceneVideo: SceneVideo, | ||
SceneVolumetric: SceneVolumetric, | ||
SceneYoutube: SceneYoutube, | ||
// page and types | ||
Page: Page, | ||
@@ -41,5 +80,15 @@ CardButton: CardButton, | ||
CardIFrame: CardIFrame, | ||
CardImage: CardImage, | ||
CardMap: CardMap, | ||
CardParagraph: CardParagraph, | ||
CardRow: CardRow, | ||
CardSlider: CardSlider, | ||
CardTitle: CardTitle, | ||
CardVideo: CardVideo, | ||
CardYoutube: CardYoutube, | ||
// product and types | ||
Product: Product, | ||
ProductVariation: ProductVariation, | ||
ProductAnnotation: ProductAnnotation, | ||
// raw file/assets and types | ||
AudioFile: FileAudio, | ||
@@ -46,0 +95,0 @@ VideoFile: FileVideo, |
{ | ||
"name": "@plattar/plattar-api", | ||
"version": "1.90.7-dev", | ||
"version": "1.90.8-dev", | ||
"description": "Module for interfacing with the Plattar API (https://www.plattar.com)", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -1,39 +0,6 @@ | ||
const got = require('got'); | ||
'use strict'; | ||
const PlattarQuery = require('../../server/plattar-query.js'); | ||
'use strict'; | ||
class PlattarObject { | ||
/** | ||
* Plattar uses GUID for all object id's. 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() { | ||
PlattarObject._GlobalObjectCache = {}; | ||
} | ||
static _HasGlobalCachedObject(obj) { | ||
return PlattarObject._GlobalObjectCache.hasOwnProperty(obj.id); | ||
} | ||
static _GetGlobalCachedObject(obj) { | ||
return PlattarObject._HasGlobalCachedObject(obj) ? PlattarObject._GlobalObjectCache[obj.id] : undefined; | ||
} | ||
static _SetGlobalCachedObject(obj) { | ||
PlattarObject._GlobalObjectCache[obj.id] = obj; | ||
} | ||
static _DeleteGlobalCachedObject(obj) { | ||
if (PlattarObject._HasGlobalCachedObject(obj)) { | ||
delete PlattarObject._GlobalObjectCache[obj.id]; | ||
} | ||
} | ||
constructor(id, server) { | ||
@@ -45,5 +12,4 @@ if (this.constructor == PlattarObject) { | ||
this._id = id; | ||
this._server = server; | ||
this._attributes = {}; | ||
this._getIncludeQuery = []; | ||
this._query = new PlattarQuery(this, server); | ||
} | ||
@@ -55,3 +21,3 @@ | ||
invalidate() { | ||
return PlattarObject._DeleteGlobalCachedObject(this); | ||
return PlattarQuery._DeleteGlobalCachedObject(this); | ||
} | ||
@@ -63,3 +29,3 @@ | ||
_cache() { | ||
return PlattarObject._SetGlobalCachedObject(this); | ||
return PlattarQuery._SetGlobalCachedObject(this); | ||
} | ||
@@ -81,72 +47,21 @@ | ||
get() { | ||
return new Promise((resolve, reject) => { | ||
if (!this._server) { | ||
reject(new Error('PlattarObject.' + this.type() + '.get() - server is missing, pass a server instance or create a default server')); | ||
return; | ||
} | ||
// we cannot perform a GET request without an ID | ||
if (!this.id) { | ||
reject(new Error('PlattarObject.' + this.type() + '.get() - object id is missing')); | ||
return; | ||
} | ||
// check global cache first | ||
const cached = PlattarObject._GetGlobalCachedObject(this); | ||
if (cached) { | ||
resolve(cached); | ||
return; | ||
} | ||
// otherwise, proceed with the fetching op | ||
const origin = this._server.originLocation; | ||
const auth = this._server.authToken; | ||
const options = { | ||
headers: auth | ||
}; | ||
const endpoint = origin + this.type() + '/' + this.id; | ||
got.get(endpoint, options).then((response) => { | ||
const body = response.body; | ||
const json = JSON.parse(body); | ||
this._attributes = json.data.attributes; | ||
// cache the current object in the global cache | ||
this._cache(); | ||
resolve(this); | ||
}).catch((error) => { | ||
if (!error || !error.response || !error.response.body) { | ||
reject(new Error('PlattarObject.' + this.type() + '.get(' + this.id + ') - critical error occured, cannot proceed')); | ||
return; | ||
} | ||
const body = error.response.body; | ||
const json = JSON.parse(body); | ||
reject(new Error('PlattarObject.' + this.type() + '.get(' + this.id + ') - ' + json.errors[0].detail)); | ||
}); | ||
}); | ||
get(opt) { | ||
return this._query._get(opt); | ||
} | ||
update() { | ||
return new Promise((resolve, reject) => { | ||
reject(new Error('PlattarObject.' + this.type() + '.update(' + this.id + ') - not implemented')); | ||
}); | ||
return this._query.update(); | ||
} | ||
create() { | ||
return new Promise((resolve, reject) => { | ||
reject(new Error('PlattarObject.' + this.type() + '.create(' + this.id + ') - not implemented')); | ||
}); | ||
/** | ||
* Creates a brand new object in Plattar. | ||
* | ||
* @param {*} reqattr the required attributes for creating this object | ||
*/ | ||
static _create(reqattr) { | ||
return PlattarQuery._create(this, reqattr); | ||
} | ||
delete() { | ||
return new Promise((resolve, reject) => { | ||
reject(new Error('PlattarObject.' + this.type() + '.delete(' + this.id + ') - not implemented')); | ||
}); | ||
return this._query.delete(); | ||
} | ||
@@ -174,6 +89,3 @@ | ||
// object passed is of PlattarObject type | ||
if (obj.prototype instanceof PlattarObject) { | ||
includes.push(`${this.type()}.${obj.type()}`); | ||
} | ||
else if (Array.isArray(obj)) { | ||
if (Array.isArray(obj)) { | ||
obj.forEach((strObject) => { | ||
@@ -188,2 +100,5 @@ if (typeof strObject === 'string' || strObject instanceof String) { | ||
} | ||
else if (obj.prototype instanceof PlattarObject) { | ||
includes.push(`${this.type()}.${obj.type()}`); | ||
} | ||
else { | ||
@@ -201,37 +116,8 @@ throw new Error('PlattarObject.' + this.type() + '.include(...args) - argument must be of type PlattarObject or Array but was type=' + (typeof obj) + ' value=' + obj); | ||
include(...args) { | ||
if (!args || args.length <= 0) { | ||
return this; | ||
} | ||
this._query._include(args); | ||
args.forEach((obj) => { | ||
// object passed is of PlattarObject type | ||
if (obj.prototype instanceof PlattarObject) { | ||
this._getIncludeQuery.push(obj.type()); | ||
} | ||
else if (Array.isArray(obj)) { | ||
obj.forEach((strObject) => { | ||
if (typeof strObject === 'string' || strObject instanceof String) { | ||
this._getIncludeQuery.push(strObject); | ||
} | ||
else { | ||
throw new Error('PlattarObject.' + this.type() + '.include(...args) - argument of Array must only include Strings'); | ||
} | ||
}); | ||
} | ||
else { | ||
throw new Error('PlattarObject.' + this.type() + '.include(...args) - argument must be of type PlattarObject or Array but was type=' + (typeof obj) + ' value=' + obj); | ||
} | ||
}); | ||
return this; | ||
} | ||
/** | ||
* Performs a combination of all include queries | ||
*/ | ||
_CombineIncludeQuery() { | ||
return `${this._getIncludeQuery.map((item, i) => `${item}`).join(',')}`; | ||
} | ||
} | ||
module.exports = PlattarObject; |
'use strict'; | ||
const PlattarBase = require('../interfaces/plattar-base.js'); | ||
const ProductBase = require('./product-base.js'); | ||
class ProductAnnotation extends PlattarBase { | ||
class ProductAnnotation extends ProductBase { | ||
static type() { | ||
@@ -6,0 +6,0 @@ return 'productannotation'; |
'use strict'; | ||
const PlattarBase = require('../interfaces/plattar-base.js'); | ||
const ProductBase = require('./product-base.js'); | ||
class ProductVariation extends PlattarBase { | ||
class ProductVariation extends ProductBase { | ||
static type() { | ||
@@ -6,0 +6,0 @@ return 'productvariation'; |
@@ -6,2 +6,16 @@ 'use strict'; | ||
const Scene = require('../types/scene/scene.js'); | ||
const SceneAnnotation = require('../types/scene/scene-annotation.js'); | ||
const SceneAudio = require('../types/scene/scene-audio.js'); | ||
const SceneButton = require('../types/scene/scene-button.js'); | ||
const SceneCamera = require('../types/scene/scene-camera.js'); | ||
const SceneCarousel = require('../types/scene/scene-carousel.js'); | ||
const SceneImage = require('../types/scene/scene-image.js'); | ||
const SceneModel = require('../types/scene/scene-model.js'); | ||
const ScenePanorama = require('../types/scene/scene-panorama.js'); | ||
const ScenePoller = require('../types/scene/scene-poller.js'); | ||
const SceneProduct = require('../types/scene/scene-product.js'); | ||
const SceneShadow = require('../types/scene/scene-shadow.js'); | ||
const SceneVideo = require('../types/scene/scene-video.js'); | ||
const SceneVolumetric = require('../types/scene/scene-volumetric.js'); | ||
const SceneYoutube = require('../types/scene/scene-youtube.js'); | ||
@@ -13,2 +27,10 @@ // import Page types and its children | ||
const CardIFrame = require('../types/page/card-iframe.js'); | ||
const CardImage = require('../types/page/card-image.js'); | ||
const CardMap = require('../types/page/card-map.js'); | ||
const CardParagraph = require('../types/page/card-paragraph.js'); | ||
const CardRow = require('../types/page/card-row.js'); | ||
const CardSlider = require('../types/page/card-slider.js'); | ||
const CardTitle = require('../types/page/card-title.js'); | ||
const CardVideo = require('../types/page/card-video.js'); | ||
const CardYoutube = require('../types/page/card-youtube.js'); | ||
@@ -29,2 +51,46 @@ // import Product types and its children | ||
/** | ||
* 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'); | ||
if (obj && obj.prototype && obj.prototype instanceof PlattarObject) { | ||
return true; | ||
} | ||
if (obj && obj instanceof PlattarObject) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
/** | ||
* 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; | ||
// cache the current object in the global cache | ||
if (options.cache == true) { | ||
parent._cache(); | ||
} | ||
// todo | ||
} | ||
/** | ||
* 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 | ||
@@ -38,2 +104,16 @@ * @param {*} id (string) the id of the object | ||
case Scene.type(): return new Scene(id, server); | ||
case SceneAnnotation.type(): return new SceneAnnotation(id, server); | ||
case SceneAudio.type(): return new SceneAudio(id, server); | ||
case SceneButton.type(): return new SceneButton(id, server); | ||
case SceneCamera.type(): return new SceneCamera(id, server); | ||
case SceneCarousel.type(): return new SceneCarousel(id, server); | ||
case SceneImage.type(): return new SceneImage(id, server); | ||
case SceneModel.type(): return new SceneModel(id, server); | ||
case ScenePanorama.type(): return new ScenePanorama(id, server); | ||
case ScenePoller.type(): return new ScenePoller(id, server); | ||
case SceneProduct.type(): return new SceneProduct(id, server); | ||
case SceneShadow.type(): return new SceneShadow(id, server); | ||
case SceneVideo.type(): return new SceneVideo(id, server); | ||
case SceneVolumetric.type(): return new SceneVolumetric(id, server); | ||
case SceneYoutube.type(): return new SceneYoutube(id, server); | ||
case Page.type(): return new Page(id, server); | ||
@@ -50,2 +130,10 @@ case CardButton.type(): return new CardButton(id, server); | ||
case FileImage.type(): return new FileImage(id, server); | ||
case CardMap.type(): return new CardMap(id, server); | ||
case CardParagraph.type(): return new CardParagraph(id, server); | ||
case CardRow.type(): return new CardRow(id, server); | ||
case CardSlider.type(): return new CardSlider(id, server); | ||
case CardTitle.type(): return new CardTitle(id, server); | ||
case CardVideo.type(): return new CardVideo(id, server); | ||
case CardYoutube.type(): return new CardYoutube(id, server); | ||
case CardImage.type(): return new CardImage(id, server); | ||
default: throw new Error('PlattarUtil.create(type, id, server) - provided type of \'' + type + '\' does not exist'); | ||
@@ -52,0 +140,0 @@ } |
31577
46
857