angular / in-memory-web-api

The code for this project has moved to the angular/angular repo. This repo is now archived.
MIT License
1.18k stars 232 forks source link

Manually trigger in-memory-web-api #128

Closed winnemucca closed 7 years ago

winnemucca commented 7 years ago

I am seeing that it is very straightforward to enable it is very straight forward to enable/disable in-memory-web-api according to your documentation. I have been playing with scenarios in which the tester can enable/disable certain mock services from a dropdown modal.

I have not been able to subscribe to the service from my component.

export class InMemPatientService implements InMemoryDbService {
  createDb() {
     const patients = [
        {
            id: 1,
            patient: "Adewumi, Anice (56, F)",
            facility: "Brighton",
            datesValid: "07/07/17 - 07/07/18",
            typeD: "Tx Hemo (CHG)",
            order: "Frequency 3 times per week; Treatment Time 4 hours 10 minutes per week (250)"
        },
        {
            id:2,
            patient: "Bowman, Jackson (56, M)",
            facility: "Brighton",
            datesValid: "06/07/17 - 07/07/18",
            typeD: "Tx Hemo (CHG)",
            order: "Frequency 3 times per week; Treatment Time 4 hours 10 minutes per week (250)"
        }
     ];
     return {patients};
  }

}

I was hoping the subscription would be as simple as injecting the service into the component and doing

 constructor(private modalService: NgbModal,
          private memPatientService: InMemPatientService) {}

 runMockServiceCheck() {
     if (!this.mockServiceCheck) {
     console.log("false");
  } else {
    this.memPatientService
     .createDb()
       .subscribe(patients => {
        this.patients = patients;
      }, 
      error => this.errorMessage = <any>error);
  }
}

However, I receive an error

Property 'subscribe' does not exist on type '{ patients: ({ id: number; patient: string; facility: string; datesValid: string; typeD: string; ...'.

This is definitely a stack overflow type question, but I am curious if this is even possible to achieve with the library?

Additionally, I changed my return statement to

return Observable.of({patients}), but this doesn't seems to lose the lose the data in the subscribe. 
wardbell commented 7 years ago

It's not designed for dynamic configuration.

In PR #130 you will be able to reset your in-mem database via POST commands/resetDb (see a test for that). Combine that capability with the passThruUnknownUrl: true option during initial configuration and I think you can get what you want.