Directual + React JS
The path to building a React app without infrastructure hassles π€
Last updated
Was this helpful?
The path to building a React app without infrastructure hassles π€
Last updated
Was this helpful?
Was this helpful?
import Directual from 'directual-api';
import { useAuth } from '../auth' // auth.js is a React-wrapper for directual-api authentication methods
//...
const api = new Directual({ apiHost: '/' })
const auth = useAuth();
//...
const dataStructure = '' // todo: insert here Sysname of your data structure
const endpoint = '' // todo: insert here Method name of your API-endpoint
//...
// Auth context:
auth.user // returns user ID
auth.sessionID // returns user session ID
auth.isAutorised() // returns true if user is authorised
auth.hasRole('role') // returns true if user.role == 'role' (see user management further)
//...
// GET request:
function getData() {
api
// Name of Data structure (table) in the Database
.structure(dataStructure)
// GET request + query params (sessionID)
.getData(endpoint, { sessionID: auth.sessionID})
// other possible query params: page, pageSize, sort and any custom parameter for Filtering
.then((response) => {
// handling response
})
.catch((e) => {
// handling errors
})
}
//...
// POST-request:
let payload = {} // Request payload
function postData() {
api
// Name of Data structure (table) in the Database
.structure(dataStructure)
// POST request + payload + query params
.setData(endpoint, payload, { sessionID: auth.sessionID })
.then((response) => {
// handling response
})
.catch((e) => {
// handling errors
})
}
//...