Skip to content
This repository was archived by the owner on Apr 26, 2022. It is now read-only.

feat(proto): add proto-interface #3

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/api/src/app/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ ts_library(
"//apps/api/src/auth",
"//apps/api/src/cats",
"//apps/api/src/user",
"//libs/api-interface/src",
"//libs/proto-interface/src",
"@npm//@nestjs/common",
"@npm//@nestjs/passport",
"@npm//@nestjs/swagger",
Expand Down
4 changes: 2 additions & 2 deletions apps/api/src/app/app.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Controller, Get, UseGuards } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { ApiBearerAuth } from '@nestjs/swagger';

import { Message } from '@api-interface';
import { IMessage } from '@proto-interface';

import { AppService } from './app.service';

Expand All @@ -13,7 +13,7 @@ export class AppController {

@Get('hello')
@UseGuards(AuthGuard('jwt'))
getData(): Message {
getData(): Required<IMessage> {
return this.appService.getData();
}
}
4 changes: 2 additions & 2 deletions apps/api/src/app/app.service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Message } from '@api-interface';
import { Injectable } from '@nestjs/common';
import { IMessage } from '@proto-interface';

@Injectable()
export class AppService {
getData(): Message {
getData(): Required<IMessage> {
return { message: 'Welcome to api!' };
}
}
2 changes: 1 addition & 1 deletion apps/api/src/auth/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ ts_library(
module_name = "@api/auth",
deps = [
"//apps/api/src/user",
"//libs/api-interface/src",
"//libs/proto-interface/src",
"@npm//@nestjs/common",
"@npm//@nestjs/jwt",
"@npm//@nestjs/passport",
Expand Down
4 changes: 2 additions & 2 deletions apps/api/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SignedUser } from '@api-interface';
import { Injectable } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import { ISignedUser } from '@proto-interface';
import { UserService } from '../user/user.service';
import { JwtPayload } from './interfaces/jwt-payload.interface';

Expand All @@ -13,7 +13,7 @@ export class AuthService {
private readonly jwtService: JwtService,
) {}

async createToken(username: string): Promise<SignedUser> {
async createToken(username: string): Promise<Required<ISignedUser>> {
const { firstName, lastName } = await this.userService.getUserByUsername(username);
const payload: JwtPayload = { username, firstName, lastName };

Expand Down
2 changes: 1 addition & 1 deletion apps/api/src/cats/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ ts_library(
),
module_name = "@api/cats",
deps = [
"//libs/api-interface/src",
"//libs/proto-interface/src",
"@npm//@nestjs/common",
"@npm//@nestjs/passport",
"@npm//@nestjs/swagger",
Expand Down
4 changes: 2 additions & 2 deletions apps/api/src/cats/dto/create-cat.dto.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ICreateCatDto } from '@api-interface';
import { ApiModelProperty } from '@nestjs/swagger';
import { ICreateCatDto } from '@proto-interface';
import { IsInt, IsNotEmpty, Max, Min } from 'class-validator';

export class CreateCatDto implements ICreateCatDto {
export class CreateCatDto implements Required<ICreateCatDto> {
@ApiModelProperty()
@IsNotEmpty()
name: string;
Expand Down
4 changes: 2 additions & 2 deletions apps/api/src/cats/entities/cat.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { CatModel } from '@api-interface';
import { ICat } from '@proto-interface';
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';

@Entity()
export class Cat implements CatModel {
export class Cat implements Required<ICat> {
@PrimaryGeneratedColumn()
id: number;

Expand Down
2 changes: 1 addition & 1 deletion apps/api/src/user/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ ts_library(
),
module_name = "@api/user",
deps = [
"//libs/api-interface/src",
"//libs/proto-interface/src",
"@npm//@nestjs/common",
"@npm//@nestjs/passport",
"@npm//@nestjs/swagger",
Expand Down
4 changes: 2 additions & 2 deletions apps/api/src/user/dto/sign-in.dto.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ISignInDto } from '@api-interface';
import { ApiModelProperty } from '@nestjs/swagger';
import { ISignInDto } from '@proto-interface';
import { IsNotEmpty, IsString } from 'class-validator';

export class SignInDto implements ISignInDto {
export class SignInDto implements Required<ISignInDto> {
@ApiModelProperty()
@IsString()
@IsNotEmpty()
Expand Down
4 changes: 2 additions & 2 deletions apps/api/src/user/dto/sign-up.dto.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ISignUpDto } from '@api-interface';
import { ApiModelProperty } from '@nestjs/swagger';
import { ISignUpDto } from '@proto-interface';
import { IsNotEmpty, IsString } from 'class-validator';

export class SignUpDto implements ISignUpDto {
export class SignUpDto implements Required<ISignUpDto> {
@ApiModelProperty()
@IsString()
@IsNotEmpty()
Expand Down
4 changes: 2 additions & 2 deletions apps/api/src/user/entities/user.entity.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { UserModel } from '@api-interface';
import { IUser } from '@proto-interface';
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';

@Entity()
export class User implements UserModel {
export class User implements Required<IUser> {
@PrimaryGeneratedColumn()
id: number;

Expand Down
4 changes: 2 additions & 2 deletions apps/lazy-loading-app/src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Component } from '@angular/core';
import { Router } from '@angular/router';
import { SignedUser } from '@api-interface';
import { ISignedUser } from '@proto-interface';

import { AuthenticationService } from './auth/authentication.service';

@Component({ selector: 'lazy-root', templateUrl: 'app.component.html' })
export class AppComponent {
currentUser: SignedUser;
currentUser: Required<ISignedUser>;

constructor(
private router: Router,
Expand Down
2 changes: 1 addition & 1 deletion apps/lazy-loading-app/src/app/auth/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ ng_module(
),
tsconfig = "//:tsconfig.json",
deps = [
"//libs/api-interface/src",
"//libs/proto-interface/src",
"@npm//@angular/common",
"@npm//@angular/core",
"@npm//@angular/router",
Expand Down
16 changes: 9 additions & 7 deletions apps/lazy-loading-app/src/app/auth/authentication.service.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { ISignUpDto, SignedUser } from '@api-interface';
import { ISignedUser, ISignUpDto } from '@proto-interface';
import { BehaviorSubject, Observable } from 'rxjs';
import { map } from 'rxjs/operators';

@Injectable()
export class AuthenticationService {
private currentUserSubject: BehaviorSubject<SignedUser>;
public currentUser: Observable<SignedUser>;
private currentUserSubject: BehaviorSubject<Required<ISignedUser>>;
public currentUser: Observable<Required<ISignedUser>>;
private apiUrl = 'http://localhost:3000';

constructor(private http: HttpClient) {
this.currentUserSubject = new BehaviorSubject<SignedUser>(JSON.parse(localStorage.getItem('currentUser')));
this.currentUserSubject = new BehaviorSubject<Required<ISignedUser>>(
JSON.parse(localStorage.getItem('currentUser')),
);
this.currentUser = this.currentUserSubject.asObservable();
}

public get currentUserValue(): SignedUser {
public get currentUserValue(): Required<ISignedUser> {
return this.currentUserSubject.value;
}

login(username: string, password: string) {
return this.http.post<SignedUser>(`${this.apiUrl}/auth/sign-in`, { username, password })
return this.http.post<Required<ISignedUser>>(`${this.apiUrl}/auth/sign-in`, { username, password })
.pipe(map((user) => {
// login successful if there's a jwt token in the response
if (user && user.token) {
Expand All @@ -33,7 +35,7 @@ export class AuthenticationService {
}));
}

register(user: ISignUpDto) {
register(user: Required<ISignUpDto>) {
return this.http.post(`${this.apiUrl}/auth/sign-up`, user);
}

Expand Down
2 changes: 1 addition & 1 deletion apps/lazy-loading-app/src/app/home/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ ng_module(
deps = [
"//apps/lazy-loading-app/src/app/alert",
"//apps/lazy-loading-app/src/app/auth",
"//libs/api-interface/src",
"//libs/proto-interface/src",
"@npm//@angular/common",
"@npm//@angular/core",
"@npm//@angular/forms",
Expand Down
18 changes: 9 additions & 9 deletions apps/lazy-loading-app/src/app/home/cats.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { CatModel, ICreateCatDto } from '@api-interface';
import { ICat, ICreateCatDto } from '@proto-interface';
import { Observable } from 'rxjs';

@Injectable()
Expand All @@ -9,19 +9,19 @@ export class CatsService {

constructor(private http: HttpClient) {}

getAll(): Observable<CatModel[]> {
return this.http.get<CatModel[]>(`${this.apiUrl}/cats`);
getAll(): Observable<Array<Required<ICat>>> {
return this.http.get<Array<Required<ICat>>>(`${this.apiUrl}/cats`);
}

getById(id: number): Observable<CatModel> {
return this.http.get<CatModel>(`${this.apiUrl}/cats/${id}`);
getById(id: number): Observable<Required<ICat>> {
return this.http.get<Required<ICat>>(`${this.apiUrl}/cats/${id}`);
}

create(createCatDto: ICreateCatDto): Observable<CatModel> {
return this.http.post<CatModel>(`${this.apiUrl}/cats`, createCatDto);
create(createCatDto: Required<ICreateCatDto>): Observable<Required<ICat>> {
return this.http.post<Required<ICat>>(`${this.apiUrl}/cats`, createCatDto);
}

delete(id: number): Observable<CatModel> {
return this.http.delete<CatModel>(`${this.apiUrl}/cats/${id}`);
delete(id: number): Observable<Required<ICat>> {
return this.http.delete<Required<ICat>>(`${this.apiUrl}/cats/${id}`);
}
}
8 changes: 4 additions & 4 deletions apps/lazy-loading-app/src/app/home/home.component.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Component, OnDestroy, OnInit } from '@angular/core';
import { AbstractControl, FormBuilder, FormGroup, Validators } from '@angular/forms';
import { CatModel, ICreateCatDto, SignedUser } from '@api-interface';
import { ICat, ICreateCatDto, ISignedUser } from '@proto-interface';
import { Subscription } from 'rxjs';
import { first } from 'rxjs/operators';

Expand All @@ -10,9 +10,9 @@ import { CatsService } from './cats.service';

@Component({ templateUrl: 'home.component.html' })
export class HomeComponent implements OnInit, OnDestroy {
currentUser: SignedUser;
currentUser: Required<ISignedUser>;
currentUserSubscription: Subscription;
cats: CatModel[] = [];
cats: Array<Required<ICat>> = [];

catForm: FormGroup;
loading = false;
Expand Down Expand Up @@ -53,7 +53,7 @@ export class HomeComponent implements OnInit, OnDestroy {
}

this.loading = true;
const createCatDto: ICreateCatDto = {
const createCatDto: Required<ICreateCatDto> = {
name: this.f.name.value,
age: this.f.age.value,
};
Expand Down
25 changes: 25 additions & 0 deletions libs/proto-interface/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# ApiInterface

This library was generated with [Angular CLI](https://github.com/angular/angular-cli) version 7.2.0.

## Code scaffolding

Run `ng generate component component-name --project api-interface` to generate a new component. You can also use `ng generate directive|pipe|service|class|guard|interface|enum|module --project api-interface`.

> Note: Don't forget to add `--project api-interface` or else it will be added to the default project in your `angular.json` file.

## Build

Run `ng build api-interface` to build the project. The build artifacts will be stored in the `dist/` directory.

## Publishing

After building your library with `ng build api-interface`, go to the dist folder `cd dist/api-interface` and run `npm publish`.

## Running unit tests

Run `ng test api-interface` to execute the unit tests via [Karma](https://karma-runner.github.io).

## Further help

To get more help on the Angular CLI use `ng help` or go check out the [Angular CLI README](https://github.com/angular/angular-cli/blob/master/README.md).
5 changes: 5 additions & 0 deletions libs/proto-interface/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
name: "api-interface",
preset: "../../jest.config.js",
coverageDirectory: "../../coverage/libs/api-interface"
};
21 changes: 21 additions & 0 deletions libs/proto-interface/src/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package(default_visibility = ["//:__subpackages__"])

load("@npm_bazel_typescript//:index.bzl", "ts_library", "ts_proto_library")

proto_library(
name = "proto",
srcs = glob(["*.proto"]),
)

ts_proto_library(
name = "ts_proto",
deps = [":proto"],
)

ts_library(
name = "src",
srcs = glob(["index.ts"]),
module_name = "@proto-interface",
tsconfig = "//:tsconfig.json",
deps = [":ts_proto"],
)
12 changes: 12 additions & 0 deletions libs/proto-interface/src/cat.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
syntax = "proto3";

message Cat {
int32 id = 1;
string name = 2;
int32 age = 3;
}

message CreateCatDto {
string name = 1;
int32 age = 2;
}
10 changes: 10 additions & 0 deletions libs/proto-interface/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export {
ICat,
ICreateCatDto,
IMessage,
IUser,
ISignUpDto,
ISignInDto,
ISignedUser,
// @ts-ignore
} from './ts_proto';
5 changes: 5 additions & 0 deletions libs/proto-interface/src/message.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
syntax = "proto3";

message Message {
string message = 1;
}
29 changes: 29 additions & 0 deletions libs/proto-interface/src/user.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
syntax = "proto3";

message User {
int32 id = 1;
string username = 2;
string firstName = 3;
string lastName = 4;
string password = 5;
}

message SignUpDto {
string username = 1;
string firstName = 2;
string lastName = 3;
string password = 4;
}

message SignInDto {
string username = 1;
string password = 2;
}

message SignedUser {
string username = 1;
string firstName = 2;
string lastName = 3;
int32 expiresIn = 4;
string token = 5;
}
Loading