@@ -12,12 +12,8 @@ import { ContingencyListType } from './elementType';
12
12
import { CONTINGENCY_ENDPOINTS } from './constants-endpoints' ;
13
13
import {
14
14
ElementType ,
15
- fetchEnv ,
16
- backendFetch ,
17
- backendFetchJson ,
18
- backendFetchText ,
19
15
getRequestParamFromList ,
20
- getUserToken ,
16
+ fetchEnv ,
21
17
} from '@gridsuite/commons-ui' ;
22
18
23
19
const PREFIX_USER_ADMIN_SERVER_QUERIES =
@@ -39,6 +35,11 @@ const PREFIX_FILTERS_QUERIES =
39
35
import . meta. env . VITE_API_GATEWAY + '/filter/v1/filters' ;
40
36
const PREFIX_STUDY_QUERIES = import . meta. env . VITE_API_GATEWAY + '/study' ;
41
37
38
+ function getToken ( ) {
39
+ const state = store . getState ( ) ;
40
+ return state . user . id_token ;
41
+ }
42
+
42
43
export function connectNotificationsWsUpdateConfig ( ) {
43
44
const webSocketBaseUrl = document . baseURI
44
45
. replace ( / ^ h t t p : \/ \/ / , 'ws://' )
@@ -50,7 +51,7 @@ export function connectNotificationsWsUpdateConfig() {
50
51
APP_NAME ;
51
52
52
53
const reconnectingWebSocket = new ReconnectingWebSocket (
53
- ( ) => webSocketUrl + '&access_token=' + getUserToken ( )
54
+ ( ) => webSocketUrl + '&access_token=' + getToken ( )
54
55
) ;
55
56
reconnectingWebSocket . onopen = function ( ) {
56
57
console . info (
@@ -60,6 +61,78 @@ export function connectNotificationsWsUpdateConfig() {
60
61
return reconnectingWebSocket ;
61
62
}
62
63
64
+ function parseError ( text ) {
65
+ try {
66
+ return JSON . parse ( text ) ;
67
+ } catch ( err ) {
68
+ return null ;
69
+ }
70
+ }
71
+
72
+ function handleError ( response ) {
73
+ return response . text ( ) . then ( ( text ) => {
74
+ const errorName = 'HttpResponseError : ' ;
75
+ let error ;
76
+ const errorJson = parseError ( text ) ;
77
+ if (
78
+ errorJson &&
79
+ errorJson . status &&
80
+ errorJson . error &&
81
+ errorJson . message
82
+ ) {
83
+ error = new Error (
84
+ errorName +
85
+ errorJson . status +
86
+ ' ' +
87
+ errorJson . error +
88
+ ', message : ' +
89
+ errorJson . message
90
+ ) ;
91
+ error . status = errorJson . status ;
92
+ } else {
93
+ error = new Error (
94
+ errorName + response . status + ' ' + response . statusText
95
+ ) ;
96
+ error . status = response . status ;
97
+ }
98
+ throw error ;
99
+ } ) ;
100
+ }
101
+
102
+ function prepareRequest ( init , token ) {
103
+ if ( ! ( typeof init == 'undefined' || typeof init == 'object' ) ) {
104
+ throw new TypeError (
105
+ 'Argument 2 of backendFetch is not an object' + typeof init
106
+ ) ;
107
+ }
108
+ const initCopy = Object . assign ( { } , init ) ;
109
+ initCopy . headers = new Headers ( initCopy . headers || { } ) ;
110
+ const tokenCopy = token ? token : getToken ( ) ;
111
+ initCopy . headers . append ( 'Authorization' , 'Bearer ' + tokenCopy ) ;
112
+ return initCopy ;
113
+ }
114
+
115
+ function safeFetch ( url , initCopy ) {
116
+ return fetch ( url , initCopy ) . then ( ( response ) =>
117
+ response . ok ? response : handleError ( response )
118
+ ) ;
119
+ }
120
+
121
+ export function backendFetch ( url , init , token ) {
122
+ const initCopy = prepareRequest ( init , token ) ;
123
+ return safeFetch ( url , initCopy ) ;
124
+ }
125
+
126
+ export function backendFetchText ( url , init , token ) {
127
+ const initCopy = prepareRequest ( init , token ) ;
128
+ return safeFetch ( url , initCopy ) . then ( ( safeResponse ) => safeResponse . text ( ) ) ;
129
+ }
130
+
131
+ export function backendFetchJson ( url , init , token ) {
132
+ const initCopy = prepareRequest ( init , token ) ;
133
+ return safeFetch ( url , initCopy ) . then ( ( safeResponse ) => safeResponse . json ( ) ) ;
134
+ }
135
+
63
136
const getContingencyUriParamType = ( contingencyListType ) => {
64
137
switch ( contingencyListType ) {
65
138
case ContingencyListType . SCRIPT . id :
@@ -411,6 +484,19 @@ export function duplicateElement(
411
484
} ) ;
412
485
}
413
486
487
+ export function elementExists ( directoryUuid , elementName , type ) {
488
+ const elementNameEncoded = encodeURIComponent ( elementName ) ;
489
+ const existsElementUrl =
490
+ PREFIX_DIRECTORY_SERVER_QUERIES +
491
+ `/v1/directories/${ directoryUuid } /elements/${ elementNameEncoded } /types/${ type } ` ;
492
+ console . debug ( existsElementUrl ) ;
493
+ return backendFetch ( existsElementUrl , { method : 'head' } ) . then (
494
+ ( response ) => {
495
+ return response . status !== 204 ; // HTTP 204 : No-content
496
+ }
497
+ ) ;
498
+ }
499
+
414
500
export function getNameCandidate ( directoryUuid , elementName , type ) {
415
501
const nameCandidateUrl =
416
502
PREFIX_DIRECTORY_SERVER_QUERIES +
@@ -632,7 +718,7 @@ export function connectNotificationsWsUpdateDirectories() {
632
718
'/notify?updateType=directories' ;
633
719
634
720
const reconnectingWebSocket = new ReconnectingWebSocket (
635
- ( ) => webSocketUrl + '&access_token=' + getUserToken ( )
721
+ ( ) => webSocketUrl + '&access_token=' + getToken ( )
636
722
) ;
637
723
reconnectingWebSocket . onopen = function ( ) {
638
724
console . info (
0 commit comments