mirror of https://github.com/nocodb/nocodb
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
115 lines
3.2 KiB
115 lines
3.2 KiB
4 years ago
|
export default class RestApi {
|
||
3 years ago
|
constructor(table, $ctx) {
|
||
3 years ago
|
this.table = table
|
||
|
this.$ctx = $ctx
|
||
4 years ago
|
}
|
||
|
|
||
|
// todo: - get version letter and use table alias
|
||
3 years ago
|
async list(params) {
|
||
3 years ago
|
const data = await this.get(`/nc/${this.$ctx.$route.params.project_id}/api/v1/${this.table}`, params)
|
||
3 years ago
|
return data.data
|
||
4 years ago
|
}
|
||
|
|
||
3 years ago
|
async read(id) {
|
||
3 years ago
|
const data = await this.get(`/nc/${this.$ctx.$route.params.project_id}/api/v1/${this.table}/${id}`)
|
||
|
return data.data
|
||
4 years ago
|
}
|
||
|
|
||
3 years ago
|
async count(params) {
|
||
4 years ago
|
if (this.timeout) {
|
||
3 years ago
|
return this.timeout
|
||
4 years ago
|
}
|
||
|
try {
|
||
|
const data = await this.get(`/nc/${this.$ctx.$route.params.project_id}/api/v1/${this.table}/count`, params, {
|
||
3 years ago
|
timeout: 10000
|
||
|
})
|
||
|
return data && data.data
|
||
4 years ago
|
} catch (e) {
|
||
3 years ago
|
if (e.code === 'ECONNABORTED') {
|
||
|
// eslint-disable-next-line no-return-assign
|
||
|
return this.timeout = { count: Infinity }
|
||
4 years ago
|
} else {
|
||
3 years ago
|
throw e
|
||
4 years ago
|
}
|
||
|
}
|
||
4 years ago
|
}
|
||
|
|
||
3 years ago
|
get(url, params, extras = {}) {
|
||
4 years ago
|
return this.$axios({
|
||
|
url,
|
||
|
params,
|
||
4 years ago
|
...extras
|
||
4 years ago
|
})
|
||
|
}
|
||
|
|
||
3 years ago
|
async paginatedList(params) {
|
||
3 years ago
|
const [list, { count }] = await Promise.all([this.list(params), this.count({
|
||
4 years ago
|
where: params.where || '',
|
||
|
conditionGraph: params.conditionGraph
|
||
3 years ago
|
})])
|
||
|
return { list, count }
|
||
4 years ago
|
}
|
||
|
|
||
3 years ago
|
async paginatedM2mNotChildrenList(params, assoc, pid) {
|
||
3 years ago
|
const { list, info: { count } } = (await this.get(`/nc/${this.$ctx.$route.params.project_id}/api/v1/${this.table}/m2mNotChildren/${assoc}/${pid}`, params)).data
|
||
|
return { list, count }
|
||
4 years ago
|
}
|
||
|
|
||
3 years ago
|
async update(id, data, oldData) {
|
||
4 years ago
|
const res = await this.$axios({
|
||
|
method: 'put',
|
||
|
url: `/nc/${this.$ctx.$route.params.project_id}/api/v1/${this.table}/${id}`,
|
||
3 years ago
|
data
|
||
|
})
|
||
4 years ago
|
|
||
3 years ago
|
return res
|
||
4 years ago
|
}
|
||
|
|
||
3 years ago
|
async insert(data) {
|
||
4 years ago
|
return (await this.$axios({
|
||
|
method: 'post',
|
||
|
url: `/nc/${this.$ctx.$route.params.project_id}/api/v1/${this.table}`,
|
||
3 years ago
|
data
|
||
|
})).data
|
||
4 years ago
|
}
|
||
|
|
||
3 years ago
|
async delete(id) {
|
||
4 years ago
|
return this.$axios({
|
||
|
method: 'delete',
|
||
3 years ago
|
url: `/nc/${this.$ctx.$route.params.project_id}/api/v1/${this.table}/${id}`
|
||
4 years ago
|
})
|
||
|
}
|
||
|
|
||
3 years ago
|
get $axios() {
|
||
3 years ago
|
return this.$ctx.$axios
|
||
4 years ago
|
}
|
||
|
|
||
3 years ago
|
get apiUrl() {
|
||
3 years ago
|
return `${process.env.NODE_ENV === 'production'
|
||
|
? `${window.location.protocol}//${window.location.hostname}${window.location.port ? `:${window.location.port}` : ''}`
|
||
4 years ago
|
: 'http://localhost:8080'}/nc/${this.$ctx.$route.params.project_id}/api/v1/${this.table}`
|
||
|
}
|
||
|
}
|
||
|
/**
|
||
|
* @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/>.
|
||
|
*
|
||
|
*/
|