@@ -3,12 +3,19 @@ import { ref, watch } from "vue";
3
3
import { SagaHistory , SagaMessage } from "@/resources/SagaHistory" ;
4
4
import { useFetchFromServiceControl } from "@/composables/serviceServiceControlUrls" ;
5
5
import Message from "@/resources/Message" ;
6
+ import { parse , stringify } from "lossless-json" ;
7
+ import xmlFormat from "xml-formatter" ;
8
+
9
+ interface DataContainer < T > {
10
+ loading ?: boolean ;
11
+ failed_to_load ?: boolean ;
12
+ not_found ?: boolean ;
13
+ data : T ;
14
+ }
6
15
7
16
export interface SagaMessageData {
8
17
message_id : string ;
9
- data : string ;
10
- type : "json" | "xml" ;
11
- error : boolean ;
18
+ body : DataContainer < { value ?: string ; content_type ?: string } > ;
12
19
}
13
20
export const useSagaDiagramStore = defineStore ( "SagaDiagramStore" , ( ) => {
14
21
const sagaHistory = ref < SagaHistory | null > ( null ) ;
@@ -71,67 +78,38 @@ export const useSagaDiagramStore = defineStore("SagaDiagramStore", () => {
71
78
72
79
async function fetchSagaMessageData ( message : SagaMessage ) : Promise < SagaMessageData > {
73
80
const bodyUrl = ( message . body_url ?? formatUrl ( MessageBodyEndpoint , message . message_id ) ) . replace ( / ^ \/ / , "" ) ;
81
+ const result : SagaMessageData = {
82
+ message_id : message . message_id ,
83
+ body : { data : { } } ,
84
+ } ;
74
85
75
- try {
76
- const response = await useFetchFromServiceControl ( bodyUrl , { cache : "no-store" } ) ;
86
+ result . body . loading = true ;
87
+ result . body . failed_to_load = false ;
77
88
78
- // Treat 404 as empty data, not as an error
89
+ try {
90
+ const response = await useFetchFromServiceControl ( bodyUrl ) ;
79
91
if ( response . status === 404 ) {
80
- return {
81
- message_id : message . message_id ,
82
- data : "" ,
83
- type : "json" ,
84
- error : false ,
85
- } ;
86
- }
87
-
88
- // Handle other non-OK responses as errors
89
- if ( ! response . ok ) {
90
- error . value = `HTTP error! status: ${ response . status } ` ;
91
- return {
92
- message_id : message . message_id ,
93
- data : "" ,
94
- type : "json" ,
95
- error : true ,
96
- } ;
92
+ result . body . not_found = true ;
93
+ return result ;
97
94
}
98
95
99
- const body = await response . text ( ) ;
96
+ const contentType = response . headers . get ( "content-type" ) ;
97
+ result . body . data . content_type = contentType ?? "text/plain" ;
98
+ result . body . data . value = await response . text ( ) ;
100
99
101
- if ( ! body ) {
102
- return {
103
- message_id : message . message_id ,
104
- data : "" ,
105
- type : "json" ,
106
- error : false ,
107
- } ;
100
+ if ( contentType === "application/json" ) {
101
+ result . body . data . value = stringify ( parse ( result . body . data . value ) , null , 2 ) ?? result . body . data . value ;
108
102
}
109
-
110
- // Determine the content type
111
- if ( body . trim ( ) . startsWith ( "<?xml" ) ) {
112
- return {
113
- message_id : message . message_id ,
114
- data : body ,
115
- type : "xml" ,
116
- error : false ,
117
- } ;
118
- } else {
119
- return {
120
- message_id : message . message_id ,
121
- data : body ,
122
- type : "json" ,
123
- error : false ,
124
- } ;
103
+ if ( contentType === "text/xml" ) {
104
+ result . body . data . value = xmlFormat ( result . body . data . value , { indentation : " " , collapseContent : true } ) ;
125
105
}
126
- } catch ( e ) {
127
- error . value = e instanceof Error ? e . message : "Unknown error occurred" ;
128
- return {
129
- message_id : message . message_id ,
130
- data : "" ,
131
- type : "json" ,
132
- error : true ,
133
- } ;
106
+ } catch {
107
+ result . body . failed_to_load = true ;
108
+ } finally {
109
+ result . body . loading = false ;
134
110
}
111
+
112
+ return result ;
135
113
}
136
114
137
115
async function getAuditMessages ( sagaId : string ) {
0 commit comments