corrad-af-2024/composables/useApiRequest.js

136 lines
3.5 KiB
JavaScript

export const useApiRequest = () => {
// MAIN SEND REQUEST FUNCTION - REAL BACKEND INTEGRATION
const sendRequest = async (requestData, callbacks = {}) => {
const { onStart, onSuccess, onError, onComplete } = callbacks
if (!requestData.url) {
onError?.('Please enter a URL')
return
}
onStart?.()
try {
// Prepare request data for backend
const payload = {
url: requestData.url,
method: requestData.method,
headers: requestData.headers,
params: requestData.params,
auth: requestData.auth,
requestBody: requestData.body,
timeout: 30000
}
// Make request to our backend proxy
const result = await $fetch('/api/api-platform/send-request', {
method: 'POST',
body: payload
})
if (result.statusCode === 200) {
onSuccess?.(result.data)
return result.data
} else {
onError?.(result.message, result.data || null)
return result.data
}
} catch (error) {
const errorResponse = {
status: 500,
statusText: 'Internal Server Error',
headers: {},
data: { error: error.message || 'Something went wrong' },
time: 0,
size: 0
}
onError?.('Failed to send request', errorResponse)
return errorResponse
} finally {
onComplete?.()
}
}
// Utility methods
const addRow = (arrayRef) => {
arrayRef.value.push({ active: true, key: '', value: '', description: '' })
}
const removeRow = (arrayRef, index) => {
if (arrayRef.value.length > 1) {
arrayRef.value.splice(index, 1)
}
}
const formatJson = (obj) => {
return JSON.stringify(obj, null, 2)
}
const getStatusVariant = (status) => {
if (status >= 200 && status < 300) return 'success'
if (status >= 300 && status < 400) return 'warning'
if (status >= 400) return 'danger'
return 'secondary'
}
const getMethodVariant = (method) => {
const variants = {
'GET': 'info',
'POST': 'success',
'PUT': 'warning',
'PATCH': 'secondary',
'DELETE': 'danger'
}
return variants[method] || 'primary'
}
// Enhanced JSON utilities with notification integration
const beautifyJson = (content, { showNotification } = {}) => {
try {
const parsed = JSON.parse(content)
const beautified = JSON.stringify(parsed, null, 2)
showNotification?.('JSON beautified successfully', 'success', 2000)
return beautified
} catch (error) {
showNotification?.('Invalid JSON format', 'error')
throw error
}
}
const minifyJson = (content, { showNotification } = {}) => {
try {
const parsed = JSON.parse(content)
const minified = JSON.stringify(parsed)
showNotification?.('JSON minified successfully', 'success', 2000)
return minified
} catch (error) {
showNotification?.('Invalid JSON format', 'error')
throw error
}
}
const copyToClipboard = async (content, message = 'Copied to clipboard', { showNotification } = {}) => {
try {
await navigator.clipboard.writeText(content)
showNotification?.(message, 'success', 2000)
return true
} catch (error) {
showNotification?.('Failed to copy to clipboard', 'error')
return false
}
}
return {
sendRequest,
addRow,
removeRow,
formatJson,
getStatusVariant,
getMethodVariant,
beautifyJson,
minifyJson,
copyToClipboard
}
}