File

src/lib/fs/caas/caas-client.ts

Description

This class handles all calls to the CaaS instance.

Index

Properties
Methods

Constructor

constructor(caasCollection: CaasAccessData, httpClient: HttpClient)

Creates an instance of a CaasClient.

Parameters :
Name Type Optional Description
caasCollection CaasAccessData No

The information about the CaaS collection to use.

httpClient HttpClient No

The HTTP client instance to use.

Properties

Private collectionUrl
Type : URL

Methods

getByIds
getByIds(ids: string[])

This method gets CaaS pages by the '_id' attribute, NOT by the 'identifier' attribute.

Parameters :
Name Type Optional Description
ids string[] No

The IDs to get the pages for.

Returns : Observable<any>

The requested page.

getByUid
getByUid(uid: string, language: string, altName?: string)

This method gets a CaaS page by its UID.

Parameters :
Name Type Optional Description
uid string No

The UID of the page to get.

language string No

The language to get the page for.

altName string Yes

The alternative name to search for.

Returns : Observable<any>

The requested page.

getPageSections
getPageSections(identifier: string, language: string)

This method gets the sections included in the given page.

Parameters :
Name Type Optional Description
identifier string No

The identifier of the page.

language string No

The language to get the sections for.

Returns : Observable<any>

The sections of the requested page.

import { HttpClient } from '@angular/common/http';
import { EMPTY, Observable } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { CaasAccessData } from './caas-access-data';

/**
 * This class handles all calls to the CaaS instance.
 */
export class CaasClient {
  private collectionUrl: URL;

  /**
   * Creates an instance of a CaasClient.
   *
   * @param {CaasAccessData} caasCollection The information about the CaaS collection to use.
   * @param {HttpClient} httpClient The HTTP client instance to use.
   * @memberof CaasClient
   */
  constructor(private caasCollection: CaasAccessData, private httpClient: HttpClient) {
    this.collectionUrl = caasCollection.collectionUrl();
  }

  /**
   * This method gets a CaaS page by its UID.
   *
   * @param {string} uid The UID of the page to get.
   * @param {string} language The language to get the page for.
   * @param {string} [altName] The alternative name to search for.
   * @return {Observable<any>} The requested page.
   * @memberof CaasClient
   */
  getByUid(uid: string, language: string, altName?: string): Observable<any> {
    altName = typeof altName === 'string' && altName.trim().length > 0 ? altName : undefined;
    return this.httpClient
      .get(this.collectionUrl.href, {
        headers: {
          Authorization: `Bearer ${this.caasCollection.apiKey}`,
        },
        params: {
          filter: `{'$or':[
            {'$and':[{'$or':[{'uid':'${uid}'},{'uid':'${uid?.toLocaleLowerCase()}'}]},{'locale.language':'${language}'}]},
            {'page.formData.pt_seoUrl.value':'${altName || uid}'},
            {'page.formData.pt_seoUrl.value':'${altName || uid?.toLocaleLowerCase()}'}
          ]}`,
        },
      })
      .pipe(
        catchError((err) => {
          if (err.status === 404) {
            return EMPTY;
          }
        })
      );
  }

  /**
   * This method gets the sections included in the given page.
   *
   * @param {string} identifier The identifier of the page.
   * @param {string} language The language to get the sections for.
   * @return {Observable<any>} The sections of the requested page.
   * @memberof CaasClient
   */
  getPageSections(identifier: string, language: string): Observable<any> {
    return this.httpClient
      .get(this.collectionUrl.href, {
        headers: {
          Authorization: `Bearer ${this.caasCollection.apiKey}`,
        },
        params: {
          filter: `{'$and':[{'page.identifier':'${identifier}'}, {'locale.language':'${language}'}]}`,
          rep: 's',
          keys: `{'page.children.children':1}`,
        },
      })
      .pipe(
        catchError((err) => {
          if (err.status === 404) {
            return EMPTY;
          }
        })
      );
  }

  /**
   * This method gets CaaS pages by the '_id' attribute, NOT by the 'identifier' attribute.
   *
   * @param {string[]} ids The IDs to get the pages for.
   * @return {Observable<any>} The requested page.
   * @memberof CaasClient
   */
  getByIds(ids: string[]): Observable<any> {
    return this.httpClient
      .get(this.collectionUrl.href, {
        headers: {
          Authorization: `Bearer ${this.caasCollection.apiKey}`,
        },
        params: {
          filter: `{'_id':{'$in':[${ids?.join(',')}]}}`,
        },
      })
      .pipe(
        catchError((err) => {
          if (err.status === 404) {
            return EMPTY;
          }
        })
      );
  }
}

results matching ""

    No results matching ""