A JavaScript library for consuming OSLC 3.0 servers. Uses axios for HTTP, rdflib for RDF parsing, and supports Basic, JEE Form, and JAS Bearer token authentication. Works in both Node.js and browser environments.
npm install oslc-clientOr, from a workspace monorepo, add a file dependency in your package.json:
{
"dependencies": {
"oslc-client": "file:../oslc-client"
}
}import OSLCClient from 'oslc-client';
const client = new OSLCClient('user', 'password');
// Connect to a server and select a service provider
await client.use('https://example.com/ccm', 'My Project', 'CM');
// Fetch a resource by URL
const resource = await client.getResource('https://example.com/ccm/resource/1');
console.log(resource.getTitle());
console.log(resource.getIdentifier());
console.log(resource.getDescription());
// Read any RDF property
const modified = resource.get('http://purl.org/dc/terms/modified');
// Update and save
resource.setTitle('Updated title');
await client.putResource(resource, resource.etag);
// Delete
await client.deleteResource(resource);import OSLCClient from 'oslc-client';Creates a client instance. An axios HTTP client is configured internally with cookie jar support (Node.js) or withCredentials (browser). If configurationContext is provided, a Configuration-Context header is sent with every request.
Connects to an OSLC server, reads its rootservices document, discovers the ServiceProviderCatalog for the given domain ('CM', 'RM', or 'QM'; defaults to 'CM'), and selects the named service provider. Must be called before createResource, queryResources, or query.
Fetches an OSLC resource and returns an OSLCResource. The response body is parsed into an rdflib graph. Defaults to OSLC version '2.0' and Accept: application/rdf+xml.
For non-RDF content types (text/xml, application/xml), returns { etag, xml }. For Atom feeds, returns { etag, feed }.
Fetches an OSLC Compact (UI Preview) resource. Returns a Compact object with getTitle() and getShortTitle() accessors. Defaults to Accept: application/x-oslc-compact+xml.
Serializes the resource's RDF graph to application/rdf+xml and PUTs it back to the server. Pass the resource's etag to enable optimistic concurrency via If-Match.
Creates a new resource using the creation factory discovered from the current service provider. resourceType is the OSLC resource type URI. Returns the newly created OSLCResource (fetched from the Location header).
Deletes the resource at its URI.
Queries for resources of the given type and returns an array of OSLCResource objects. The query object supports prefix, select, where, and orderBy properties corresponding to the OSLC query parameters. Handles paged results automatically.
Like queryResources, but returns the raw rdflib graph (store) instead of individual OSLCResource instances.
The underlying axios instance. Use this for direct HTTP requests when the higher-level API is insufficient.
import OSLCResource from 'oslc-client/OSLCResource.js';An RDF resource backed by an rdflib IndexedFormula (graph store). All OSLC properties are accessible through the generic get/set methods, with convenience accessors for common Dublin Core and OSLC Core properties.
Creates a resource. When called with no arguments, creates a blank node with an empty graph.
Returns the resource URI as a string.
The rdflib IndexedFormula containing the resource's RDF statements.
The HTTP ETag from the server response, used for optimistic concurrency on updates.
Returns the value(s) of an RDF property. property can be a URI string or an rdflib NamedNode. Returns undefined if not present, a single string value if one triple matches, or an array of strings if multiple triples match.
Sets an RDF property, replacing all existing values. Pass undefined to remove the property. Pass an array to set multiple values.
Get or set dcterms:title.
Get or set dcterms:description.
Get dcterms:identifier.
Get oslc:shortTitle.
Returns a plain object mapping predicate URIs to their values (string or array of strings) for all statements about this resource.
Returns a Set of predicate URIs where the object is a NamedNode (i.e., outgoing links).
Returns an array of { sourceURL, linkType, targetURL } objects for outgoing links. Optionally filter by a Set or Array of link type URIs.
The client handles authentication transparently via an axios response interceptor. Three mechanisms are supported, tried in order when a server issues a challenge:
- JEE Form authentication -- triggered by the
x-com-ibm-team-repository-web-auth-msg: authrequiredheader. The client POSTs credentials toj_security_check. - JAS Bearer token -- triggered by a
WWW-Authenticate: jauth realmheader containing atoken_uri. The client obtains a bearer token and retries the request. - HTTP Basic authentication -- fallback for any
401response. The client retries with anAuthorization: Basicheader.
No additional configuration is needed; provide your credentials to the constructor and authentication is handled automatically.
All OSLC resources are parsed into rdflib IndexedFormula graphs. The client negotiates RDF content types with the server, preferring application/rdf+xml but also accepting Turtle and JSON-LD.
Properties are accessed via rdflib NamedNode symbols or URI strings. The OSLCResource.get() and set() methods abstract the underlying triple store, but you can also work directly with resource.store for advanced queries using rdflib's API.
- Jim Amsden (IBM)
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.