Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Toolkit
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
digital blueprint
Web Component Framework
Toolkit
Commits
5826a7a3
Commit
5826a7a3
authored
5 years ago
by
Bekerle, Patrizio
Committed by
Reiter, Christoph
4 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Do JSONLD code refactoring
parent
7830e3ab
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
packages/common/jsonld.js
+74
-59
74 additions, 59 deletions
packages/common/jsonld.js
with
74 additions
and
59 deletions
packages/common/jsonld.js
+
74
−
59
View file @
5826a7a3
...
...
@@ -61,73 +61,88 @@ export default class JSONLD {
xhr
.
setRequestHeader
(
'
Authorization
'
,
'
Bearer
'
+
window
.
VPUAuthToken
);
xhr
.
onreadystatechange
=
function
()
{
if
(
xhr
.
readyState
===
4
)
{
if
(
xhr
.
status
===
200
)
{
const
json
=
JSON
.
parse
(
xhr
.
responseText
);
let
entryPoints
=
{};
for
(
let
property
in
json
)
{
// for some reason the properties start with a lower case character
if
(
!
property
.
startsWith
(
"
@
"
))
entryPoints
[
property
.
toLowerCase
()]
=
json
[
property
];
}
// read the link header of the api response
// const utils = require("./utils");
const
links
=
utils
.
parseLinkHeader
(
this
.
getResponseHeader
(
"
link
"
));
// get the hydra apiDocumentation url
const
apiDocUrl
=
links
[
"
http://www.w3.org/ns/hydra/core#apiDocumentation
"
];
if
(
apiDocUrl
!==
undefined
)
{
// load the hydra apiDocumentation
const
docXhr
=
new
XMLHttpRequest
();
docXhr
.
open
(
"
GET
"
,
apiDocUrl
,
true
);
docXhr
.
setRequestHeader
(
"
Content-Type
"
,
"
application/json
"
);
docXhr
.
onreadystatechange
=
function
()
{
if
(
docXhr
.
readyState
===
4
)
{
if
(
docXhr
.
status
===
200
)
{
const
json
=
JSON
.
parse
(
docXhr
.
responseText
);
const
supportedClasses
=
json
[
"
hydra:supportedClass
"
];
let
entities
=
{};
const
baseUrl
=
utils
.
parseBaseUrl
(
apiUrl
);
// gather the entities
supportedClasses
.
forEach
(
function
(
classData
)
{
// add entry point url
const
entityName
=
classData
[
"
hydra:title
"
];
let
entryPoint
=
entryPoints
[
entityName
.
toLowerCase
()];
if
(
entryPoint
!==
undefined
&&
!
entryPoint
.
startsWith
(
"
http
"
))
entryPoint
=
baseUrl
+
entryPoint
;
classData
[
"
@entryPoint
"
]
=
entryPoint
;
entities
[
entityName
]
=
classData
;
});
const
instance
=
new
JSONLD
(
baseUrl
,
entities
);
instances
[
apiUrl
]
=
instance
;
// return the initialized JSONLD object
for
(
const
fnc
of
successFunctions
[
apiUrl
])
if
(
typeof
fnc
==
'
function
'
)
fnc
(
instance
);
successFunctions
[
apiUrl
]
=
[];
}
else
{
JSONLD
.
executeFailureFunctions
(
apiUrl
,
i18n
.
t
(
'
jsonld.api-documentation-server
'
,
{
apiUrl
:
apiDocUrl
}));
}
}
};
docXhr
.
send
();
}
else
{
JSONLD
.
executeFailureFunctions
(
apiUrl
,
i18n
.
t
(
'
jsonld.error-hydra-documentation-url-not-set
'
,
{
apiUrl
:
apiUrl
}));
}
if
(
xhr
.
readyState
!==
4
)
{
return
;
}
if
(
xhr
.
status
===
200
)
{
const
json
=
JSON
.
parse
(
xhr
.
responseText
);
let
entryPoints
=
{};
for
(
let
property
in
json
)
{
// for some reason the properties start with a lower case character
if
(
!
property
.
startsWith
(
"
@
"
))
entryPoints
[
property
.
toLowerCase
()]
=
json
[
property
];
}
// read the link header of the api response
// const utils = require("./utils");
const
links
=
utils
.
parseLinkHeader
(
this
.
getResponseHeader
(
"
link
"
));
// get the hydra apiDocumentation url
const
apiDocUrl
=
links
[
"
http://www.w3.org/ns/hydra/core#apiDocumentation
"
];
if
(
apiDocUrl
!==
undefined
)
{
// load the hydra apiDocumentation
const
docXhr
=
new
XMLHttpRequest
();
docXhr
.
open
(
"
GET
"
,
apiDocUrl
,
true
);
docXhr
.
setRequestHeader
(
"
Content-Type
"
,
"
application/json
"
);
docXhr
.
onreadystatechange
=
function
()
{
if
(
docXhr
.
readyState
!==
4
)
{
return
;
}
if
(
docXhr
.
status
===
200
)
{
JSONLD
.
gatherEntities
(
docXhr
,
apiUrl
,
entryPoints
);
}
else
{
JSONLD
.
executeFailureFunctions
(
apiUrl
,
i18n
.
t
(
'
jsonld.api-documentation-server
'
,
{
apiUrl
:
apiDocUrl
}));
}
};
docXhr
.
send
();
}
else
{
JSONLD
.
executeFailureFunctions
(
apiUrl
,
i18n
.
t
(
'
jsonld.error-
api-server
'
,
{
apiUrl
:
apiUrl
}));
JSONLD
.
executeFailureFunctions
(
apiUrl
,
i18n
.
t
(
'
jsonld.error-
hydra-documentation-url-not-set
'
,
{
apiUrl
:
apiUrl
}));
}
}
else
{
JSONLD
.
executeFailureFunctions
(
apiUrl
,
i18n
.
t
(
'
jsonld.error-api-server
'
,
{
apiUrl
:
apiUrl
}));
}
};
xhr
.
send
();
}
/**
* Gather the entities
*
* @param docXhr
* @param apiUrl
* @param entryPoints
*/
static
gatherEntities
(
docXhr
,
apiUrl
,
entryPoints
)
{
const
json
=
JSON
.
parse
(
docXhr
.
responseText
);
const
supportedClasses
=
json
[
"
hydra:supportedClass
"
];
let
entities
=
{};
const
baseUrl
=
utils
.
parseBaseUrl
(
apiUrl
);
// gather the entities
supportedClasses
.
forEach
(
function
(
classData
)
{
// add entry point url
const
entityName
=
classData
[
"
hydra:title
"
];
let
entryPoint
=
entryPoints
[
entityName
.
toLowerCase
()];
if
(
entryPoint
!==
undefined
&&
!
entryPoint
.
startsWith
(
"
http
"
))
entryPoint
=
baseUrl
+
entryPoint
;
classData
[
"
@entryPoint
"
]
=
entryPoint
;
entities
[
entityName
]
=
classData
;
});
const
instance
=
new
JSONLD
(
baseUrl
,
entities
);
instances
[
apiUrl
]
=
instance
;
// return the initialized JSONLD object
for
(
const
fnc
of
successFunctions
[
apiUrl
])
if
(
typeof
fnc
==
'
function
'
)
fnc
(
instance
);
successFunctions
[
apiUrl
]
=
[];
}
/**
* Execute failure functions and send general notification
*
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment