examples to new tsrpc-cli

This commit is contained in:
King Wang
2021-10-07 15:30:02 +08:00
parent c21445548e
commit 69a90884d5
86 changed files with 652 additions and 1697 deletions

View File

@@ -4,22 +4,22 @@
"main": "index.js",
"private": true,
"scripts": {
"proto": "tsrpc proto -i src/shared/protocols -o src/shared/protocols/serviceProto.ts",
"sync": "tsrpc sync --from src/shared --to ../frontend/src/shared",
"api": "tsrpc api -i src/shared/protocols/serviceProto.ts -o src/api",
"dev": "onchange \"src/**/*.ts\" -i -k -- ts-node \"src/index.ts\"",
"build": "tsrpc build"
"proto": "tsrpc proto --config tsrpc.config.ts",
"sync": "tsrpc sync --config tsrpc.config.ts",
"api": "tsrpc api --config tsrpc.config.ts",
"dev": "tsrpc dev --config tsrpc.config.ts",
"build": "tsrpc build --config tsrpc.config.ts"
},
"devDependencies": {
"@types/node": "^15.12.3",
"@types/uuid": "^8.3.0",
"@types/node": "^15.14.9",
"@types/uuid": "^8.3.1",
"onchange": "^7.1.0",
"ts-node": "^9.1.1",
"tsrpc-cli": "^2.0.3",
"typescript": "^4.3.4"
"tsrpc-cli": "^2.0.8",
"typescript": "^4.4.3"
},
"dependencies": {
"tsrpc": "^3.0.2",
"tsrpc": "^3.0.9",
"uuid": "^8.3.2"
}
}

View File

@@ -34,7 +34,7 @@ export interface ServiceType {
}
export const serviceProto: ServiceProto<ServiceType> = {
"version": 2,
"version": 3,
"services": [
{
"id": 0,
@@ -248,6 +248,43 @@ export const serviceProto: ServiceProto<ServiceType> = {
"type": {
"type": "String"
}
},
{
"id": 1,
"name": "user",
"type": {
"type": "Reference",
"target": "../../models/CurrentUser/CurrentUser"
}
}
]
},
"../../models/CurrentUser/CurrentUser": {
"type": "Interface",
"properties": [
{
"id": 0,
"name": "uid",
"type": {
"type": "Number"
}
},
{
"id": 1,
"name": "username",
"type": {
"type": "String"
}
},
{
"id": 2,
"name": "roles",
"type": {
"type": "Array",
"elementType": {
"type": "String"
}
}
}
]
},

View File

@@ -0,0 +1,36 @@
import { TsrpcConfig } from 'tsrpc-cli';
const tsrpcConf: TsrpcConfig = {
// Generate ServiceProto
proto: [
{
ptlDir: 'src/shared/protocols', // Protocol dir
output: 'src/shared/protocols/serviceProto.ts', // Path for generated ServiceProto
apiDir: 'src/api' // API dir
}
],
// Sync shared code
sync: [
{
from: 'src/shared',
to: '../frontend/src/shared',
type: 'symlink' // Change this to 'copy' if your environment not support symlink
}
],
// Dev server
dev: {
autoProto: true, // Auto regenerate proto
autoSync: true, // Auto sync when file changed
autoApi: true, // Auto create API when ServiceProto updated
watch: 'src', // Restart dev server when these files changed
entry: 'src/index.ts', // Dev server command: node -r ts-node/register {entry}
},
// Build config
build: {
autoProto: true, // Auto generate proto before build
autoSync: true, // Auto sync before build
autoApi: true, // Auto generate API before build
outDir: 'dist', // Clean this dir before build
}
}
export default tsrpcConf;

View File

@@ -7,15 +7,15 @@
"build": "webpack --mode=production"
},
"devDependencies": {
"copy-webpack-plugin": "^9.0.0",
"html-webpack-plugin": "^5.3.1",
"ts-loader": "^9.2.3",
"typescript": "^4.3.4",
"webpack": "^5.39.1",
"webpack-cli": "^4.7.2",
"copy-webpack-plugin": "^9.0.1",
"html-webpack-plugin": "^5.3.2",
"ts-loader": "^9.2.6",
"typescript": "^4.4.3",
"webpack": "^5.57.1",
"webpack-cli": "^4.9.0",
"webpack-dev-server": "^3.11.2"
},
"dependencies": {
"tsrpc-browser": "^3.0.3"
"tsrpc-browser": "^3.0.7"
}
}

View File

@@ -0,0 +1 @@
../../backend/src/shared

View File

@@ -1,14 +0,0 @@
import { BaseRequest, BaseResponse, BaseConf } from '../base'
export interface ReqAdminAction extends BaseRequest {
}
export interface ResAdminAction extends BaseResponse {
result: string
}
export const conf: BaseConf = {
needLogin: true,
needRoles: ['Admin']
};

View File

@@ -1,13 +0,0 @@
import { BaseConf, BaseRequest, BaseResponse } from '../base';
export interface ReqGuestAction extends BaseRequest {
}
export interface ResGuestAction extends BaseResponse {
result: string
}
export const conf: BaseConf = {
needLogin: false
};

View File

@@ -1,13 +0,0 @@
import { BaseConf, BaseRequest, BaseResponse } from '../base';
export interface ReqNormalAction extends BaseRequest {
}
export interface ResNormalAction extends BaseResponse {
result: string
}
export const conf: BaseConf = {
needLogin: true
};

View File

