import axios from "axios"; class RepEs { constructor(config) { this.config = config this.index_url = config.index_url this.index_name = config.index_name this.token = process.env?.INSCODE_API_KEY??'default' this.loadToken() } loadToken () { axios.get('/token.json', { headers: { 'Accept': 'application/json' } }).then(res => { debugger const token = res?.data??'' if (token) { this.token = token.trim() console.info('get token from env [' + this.token + ']') } }) } query (keyword) { const token = this.token const url = this.index_url + '/' + this.index_name + '/_search' const data = { "query": { "bool": { "must": [ { "term": { "user_token": token } } ] } } } if (keyword) { data.query.bool.must.push({ "match": { "content": keyword } }) } return new Promise(function(resolve,rejcet){ axios.post(url, data).then(res => { const result = [] const total = res?.data?.hits?.total?.value??0 if (res?.status === 200 && total > 0) { res?.data?.hits?.hits.forEach(element => { result.push({ ...element, ... element?._source }) }); } resolve({ total: total, list: result }) }).catch((err) => { console.error(err) rejcet() }) }) } add (content) { const url = this.index_url + '/' + this.index_name + '/_doc' const token = this.token return new Promise(function(resolve,rejcet){ axios.post(url, { user_token: token, content: content, }).then(res => { console.info(res) resolve(res) }).catch((err) => { console.error(err) rejcet() }) }) } delete (id) { const url = this.index_url + '/' + this.index_name + '/_doc/' + id const token = this.token return new Promise(function(resolve,rejcet){ axios.delete(url).then(res => { console.info(res) resolve(res) }).catch((err) => { console.error(err) rejcet() }) }) } update (id, content) { debugger const url = this.index_url + '/' + this.index_name + '/_doc/' + id const token = this.token return new Promise(function(resolve,rejcet){ axios.put(url, { user_token: token, content: content }).then(res => { console.info(res) resolve(res) }).catch((err) => { console.error(err) rejcet() }) }) } } export default RepEs