Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Share interfaces between API and frontend

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

like image 514
Peter Dub Avatar asked Mar 01 '19 09:03

Peter Dub


People also ask

Does API connect backend to frontend?

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.

Can backend and frontend run on same port?

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.

What would you associate an API with front end?

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.

Is it possible to share data between frontend and backend?

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…

Is a web service API frontend or backend?

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.

What is API (Application Programming Interface)?

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.

What is the difference between front-end and back-end in PHP?

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.


2 Answers

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:


I have my frontend and backend in one repository, but I guess you could have them separate, but you would still need to keep them next to each other somehow. The file structure would be something like this:
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.

Example

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.

like image 106
Jan Kytka Avatar answered Sep 20 '22 17:09

Jan Kytka


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.

like image 28
Chris Fremgen Avatar answered Sep 21 '22 17:09

Chris Fremgen