@@ -1,13 +0,0 @@
export interface BaseRequest {
__ssoToken?: string;
}
export interface BaseResponse {
// Init or refresh sso token
__ssoToken?: string;
}
export interface BaseConf {
needLogin?: boolean,
needRoles?: string[]
}

View File

@@ -1,279 +0,0 @@
import { ServiceProto } from 'tsrpc-proto';
import { ReqAdminAction, ResAdminAction } from './action/PtlAdminAction';
import { ReqGuestAction, ResGuestAction } from './action/PtlGuestAction';
import { ReqNormalAction, ResNormalAction } from './action/PtlNormalAction';
import { ReqLogin, ResLogin } from './user/PtlLogin';
import { ReqLogout, ResLogout } from './user/PtlLogout';
export interface ServiceType {
api: {
"action/AdminAction": {
req: ReqAdminAction,
res: ResAdminAction
},
"action/GuestAction": {
req: ReqGuestAction,
res: ResGuestAction
},
"action/NormalAction": {
req: ReqNormalAction,
res: ResNormalAction
},
"user/Login": {
req: ReqLogin,
res: ResLogin
},
"user/Logout": {
req: ReqLogout,
res: ResLogout
}
},
msg: {
}
}
export const serviceProto: ServiceProto<ServiceType> = {
"version": 2,
"services": [
{
"id": 0,
"name": "action/AdminAction",
"type": "api",
"conf": {
"needLogin": true,
"needRoles": [
"Admin"
]
}
},
{
"id": 1,
"name": "action/GuestAction",
"type": "api",
"conf": {
"needLogin": false
}
},
{
"id": 2,
"name": "action/NormalAction",
"type": "api",
"conf": {
"needLogin": true
}
},
{
"id": 3,
"name": "user/Login",
"type": "api",
"conf": {}
},
{
"id": 4,
"name": "user/Logout",
"type": "api",
"conf": {}
}
],
"types": {
"action/PtlAdminAction/ReqAdminAction": {
"type": "Interface",
"extends": [
{
"id": 0,
"type": {
"type": "Reference",
"target": "base/BaseRequest"
}
}
]
},
"base/BaseRequest": {
"type": "Interface",
"properties": [
{
"id": 0,
"name": "__ssoToken",
"type": {
"type": "String"
},
"optional": true
}
]
},
"action/PtlAdminAction/ResAdminAction": {
"type": "Interface",
"extends": [
{
"id": 0,
"type": {
"type": "Reference",
"target": "base/BaseResponse"
}
}
],
"properties": [
{
"id": 0,
"name": "result",
"type": {
"type": "String"
}
}
]
},
"base/BaseResponse": {
"type": "Interface",
"properties": [
{
"id": 0,
"name": "__ssoToken",
"type": {
"type": "String"
},
"optional": true
}
]
},
"action/PtlGuestAction/ReqGuestAction": {
"type": "Interface",
"extends": [
{
"id": 0,
"type": {
"type": "Reference",
"target": "base/BaseRequest"
}
}
]
},
"action/PtlGuestAction/ResGuestAction": {
"type": "Interface",
"extends": [
{
"id": 0,
"type": {
"type": "Reference",
"target": "base/BaseResponse"
}
}
],
"properties": [
{
"id": 0,
"name": "result",
"type": {
"type": "String"
}
}
]
},
"action/PtlNormalAction/ReqNormalAction": {
"type": "Interface",
"extends": [
{
"id": 0,
"type": {
"type": "Reference",
"target": "base/BaseRequest"
}
}
]
},
"action/PtlNormalAction/ResNormalAction": {
"type": "Interface",
"extends": [
{
"id": 0,
"type": {
"type": "Reference",
"target": "base/BaseResponse"
}
}
],
"properties": [
{
"id": 0,
"name": "result",
"type": {
"type": "String"
}
}
]
},
"user/PtlLogin/ReqLogin": {
"type": "Interface",
"extends": [
{
"id": 0,
"type": {
"type": "Reference",
"target": "base/BaseRequest"
}
}
],
"properties": [
{
"id": 0,
"name": "username",
"type": {
"type": "String"
}
},
{
"id": 1,
"name": "password",
"type": {
"type": "String"
}
}
]
},
"user/PtlLogin/ResLogin": {
"type": "Interface",
"extends": [
{
"id": 0,
"type": {
"type": "Reference",
"target": "base/BaseResponse"
}
}
],
"properties": [
{
"id": 0,
"name": "__ssoToken",
"type": {
"type": "String"
}
}
]
},
"user/PtlLogout/ReqLogout": {
"type": "Interface",
"extends": [
{
"id": 0,
"type": {
"type": "Reference",
"target": "base/BaseRequest"
}
}
]
},
"user/PtlLogout/ResLogout": {
"type": "Interface",
"extends": [
{
"id": 0,
"type": {
"type": "Reference",
"target": "base/BaseResponse"
}
}
]
}
}
};

View File

@@ -1,14 +0,0 @@
import { BaseConf, BaseRequest, BaseResponse } from '../base';
export interface ReqLogin extends BaseRequest {
username: string,
password: string
}
export interface ResLogin extends BaseResponse {
__ssoToken: string;
}
export const conf: BaseConf = {
};

View File

@@ -1,13 +0,0 @@
import { BaseRequest, BaseResponse, BaseConf } from '../base'
export interface ReqLogout extends BaseRequest {
}
export interface ResLogout extends BaseResponse {
}
export const conf: BaseConf = {
};