import axios, {AxiosInstance} from "axios"; import {Article} from "./types/Article"; import {Image} from "./types/Image"; import {Category} from "./types/Category"; class BluaBlueNodeSdk{ private readonly APIkey: string; private readonly PublicId: string; private endpoint: string; private apiVersion: string; private token:string; private api: AxiosInstance; constructor(PublicId: string = null, APIkey: string = null, endpoint='https://blua.blue', apiVersion='v1') { this.PublicId = PublicId || (typeof process !== 'undefined' ? process.env.BLUA_BLUE_PUBLIC_KEY : null); this.APIkey = APIkey || (typeof process !== 'undefined' ? process.env.BLUA_BLUE_PRIVATE_KEY : null); this.endpoint = endpoint; this.apiVersion = apiVersion; this.api = axios.create({ baseURL: endpoint+'/api.'+apiVersion, headers:{} }) this.api.interceptors.request.use(config => { if(this.token){ config.headers.Authorization = `baerer ${this.token}`; } return config; }) } async authenticate(): Promise{ return this.api.post('/auth/'+this.PublicId, {apiKey:this.APIkey}).then(res => { this.token = res.data.token; }).catch(BluaBlueNodeSdk.evaluateError) } getToken(){ return this.token; } setToken(token):void{ this.token = token; } login(redirect: string, scope:string='all', endpoint: string=null){ return this.endpoint + `/auth?scope=${scope}&public_key=${this.PublicId}&redirect=`+ redirect + (endpoint? '&endpoint='+endpoint:''); } async getArticle(slug: string): Promise
{ try{ const {data} = await this.api.get('/article/slug/'+slug) return data; } catch (e) { BluaBlueNodeSdk.evaluateError(e) } } async getOwnArticles(): Promise>{ try{ const {data} = await this.api.get('/article/mine') return data; } catch (e) { BluaBlueNodeSdk.evaluateError(e) } } async updateArticle(article:Article):Promise
{ try{ const {data} = await this.api.put('/article', article) return data; } catch (e) { BluaBlueNodeSdk.evaluateError(e) } } async createArticle(article:Article):Promise
{ try{ const {data} = await this.api.post('/article', article) return data; } catch (e) { BluaBlueNodeSdk.evaluateError(e) } } async getImages():Promise>{ try{ const {data} = await this.api.get('/image') return data; } catch (e) { BluaBlueNodeSdk.evaluateError(e) } } async registerImage(image: string, mode= 'external'):Promise>{ try{ const {data} = await this.api.post('/image',{mode,image}) return data; } catch (e) { BluaBlueNodeSdk.evaluateError(e) } } async deleteImage(id){ try{ const {data} = await this.api.delete('/image/'+id) return data; } catch (e) { BluaBlueNodeSdk.evaluateError(e) } } async getCategories():Promise>{ try{ const {data} = await this.api.get('/category') return data; } catch (e) { BluaBlueNodeSdk.evaluateError(e) } } async getAccountInfo(){ try{ const {data} = await this.api.get('/profile-settings') return data; } catch (e) { BluaBlueNodeSdk.evaluateError(e) } } private static evaluateError(error){ throw new Error("Failed to execute method - "+error); } } export default BluaBlueNodeSdk;