mirror of
https://github.com/ToolJet/ToolJet
synced 2026-05-23 08:58:26 +00:00
Refactor API services to remove repeated code
This commit is contained in:
parent
dfe657f396
commit
a3ecf4abde
7 changed files with 34 additions and 96 deletions
|
|
@ -4,7 +4,10 @@ export function authHeader() {
|
|||
// return authorization header with jwt token
|
||||
const currentUser = authenticationService.currentUserValue;
|
||||
if (currentUser && currentUser.auth_token) {
|
||||
return { Authorization: `${currentUser.auth_token}` };
|
||||
return {
|
||||
Authorization: `${currentUser.auth_token}`,
|
||||
'Content-Type': 'application/json'
|
||||
};
|
||||
} else {
|
||||
return {};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,11 +18,8 @@ function getAll() {
|
|||
function createApp() {
|
||||
const body = {
|
||||
}
|
||||
const headers = {
|
||||
...authHeader(),
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
const requestOptions = { method: 'POST', headers: headers, body: JSON.stringify(body) };
|
||||
|
||||
const requestOptions = { method: 'POST', headers: authHeader(), body: JSON.stringify(body) };
|
||||
return fetch(`${config.apiUrl}/apps`, requestOptions).then(handleResponse);
|
||||
}
|
||||
|
||||
|
|
@ -33,12 +30,7 @@ function getApp(id) {
|
|||
|
||||
|
||||
function saveApp(id, attributes) {
|
||||
const headers = {
|
||||
...authHeader(),
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
|
||||
const requestOptions = { method: 'PUT', headers: headers, body: JSON.stringify({app: attributes}) };
|
||||
const requestOptions = { method: 'PUT', headers: authHeader(), body: JSON.stringify({app: attributes}) };
|
||||
return fetch(`${config.apiUrl}/apps/${id}`, requestOptions).then(handleResponse);
|
||||
}
|
||||
|
||||
|
|
@ -53,10 +45,7 @@ function createAppUser(app_id, org_user_id, role) {
|
|||
org_user_id,
|
||||
role
|
||||
}
|
||||
const headers = {
|
||||
...authHeader(),
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
const requestOptions = { method: 'POST', headers: headers, body: JSON.stringify(body) };
|
||||
|
||||
const requestOptions = { method: 'POST', headers: authHeader(), body: JSON.stringify(body) };
|
||||
return fetch(`${config.apiUrl}/app_users`, requestOptions).then(handleResponse);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,24 +17,16 @@ function create(appId, versionName) {
|
|||
const body = {
|
||||
versionName
|
||||
}
|
||||
const headers = {
|
||||
...authHeader(),
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
const requestOptions = { method: 'POST', headers: headers, body: JSON.stringify(body) };
|
||||
|
||||
const requestOptions = { method: 'POST', headers: authHeader(), body: JSON.stringify(body) };
|
||||
return fetch(`${config.apiUrl}/apps/${appId}/versions`, requestOptions).then(handleResponse);
|
||||
}
|
||||
|
||||
function save(appId, versionId, definition) {
|
||||
const headers = {
|
||||
...authHeader(),
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
|
||||
const body = {
|
||||
definition
|
||||
};
|
||||
|
||||
const requestOptions = { method: 'PUT', headers: headers, body: JSON.stringify(body) };
|
||||
const requestOptions = { method: 'PUT', headers: authHeader(), body: JSON.stringify(body) };
|
||||
return fetch(`${config.apiUrl}/apps/${appId}/versions/${versionId}`, requestOptions).then(handleResponse);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,11 +21,8 @@ function create(app_id, name, kind, options, data_source_id) {
|
|||
options,
|
||||
data_source_id
|
||||
}
|
||||
const headers = {
|
||||
...authHeader(),
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
const requestOptions = { method: 'POST', headers: headers, body: JSON.stringify(body) };
|
||||
|
||||
const requestOptions = { method: 'POST', headers: authHeader(), body: JSON.stringify(body) };
|
||||
return fetch(`${config.apiUrl}/data_queries`, requestOptions).then(handleResponse);
|
||||
}
|
||||
|
||||
|
|
@ -34,11 +31,8 @@ function update(id, name, options) {
|
|||
options,
|
||||
name
|
||||
}
|
||||
const headers = {
|
||||
...authHeader(),
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
const requestOptions = { method: 'PATCH', headers: headers, body: JSON.stringify(body) };
|
||||
|
||||
const requestOptions = { method: 'PATCH', headers: authHeader(), body: JSON.stringify(body) };
|
||||
return fetch(`${config.apiUrl}/data_queries/${id}`, requestOptions).then(handleResponse);
|
||||
}
|
||||
|
||||
|
|
@ -46,10 +40,7 @@ function run(queryId, options) {
|
|||
const body = {
|
||||
options: options
|
||||
}
|
||||
const headers = {
|
||||
...authHeader(),
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
const requestOptions = { method: 'POST', headers: headers, body: JSON.stringify(body) };
|
||||
|
||||
const requestOptions = { method: 'POST', headers: authHeader(), body: JSON.stringify(body) };
|
||||
return fetch(`${config.apiUrl}/data_queries/${queryId}/run`, requestOptions).then(handleResponse);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,11 +22,8 @@ function create(app_id, name, kind, options) {
|
|||
kind,
|
||||
options
|
||||
}
|
||||
const headers = {
|
||||
...authHeader(),
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
const requestOptions = { method: 'POST', headers: headers, body: JSON.stringify(body) };
|
||||
|
||||
const requestOptions = { method: 'POST', headers: authHeader(), body: JSON.stringify(body) };
|
||||
return fetch(`${config.apiUrl}/data_sources`, requestOptions).then(handleResponse);
|
||||
}
|
||||
|
||||
|
|
@ -36,11 +33,8 @@ function save(id, app_id, name, options) {
|
|||
name,
|
||||
options
|
||||
}
|
||||
const headers = {
|
||||
...authHeader(),
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
const requestOptions = { method: 'PUT', headers: headers, body: JSON.stringify(body) };
|
||||
|
||||
const requestOptions = { method: 'PUT', headers: authHeader(), body: JSON.stringify(body) };
|
||||
return fetch(`${config.apiUrl}/data_sources/${id}`, requestOptions).then(handleResponse);
|
||||
}
|
||||
|
||||
|
|
@ -51,31 +45,16 @@ function test(kind, options) {
|
|||
options
|
||||
};
|
||||
|
||||
const headers = {
|
||||
...authHeader(),
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
|
||||
const requestOptions = { method: 'POST', headers: headers, body: JSON.stringify(body) };
|
||||
const requestOptions = { method: 'POST', headers: authHeader(), body: JSON.stringify(body) };
|
||||
return fetch(`${config.apiUrl}/data_sources/test_connection`, requestOptions).then(handleResponse);
|
||||
}
|
||||
|
||||
function setOauth2Token(dataSourceId, body) {
|
||||
const headers = {
|
||||
...authHeader(),
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
|
||||
const requestOptions = { method: 'POST', headers: headers, body: JSON.stringify(body) };
|
||||
const requestOptions = { method: 'POST', headers: authHeader(), body: JSON.stringify(body) };
|
||||
return fetch(`${config.apiUrl}/data_sources/${dataSourceId}/authorize_oauth2`, requestOptions).then(handleResponse);
|
||||
}
|
||||
|
||||
function fetchOauth2BaseUrl(provider) {
|
||||
const headers = {
|
||||
...authHeader(),
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
|
||||
const requestOptions = { method: 'POST', headers: headers, body: JSON.stringify({provider}) };
|
||||
const requestOptions = { method: 'POST', headers: authHeader(), body: JSON.stringify({provider}) };
|
||||
return fetch(`${config.apiUrl}/data_sources/fetch_oauth2_base_url`, requestOptions).then(handleResponse);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,11 +13,8 @@ function create(first_name, last_name, email, role) {
|
|||
email,
|
||||
role
|
||||
}
|
||||
const headers = {
|
||||
...authHeader(),
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
const requestOptions = { method: 'POST', headers: headers, body: JSON.stringify(body) };
|
||||
|
||||
const requestOptions = { method: 'POST', headers: authHeader(), body: JSON.stringify(body) };
|
||||
return fetch(`${config.apiUrl}/organization_users`, requestOptions).then(handleResponse);
|
||||
}
|
||||
|
||||
|
|
@ -26,10 +23,7 @@ function changeRole(id, role) {
|
|||
const body = {
|
||||
role
|
||||
}
|
||||
const headers = {
|
||||
...authHeader(),
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
const requestOptions = { method: 'POST', headers: headers, body: JSON.stringify(body) };
|
||||
|
||||
const requestOptions = { method: 'POST', headers: authHeader(), body: JSON.stringify(body) };
|
||||
return fetch(`${config.apiUrl}/organization_users/${id}/change_role`, requestOptions).then(handleResponse);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,20 +20,13 @@ function createUser(first_name, last_name, email, role) {
|
|||
email,
|
||||
role
|
||||
}
|
||||
const headers = {
|
||||
...authHeader(),
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
const requestOptions = { method: 'POST', headers: headers, body: JSON.stringify(body) };
|
||||
|
||||
const requestOptions = { method: 'POST', headers: authHeader(), body: JSON.stringify(body) };
|
||||
return fetch(`${config.apiUrl}/users`, requestOptions).then(handleResponse);
|
||||
}
|
||||
|
||||
function deleteUser(id) {
|
||||
const headers = {
|
||||
...authHeader(),
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
const requestOptions = { method: 'DELETE', headers: headers, body: JSON.stringify({}) };
|
||||
const requestOptions = { method: 'DELETE', headers: authHeader(), body: JSON.stringify({}) };
|
||||
return fetch(`${config.apiUrl}/users/${id}`, requestOptions).then(handleResponse);
|
||||
}
|
||||
|
||||
|
|
@ -42,11 +35,8 @@ function setPasswordFromToken(token, password) {
|
|||
token,
|
||||
password
|
||||
}
|
||||
const headers = {
|
||||
...authHeader(),
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
const requestOptions = { method: 'POST', headers: headers, body: JSON.stringify(body) };
|
||||
|
||||
const requestOptions = { method: 'POST', headers: authHeader(), body: JSON.stringify(body) };
|
||||
return fetch(`${config.apiUrl}/user/set_password_from_token`, requestOptions).then(handleResponse);
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue