first functionable
This commit is contained in:
99
src/stores/requestStore.ts
Normal file
99
src/stores/requestStore.ts
Normal file
@@ -0,0 +1,99 @@
|
||||
import { defineStore } from 'pinia';
|
||||
import { ref, watch } from 'vue';
|
||||
|
||||
export interface KeyValue {
|
||||
id: string;
|
||||
key: string;
|
||||
value: string;
|
||||
enabled: boolean;
|
||||
}
|
||||
|
||||
export interface RequestData {
|
||||
id: string;
|
||||
method: string;
|
||||
url: string;
|
||||
params: KeyValue[];
|
||||
headers: KeyValue[];
|
||||
body: string;
|
||||
response?: {
|
||||
status: number;
|
||||
headers: Record<string, string>;
|
||||
body: string;
|
||||
time: number;
|
||||
size: number;
|
||||
};
|
||||
timestamp: number;
|
||||
}
|
||||
|
||||
export const useRequestStore = defineStore('request', () => {
|
||||
// State
|
||||
const history = ref<RequestData[]>([]);
|
||||
const activeRequest = ref<RequestData>({
|
||||
id: crypto.randomUUID(),
|
||||
method: 'GET',
|
||||
url: '',
|
||||
params: [
|
||||
{ id: crypto.randomUUID(), key: '', value: '', enabled: true }
|
||||
],
|
||||
headers: [
|
||||
{ id: crypto.randomUUID(), key: '', value: '', enabled: true }
|
||||
],
|
||||
body: '',
|
||||
timestamp: Date.now(),
|
||||
});
|
||||
|
||||
// Load history from local storage
|
||||
const savedHistory = localStorage.getItem('request_history');
|
||||
if (savedHistory) {
|
||||
try {
|
||||
history.value = JSON.parse(savedHistory);
|
||||
} catch (e) {
|
||||
console.error('Failed to parse history', e);
|
||||
}
|
||||
}
|
||||
|
||||
// Actions
|
||||
const addToHistory = (req: RequestData) => {
|
||||
// Add to beginning, limit to 50
|
||||
const newEntry = { ...req, timestamp: Date.now() };
|
||||
// Don't store the empty trailing rows in history to save space?
|
||||
// Actually keep them for consistency or filter them. Let's filter empty keys.
|
||||
newEntry.params = newEntry.params.filter(p => p.key.trim() !== '');
|
||||
newEntry.headers = newEntry.headers.filter(h => h.key.trim() !== '');
|
||||
|
||||
history.value.unshift(newEntry);
|
||||
if (history.value.length > 50) {
|
||||
history.value.pop();
|
||||
}
|
||||
};
|
||||
|
||||
const clearHistory = () => {
|
||||
history.value = [];
|
||||
};
|
||||
|
||||
const loadRequest = (req: RequestData) => {
|
||||
// Deep copy
|
||||
const loaded = JSON.parse(JSON.stringify(req));
|
||||
loaded.id = crypto.randomUUID();
|
||||
loaded.response = undefined;
|
||||
|
||||
// Ensure at least one empty row for editing
|
||||
if (loaded.params.length === 0) loaded.params.push({ id: crypto.randomUUID(), key: '', value: '', enabled: true });
|
||||
if (loaded.headers.length === 0) loaded.headers.push({ id: crypto.randomUUID(), key: '', value: '', enabled: true });
|
||||
|
||||
activeRequest.value = loaded;
|
||||
};
|
||||
|
||||
// Watch and Save
|
||||
watch(history, (newVal) => {
|
||||
localStorage.setItem('request_history', JSON.stringify(newVal));
|
||||
}, { deep: true });
|
||||
|
||||
return {
|
||||
history,
|
||||
activeRequest,
|
||||
addToHistory,
|
||||
clearHistory,
|
||||
loadRequest
|
||||
};
|
||||
});
|
||||
Reference in New Issue
Block a user