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 { lastValueFrom, Observable } from 'rxjs';
import { PfeUserInputState } from '../../models/pfe-state/user-input-state.model';
import { PfeModuleConfigurationService } from '../pfe-module-configuration/pfe-module-configuration.service';
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 {
  private pfeModuleConfigurationService = inject(PfeModuleConfigurationService);

  constructor(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.pfeModuleConfigurationService.pfeModuleConfig().stateServiceEndpoint) {
      return Promise.reject(undefined);
    }
    return lastValueFrom(
      this.http.post<StateServiceStateIDResponse>(
        this.pfeModuleConfigurationService.pfeModuleConfig().stateServiceEndpoint as string,
        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.pfeModuleConfigurationService.pfeModuleConfig().stateServiceEndpoint) {
      return Promise.reject(undefined);
    }
    return this.http
      .put<void>(this.pfeModuleConfigurationService.pfeModuleConfig().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.pfeModuleConfigurationService.pfeModuleConfig().stateServiceEndpoint + `/${stateID}`);
  }
}

results matching ""

    No results matching ""