I need help to create a fake service

Hi everyone,

How can I create a fake service to return a message that depends on the phone passed in the url ?

messages = [
{ phone: ‘11113333’, message: ‘hello’ },
{ phone: ‘22223333’, message: ‘bye’ },
{ phone: ‘33334444’, message: ‘good-bye’ },

my url: localhost:3333/page1/33334444

I have two pages: page1 and page2. I can’t put the message on API, so I need to create a fake api service to return me the message related to each phone to show on page2.

My API is sending the id(phone) by url, I just need to know how to send the message related to each phone.

As I understand you want to share a message between two pages as you said page 1 to page 2 if yes than you have to create a service and define static variable in that service then assign value it in one page and get value from it in page 2,

Yes, but each message has to be shown by the phone number. Each phone number has a different message. I have to click in a message and open the page 2 with the right message. It’s like an api, like if I had phone and message in an object. But in this case, the message must be a separated service that is not coming from api. I have to use a fake one. If it was coming from api, it was ok to me, but in this case… I dont know how to do it.

There are a bunch of ways to approach this, and here’s one.

I prefer to program backwards from consumers to producers, because I find that when I write producers first, I put all sorts of stupid stuff in them that I never use and leave out super-important stuff that I have to add later.

So what does our consumer want? As I understand it, it wants to give us a phone number and get a message back:

class MessageService {
  messagesForTelno(telno: string): Observable<string> {
    // TODO

Now, given that we have the JSON object you posted, and for the sake of simplicity we’ll just hardcode it into our service:

interface Message {
  phone: string;
  message: string;
private upstream: Message[] = [
{ phone: ‘11113333’, message: ‘hello’ },
{ phone: ‘22223333’, message: ‘bye’ },
{ phone: ‘33334444’, message: ‘good-bye’ },

That’s not very convenient for messagesForTelno, because it would like to do something like this:

messagesForTelno(telno: string): Observable<string> {
  return of(this.messages[telno]);

So now we’ve reduced our problem to how to get what we have (upstream) into what we want (messages):

private messages: { [telno: string]: string };
constructor() {
  this.messages = {};
  this.upstream.forEach(msg => {
    this.messages[msg.phone] = msg.message;

Now you can inject MessageService into each of your pages and call messagesForTelno() with a phone number, and you will receive an Observable<string> of messages intended for that phone number. Currently as written, that’s only a single message, but this design would extend naturally to providing a stream of messages instead.

1 Like

Ok, thank you! I’m going to try to do this.

Is this ‘private upstream’ into interface file?

Is this interface in a separated file?

Because I can’t use ‘upstream’ into a separated file.

No, since it’s private it would have to be a member of the MessageService class in order for that class’s constructor to be able to access it. Of course, there’s not any compelling reason to make it private, aside from the general PoLP design principle.