@plattar/plattar-api
Advanced tools
Comparing version
104
index.js
const got = require('got'); | ||
'use strict'; | ||
const PlattarServer = require('./server/plattar-server.js'); | ||
const Project = require('./types/project.js'); | ||
class Plattar { | ||
constructor() { | ||
this._authToken = {}; | ||
this._serverLocation = undefined; | ||
} | ||
// create a default server instance to be used globally | ||
PlattarServer.create(); | ||
get prod() { | ||
return 'https://app.plattar.com/api/v2/'; | ||
} | ||
get staging() { | ||
return 'https://staging.plattar.space/api/v2/'; | ||
} | ||
get dev() { | ||
return 'https://localhost/api/v2/'; | ||
} | ||
get authToken() { | ||
return this._authToken; | ||
} | ||
get originLocation() { | ||
return this._serverLocation; | ||
} | ||
auth(token, opt) { | ||
const copt = opt || { validate: false }; | ||
return new Promise((resolve, reject) => { | ||
const server = this.originLocation; | ||
if (!server) { | ||
reject(new Error('Plattar.auth(token) - cannot authenticate as server not set via Plattar.origin(server)')); | ||
return; | ||
} | ||
if (!token) { | ||
reject(new Error('Plattar.auth(token) - token variable is undefined')); | ||
return; | ||
} | ||
if (!copt.validate) { | ||
this._authToken = { | ||
'plattar-auth-token': token | ||
}; | ||
resolve(this); | ||
return; | ||
} | ||
const validate = server + 'plattaruser/xauth/validate'; | ||
const options = { | ||
headers: { | ||
'plattar-auth-token': token | ||
} | ||
}; | ||
got.get(validate, options).then((response) => { | ||
this._authToken = { | ||
'plattar-auth-token': token | ||
}; | ||
resolve(this); | ||
}).catch((error) => { | ||
reject(new Error('Plattar.auth(token) - failed to validate authentication token at ' + validate)); | ||
}); | ||
}); | ||
} | ||
origin(server, opt) { | ||
const copt = opt || { validate: false }; | ||
return new Promise((resolve, reject) => { | ||
if (!server) { | ||
reject(new Error('Plattar.origin(server) - server variable is undefined')); | ||
return; | ||
} | ||
if (!copt.validate) { | ||
this._serverLocation = server; | ||
resolve(this); | ||
return; | ||
} | ||
const ping = server + 'ping'; | ||
got.get(ping).then((response) => { | ||
this._serverLocation = server; | ||
resolve(this); | ||
}).catch((error) => { | ||
reject(new Error('Plattar.origin(server) - failed to ping server at ' + ping)); | ||
}); | ||
}); | ||
} | ||
} | ||
module.exports = { | ||
Server: Plattar, | ||
Server: PlattarServer, | ||
Project: Project | ||
} |
{ | ||
"name": "@plattar/plattar-api", | ||
"version": "1.90.4-dev", | ||
"version": "1.90.5-dev", | ||
"description": "Module for interfacing with the Plattar API (https://www.plattar.com)", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -83,2 +83,7 @@ const got = require('got'); | ||
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 | ||
@@ -118,2 +123,7 @@ if (!this.id) { | ||
}).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; | ||
@@ -120,0 +130,0 @@ const json = JSON.parse(body); |
@@ -5,6 +5,7 @@ const got = require('got'); | ||
const PlattarObject = require('./interfaces/plattar-object.js'); | ||
const Server = require('./../server/plattar-server.js'); | ||
class Project extends PlattarObject { | ||
constructor(server, id) { | ||
super(server, id, Project.type()); | ||
constructor(id, server) { | ||
super(server || Server.default(), id, Project.type()); | ||
} | ||
@@ -16,4 +17,4 @@ | ||
static get(server, id) { | ||
return new Project(server, id).get(); | ||
static get(id, server) { | ||
return new Project(id, server || Server.default()).get(); | ||
} | ||
@@ -20,0 +21,0 @@ } |
9083
16.48%5
25%246
15.49%