1
0
Fork 0

Updated endpoints in utils/backend

This commit is contained in:
Joost De Cock 2019-08-17 18:18:38 +02:00
parent 112ead9796
commit 2283660d38

View file

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