File

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

Index

Properties

Properties

id
id: string
Type : string
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 ""