Check out a free preview of the full Rapid Application Development with Code Generation course

The "Mock API Generation" Lesson is part of the full, Rapid Application Development with Code Generation course featured in this preview video. Here's what you'd learn in this lesson:

Lukas demonstrates mock API generation functionality using faker.js to generate fake data and moment.js to generate dates.


Transcript from the "Mock API Generation" Lesson

>> And I have one more example that I will leave you with because I believe this is the future. So in example nine, I want to show you, a bit of functionality for generating a mock API. And from here, the difference is I'm using Faker and I'm using Moment so Faker generates fake data moment generate dates.

And in this case I want to generate an API that has users. So the first thing to do when you're coming from a horizontal approach or a domain driven design approach is to understand the model so here I have an interface for user. Now what I have done is I have created an array that has the props in it.

And you'll see why in just a moment and I've built out a few very small functions. Random int, get random date and this is starting to look familiar. I have a generators function or object rather, that has a key that maps to the properties on this interface. And I have an object attached to that with the appropriate faker function to generate that data.

So faker, if you don't know, there is a number of functions that exist on within the faker library that you can say, I wanna fake first name, last name, email words whatever. And so now I have a generate method Hopefully you're starting to see this repetition I mean convention we're reducing over this and I am calling, The, I'm passing in the prop, which is, this right here.

And I'm getting the generator. From here, I'm calling generator function and passing in generator.args. Again, this is just a slow variation of what we've done. Now, what I do want to call out here and this is something that I did not know until I endeavored to build this.

The, if somebody would have asked me well, is there ever a case that we need to use the classic fortlib. Prior to this, I would have said, well, generally not. But the only place that I can think of that you would need a classic for loop is if you needed to iterate for a specific number of iterations.

So if I needed to loop over something or create a loop for let's say 50 times, then I was left doing length equals 50, i equals zero increment i, i is less than length. And by poking around, I found this little bit of code here, this gem that basically creates new array, so array.apply.

And so, what this does is essentially I'm calling a function. And I'm basically saying create a new array to whatever length it is. And then map over it. And then from here generate call the generator and passing the user props and the generators. And so somebody asked like, am I going to talk about call or apply?

In this case, here we have it. I've played my hand this alone in my opinion was worth the price of admission that when I found this out I thought this was incredibly cool. Is I'm basically saying I want to create an empty array of this length and then I'm going to iterate over it.

So what's neat about this as well is that you'll notice that the generate function Is, very generic, then the function that calls it is specific. And so, in this case I could, call or I could create a generate, courses or any kind of a domain specific function that then defers to this underlying function of generate.

And so this was intentionally vague and then this is a little bit more specific. Now, what you could do is well from here. Imagine that we have another object that has title and possibly like progress. Because I have this segmented out and it's not specifically to the user object itself that I've essentially created a registry or a HashMap to be able to generate these specific properties.

But what I could do is for instance, and I'll just make something up, let's just go interface and let's do this book, and we're going to go id: string and we're going to go title, also string And we'll go notes, also a string. And then from here I'm just going to call out the props.

So const_bookProps equals, and I'm doing this so when I iterate like I'm using this as the key to look up the generator in the generators object. And so I'm certain, there's probably another way to do this, but, For efficiency's sake, I'm just doing this. Now, what I can do is I can create a generatebooks method that will just go here like this, and we're gonna go mean let's just say 10 books, which is not nearly enough and we'll go 10,000.

And from here length equals getRandomInt min, max and then we're going to go const books. equals array.apply null, Array length map. And from here generate And will do bookProps and generators And return books. And I did something this does not like const get over there. There we go generate books and from here, what I can do is I'll just copy this out, And, generate books.

And let's see what this looks like. All right, mock API users and under here books. And so It's not it wouldn't take much to turn around and drop this into like a serverless function into a lambda function. And then all of a sudden now, you actually have a remote API that you can hit.

And so what's interesting again, the extension of this is that just as much as I borrowed from immutable infrastructure, that there's absolutely nothing that's keeping us from extending this into generating. Infrastructures code by that, I mean, typically CloudFormation templates, TerraForm templates, Serverless templates etc. And I think the Holy Grail, in my opinion, would be able to you where you would define this in let's say stack Blitz.

And then you could press a button and not only would it build your code, but it would deploy it into the cloud on your behalf. And to me, that is where things or at least mentally, where I think somebody who could do that would have a huge competitive advantage.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now