UNPKG

@viewdo/dxp-story-cli

Version:
57 lines (46 loc) 1.96 kB
const axios = require("axios") const {requireValue} = require('./utils') const console = require('./console-service') module.exports = class ApiService { constructor(api_url = requireValue('api_url')) { Object.assign(this, { baseURL: `${api_url}/v1` }) this._api = axios.create({ baseURL: this.baseURL }) } getStory (story_key, token) { let remote_path = `/stories/${story_key}` console.debug(`API: GET ${this.baseURL}${remote_path}`.gray) return this._api.get(remote_path, { headers: {'Authorization' : `Bearer ${token}` }}) .then(r => r.data) } getOrganization (organization_key, token) { let remote_path = `/organizations/${organization_key}` console.debug(`API: GET ${this.baseURL}${remote_path}`.gray) return this._api.get(remote_path, { headers: {'Authorization' : `Bearer ${token}` }}) .then(r => r.data) } getOrganizationStories(organization_key, token) { let remote_path = `/organizations/${organization_key}/stories` console.debug(`API: GET ${this.baseURL}${remote_path}`.gray) return this._api.get(remote_path, { headers: {'Authorization' : `Bearer ${token}` }}) .then(r => r.data.items) } validateFile(story_key, content, token) { let remote_path = `/tokens/story/${story_key}/validate` console.debug(`API: POST ${this.baseURL}${remote_path}`.gray) return this._api.post(remote_path, {content}, { headers: {'Authorization' : `Bearer ${token}` }}) .then(r => r.data) } getFile(remote_path, token) { console.debug(`API: GET ${this.baseURL}${remote_path}`.gray) return this._api.get(remote_path, { headers: {'Authorization' : `Bearer ${token}` }}) .then(r => r.data.content || '') } putFile(remote_path, content, token) { console.debug(`API: PUT {content} ${this.baseURL}${remote_path}`.gray) return this._api.put(remote_path, {content}, { headers: {'Authorization' : `Bearer ${token}` }}) } }