@dotcms/client
Version:
Official JavaScript library for interacting with DotCMS REST APIs.
227 lines (226 loc) • 8.11 kB
TypeScript
import { ClientOptions } from '../../../sdk-js-client';
import { GetCollectionResponse, BuildQuery, SortBy, GetCollectionError, OnFullfilled, OnRejected } from '../../shared/types';
/**
* Creates a Builder to filter and fetch content from the content API for a specific content type.
*
* @export
* @class CollectionBuilder
* @template T Represents the type of the content type to fetch. Defaults to unknown.
*/
export declare class CollectionBuilder<T = unknown> {
#private;
/**
* Creates an instance of CollectionBuilder.
* @param {ClientOptions} requestOptions Options for the client request.
* @param {string} serverUrl The server URL.
* @param {string} contentType The content type to fetch.
* @memberof CollectionBuilder
*/
constructor(requestOptions: ClientOptions, serverUrl: string, contentType: string);
/**
* Returns the sort query in the format: field order, field order, ...
*
* @readonly
* @private
* @memberof CollectionBuilder
*/
private get sort();
/**
* Returns the offset for pagination.
*
* @readonly
* @private
* @memberof CollectionBuilder
*/
private get offset();
/**
* Returns the full URL for the content API.
*
* @readonly
* @private
* @memberof CollectionBuilder
*/
private get url();
/**
* Returns the current query built.
*
* @readonly
* @private
* @memberof CollectionBuilder
*/
private get currentQuery();
/**
* Filters the content by the specified language ID.
*
* @example
* ```typescript
* const client = new DotCMSClient(config);
* const collectionBuilder = client.content.getCollection("Blog");
* collectionBuilder.language(1);
* ```
*
* @param {number | string} languageId The language ID to filter the content by.
* @return {CollectionBuilder} A CollectionBuilder instance.
* @memberof CollectionBuilder
*/
language(languageId: number | string): this;
/**
* Setting this to true will server side render (using velocity) any widgets that are returned by the content query.
*
* More information here: {@link https://www.dotcms.com/docs/latest/content-api-retrieval-and-querying#ParamsOptional}
*
* @return {CollectionBuilder} A CollectionBuilder instance.
* @memberof CollectionBuilder
*/
render(): this;
/**
* Sorts the content by the specified fields and orders.
*
* @example
* ```typescript
* const client = new DotCMSClient(config);
* const collectionBuilder = client.content.getCollection("Blog");
* const sortBy = [{ field: 'title', order: 'asc' }, { field: 'modDate', order: 'desc' }];
* collectionBuilder("Blog").sortBy(sortBy);
* ```
*
* @param {SortBy[]} sortBy Array of constraints to sort the content by.
* @return {CollectionBuilder} A CollectionBuilder instance.
* @memberof CollectionBuilder
*/
sortBy(sortBy: SortBy[]): this;
/**
* Sets the maximum amount of content to fetch.
*
* @param {number} limit The maximum amount of content to fetch.
* @return {CollectionBuilder} A CollectionBuilder instance.
* @memberof CollectionBuilder
*/
limit(limit: number): this;
/**
* Sets the page number to fetch.
*
* @param {number} page The page number to fetch.
* @return {CollectionBuilder} A CollectionBuilder instance.
* @memberof CollectionBuilder
*/
page(page: number): this;
/**
* Filters the content by a Lucene query string.
*
* @param {string} query A Lucene query string.
* @return {CollectionBuilder} A CollectionBuilder instance.
* @memberof CollectionBuilder
*/
query(query: string): this;
/**
* Filters the content by building a query using a QueryBuilder function.
*
* @example
* ```typescript
* const client = new DotCMSClient(config);
* const collectionBuilder = client.content.getCollection("Blog");
* collectionBuilder.query((queryBuilder) =>
* queryBuilder.field('title').equals('Hello World').or().equals('Hello World 2')
* );
* ```
*
* @param {BuildQuery} buildQuery A function that receives a QueryBuilder instance and returns a valid query.
* @return {CollectionBuilder} A CollectionBuilder instance.
* @memberof CollectionBuilder
*/
query(buildQuery: BuildQuery): this;
/**
* Retrieves draft content.
* @example
* ```ts
* const client = new DotCMSClient(config);
* const collectionBuilder = client.content.getCollection("Blog");
* collectionBuilder
* .draft() // This will retrieve draft/working content
* .then((response) => // Your code here })
* .catch((error) => // Your code here })
* ```
*
* @return {CollectionBuilder} A CollectionBuilder instance.
* @memberof CollectionBuilder
*/
draft(): this;
/**
* Filters the content by a variant ID for [Experiments](https://www.dotcms.com/docs/latest/experiments-and-a-b-testing)
*
* More information here: {@link https://www.dotcms.com/docs/latest/content-api-retrieval-and-querying#ParamsOptional}
*
* @example
* ```ts
* const client = new DotCMSClient(config);
* const collectionBuilder = client.content.getCollection("Blog");
* collectionBuilder
* .variant("YOUR_VARIANT_ID")
* .then((response) => // Your code here })
* .catch((error) => // Your code here })
* ```
*
* @param {string} variantId A string that represents a variant ID.
* @return {CollectionBuilder} A CollectionBuilder instance.
* @memberof CollectionBuilder
*/
variant(variantId: string): this;
/**
* Sets the depth of the relationships of the content.
* Specifies the depth of related content to return in the results.
*
* More information here: {@link https://www.dotcms.com/docs/latest/content-api-retrieval-and-querying#ParamsOptional}
*
* @example
* ```ts
* const client = new DotCMSClient(config);
* const collectionBuilder = client.content.getCollection("Blog");
* collectionBuilder
* .depth(1)
* .then((response) => // Your code here })
* .catch((error) => // Your code here })
* ```
*
* @param {number} depth The depth of the relationships of the content.
* @return {CollectionBuilder} A CollectionBuilder instance.
* @memberof CollectionBuilder
*/
depth(depth: number): this;
/**
* Executes the fetch and returns a promise that resolves to the content or rejects with an error.
*
* @example
* ```ts
* const client = new DotCMSClient(config);
* const collectionBuilder = client.content.getCollection("Blog");
* collectionBuilder
* .limit(10)
* .then((response) => // Your code here })
* .catch((error) => // Your code here })
* ```
*
* @param {OnFullfilled} [onfulfilled] A callback that is called when the fetch is successful.
* @param {OnRejected} [onrejected] A callback that is called when the fetch fails.
* @return {Promise<GetCollectionResponse<T> | GetCollectionError>} A promise that resolves to the content or rejects with an error.
* @memberof CollectionBuilder
*/
then(onfulfilled?: OnFullfilled<T>, onrejected?: OnRejected): Promise<GetCollectionResponse<T> | GetCollectionError>;
/**
* Formats the response to the desired format.
*
* @private
* @param {GetCollectionRawResponse<T>} data The raw response data.
* @return {GetCollectionResponse<T>} The formatted response.
* @memberof CollectionBuilder
*/
private formatResponse;
/**
* Calls the content API to fetch the content.
*
* @private
* @return {Promise<Response>} The fetch response.
* @memberof CollectionBuilder
*/
private fetch;
}