Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

[NestJS] I made backend server simulator without backend server

Posted on Jun 9 I made NestJS Backend Server simulator in my library nestia.You can build the backend server simulator by only one line command. nestia will analyze your server codes, and generate imitation codes validating and composing mock-up data. It can be used in frontend application, without real backend server interaction.Furthermore, nestia supports much more convenient and powerful features like below. With those features, you can get high productivity even gaining both high performance and easy development.p.s) Currently, frontend level backend server simulator is only possible, when the backend server be implemented by NestJS (+nestia). However, it would be possible in every backend server frameworks, soon.In nowadays, I've developed a NestJS backend server simulator in my library nestia.For reference, the simulator does not connect to remote backend server. It just imitates request data validation and response data construction, by itself. Therefore, frontend developers can start application development only with API interfaces, even if the backend server is not ready.Below is a piece of SDK (Software Development Kit) code, generated by nestia, supporting the simulation mode. AS you can see, the simulator be activated just by configuring IConnection.random value to be true. Otherwise, the SDK would connect to the remote backend server.Within framework of backend developers, they also do not need to compose mock-up data, either. Mock-up data composition would be automated by the simulator, and backend developers may focus on the business logic like API interface designs and main program developments.Like below example code, just define only API interfaces. Then, builds simulator and delivers to frontend developers. Then you backend developer can implement application at the same time with frontend developers in parallel.For referecen, nestia also can generate e2e test functions automatically. Therefore, if you've succeeded to determine API specs, you can concentrate only on the main program development.In traditional software development, frontend developers recognize backend server API spec by reading Swagger Documents or similar one like RestDocs. By the way, as frontend developers are not robots but humans, reading and re-writing API specs in frontend application is a very annoying and error-prone work.nestia also can generate Swagger Documents, and it is much evolved than traditional NestJS. However, I recommend NestJS backend developers to utilize SDK library much more, for frontend developersIn nestia case, it can generate SDK (Software Development Kit) library for frontend developers. As you can see from above example code, the SDK library is very simple and easy to use. Only import and function call statements are required. The SDK library will make frontend development much safer through type hints. NestJS backend server simulator also provided by the SDK library.By the way, how nestia generates the SDK library? The secret is on your source code. nestia reads your NestJS backend server code directly (especially controllers), and analyzes which API routes are provided, and which DTO types are being used.After these analyses, nestia writes fetch functios for each API routes, and import statemtns that are used in each controller classes. The SDK library is generated by such source code analyses and combininng fetch functions and import statements.The NestJS backend server simulator, it is just a little extension of such SDK library.By the way, in the previous #Summary section, I'd told that nestia is much easier than traditional NestJS. I'd told that when defining DTO schema, nestia needs only pure TypeScript type, but traditinal NestJS needs triple times duplicated definitions.Looking at below example DTO schema definitions, you may understand what I am saying.Looking at above example code, someone may ask me:"Hey Samchon, how to validate the IBbsArticle.IStore type?If a client requests with invalid data, is it possible to reject? As you know, TypeScript interface does not have any schema info in the runtime, and it is the reason why traditional NestJS has enforced developers to define triple times duplicated DTO schemas."However, my answer is "It is enougly possible". Also, even in this case, the secret is on your source code.When you compile your NestJS backend servere, nestia will analyze your source codes, and analyzes how DTO schemas are being composed. Reading your DTO schemas and traveling properties of them, nestia will generate validation and JSON serialization codes for each API routes.Looking at below compiled code, it is dedicatedly optimized for IBbsArticle.IStore type. It's the secret of nestia, which does not require triple times duplicated DTO schema definitions, but requires only pure TypeScript type. Also, such compile time optimization through source code analyzing is called, "AOT (Ahead of Time) compilation".For reference, such AOT compilation optimization is much light and faster than general logics accessing to each properties through for (const key in obj) statements like traditional NestJS. Measuring benchmark, it's 25,000 times faster.About this secret (static access vs dynamic access), I'll write another article later. This is related to v8 engine optimization, and I can sure that it would be much interesting than any other stories.If you've read my article carefully, you may understand how to simulate response data, by yourself. Yes, the response data also be composed by AOT compilation skill. Let's read the simulator (SDK) code again, then you can find that typia.random() function be used. The typia.random() is the last secret of nestia generated simulator. It generates random response data by analyzing the response DTO type.When you compile the SDK library, such script would be written in the update.random() function, through AOT compilation. The parameter g?: Partial is a random seeder, but it is not neccessary.In current article, I've introduced NestJS backend server simulator through SDK library. However, as you know, frontend level backend server simulation is only possible when the backend server be developed with NestJS (+nestia).By the way, I'm developing a new library @nestia/migrate, which can convert swagger.json file to a NestJS project. It's still in development, but most reached to the goal. After the @nestia/migrate library be released, you can simulate any backend server, even it's not developed with NestJS.Look forward to it, application development would be much easier.Templates let you quickly answer FAQs or store snippets for re-use. Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink. Hide child comments as well Confirm For further actions, you may consider blocking this person and/or reporting abuse Andrew Bone - May 23 Chetan - May 22 Megan Grant - May 12 Jonathan Reimer - May 30 Once suspended, samchon will not be able to comment or publish posts until their suspension is removed. Once unsuspended, samchon will be able to comment and publish posts again. Once unpublished, all posts by samchon will become hidden and only accessible to themselves. If samchon is not suspended, they can still re-publish their posts from their dashboard. Note: Once unpublished, this post will become invisible to the public and only accessible to Jeongho Nam. They can still re-publish the post if they are not suspended. Thanks for keeping DEV Community safe. Here is what you can do to flag samchon: samchon consistently posts content that violates DEV Community's code of conduct because it is harassing, offensive or spammy. Unflagging samchon will restore default visibility to their posts. DEV Community — A constructive and inclusive social network for software developers. With you every step of your journey. Built on Forem — the open source software that powers DEV and other inclusive communities.Made with love and Ruby on Rails. DEV Community © 2016 - 2023. We're a place where coders share, stay up-to-date and grow their careers.



This post first appeared on VedVyas Articles, please read the originial post: here

Share the post

[NestJS] I made backend server simulator without backend server

×

Subscribe to Vedvyas Articles

Get updates delivered right to your inbox!

Thank you for your subscription

×