mirror of https://github.com/nocodb/nocodb
212 lines
5.4 KiB
212 lines
5.4 KiB
3 years ago
|
import axios from 'axios'
|
||
|
import Vue from 'vue'
|
||
4 years ago
|
|
||
|
// const {autocannon} = require("electron").remote.require(
|
||
|
// "./libs"
|
||
|
// );
|
||
|
|
||
|
export const state = () => ({
|
||
|
|
||
|
/**
|
||
|
*
|
||
|
* type : GET, PUT, POST, DELETE, HEAD, OPTIONS, PATCH
|
||
|
*
|
||
|
* URL :
|
||
|
*
|
||
|
* headers :
|
||
|
* body :
|
||
|
* auth :
|
||
|
*
|
||
|
* response :
|
||
|
*
|
||
|
*/
|
||
|
list: [{
|
||
|
type: 'GET',
|
||
|
url: 'localhost:8080',
|
||
|
body: '{}',
|
||
|
headers: [],
|
||
3 years ago
|
response: { status: 200 },
|
||
4 years ago
|
createAt: Date.now()
|
||
|
}],
|
||
|
projectApiFilePaths: {},
|
||
|
activeEnvironment: {},
|
||
|
currentProjectKey: ''
|
||
|
|
||
3 years ago
|
})
|
||
4 years ago
|
|
||
|
export const mutations = {
|
||
|
|
||
3 years ago
|
MutApiFilePathsAdd(state, args) {
|
||
4 years ago
|
state.projectApiFilePaths = {
|
||
|
...state.projectApiFilePaths,
|
||
|
[state.currentProjectKey]: [...state.projectApiFilePaths[state.currentProjectKey], args]
|
||
|
}
|
||
|
},
|
||
|
|
||
3 years ago
|
MutApiFilePathsRemove(state, index) {
|
||
3 years ago
|
state.projectApiFilePaths[state.currentProjectKey].splice(index, 1)
|
||
|
Vue.set(state.projectApiFilePaths, state.currentProjectKey, [...state.projectApiFilePaths[state.currentProjectKey]])
|
||
4 years ago
|
},
|
||
|
|
||
3 years ago
|
MutListAdd(state, args) {
|
||
3 years ago
|
state.list.unshift(args)
|
||
|
if (state.list.length > 500) {
|
||
|
state.list.pop()
|
||
|
}
|
||
4 years ago
|
},
|
||
|
|
||
3 years ago
|
MutListRemove(state, index) {
|
||
4 years ago
|
// find index and set status
|
||
3 years ago
|
state.list.splice(index, 1)
|
||
|
// state.list = [...state.list]
|
||
4 years ago
|
},
|
||
3 years ago
|
MutList(state, list) {
|
||
3 years ago
|
state.projectApiFilePaths[state.currentProjectKey] = list
|
||
4 years ago
|
},
|
||
3 years ago
|
MutCurrentProjectKey(state, currentProjectKey) {
|
||
3 years ago
|
state.currentProjectKey = currentProjectKey
|
||
4 years ago
|
},
|
||
3 years ago
|
MutActiveEnvironment(state, { projectId, env }) {
|
||
3 years ago
|
state.activeEnvironment[projectId] = env
|
||
4 years ago
|
}
|
||
|
|
||
3 years ago
|
}
|
||
4 years ago
|
|
||
|
export const getters = {
|
||
3 years ago
|
GtrCurrentApiFilePaths(state) {
|
||
3 years ago
|
return state.projectApiFilePaths[state.currentProjectKey]
|
||
4 years ago
|
}
|
||
3 years ago
|
}
|
||
4 years ago
|
|
||
|
export const actions = {
|
||
|
|
||
3 years ago
|
async send({ commit, state }, { apiDecoded, api }) {
|
||
3 years ago
|
const a = { ...apiDecoded }
|
||
|
let t, t1, t2
|
||
4 years ago
|
try {
|
||
3 years ago
|
commit('notification/MutToggleProgressBar', true, { root: true })
|
||
4 years ago
|
|
||
|
if (apiDecoded.body) {
|
||
|
try {
|
||
3 years ago
|
a.body = JSON.parse(a.body)
|
||
4 years ago
|
} catch (e) {
|
||
3 years ago
|
console.log(e)
|
||
4 years ago
|
}
|
||
|
}
|
||
|
|
||
|
if (apiDecoded.auth) {
|
||
|
try {
|
||
3 years ago
|
a.auth = JSON.parse(a.auth)
|
||
4 years ago
|
} catch (e) {
|
||
3 years ago
|
console.log(e)
|
||
4 years ago
|
}
|
||
|
}
|
||
|
|
||
3 years ago
|
a.response = {}
|
||
4 years ago
|
// t = process.hrtime();
|
||
|
const req = {
|
||
3 years ago
|
params: a.params
|
||
|
? a.params.reduce((paramsObj, param) => {
|
||
|
if (param.name && param.enabled) {
|
||
|
paramsObj[param.name] = param.value
|
||
|
}
|
||
|
return paramsObj
|
||
|
}, {})
|
||
|
: {},
|
||
4 years ago
|
url: a.url,
|
||
|
method: a.type,
|
||
|
data: a.body,
|
||
3 years ago
|
headers: a.headers
|
||
|
? a.headers.reduce((headersObj, header) => {
|
||
|
if (header.name && header.enabled) {
|
||
|
headersObj[header.name] = header.value
|
||
|
}
|
||
|
return headersObj
|
||
|
}, {})
|
||
|
: {},
|
||
4 years ago
|
withCredentials: true
|
||
3 years ago
|
}
|
||
4 years ago
|
|
||
|
if (Object.values(api.perf).every(v => !v)) {
|
||
3 years ago
|
const data = await axios(req)
|
||
4 years ago
|
|
||
|
// t1 = process.hrtime(t);
|
||
|
// t2 = (t1[0] + t1[1] / 1000000000).toFixed(2);
|
||
|
|
||
3 years ago
|
a.response.status = data.status
|
||
|
a.response.headers = data.headers
|
||
|
a.response.data = data.data
|
||
4 years ago
|
} else {
|
||
3 years ago
|
this.$toast.info('Starting performance test').goAway(3000)
|
||
4 years ago
|
try {
|
||
|
// const res = await autocannon({
|
||
|
// ...req,
|
||
|
// ...api.perf,
|
||
|
// body: req.data
|
||
|
// });
|
||
3 years ago
|
// console.log('perf result', res)
|
||
4 years ago
|
|
||
3 years ago
|
a.response.status = 200
|
||
|
a.response.headers = []
|
||
|
a.response.data = {} // res
|
||
4 years ago
|
|
||
3 years ago
|
this.$toast.info('Finished performance test').goAway(3000)
|
||
4 years ago
|
} catch (e) {
|
||
|
console.log(e)
|
||
3 years ago
|
a.response.status = 400
|
||
|
a.response.data = e
|
||
4 years ago
|
}
|
||
|
}
|
||
|
} catch (e) {
|
||
3 years ago
|
t1 = process.hrtime(t)
|
||
|
t2 = (t1[0] + t1[1] / 1000000000).toFixed(2)
|
||
4 years ago
|
|
||
3 years ago
|
a.response = e.response
|
||
|
// throw e;
|
||
4 years ago
|
} finally {
|
||
3 years ago
|
commit('notification/MutToggleProgressBar', false, { root: true })
|
||
4 years ago
|
commit('MutListAdd', {
|
||
|
...api,
|
||
3 years ago
|
params: api.params && api.params.map(param => ({ ...param })),
|
||
|
headers: api.headers && api.headers.map(header => ({ ...header })),
|
||
4 years ago
|
body: JSON.stringify(api.body),
|
||
|
auth: JSON.stringify(api.auth),
|
||
|
timeTaken: t2,
|
||
|
createdAt: Date.now()
|
||
3 years ago
|
})
|
||
4 years ago
|
}
|
||
3 years ago
|
return a.response
|
||
4 years ago
|
},
|
||
|
|
||
3 years ago
|
async loadApiCollectionForProject({ commit, state }, { projectName, projectId }) {
|
||
3 years ago
|
const key = projectName + '__' + projectId
|
||
4 years ago
|
commit('MutCurrentProjectKey', key)
|
||
|
if (!(key in state.projectApiFilePaths)) {
|
||
3 years ago
|
commit('MutList', [])
|
||
4 years ago
|
}
|
||
|
}
|
||
3 years ago
|
}
|
||
4 years ago
|
|
||
|
/**
|
||
|
* @copyright Copyright (c) 2021, Xgene Cloud Ltd
|
||
|
*
|
||
|
* @author Naveen MR <oof1lab@gmail.com>
|
||
|
* @author Pranav C Balan <pranavxc@gmail.com>
|
||
|
*
|
||
|
* @license GNU AGPL version 3 or any later version
|
||
|
*
|
||
|
* This program is free software: you can redistribute it and/or modify
|
||
|
* it under the terms of the GNU Affero General Public License as
|
||
|
* published by the Free Software Foundation, either version 3 of the
|
||
|
* License, or (at your option) any later version.
|
||
|
*
|
||
|
* This program is distributed in the hope that it will be useful,
|
||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
* GNU Affero General Public License for more details.
|
||
|
*
|
||
|
* You should have received a copy of the GNU Affero General Public License
|
||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||
|
*
|
||
|
*/
|