1
0
Fork 0

🐛 Fixed issue in utils/backend

This commit is contained in:
Joost De Cock 2019-08-14 16:50:50 +02:00
parent 7ebc512142
commit cd11ea96fa
2 changed files with 54 additions and 39 deletions

8
.circleci/config.yml Normal file
View file

@ -0,0 +1,8 @@
version: 2
jobs:
build:
docker:
- image: circleci/node:lts
steps:
- checkout
- run: npm install --global lerna && lerna bootstrap

View file

@ -1,61 +1,68 @@
import axios from 'axios'
import axios from "axios";
function useBackend(baseURL, timeout = 10000) {
// Configure Axios
const api = axios.create({ baseURL, timeout })
const api = axios.create({ baseURL, timeout });
// Helper method for Authorization header
const auth = token => ({
headers: { Authorization: 'Bearer ' + token }
})
headers: { Authorization: "Bearer " + token }
});
const backend = {}
const backend = {};
// Oauth
backend.initOauth = data => api.post('/oauth/init', data) // Init Oauth (to get state)
backend.providerLogin = data => api.post('/oauth/login', data) // Finalize Oauth login
backend.initOauth = data => api.post("/oauth/init", data); // Init Oauth (to get state)
backend.providerLogin = data => api.post("/oauth/login", data); // Finalize Oauth login
// Signup flow
backend.signup = (email, password, language) => api.post('/signup', { email, password, language }) // Signup
backend.confirm = confirmId => api.post('/account', { id: confirmId }) // Confirm
backend.createAccount = (confirmId, consent) => api.post('/account', { id: confirmId, consent }) // Create account
backend.signup = (email, password, language) =>
api.post("/signup", { email, password, language }); // Signup
backend.confirm = confirmId => api.post("/account", { id: confirmId }); // Confirm
backend.createAccount = (confirmId, consent) =>
api.post("/account", { id: confirmId, consent }); // Create account
// Other non-authenticated calls
backend.login = (username, password) => api.post('/login', { username, password }) // Login
backend.confirmationLogin = id => api.post('/confirm/login', { id }) // Confirmation-based login
backend.resetPassword = username => api.post('/reset/password', { username: username }) // Ask for a password reset
backend.loadGist = handle => api.get('/gist/' + handle) // Load recipe/gist anonymously
backend.loadPatrons = handle => api.get('/patrons') // Load patron list
backend.login = (username, password) =>
api.post("/login", { username, password }); // Login
backend.confirmationLogin = id => api.post("/confirm/login", { id }); // Confirmation-based login
backend.resetPassword = username =>
api.post("/reset/password", { username: username }); // Ask for a password reset
backend.loadGist = handle => api.get("/gist/" + handle); // Load recipe/gist anonymously
backend.loadPatrons = handle => api.get("/patrons"); // Load patron list
// Users
backend.profile = (username, token) => api.get('/users/' + username, auth(token)) // Load user profile
backend.account = token => api.get('/account', auth(token)) // Try to authenticate based on stored token
backend.export = token => api.get('/export', auth(token)) // Export data
backend.restrict = token => api.get('/restrict', auth(token)) // Restrict data processing (freeze account)
backend.remove = token => api.get('/remove', auth(token)) // Remove account
backend.saveAccount = (data, token) => api.put('/account', data, auth(token)) // Update account
backend.availableUsername = (data, token) => api.post('/available/username', data, auth(token)) // Check is a username is available
backend.setPassword = (data, token) => api.post('/set/password', data, auth(token)) // (re)set a new password
backend.profile = (username, token) =>
api.get("/users/" + username, auth(token)); // Load user profile
backend.account = token => api.get("/account", auth(token)); // Try to authenticate based on stored token
backend.export = token => api.get("/export", auth(token)); // Export data
backend.restrict = token => api.get("/restrict", auth(token)); // Restrict data processing (freeze account)
backend.remove = token => api.get("/remove", auth(token)); // Remove account
backend.saveAccount = (data, token) => api.put("/account", data, auth(token)); // Update account
backend.availableUsername = (data, token) =>
api.post("/available/username", data, auth(token)); // Check is a username is available
backend.setPassword = (data, token) =>
api.post("/set/password", data, auth(token)); // (re)set a new password
// Models
backend.createModel = (data, token) => api.post('/model', data, auth(token)) // Create model
backend.saveModel = (handle, data, token) => api.put('/model/' + handle, data, auth(token)) // Update model
backend.removeModel = (handle, token) => api.delete('/model/' + handle, auth(token)) // Remove model
backend.removeModels = (data, token) => api.post('/remove/models', data, auth(token)) // Delete multiple models
backend.createModel = (data, token) => api.post("/models", data, auth(token)); // Create model
backend.saveModel = (handle, data, token) =>
api.put("/models/" + handle, data, auth(token)); // Update model
backend.removeModel = (handle, token) =>
api.delete("/models/" + handle, auth(token)); // Remove model
//backend.removeModels = (data, token) => api.post('/remove/models', data, auth(token)) // Delete multiple models
// Recipes
backend.loadRecipe = (handle, token) => api.get('/recipe/' + handle, auth(token)) // Load recipe
backend.createRecipe = (data, token) => api.post('/recipe', data, auth(token)) // Create recipe
backend.removeRecipe = (handle, token) => api.delete('/recipe/' + handle, auth(token)) // Remove recipe
backend.saveRecipe = (handle, data, token) => api.put('/recipe/' + handle, data, auth(token)) // Update recipe
backend.loadRecipe = (handle, token) =>
api.get("/recipes/" + handle, auth(token)); // Load recipe
backend.createRecipe = (data, token) =>
api.post("/recipes", data, auth(token)); // Create recipe
backend.removeRecipe = (handle, token) =>
api.delete("/recipes/" + handle, auth(token)); // Remove recipe
backend.saveRecipe = (handle, data, token) =>
api.put("/recipes/" + handle, data, auth(token)); // Update recipe
//backend.createDraft = (data, token) => api.post("/draft", data, auth(token)); // Create draft
//backend.saveDraft = (handle, data, token) =>
// api.put("/draft/" + handle, data, auth(token)); // Update draft
//backend.removeDrafts = (data, token) =>
// api.post("/remove/drafts", data, auth(token)); // Delete multiple drafts
return backend
return backend;
}
export default useBackend
export default useBackend;