![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
@dotcms/client
Advanced tools
Official JavaScript library for interacting with DotCMS REST APIs.
@dotcms/client
The @dotcms/client
is a JavaScript/TypeScript library for interacting with a dotCMS instance. It allows you to easily fetch pages, content, and navigation information in JSON format, as well as to make complex queries on content collections.
This client library provides a streamlined, promise-based interface to fetch pages and navigation API.
To get started, install the client via npm or yarn:
npm install @dotcms/client
Or using Yarn:
yarn add @dotcms/client
@dotcms/client
supports both ES modules and CommonJS. You can import it using either syntax:
import { DotCmsClient } from '@dotcms/client';
const { DotCmsClient } = require('@dotcms/client');
First, initialize the client with your dotCMS instance details.
const client = DotCmsClient.init({
dotcmsUrl: 'https://your-dotcms-instance.com',
authToken: 'your-auth-token',
siteId: 'your-site-id'
});
You can retrieve the elements of any page in your dotCMS system in JSON format using the client.page.get()
method.
const pageData = await client.page.get({
path: '/your-page-path',
language_id: 1,
personaId: 'optional-persona-id'
});
Retrieve the dotCMS file and folder tree to get information about the navigation structure.
const navData = await client.nav.get({
path: '/',
depth: 2,
languageId: 1
});
The getCollection
method allows you to fetch a collection of content items (sometimes called "contentlets") using a builder pattern for complex queries.
Here’s a simple example to fetch content from a collection:
import { DotCmsClient } from '@dotcms/client';
const client = DotCmsClient.init({
dotcmsUrl: 'https://your-dotcms-instance.com',
authToken: 'your-auth-token'
});
const collectionResponse = await client.content
.getCollection('Blog') // Collection name
.limit(10) // Limit results to 10 items
.page(1) // Fetch the first page
.fetch(); // Execute the query
console.log(collectionResponse.contentlets);
You can sort the content by any field in ascending or descending order:
const sortedResponse = await client.content
.getCollection('Blog')
.sortBy([{ field: 'title', order: 'asc' }]) // Sort by title in ascending order
.fetch();
If you need to filter content by language, you can specify the language
parameter:
const languageFilteredResponse = await client.content
.getCollection('Blog')
.language(2) // Filter by language ID (e.g., 2)
.fetch();
You can build more complex queries using the query builder. For example, filter by author
and title
:
const complexQueryResponse = await client.content
.getCollection('Blog')
.query((qb) => qb.field('author').equals('John Doe').and().field('title').equals('Hello World'))
.fetch();
To only fetch draft content, use the draft()
method:
const draftContentResponse = await client.content
.getCollection('Blog')
.draft() // Fetch only drafts content
.fetch();
To fetch content with a specific relationship depth, use the depth()
method:
const depthResponse = await client.content
.getCollection('Blog')
.depth(2) // Fetch related content up to depth 2
.fetch();
You can combine multiple methods to build more complex queries. For example, limit results, sort them, and filter by author:
const combinedResponse = await client.content
.getCollection('Blog')
.limit(5)
.page(2)
.sortBy([{ field: 'title', order: 'asc' }])
.query((qb) => qb.field('author').equals('John Doe'))
.depth(1)
.fetch();
To handle errors gracefully, you can use a try-catch
block around your API calls. Here’s an example:
try {
const pageData = await client.page.get({
path: '/your-page-path',
languageId: 1
});
} catch (error) {
console.error('Failed to fetch page data:', error);
}
This ensures that any errors that occur during the fetch (e.g., network issues, invalid paths, etc.) are caught and logged properly.
When fetching large collections of content, pagination is key to managing the number of results returned:
const paginatedResponse = await client.content
.getCollection('Blog')
.limit(10) // Limit to 10 items per page
.page(2) // Get the second page of results
.fetch();
Detailed documentation of the @dotcms/client
methods, parameters, and types can be found below:
DotCmsClient.init(config: ClientConfig): DotCmsClient
Initializes the dotCMS client with the specified configuration.
DotCmsClient.page.get(options: PageApiOptions): Promise<unknown>
Retrieves the specified page's elements from your dotCMS system in JSON format.
DotCmsClient.nav.get(options: NavApiOptions): Promise<unknown>
Retrieves information about the dotCMS file and folder tree.
DotCmsClient.content.getCollection(contentType: string): CollectionBuilder<T>
Creates a builder to filter and fetches a collection of content items for a specific content type.
contentType
(string): The content type to retrieve.
CollectionBuilder<T>
: A builder instance for chaining filters and executing the query.
GitHub pull requests are the preferred method to contribute code to dotCMS. Before any pull requests can be accepted, an automated tool will ask you to agree to the dotCMS Contributor's Agreement.
dotCMS comes in multiple editions and as such is dual licensed. The dotCMS Community Edition is licensed under the GPL 3.0 and is freely available for download, customization and deployment for use within organizations of all stripes. dotCMS Enterprise Editions (EE) adds a number of enterprise features and is available via a supported, indemnified commercial license from dotCMS. For the differences between the editions, see the feature page.
If you need help or have any questions, please open an issue in the GitHub repository.
Always refer to the official dotCMS documentation for comprehensive guides and API references.
Source | Location |
---|---|
Installation | Installation |
Documentation | Documentation |
Videos | Helpful Videos |
Forums/Listserv | via Google Groups |
@dotCMS | |
Main Site | dotCMS.com |
FAQs
Official JavaScript library for interacting with DotCMS REST APIs.
The npm package @dotcms/client receives a total of 57 weekly downloads. As such, @dotcms/client popularity was classified as not popular.
We found that @dotcms/client demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.