File

src/api/content-data-extension.ts

Description

Handles the content data of a request

Index

Methods

Constructor

Public constructor(advancedLogic: AdvancedLogicTypes.IAdvancedLogic)
Parameters :
Name Type Optional Description
advancedLogic AdvancedLogicTypes.IAdvancedLogic No

Instance of the advanced logic layer

Methods

Public createExtensionsDataForCreation
createExtensionsDataForCreation(content: any)

Creates the extensions data for the creation of this extension

Parameters :
Name Type Optional Description
content any No

Content to link to the request

Returns : any

ExtensionsData ready to be added to the request

Public getContent
getContent(request: RequestLogicTypes.IRequest)

Gets the content from the extensions state

Parameters :
Name Type Optional Description
request RequestLogicTypes.IRequest No

The request of which we want the content

Returns : any

The content

import DataFormat from '@requestnetwork/data-format';
import {
  AdvancedLogicTypes,
  ExtensionTypes,
  RequestLogicTypes,
} from '@requestnetwork/types';

// Extension ID for this class: content data
const CONTENT_DATA_ID = ExtensionTypes.ID.CONTENT_DATA;

/**
 * Handles the content data of a request
 */
export default class ContentDataExtension {
  // Content data extension
  private extension: ExtensionTypes.ContentData.IContentData;

  /**
   * @param advancedLogic Instance of the advanced logic layer
   */
  public constructor(advancedLogic: AdvancedLogicTypes.IAdvancedLogic) {
    this.extension = advancedLogic.extensions.contentData;
  }

  /**
   * Creates the extensions data for the creation of this extension
   *
   * @param content Content to link to the request
   * @returns ExtensionsData ready to be added to the request
   */
  public createExtensionsDataForCreation(content: any): any {
    if (DataFormat.isKnownFormat(content)) {
      const { valid, errors } = DataFormat.validate(content);

      if (!valid) {
        const errorsToShow = errors.map((e: any) => JSON.stringify(e)).join('\n');
        throw new Error(
          `The content data seem to follow a request format but contains errors: ${errorsToShow}`,
        );
      }
    }

    return this.extension.createCreationAction({ content });
  }

  /**
   * Gets the content from the extensions state
   *
   * @param request The request of which we want the content
   * @returns The content
   */
  public getContent(request: RequestLogicTypes.IRequest): any {
    if (request.extensions[CONTENT_DATA_ID]) {
      return request.extensions[CONTENT_DATA_ID].values.content;
    }
    return null;
  }
}

result-matching ""

    No results matching ""