I am developing both sides. An API is in nest.js and frontend in Angular. Both sides uses typescript and I am facing problem of sharing interfaces, that should be same. For example ILoginRequest and ILoginResponse. I want to have both projects in separate GIT Repositories. Should I use GIT submodule with 3rd shared GIT repo or somehow create shared npm package or is there some good tool to generate classes automatically (from swagger definition) into frontend or anything else?
EDIT: to generate code for client from swagger, look at openapi-generator
the API is the agreed way that the front-end and back-end will work together. This is structurally implied with matching code, and explicitly planned with documentation. the web-service ultimately is about data storage and retrieval in the backend database.
You'd want to somehow combine both servers, so they can interact with each other. But each can only run on its own port. You can't start both on the same one and if they are apart, it's impossible for the frontend code to access the backend endpoints.
While a Backend API authenticates with a secret key, a Frontend API authenticates with the current user's session. Because of this, Frontend API endpoints should only read data and execute actions if the current user has permission.
We highly recommend sharing types between your frontend and backend using a component. It’s much easier, and it ensures nothing in your code will break even as APIs and code are changing. You’re welcome :) Your backend API has been updated to return data of a new type. The frontend team must be informed to update…
In terms of frontend and backend, this web service API (and its implementation) is the backend. Some parts of it may be publically accessible and others only to your frontend. A different name for this is "service layer", i.e. code that represents services which the frontend calls.
API is an acronym that stands for Application Programming Interface and according to wikipedia, an application programming interface (API) is a computing interface which defines interactions between multiple software intermediaries.
Front-End: The PHP server that the DOM is pointed to, containing both the individual page requested and some AJAX style XML or JSON access points. Back-end: A database server, where MySQL runs. For a properly designed program, each of these componentshas a private API to communicate with the others.
NOTE: I have recently stumbled upon typeorm-entitysharer which "can be used to create client/server classes based on the same TypeORM entities, allowing you to share them." It gives you more control over what you want to share, you can take a look at their example. I didn't opt to use it because it's kind of overkill for me I guess. However, this is how I structured my last project:
workspace
├─backend <- repo #1
│ ├─src
│ │ ├─shared <- shared code goes here
│ │ └─proxy.ts
│ └─tsconfig.json
└─frontend <- repo #2
├─src
│ └─proxy.ts
└─tsconfig.json
Then in backend/tsconfig.json
you introduce
{
...
"paths": {
"@shared/*": [ "src/shared/*" ],
}
}
and in frontend/tsconfig.json
you introduce
{
...
"paths": {
"@shared/*": [ "../backend/src/shared/*" ],
// if you're using TypeORM, this package has dummy decorators
"typeorm": [ "node_modules/typeorm/typeorm-model-shim.js" ]
// you can do the same for other packages, point them to dummy paths
// altirnatively you can route the shared imports through the proxy.ts
// and replace them in frontend/src/proxy.ts with dummy ones
}
}
also don't forget to npm i typeorm
in your frontend.
Let's say I have this in backend/src/shared/user.entity.ts
import { PrimaryGeneratedColumn, Column } from 'typeorm';
export class UserEntity
{
@PrimaryGeneratedColumn() id: number;
@Column() name: string;
@Column() password: string;
}
Now, I can use it anywhere like this:
import { UserEntity } from '@shared/model/user.entity';
In backend, it's obvious, and in frontend, @shared/model/user.entity
gets mapped to ../backend/src/shared/model/user.entity
, and the import from 'typeorm'
inside the entity gets mapped to dummy package.
After wasting a full day trying to get this working, I came across NX/NRWL.
It's basically just a CLI with tools to help structure your app properly. I was able to get it working properly in about an hour. Shared interfaces in a mono-repo is the way to go.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With