File

libs/ngx-pfe/services/pfe-rest-service/rest.service.ts

Description

The PfeRestService is the central service for all backend communication. All backend calls should be issued only from this service.

Index

Methods

Constructor

constructor(ngxPfeModuleConfiguration: NgxPfeModuleConfiguration, http: HttpClient)
Parameters :
Name Type Optional
ngxPfeModuleConfiguration NgxPfeModuleConfiguration No
http HttpClient No

Methods

Async createClientState
createClientState(state: PfeUserInputState)

Stores the client state in the backend.

Parameters :
Name Type Optional Description
state PfeUserInputState No

The current state.

Observable to react to the result of the call.

getClientState
getClientState(stateID: StateID)

Retrieves a client state from the backend.

Parameters :
Name Type Optional Description
stateID StateID No

The ID of the state.

Observable to react to the result of the call.

Async updateClientState
updateClientState(stateID: StateID, state: PfeUserInputState)

Stores the client state in the backend.

Parameters :
Name Type Optional Description
stateID StateID No

The ID that is used by the backend as a key.

state PfeUserInputState No

The current state.

Returns : Promise<void>

Observable to react to the result of the call.

import { HttpClient } from '@angular/common/http';
import { Inject, Injectable } from '@angular/core';
import { Observable, lastValueFrom } from 'rxjs';
import { NgxPfeModuleConfiguration, NGX_PFE_CONFIGURATION } from '../../models/ngx-pfe-module-configuration.model';
import { PfeUserInputState } from '../../models/pfe-state/user-input-state.model';
import { StateID } from '../pfe-state-service/state.service';

export interface StateServiceStateIDResponse {
  id: string;
}

/**
 * The PfeRestService is the central service for all backend communication.
 * All backend calls should be issued only from this service.
 *
 * @export
 */
@Injectable()
export class PfeRestService {
  constructor(
    @Inject(NGX_PFE_CONFIGURATION) private ngxPfeModuleConfiguration: NgxPfeModuleConfiguration,
    private http: HttpClient
  ) {}

  /**
   * Stores the client state in the backend.
   *
   * @param stateID The ID that is used by the backend as a key.
   * @param state The current state.
   * @returns Observable to react to the result of the call.
   */
  async createClientState(state: PfeUserInputState): Promise<StateServiceStateIDResponse> {
    if (!this.ngxPfeModuleConfiguration.stateServiceEndpoint) {
      return Promise.reject(undefined);
    }
    return lastValueFrom(this.http.post<StateServiceStateIDResponse>(this.ngxPfeModuleConfiguration.stateServiceEndpoint, state));
  }

  /**
   * Stores the client state in the backend.
   *
   * @param stateID The ID that is used by the backend as a key.
   * @param state The current state.
   * @returns Observable to react to the result of the call.
   */
  async updateClientState(stateID: StateID, state: PfeUserInputState): Promise<void> {
    if (!this.ngxPfeModuleConfiguration.stateServiceEndpoint) {
      return Promise.reject(undefined);
    }
    return this.http.put<void>(this.ngxPfeModuleConfiguration.stateServiceEndpoint + `/${stateID}`, state).toPromise();
  }

  /**
   * Retrieves a client state from the backend.
   *
   * @param stateID The ID of the state.
   * @returns Observable to react to the result of the call.
   */
  getClientState(stateID: StateID): Observable<PfeUserInputState> {
    return this.http.get<PfeUserInputState>(this.ngxPfeModuleConfiguration.stateServiceEndpoint + `/${stateID}`);
  }
}

results matching ""

    No results matching ""