Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Implementing AppAsap’s tRPC-REST Integration

Ibrahim Amin – Tech Hive


August 2023

1 Abstract 3 Rules
This section sets a guideline to follow so that we know
This document describes a stage one implementation what is the scope of the side effects that this imple-
of integrating generic RESTful API generation func- mentation will have on the existing code base. Rules
tionality into the already existing tRPC API genera- include the following:
tion, this is part of sprint 6 with an estimated time
span of (7/23 - 8/5), this should describe the approach
taken to achieve that. I will talk about the rules that 3.1 Isolation
I’ve set for the implementation design, then describe
the steps needed to finish that implementation. I will The REST generation implementation should and will
also try to constructively criticize the implementation be isolated from the tRPC implementation, so if there
by mainly talking about the disadvantages and the exist any breaking changes after the merge, we most
things that can possibly go bad using this implemen- likely know that this happens on the REST generation
tation and finally talk in general about operations we and not on generating a tRPC API, because integrat-
can perform on the output API like automatic CI/CD, ing REST didn’t introduce any breaking changes to
deployment options, scalability, etc... the stable tRPC generation process.

3.2 Mutual Exclusiveness


The client should be able to set a flag using the UI so
2 Introduction that the backend generator either generates a tRPC
API or plain REST API but not both at least for now.
AppAsap is currently able to generate an application If we followed the first rule of implementation isolation,
backend including an admin dashboard to interact this won’t be hard to ensure.
with that backend, this backend API is implemented
using tRPC routes, and both the dashboard and 3.3 Reusability
the API are generated in a single monorepo that
uses turborepo from Vercel. We aim to provide two The output RESTful API will be using the same ser-
variations, one for the API, and the other one for the vice and validation that the output tRPC API uses,
admin dashboard, also we should be able to choose it should not introduce new service or validation lay-
which implementation we want to generate (REST ers but try to adapt the current implementation to be
or tRPC). The API should have a RESTful variation compatible with it.
generated in a monorepo with the admin dashboard
variation that is compatible with that RESTful API
and is able to communicate with it over HTTP out 4 Steps
of the box. The implementation priority is for the
RESTful API and not the admin dashboard because This section will give an overview of the steps taken
the dashboard needs an API to call. to achieve an integration that follows the rules I set
above.

1
4.2.1 Controller Template
I will provide an example output of the controller tem-
plate for a single resource and you should understand
the pattern, on the left is an example output for the
user resource, it uses the createCRUDRouter()
helper function, helpers are a topic on themselves and
I will talk about them later in the document, but the
main idea is. any output backend (the API part) will
be divided into two folders entities and helpers, en-
tities contain generated code and each entity should
be divided into three components controller, service,
and validator, but helpers doesn’t contain generated
code, these are (at least in development environment)
injected at generation-time, also as a note, a REST
Figure 1: Controller Template Example Output API helpers won’t be the same as the helpers from a
tRPC API but might reuse some of it.

4.1 Templates 4.2.2 Service Template

Referencing the pickle repository, we should try to The service template is simple, this is an example of
adapt the templates implemented there and move it the output code related to a user resources:
to the appasap-beta codebase, the template we are
interested in the most is the template containing the
swagger jsdocs, we might also transfer the template
containing the code for the entry point to the server
(the code initializing the express server) if it doesn’t
need many changes, otherwise, we can rewrite it to fit
our needs. We are not interested in the Prisma schema
template because it’s not relevant to us anymore, it
was just adding a datasource and a generator defini-
tions to the Prisma schema, but we already have these
definitions in the input Prisma schema when dealing
with the appasap-beta code base.
Figure 2: Service Template Example Output

4.2 Adapting Templates In contrast, the service template uses another helper
called crudService(), which automatically provides
After transferring the relevant templates we will most business logic for Create-Read-Update-Delete to all
likely need to change things including but not limited entities generated, it can also be extended by adding
to the following: custom business logic
• Naming.
4.2.3 Validation
• Import paths inside the templates. This document won’t go over templates or code related
to validation, or how to adapt the current validation
• Optionally add meaningful JSDocs comments. layer implementation to fit the RESTful API, this is
done for simplicity and because I need more research
This will make these templates usable inside the on this and need to study more possible scenarios be-
appasap-beta code base. fore I am able to settle on the details.

2
4.3 Helpers 5 Standard
At this point, we have templates capable of generat- This section talks about assumptions in the implemen-
ing an extendable CRUD controller and an extend- tation, the front-end client should take these assump-
able CRUD service/business logic that the controller tions into consideration for a working predictable re-
will use, each entity will get its own (controller, ser- quest life-cycle.
vice) pair. But looking at the templates’ code above,
we see that the implementation is abstracted and pro-
vided via the helper functions, namely, the create- 5.1
CRUDRouter() for the controller implementation
and crudService() for the service implementation. It 6 Improvements
is not important to show the code of these functions,
In this section, I will talk about the things that I don’t
it is important to understand the idea that the entities
necessarily like about this implementation and that
will use helpers to implement most of their function-
have gaps for improvement.
ality and that entities are generated but helpers will
most likely be pre-written and injected. These are the
current helpers for tRPC APIs: 6.1 Extension
We should be improving the way we extend the output
• auth
controller and service, I would prefer a more structured
• cache way of handling that and not to leave things very flex-
ible, at this point, we write customRoutes in the same
• configs file as the controller entity which isn’t very scalable,
• cryptography so this probably needs to change.

• database 6.2 Usage


• deploy A CLI tool to scaffold boilerplate code for the con-
troller, service, or validator would be nice so that it
• error
supports the first idea of having a structure and mak-
• generic-crud ing the coder follow it and also save him time.

• mail
6.3 More
• monitoring
This is currently the end of the document, I will add
• payments more as I go deeper through the integration process.

• server
• types

If we can adapt a variation of all of these helpers to


work with a REST-based API the job would be done
(some of them aren’t implemented yet even for tRPC
APIs). At the time of writing this, some helpers are
already adapted to fit REST usage (generic-crud
excluding validation, and database Prisma helpers).

You might also like