Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
synergy-components
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
community
synergy-components
Commits
d2dcc2ce
Commit
d2dcc2ce
authored
Apr 24, 2026
by
Samir Sadyhov
🤔
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
new ApiClient
parent
bd122c8d
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
157 additions
and
2 deletions
+157
-2
constructor/library/ApiClient.js
constructor/library/ApiClient.js
+155
-0
constructor/scripts/openDocument/openDocument.js
constructor/scripts/openDocument/openDocument.js
+2
-2
No files found.
constructor/library/ApiClient.js
0 → 100644
View file @
d2dcc2ce
this
.
ApiClient
=
class
{
constructor
()
{
this
.
_baseURL
=
`
${
window
.
location
.
origin
}
/Synergy`
;
this
.
_authHeader
=
this
.
_buildAuthHeader
();
this
.
_requestInterceptors
=
[];
this
.
_responseInterceptors
=
[];
}
async
request
({
url
,
method
=
'
GET
'
,
body
,
headers
=
{}
})
{
let
finalHeaders
=
this
.
_normalizeHeaders
({
authorization
:
this
.
_authHeader
,
...
headers
,
});
let
finalBody
=
body
;
const
contentType
=
this
.
_getContentType
(
finalHeaders
);
if
(
body
!=
null
)
{
// FormData
if
(
body
instanceof
FormData
)
{
delete
finalHeaders
[
'
content-type
'
];
}
// JSON
else
if
(
contentType
===
'
application/json
'
)
{
finalBody
=
JSON
.
stringify
(
body
);
}
// x-www-form-urlencoded
else
if
(
contentType
===
'
application/x-www-form-urlencoded
'
)
{
finalBody
=
new
URLSearchParams
(
body
).
toString
();
}
// авто JSON
else
if
(
!
contentType
&&
this
.
_isJsonLike
(
body
))
{
finalHeaders
[
'
content-type
'
]
=
'
application/json
'
;
finalBody
=
JSON
.
stringify
(
body
);
}
}
let
config
=
{
url
:
`
${
this
.
_baseURL
}
/
${
url
}
`
,
method
,
headers
:
finalHeaders
,
body
:
[
'
GET
'
,
'
HEAD
'
].
includes
(
method
)
?
undefined
:
finalBody
,
};
// request interceptors
for
(
const
interceptor
of
this
.
_requestInterceptors
)
{
config
=
(
await
interceptor
(
config
))
||
config
;
}
let
response
;
try
{
response
=
await
fetch
(
config
.
url
,
config
);
}
catch
(
err
)
{
console
.
error
({
config
,
msg
:
err
});
throw
this
.
_handleNetworkError
(
err
);
}
const
parsedData
=
await
this
.
_parseResponse
(
response
);
let
result
=
{
ok
:
response
.
ok
,
status
:
response
.
status
,
headers
:
response
.
headers
,
data
:
parsedData
,
};
// response interceptors
for
(
const
interceptor
of
this
.
_responseInterceptors
)
{
result
=
(
await
interceptor
(
result
))
||
result
;
}
if
(
!
response
.
ok
)
{
console
.
error
({
config
,
msg
:
result
});
throw
this
.
_handleHttpError
(
result
);
}
return
result
.
data
;
}
addRequestInterceptor
(
fn
)
{
this
.
_requestInterceptors
.
push
(
fn
);
}
addResponseInterceptor
(
fn
)
{
this
.
_responseInterceptors
.
push
(
fn
);
}
_buildAuthHeader
()
{
const
{
login
,
password
,
token
}
=
AS
.
OPTIONS
;
if
(
token
)
{
return
`Bearer
${
token
}
`
;
}
else
{
const
base64
=
btoa
(
new
TextEncoder
().
encode
(
`
${
login
}
:
${
password
}
`
)
.
reduce
((
str
,
byte
)
=>
str
+
String
.
fromCharCode
(
byte
),
''
)
);
return
`Basic
${
base64
}
`
;
}
}
_normalizeHeaders
(
headers
=
{})
{
const
result
=
{};
for
(
const
key
in
headers
)
{
result
[
key
.
toLowerCase
()]
=
headers
[
key
];
}
return
result
;
}
_getContentType
(
headers
=
{})
{
const
ct
=
headers
[
'
content-type
'
];
if
(
!
ct
)
return
null
;
return
ct
.
split
(
'
;
'
)[
0
].
trim
().
toLowerCase
();
}
_isJsonLike
(
body
)
{
return
(
typeof
body
===
'
object
'
&&
!
(
body
instanceof
FormData
)
&&
!
(
body
instanceof
Blob
)
&&
!
(
body
instanceof
ArrayBuffer
)
);
}
async
_parseResponse
(
response
)
{
const
contentType
=
response
.
headers
.
get
(
'
content-type
'
)
||
''
;
try
{
if
(
contentType
.
includes
(
'
application/json
'
))
{
return
await
response
.
json
();
}
if
(
contentType
.
includes
(
'
text/
'
))
{
return
await
response
.
text
();
}
return
await
response
.
blob
();
}
catch
{
return
null
;
}
}
_handleNetworkError
(
err
)
{
return
new
Error
(
`Network error:
${
err
.
message
}
`
);
}
_handleHttpError
(
res
)
{
return
new
Error
(
`HTTP error:
${
res
.
status
}
-
${
JSON
.
stringify
(
res
.
data
)}
`
);
}
}
\ No newline at end of file
constructor/scripts/openDocument/openDocument.js
View file @
d2dcc2ce
...
...
@@ -40,13 +40,13 @@ const getRegistryRoute = async registryID => {
return
new
Promise
(
resolve
=>
{
rest
.
synergyGet
(
`api/registry/route?registryID=
${
registryID
}
&type=ACTIVATE&locale=
${
AS
.
OPTIONS
.
locale
}
`
,
res
=>
{
if
(
res
.
hasOwnProperty
(
'
errorCode
'
))
{
console
.
log
(
`ERROR [ getRegistryRoute ]
:
${
JSON
.
stringify
(
res
)}
`
);
console
.
log
(
`ERROR [ getRegistryRoute ]
`
,
res
);
resolve
(
null
);
}
else
{
resolve
(
res
);
}
},
err
=>
{
console
.
log
(
`ERROR [ getRegistryRoute ]
:
${
JSON
.
stringify
(
err
)}
`
);
console
.
log
(
`ERROR [ getRegistryRoute ]
`
,
err
);
resolve
(
null
);
});
});
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment