File

projects/wms-framework/src/lib/wcfserviceinvocationsupport/WcfMessage.ts

Description

Concrete implemenation of a Message object that uses json as messaging format

Extends

Message

Index

Properties
Methods

Constructor

constructor()

Properties

Public BaseUrl
Type : string
Inherited from Message
Defined in Message:36

The service base url. This message uses the base url to create the final url using message information such as end point suffic, post or get or method name

Public EndpointSuffix
Type : string
Default value : ''
Inherited from Message
Defined in Message:44

Specifies and addition suffix to be appended to the base url

Headers
Type : SimpleList<MessageHeader>
Default value : new SimpleList<MessageHeader>()
Inherited from Message
Defined in Message:61

List of Headers

methodName
Type : string
Default value : null
Inherited from Message
Defined in Message:69

Name of the method to call

requestArgs
Type : any[]
Default value : []
Inherited from Message
Defined in Message:77

Request arguments

responseType
Type : string
Default value : 'json'
Inherited from Message
Defined in Message:93

The expected response type from the request.

result
Type : any
Inherited from Message
Defined in Message:85

Service result

Public UsePost
Type : boolean
Default value : true
Inherited from Message
Defined in Message:53

Indicates whether this message must be sent using a POST called instead of the default GET method.

Methods

Public buildBody
buildBody()
Inherited from Message
Defined in Message:47
Returns : any
Public buildPostResult
buildPostResult(userData: any, result: any)
Inherited from Message
Defined in Message:58
Parameters :
Name Type Optional
userData any No
result any No
Returns : IAsyncResult
Public buildUrl
buildUrl()
Inherited from Message
Defined in Message:38
Returns : string
Private createArguments
createArguments(requestArgs: any[], argsObject: any)

Creates the arguments for the request to the service

Parameters :
Name Type Optional Description
requestArgs any[] No

The request arguments

argsObject any No

The object that will store the processed arguments

Returns : void
Private createParametersUrl
createParametersUrl()

Creates a url with all parameters from this message.

Returns : string

{string}

Private getPropertyValueToUse
getPropertyValueToUse(originObject: any)
Parameters :
Name Type Optional
originObject any No
Returns : unknown
Private serializeIntoTargetObject
serializeIntoTargetObject(sourceObj: any, targetObj: any)
Parameters :
Name Type Optional
sourceObj any No
targetObj any No
Returns : void
import { iuSelect, iuToArray } from '../baseframework/collections';
import { ReflectionHelper } from '../baseframework/ReflectionSupport';
import {
  getSpecialJsonMappedValueIfExists,
  requiresSpecialTypeConversion,
} from '../baseframework/SerializationHelper';
import { IAsyncResult } from './AsyncResultSupport';
import { Message } from './Message';

/**
 *  Concrete implemenation of a Message object that uses json as messaging format
 *
 * @export
 * @class WcfMessage
 * @wType System.ServiceModel.Channels.Message
 */
export class WcfMessage extends Message {
  constructor() {
    super();
    this.EndpointSuffix = '/json';
  }

  public buildUrl(): string {
    let actualArguments = this.UsePost ? '' : this.createParametersUrl();
    let args: string = '';
    if (!this.UsePost) {
      args = `?${actualArguments}`;
    }
    return `${this.BaseUrl}${this.EndpointSuffix}/${this.methodName}${args}`;
  }

  public buildBody(): any {
    if (this.UsePost) {
      const argsObject: any = {};
      this.createArguments(this.requestArgs, argsObject);
      return argsObject;
    } else {
      return '';
    }
  }

  // inherited
  public buildPostResult(userData: any, result: any): IAsyncResult {
    return {
      AsyncState: {
        message: this,
        d: typeof result?.d !== 'undefined' ? result.d : result,
      },
      IsCompleted: true,
    };
  }

  /**
   *  Creates the arguments for the request to the service
   *
   * @param {any[]} requestArgs The request arguments
   * @param {any} argsObject The object that will store the processed arguments
   * @memberof HttpChannel
   */
  private createArguments(requestArgs: any[], argsObject: any): void {
    let i = 0;
    for (const arg of requestArgs) {
      if (typeof arg.name !== 'undefined' && typeof arg.value !== 'undefined') {
        let valueToUse: any = null;
        if (!requiresSpecialTypeConversion(arg.value, argsObject, arg.name)) {
          valueToUse = this.getPropertyValueToUse(arg.value);
          argsObject[arg.name] = valueToUse;
        }
      } else {
        argsObject[`p${i}`] = this.getPropertyValueToUse(arg.value);
      }
      i++;
    }
  }

  /**
   * Creates a url with all parameters from this message.
   *
   * @private
   * @return {*}  {string}
   * @memberof WcfMessage
   */
  private createParametersUrl(): string {
    return this.requestArgs
      .map((arg) =>
        typeof arg.name !== 'undefined' && typeof arg.value !== 'undefined'
          ? arg.name + '=' + arg.value
          : 'p=' + arg.toString()
      )
      .join('&');
  }

  private getPropertyValueToUse(originObject: any): unknown {
    let valueToUse: any = null;
    if (
      originObject &&
      typeof originObject === 'object' &&
      originObject[Symbol.iterator]
    ) {
      valueToUse = iuToArray(
        iuSelect((obj) => this.getPropertyValueToUse(obj), originObject)
      );
    } else if (typeof originObject === 'object' && originObject?.constructor) {
      const specialValue = getSpecialJsonMappedValueIfExists(originObject);
      if (typeof specialValue !== 'undefined') {
        valueToUse = specialValue;
      } else {
        const objectToUseInRequest: any = {};
        this.serializeIntoTargetObject(originObject, objectToUseInRequest);
        valueToUse = objectToUseInRequest;
      }
    } else {
      valueToUse = originObject;
    }
    return valueToUse;
  }

  private serializeIntoTargetObject(sourceObj: any, targetObj: any): void {
    const typeInfo = ReflectionHelper.getTypeInfo(sourceObj.constructor);
    for (const prop of typeInfo.getProperties()) {
      if (
        !requiresSpecialTypeConversion(
          sourceObj[prop.Name],
          targetObj,
          prop.Name
        )
      ) {
        const valueForProperty = this.getPropertyValueToUse(
          sourceObj[prop.Name]
        );
        targetObj[prop.Name] = valueForProperty;
      }
    }
  }
}

result-matching ""

    No results matching ""