Skip to content

Commit

Permalink
feat(sdk-client): getCollection method for content (#28635)
Browse files Browse the repository at this point in the history
## Proposed Changes
* Add `Content` Class to handle Content API Methods and expose it as a
method of the Dotcms Client
* Add `GetCollection` Class to handle the building of a query for
collections and expose it as a method of `Content` Class

## Usage example for standalone `getCollection`

This an example of a query with all possible methods it supports (could
not be a real use case and it should be used from the `client.content`).

```typescript
        const requestOptions: Omit<RequestInit, 'body' | 'method'> = {
            cache: 'no-cache' // To simulate a valid request
        };

        const serverUrl = 'http://localhost:8080';

        const contentType = 'forceSensitive';
        const client = new GetCollection(requestOptions, serverUrl, contentType);

        const response = await client
            .language(13) // Language Id
            .render(true) // To retrieve the content with the render
            .sortBy([
                // Sort by multiple fields
                {
                    field: 'name',
                    order: 'asc'
                },
                {
                    field: 'midichlorians',
                    order: 'desc'
                }
            ])
            .depth(2) // Depth of the content for relationships
            .limit(20) // Limit of content per page
            .page(3) // Page to fetch
            .query(
                (
                    qb // Lucene query to append to the main query for more complex queries
                ) =>
                    qb
                        .field('kyberCrystal')
                        .equals('red')
                        .and()
                        .equals('blue')
                        .field('master')
                        .equals('Yoda')
                        .or()
                        .equals('Obi-Wan')
            )
            .draft(true) // To retrieve the draft content
            .variant('legends-forceSensitive') // Variant of the content
            .rawQuery('+modDate:2024-05-28 +conhost:MyCoolSite') // Raw query to append to the main query
            .fetch(); // Fetch the content
```
  • Loading branch information
zJaaal authored and spbolton committed Jun 3, 2024
1 parent 4ea2d78 commit 0218f3c
Show file tree
Hide file tree
Showing 15 changed files with 788 additions and 13 deletions.
30 changes: 30 additions & 0 deletions core-web/libs/sdk/client/src/lib/client/content/content-api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { GetCollection } from './methods/get-collection/get-collection';

import { ClientOptions } from '../sdk-js-client';

/**
* Content classs exposes the content api methods
*
* @export
* @class Content
*/
export class Content {
private requestOptions: ClientOptions;
private serverUrl;

constructor(requestOptions: ClientOptions, serverUrl: string) {
this.requestOptions = requestOptions;
this.serverUrl = serverUrl;
}

/**
* Allows you to build a query to get a collection of an specified content type
*
* @param {string} contentType
* @return {*}
* @memberof Content
*/
getCollection(contentType: string): GetCollection {
return new GetCollection(this.requestOptions, this.serverUrl, contentType);
}
}
Loading

0 comments on commit 0218f3c

Please sign in to comment.