1
- import { CompasSettings } from "../compas/CompasSettings.js" ;
2
- import { extractSclFromResponse , handleError , handleResponse , parseXml } from "./foundation.js" ;
1
+ import { formatXml } from '../file.js' ;
3
2
4
- export const SDS_NAMESPACE = 'https://www.lfenergy.org/compas/SclDataService/v1' ;
3
+ import { CompasSettings } from '../compas/CompasSettings.js' ;
4
+ import {
5
+ extractSclFromResponse ,
6
+ handleError ,
7
+ handleResponse ,
8
+ parseXml ,
9
+ } from './foundation.js' ;
10
+
11
+ export const SDS_NAMESPACE =
12
+ 'https://www.lfenergy.org/compas/SclDataService/v1' ;
5
13
6
14
export enum ChangeSet {
7
- MAJOR = " MAJOR" ,
8
- MINOR = " MINOR" ,
9
- PATCH = " PATCH" ,
15
+ MAJOR = ' MAJOR' ,
16
+ MINOR = ' MINOR' ,
17
+ PATCH = ' PATCH' ,
10
18
}
11
19
12
20
export interface CreateRequestBody {
13
- sclName : string ,
14
- comment : string ,
15
- doc : Document
21
+ sclName : string ;
22
+ comment : string | null ;
23
+ doc : Document ;
16
24
}
17
25
18
26
export interface UpdateRequestBody {
19
- changeSet : ChangeSet ,
20
- comment : string ,
21
- doc : Document
27
+ changeSet : ChangeSet ;
28
+ comment : string | null ;
29
+ doc : Document ;
22
30
}
23
31
24
32
export function CompasSclDataService ( ) {
25
-
26
33
function getCompasSettings ( ) {
27
34
return CompasSettings ( ) . compasSettings ;
28
35
}
29
36
30
37
return {
31
38
listSclTypes ( ) : Promise < Document > {
32
- const sclUrl = getCompasSettings ( ) . sclDataServiceUrl + '/common/v1/type/list' ;
39
+ const sclUrl =
40
+ getCompasSettings ( ) . sclDataServiceUrl + '/common/v1/type/list' ;
33
41
return fetch ( sclUrl )
34
42
. catch ( handleError )
35
43
. then ( handleResponse )
36
44
. then ( parseXml ) ;
37
45
} ,
38
46
39
47
listSclTypesAndOrder ( ) : Promise < Element [ ] > {
40
- return this . listSclTypes ( )
41
- . then ( xmlResponse => {
42
- return Array . from ( xmlResponse . querySelectorAll ( '*|Type' ) ?? [ ] )
43
- . sort ( ( type1 , type2 ) => {
44
- const description1 = type1 . getElementsByTagNameNS ( SDS_NAMESPACE , "Description" ) ! . item ( 0 ) ! . textContent ?? "" ;
45
- const description2 = type2 . getElementsByTagNameNS ( SDS_NAMESPACE , "Description" ) ! . item ( 0 ) ! . textContent ?? "" ;
46
- return description1 . localeCompare ( description2 )
47
- } ) ;
48
- } )
48
+ return this . listSclTypes ( ) . then ( xmlResponse => {
49
+ return Array . from ( xmlResponse . querySelectorAll ( '*|Type' ) ?? [ ] ) . sort (
50
+ ( type1 , type2 ) => {
51
+ const description1 =
52
+ type1
53
+ . getElementsByTagNameNS ( SDS_NAMESPACE , 'Description' ) !
54
+ . item ( 0 ) ! . textContent ?? '' ;
55
+ const description2 =
56
+ type2
57
+ . getElementsByTagNameNS ( SDS_NAMESPACE , 'Description' ) !
58
+ . item ( 0 ) ! . textContent ?? '' ;
59
+ return description1 . localeCompare ( description2 ) ;
60
+ }
61
+ ) ;
62
+ } ) ;
49
63
} ,
50
64
51
65
listScls ( type : string ) : Promise < Document > {
52
- const sclUrl = getCompasSettings ( ) . sclDataServiceUrl + '/scl/v1/' + type + '/list' ;
66
+ const sclUrl =
67
+ getCompasSettings ( ) . sclDataServiceUrl + '/scl/v1/' + type + '/list' ;
53
68
return fetch ( sclUrl )
54
69
. catch ( handleError )
55
70
. then ( handleResponse )
56
71
. then ( parseXml ) ;
57
72
} ,
58
73
59
74
listVersions ( type : string , id : string ) : Promise < Document > {
60
- const sclUrl = getCompasSettings ( ) . sclDataServiceUrl + '/scl/v1/' + type + '/' + id + "/versions" ;
75
+ const sclUrl =
76
+ getCompasSettings ( ) . sclDataServiceUrl +
77
+ '/scl/v1/' +
78
+ type +
79
+ '/' +
80
+ id +
81
+ '/versions' ;
61
82
return fetch ( sclUrl )
62
83
. catch ( handleError )
63
84
. then ( handleResponse )
64
85
. then ( parseXml ) ;
65
86
} ,
66
87
67
88
getSclDocument ( type : string , id : string ) : Promise < Document > {
68
- const sclUrl = getCompasSettings ( ) . sclDataServiceUrl + '/scl/v1/' + type + '/' + id ;
89
+ const sclUrl =
90
+ getCompasSettings ( ) . sclDataServiceUrl + '/scl/v1/' + type + '/' + id ;
69
91
return fetch ( sclUrl )
70
92
. catch ( handleError )
71
93
. then ( handleResponse )
72
94
. then ( parseXml )
73
95
. then ( extractSclFromResponse ) ;
74
96
} ,
75
97
76
- getSclDocumentVersion ( type : string , id : string , version : string ) : Promise < Document > {
77
- const sclUrl = getCompasSettings ( ) . sclDataServiceUrl + '/scl/v1/' + type + '/' + id + '/' + version ;
98
+ getSclDocumentVersion (
99
+ type : string ,
100
+ id : string ,
101
+ version : string
102
+ ) : Promise < Document > {
103
+ const sclUrl =
104
+ getCompasSettings ( ) . sclDataServiceUrl +
105
+ '/scl/v1/' +
106
+ type +
107
+ '/' +
108
+ id +
109
+ '/' +
110
+ version ;
78
111
return fetch ( sclUrl )
79
112
. catch ( handleError )
80
113
. then ( handleResponse )
81
114
. then ( parseXml )
82
115
. then ( extractSclFromResponse ) ;
83
116
} ,
84
117
85
- deleteSclDocumentVersion ( type : string , id : string , version : string ) : Promise < string > {
86
- const sclUrl = getCompasSettings ( ) . sclDataServiceUrl + '/scl/v1/' + type + '/' + id + '/' + version ;
87
- return fetch ( sclUrl , { method : 'DELETE' } )
118
+ deleteSclDocumentVersion (
119
+ type : string ,
120
+ id : string ,
121
+ version : string
122
+ ) : Promise < string > {
123
+ const sclUrl =
124
+ getCompasSettings ( ) . sclDataServiceUrl +
125
+ '/scl/v1/' +
126
+ type +
127
+ '/' +
128
+ id +
129
+ '/' +
130
+ version ;
131
+ return fetch ( sclUrl , { method : 'DELETE' } )
88
132
. catch ( handleError )
89
133
. then ( handleResponse ) ;
90
134
} ,
91
135
92
136
deleteSclDocument ( type : string , id : string ) : Promise < string > {
93
- const sclUrl = getCompasSettings ( ) . sclDataServiceUrl + '/scl/v1/' + type + '/' + id ;
94
- return fetch ( sclUrl , { method : 'DELETE' } )
137
+ const sclUrl =
138
+ getCompasSettings ( ) . sclDataServiceUrl + '/scl/v1/' + type + '/' + id ;
139
+ return fetch ( sclUrl , { method : 'DELETE' } )
95
140
. catch ( handleError )
96
141
. then ( handleResponse ) ;
97
142
} ,
@@ -101,37 +146,52 @@ export function CompasSclDataService() {
101
146
return fetch ( sclUrl , {
102
147
method : 'POST' ,
103
148
headers : {
104
- 'Content-Type' : 'application/xml'
149
+ 'Content-Type' : 'application/xml' ,
105
150
} ,
106
151
body : `<?xml version="1.0" encoding="UTF-8"?>
107
152
<sds:CreateRequest xmlns:sds="${ SDS_NAMESPACE } ">
108
153
<sds:Name>${ body . sclName } </sds:Name>
109
- <sds:Comment>${ body . comment } </sds:Comment>
110
- <sds:SclData><![CDATA[${ new XMLSerializer ( ) . serializeToString ( body . doc . documentElement ) } ]]></sds:SclData>
111
- </sds:CreateRequest>`
112
- } ) . catch ( handleError )
154
+ <sds:Comment>${ body . comment ?? '' } </sds:Comment>
155
+ <sds:SclData><![CDATA[${ formatXml (
156
+ new XMLSerializer ( ) . serializeToString (
157
+ body . doc . documentElement
158
+ )
159
+ ) } ]]></sds:SclData>
160
+ </sds:CreateRequest>` ,
161
+ } )
162
+ . catch ( handleError )
113
163
. then ( handleResponse )
114
164
. then ( parseXml )
115
165
. then ( extractSclFromResponse ) ;
116
166
} ,
117
167
118
- updateSclDocument ( type : string , id : string , body : UpdateRequestBody ) : Promise < Document > {
119
- const sclUrl = getCompasSettings ( ) . sclDataServiceUrl + '/scl/v1/' + type + '/' + id ;
168
+ updateSclDocument (
169
+ type : string ,
170
+ id : string ,
171
+ body : UpdateRequestBody
172
+ ) : Promise < Document > {
173
+ const sclUrl =
174
+ getCompasSettings ( ) . sclDataServiceUrl + '/scl/v1/' + type + '/' + id ;
120
175
return fetch ( sclUrl , {
121
176
method : 'PUT' ,
122
177
headers : {
123
- 'Content-Type' : 'application/xml'
178
+ 'Content-Type' : 'application/xml' ,
124
179
} ,
125
180
body : `<?xml version="1.0" encoding="UTF-8"?>
126
181
<sds:UpdateRequest xmlns:sds="${ SDS_NAMESPACE } ">
127
182
<sds:ChangeSet>${ body . changeSet } </sds:ChangeSet>
128
- <sds:Comment>${ body . comment } </sds:Comment>
129
- <sds:SclData><![CDATA[${ new XMLSerializer ( ) . serializeToString ( body . doc . documentElement ) } ]]></sds:SclData>
130
- </sds:UpdateRequest>`
131
- } ) . catch ( handleError )
183
+ <sds:Comment>${ body . comment ?? '' } </sds:Comment>
184
+ <sds:SclData><![CDATA[${ formatXml (
185
+ new XMLSerializer ( ) . serializeToString (
186
+ body . doc . documentElement
187
+ )
188
+ ) } ]]></sds:SclData>
189
+ </sds:UpdateRequest>` ,
190
+ } )
191
+ . catch ( handleError )
132
192
. then ( handleResponse )
133
193
. then ( parseXml )
134
194
. then ( extractSclFromResponse ) ;
135
- }
136
- }
195
+ } ,
196
+ } ;
137
197
}
0 commit comments