diff --git a/apps/api/src/app/BUILD.bazel b/apps/api/src/app/BUILD.bazel index dcea6df..f6805a6 100644 --- a/apps/api/src/app/BUILD.bazel +++ b/apps/api/src/app/BUILD.bazel @@ -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", diff --git a/apps/api/src/app/app.controller.ts b/apps/api/src/app/app.controller.ts index d66af2d..b2b2918 100644 --- a/apps/api/src/app/app.controller.ts +++ b/apps/api/src/app/app.controller.ts @@ -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'; @@ -13,7 +13,7 @@ export class AppController { @Get('hello') @UseGuards(AuthGuard('jwt')) - getData(): Message { + getData(): Required { return this.appService.getData(); } } diff --git a/apps/api/src/app/app.service.ts b/apps/api/src/app/app.service.ts index cf43fac..d232d10 100644 --- a/apps/api/src/app/app.service.ts +++ b/apps/api/src/app/app.service.ts @@ -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 { return { message: 'Welcome to api!' }; } } diff --git a/apps/api/src/auth/BUILD.bazel b/apps/api/src/auth/BUILD.bazel index 15e46ac..bd15ecb 100644 --- a/apps/api/src/auth/BUILD.bazel +++ b/apps/api/src/auth/BUILD.bazel @@ -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", diff --git a/apps/api/src/auth/auth.service.ts b/apps/api/src/auth/auth.service.ts index 2d2f203..33ad862 100644 --- a/apps/api/src/auth/auth.service.ts +++ b/apps/api/src/auth/auth.service.ts @@ -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'; @@ -13,7 +13,7 @@ export class AuthService { private readonly jwtService: JwtService, ) {} - async createToken(username: string): Promise { + async createToken(username: string): Promise> { const { firstName, lastName } = await this.userService.getUserByUsername(username); const payload: JwtPayload = { username, firstName, lastName }; diff --git a/apps/api/src/cats/BUILD.bazel b/apps/api/src/cats/BUILD.bazel index be76229..7c357d1 100644 --- a/apps/api/src/cats/BUILD.bazel +++ b/apps/api/src/cats/BUILD.bazel @@ -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", diff --git a/apps/api/src/cats/dto/create-cat.dto.ts b/apps/api/src/cats/dto/create-cat.dto.ts index 69f8ed7..d67b9e1 100644 --- a/apps/api/src/cats/dto/create-cat.dto.ts +++ b/apps/api/src/cats/dto/create-cat.dto.ts @@ -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 { @ApiModelProperty() @IsNotEmpty() name: string; diff --git a/apps/api/src/cats/entities/cat.ts b/apps/api/src/cats/entities/cat.ts index 89f03d1..bffaad7 100644 --- a/apps/api/src/cats/entities/cat.ts +++ b/apps/api/src/cats/entities/cat.ts @@ -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 { @PrimaryGeneratedColumn() id: number; diff --git a/apps/api/src/user/BUILD.bazel b/apps/api/src/user/BUILD.bazel index 8763d2d..7ebb1a1 100644 --- a/apps/api/src/user/BUILD.bazel +++ b/apps/api/src/user/BUILD.bazel @@ -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", diff --git a/apps/api/src/user/dto/sign-in.dto.ts b/apps/api/src/user/dto/sign-in.dto.ts index e2d01e8..5d74417 100644 --- a/apps/api/src/user/dto/sign-in.dto.ts +++ b/apps/api/src/user/dto/sign-in.dto.ts @@ -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 { @ApiModelProperty() @IsString() @IsNotEmpty() diff --git a/apps/api/src/user/dto/sign-up.dto.ts b/apps/api/src/user/dto/sign-up.dto.ts index 3d8d51d..661b151 100644 --- a/apps/api/src/user/dto/sign-up.dto.ts +++ b/apps/api/src/user/dto/sign-up.dto.ts @@ -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 { @ApiModelProperty() @IsString() @IsNotEmpty() diff --git a/apps/api/src/user/entities/user.entity.ts b/apps/api/src/user/entities/user.entity.ts index 18c88fb..21a6c8c 100644 --- a/apps/api/src/user/entities/user.entity.ts +++ b/apps/api/src/user/entities/user.entity.ts @@ -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 { @PrimaryGeneratedColumn() id: number; diff --git a/apps/lazy-loading-app/src/app/app.component.ts b/apps/lazy-loading-app/src/app/app.component.ts index e209dce..2574f44 100644 --- a/apps/lazy-loading-app/src/app/app.component.ts +++ b/apps/lazy-loading-app/src/app/app.component.ts @@ -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; constructor( private router: Router, diff --git a/apps/lazy-loading-app/src/app/auth/BUILD.bazel b/apps/lazy-loading-app/src/app/auth/BUILD.bazel index 336e173..f7e2aa0 100644 --- a/apps/lazy-loading-app/src/app/auth/BUILD.bazel +++ b/apps/lazy-loading-app/src/app/auth/BUILD.bazel @@ -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", diff --git a/apps/lazy-loading-app/src/app/auth/authentication.service.ts b/apps/lazy-loading-app/src/app/auth/authentication.service.ts index ed685e5..f1a06b1 100644 --- a/apps/lazy-loading-app/src/app/auth/authentication.service.ts +++ b/apps/lazy-loading-app/src/app/auth/authentication.service.ts @@ -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; - public currentUser: Observable; + private currentUserSubject: BehaviorSubject>; + public currentUser: Observable>; private apiUrl = 'http://localhost:3000'; constructor(private http: HttpClient) { - this.currentUserSubject = new BehaviorSubject(JSON.parse(localStorage.getItem('currentUser'))); + this.currentUserSubject = new BehaviorSubject>( + JSON.parse(localStorage.getItem('currentUser')), + ); this.currentUser = this.currentUserSubject.asObservable(); } - public get currentUserValue(): SignedUser { + public get currentUserValue(): Required { return this.currentUserSubject.value; } login(username: string, password: string) { - return this.http.post(`${this.apiUrl}/auth/sign-in`, { username, password }) + return this.http.post>(`${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) { @@ -33,7 +35,7 @@ export class AuthenticationService { })); } - register(user: ISignUpDto) { + register(user: Required) { return this.http.post(`${this.apiUrl}/auth/sign-up`, user); } diff --git a/apps/lazy-loading-app/src/app/home/BUILD.bazel b/apps/lazy-loading-app/src/app/home/BUILD.bazel index d9a51cb..36bdb70 100644 --- a/apps/lazy-loading-app/src/app/home/BUILD.bazel +++ b/apps/lazy-loading-app/src/app/home/BUILD.bazel @@ -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", diff --git a/apps/lazy-loading-app/src/app/home/cats.service.ts b/apps/lazy-loading-app/src/app/home/cats.service.ts index f1b41f2..bb34622 100644 --- a/apps/lazy-loading-app/src/app/home/cats.service.ts +++ b/apps/lazy-loading-app/src/app/home/cats.service.ts @@ -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() @@ -9,19 +9,19 @@ export class CatsService { constructor(private http: HttpClient) {} - getAll(): Observable { - return this.http.get(`${this.apiUrl}/cats`); + getAll(): Observable>> { + return this.http.get>>(`${this.apiUrl}/cats`); } - getById(id: number): Observable { - return this.http.get(`${this.apiUrl}/cats/${id}`); + getById(id: number): Observable> { + return this.http.get>(`${this.apiUrl}/cats/${id}`); } - create(createCatDto: ICreateCatDto): Observable { - return this.http.post(`${this.apiUrl}/cats`, createCatDto); + create(createCatDto: Required): Observable> { + return this.http.post>(`${this.apiUrl}/cats`, createCatDto); } - delete(id: number): Observable { - return this.http.delete(`${this.apiUrl}/cats/${id}`); + delete(id: number): Observable> { + return this.http.delete>(`${this.apiUrl}/cats/${id}`); } } diff --git a/apps/lazy-loading-app/src/app/home/home.component.ts b/apps/lazy-loading-app/src/app/home/home.component.ts index 4582fc1..7992412 100644 --- a/apps/lazy-loading-app/src/app/home/home.component.ts +++ b/apps/lazy-loading-app/src/app/home/home.component.ts @@ -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'; @@ -10,9 +10,9 @@ import { CatsService } from './cats.service'; @Component({ templateUrl: 'home.component.html' }) export class HomeComponent implements OnInit, OnDestroy { - currentUser: SignedUser; + currentUser: Required; currentUserSubscription: Subscription; - cats: CatModel[] = []; + cats: Array> = []; catForm: FormGroup; loading = false; @@ -53,7 +53,7 @@ export class HomeComponent implements OnInit, OnDestroy { } this.loading = true; - const createCatDto: ICreateCatDto = { + const createCatDto: Required = { name: this.f.name.value, age: this.f.age.value, }; diff --git a/libs/proto-interface/README.md b/libs/proto-interface/README.md new file mode 100644 index 0000000..671402d --- /dev/null +++ b/libs/proto-interface/README.md @@ -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). diff --git a/libs/proto-interface/jest.config.js b/libs/proto-interface/jest.config.js new file mode 100644 index 0000000..14d5b08 --- /dev/null +++ b/libs/proto-interface/jest.config.js @@ -0,0 +1,5 @@ +module.exports = { + name: "api-interface", + preset: "../../jest.config.js", + coverageDirectory: "../../coverage/libs/api-interface" +}; diff --git a/libs/proto-interface/src/BUILD.bazel b/libs/proto-interface/src/BUILD.bazel new file mode 100644 index 0000000..a55afeb --- /dev/null +++ b/libs/proto-interface/src/BUILD.bazel @@ -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"], +) diff --git a/libs/proto-interface/src/cat.proto b/libs/proto-interface/src/cat.proto new file mode 100644 index 0000000..5a00c4e --- /dev/null +++ b/libs/proto-interface/src/cat.proto @@ -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; +} diff --git a/libs/proto-interface/src/index.ts b/libs/proto-interface/src/index.ts new file mode 100644 index 0000000..3a9f70c --- /dev/null +++ b/libs/proto-interface/src/index.ts @@ -0,0 +1,10 @@ +export { + ICat, + ICreateCatDto, + IMessage, + IUser, + ISignUpDto, + ISignInDto, + ISignedUser, +// @ts-ignore +} from './ts_proto'; diff --git a/libs/proto-interface/src/message.proto b/libs/proto-interface/src/message.proto new file mode 100644 index 0000000..0694981 --- /dev/null +++ b/libs/proto-interface/src/message.proto @@ -0,0 +1,5 @@ +syntax = "proto3"; + +message Message { + string message = 1; +} diff --git a/libs/proto-interface/src/user.proto b/libs/proto-interface/src/user.proto new file mode 100644 index 0000000..4900e1f --- /dev/null +++ b/libs/proto-interface/src/user.proto @@ -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; +} diff --git a/libs/proto-interface/tsconfig.json b/libs/proto-interface/tsconfig.json new file mode 100644 index 0000000..e5decd5 --- /dev/null +++ b/libs/proto-interface/tsconfig.json @@ -0,0 +1,7 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "types": ["node", "jest"] + }, + "include": ["**/*.ts"] +} diff --git a/libs/proto-interface/tsconfig.lib.json b/libs/proto-interface/tsconfig.lib.json new file mode 100644 index 0000000..6a7446f --- /dev/null +++ b/libs/proto-interface/tsconfig.lib.json @@ -0,0 +1,18 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "../../dist/out-tsc/libs/api-interface", + "target": "es2015", + "module": "es2015", + "moduleResolution": "node", + "declaration": true, + "sourceMap": true, + "inlineSources": true, + "emitDecoratorMetadata": true, + "experimentalDecorators": true, + "importHelpers": true, + "types": [], + "lib": ["dom", "es2018"] + }, + "exclude": ["src/test.ts", "**/*.spec.ts"] +} diff --git a/libs/proto-interface/tsconfig.spec.json b/libs/proto-interface/tsconfig.spec.json new file mode 100644 index 0000000..5040bbc --- /dev/null +++ b/libs/proto-interface/tsconfig.spec.json @@ -0,0 +1,9 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "../../dist/out-tsc/libs/api-interface", + "module": "commonjs", + "types": ["jest", "node"] + }, + "include": ["**/*.spec.ts", "**/*.d.ts"] +} diff --git a/libs/proto-interface/tslint.json b/libs/proto-interface/tslint.json new file mode 100644 index 0000000..4fa9be3 --- /dev/null +++ b/libs/proto-interface/tslint.json @@ -0,0 +1,4 @@ +{ + "extends": "../../tslint.json", + "rules": [] +} diff --git a/tsconfig.json b/tsconfig.json index d2c0657..90403bb 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -13,16 +13,8 @@ "typeRoots": [ "node_modules/@types" ], -// "lib": [ -// "dom", -// "es5", -// "es2015.collection", -// "es2015.iterable", -// "es2015.promise" -// ], "rootDirs": [ - "./apps/lazy-loading-app/src", - "./dist/bin/apps/lazy-loading-app/src" + "./dist/bin" ], "lib": [ "es2017", @@ -35,6 +27,9 @@ "paths": { "@api-interface": [ "libs/api-interface/src/index.ts" + ], + "@proto-interface": [ + "dist/bin/libs/proto-interface/src/index" ] } },