SDK allowing you to quickly setup authorization and websocket life cycle in an Angular app. Some utils are also included
Suited for Angular.
There are packages, their requirements and exported functionality listed below.
A package for a websocket life cycle management.
This package:
A package for an authorization and a JWT management.
This package:
JwtService
which is responsible for maintaining tokens fresh.A package for handling errors according to our own conventions.
This package:
A package that handle details of working with proto over http. Helps to manage headers, errors, serialization, deserialization.
This package:
RequestService
for sending http request.Connects together rxjs-websocket and jwt packages to implement pattern "Auth before an authorized or unauthorized socket".
This package:
Implemented flow:
Suppose you have an Angular + NgRx app using protocol buffers for communication with backend. The app has login/password authorization and uses websockets to handle updates and send some requests.
Suppose your proto3 description of the api messages looks like this:
message Request {
uint64 id = 1; // id запроса
reserved 2, 3, 4, 9;
oneof data {
option (validate.required) = true;
CheckVersionRequest check_version = 5 [(validate.rules).message.required = true]; // проверка версии
AuthCodeRequest auth_code = 6 [(validate.rules).message.required = true]; // запрос смс-кода авторизации
AuthRequest auth = 7 [(validate.rules).message.required = true]; // авторизация
SocketAuthRequest socket_auth = 8 [(validate.rules).message.required = true]; // авторизация WS
ClientListRequest clients = 10 [(validate.rules).message.required = true]; // список клиентов
ClientUpdateRequest client_update = 11 [(validate.rules).message.required = true]; // редактирование клиента
ClientAddressListRequest client_addresses = 12 [(validate.rules).message.required = true]; // список адресов
ClientAddressAddRequest client_address_add = 13 [(validate.rules).message.required = true]; // добавление адреса
ClientAddressUpdateRequest client_address_update = 14 [(validate.rules).message.required = true]; // редактирование адреса
ClientAddressDeleteRequest client_address_delete = 15 [(validate.rules).message.required = true]; // удаление адреса
CategoryListRequest categories = 16 [(validate.rules).message.required = true]; // список категорий
CategoryAddRequest category_add = 17 [(validate.rules).message.required = true]; // добавление категории
CategoryUpdateRequest category_update = 18 [(validate.rules).message.required = true]; // редактирование категории
CategoryDeleteRequest category_delete = 19 [(validate.rules).message.required = true]; // удаление категории
MenuListRequest menu = 20 [(validate.rules).message.required = true]; // список анализов
MenuAddRequest menu_add = 21 [(validate.rules).message.required = true]; // добавление анализа
MenuUpdateRequest menu_update = 22 [(validate.rules).message.required = true]; // редактирование анализа
MenuDeleteRequest menu_delete = 23 [(validate.rules).message.required = true]; // удаление анализа
CartInfoRequest cart = 24 [(validate.rules).message.required = true]; // корзина
CartUpdateRequest cart_update = 25 [(validate.rules).message.required = true]; // редактирование корзины
OrderListRequest orders = 26 [(validate.rules).message.required = true]; // список заказов
OrderAddRequest order_add = 27 [(validate.rules).message.required = true]; // добавление заказа
OrderUpdateRequest order_update = 28 [(validate.rules).message.required = true]; // редактирование заказа
PageConfigRequest page_config = 29 [(validate.rules).message.required = true]; // конфиг постраничного вывода
ConfigUpdateRequest config_update = 30 [(validate.rules).message.required = true]; // редактирование конфига
BugReportRequest bug_report = 31 [(validate.rules).message.required = true]; // баг репорт
SubscribeRequest sub = 32 [(validate.rules).message.required = true]; // подписка
UnsubscribeRequest unsub = 33 [(validate.rules).message.required = true]; // отписка
}
}
Install deps for non-angular packages.
yarn
Install deps for angular packages.
cd angular-packages
yarn
Build all packages.
# go to the project root
cd ..
yarn build
Running tests in watch mode is only available per single package. For example, customapp-rxjs-websocket
cd packages/rxjs-websocket
yarn test
To run the tests for all packages once, use this command.
yarn test-ci
To set version of all packages to 1.2.3, run
yarn set-version 1.2.3
To publish all packages, run
yarn publish
Create a new library in the Angular workspace. Package name should be prefixed with ngx-customapp-
cd angular-packages
ng g library ngx-customapp-jwt
Add watch:pkg, build:pkg and test:pkg scripts to package.json
{
"scripts": {
"watch:jwt": "yarn build:jwt --watch --configuration=development",
"build:jwt": "ng build ngx-customapp-jwt",
"test:jwt": "ng test ngx-customapp-jwt"
}
}
Modify common build script, by adding && yarn build:pkg && yarn
{
"scripts": {
"build": "/* ... old command */ && yarn build:jwt && yarn"
}
}
Done! All other common scripts should just work.
To reproduce and fix an error, you need to link local version of the ngx-customapp-sdk with a reproduction app.
To do so, the app needs to use yarn 2, because of new portal:
protocol.
Yarn migration docs.
Suppose your local ngx-customapp-sdk version is located in ~/WebstormProjects/ngx-customapp-sdk
.
Register packages from ngx-customapp-sdk for use in the app.
yarn link --all ~/WebstormProjects/ngx-customapp-sdk
yarn link --all ~/WebstormProjects/ngx-customapp-sdk/angular-packages/
Add preserveSymlink option to the tsconfig.json
. This allows Typescript to properly resolve dependencies and
peer dependencies of the linked packages.
{
"compilerOptions": {
"preserveSymlinks": true
}
}
Run the app and debug with pleasure (impossible).
Generated using TypeDoc