UNPKG

@dotcms/client

Version:

Official JavaScript library for interacting with DotCMS REST APIs.

96 lines (95 loc) 3.32 kB
import { DotCMSClientConfig, DotCMSComposedPageResponse, DotCMSExtendedPageResponse, DotCMSPageResponse, DotCMSPageRequestParams, RequestOptions } from '@dotcms/types'; /** * Client for interacting with the DotCMS Page API. * Provides methods to retrieve and manipulate pages. */ export declare class PageClient { /** * Request options including authorization headers. * @private */ private requestOptions; /** * Site ID for page requests. * @private */ private siteId; /** * DotCMS URL for page requests. * @private */ private dotcmsUrl; /** * Creates a new PageClient instance. * * @param {DotCMSClientConfig} config - Configuration options for the DotCMS client * @param {RequestOptions} requestOptions - Options for fetch requests including authorization headers * @example * ```typescript * const pageClient = new PageClient( * { * dotcmsUrl: 'https://demo.dotcms.com', * authToken: 'your-auth-token', * siteId: 'demo.dotcms.com' * }, * { * headers: { * Authorization: 'Bearer your-auth-token' * } * } * ); * ``` */ constructor(config: DotCMSClientConfig, requestOptions: RequestOptions); /** * Retrieves a page from DotCMS using GraphQL. * * @param {string} url - The URL of the page to retrieve * @param {DotCMSPageRequestParams} [options] - Options for the request * @template T - The type of the page and content, defaults to DotCMSBasicPage and Record<string, unknown> | unknown * @returns {Promise<DotCMSComposedPageResponse<T>>} A Promise that resolves to the page data * * @example Using GraphQL * ```typescript * const page = await pageClient.get<{ page: MyPageWithBanners; content: { blogPosts: { blogTitle: string } } }>( * '/index', * { * languageId: '1', * mode: 'LIVE', * graphql: { * page: ` * containers { * containerContentlets { * contentlets { * ... on Banner { * ...bannerFragment * } * } * } * `, * content: { * blogPosts: ` * BlogCollection(limit: 3) { * ...blogFragment * } * `, * }, * fragments: [ * ` * fragment bannerFragment on Banner { * caption * } * `, * ` * fragment blogFragment on Blog { * title * urlTitle * } * ` * ] * } * }); * ``` */ get<T extends DotCMSExtendedPageResponse = DotCMSPageResponse>(url: string, options?: DotCMSPageRequestParams): Promise<DotCMSComposedPageResponse<T>>; }