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

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

View File

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

View File

@ -1,4 +0,0 @@
export interface MsgChat {
content: string,
time: Date
}

View File

@ -1,7 +0,0 @@
export interface ReqSend {
content: string
}
export interface ResSend {
time: Date
}

View File

@ -1,75 +0,0 @@
import { ServiceProto } from 'tsrpc-proto';
import { MsgChat } from './MsgChat';
import { ReqSend, ResSend } from './PtlSend';
export interface ServiceType {
api: {
"Send": {
req: ReqSend,
res: ResSend
}
},
msg: {
"Chat": MsgChat
}
}
export const serviceProto: ServiceProto<ServiceType> = {
"services": [
{
"id": 0,
"name": "Chat",
"type": "msg"
},
{
"id": 1,
"name": "Send",
"type": "api"
}
],
"types": {
"MsgChat/MsgChat": {
"type": "Interface",
"properties": [
{
"id": 0,
"name": "content",
"type": {
"type": "String"
}
},
{
"id": 1,
"name": "time",
"type": {
"type": "Date"
}
}
]
},
"PtlSend/ReqSend": {
"type": "Interface",
"properties": [
{
"id": 0,
"name": "content",
"type": {
"type": "String"
}
}
]
},
"PtlSend/ResSend": {
"type": "Interface",
"properties": [
{
"id": 0,
"name": "time",
"type": {
"type": "Date"
}
}
]
}
}
};

View File

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

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

@ -9,14 +9,14 @@
"devDependencies": { "devDependencies": {
"copy-webpack-plugin": "^9.0.1", "copy-webpack-plugin": "^9.0.1",
"html-webpack-plugin": "^5.3.2", "html-webpack-plugin": "^5.3.2",
"ts-loader": "^9.2.3", "ts-loader": "^9.2.6",
"typescript": "^4.3.4", "typescript": "^4.4.3",
"webpack": "^5.41.1", "webpack": "^5.57.1",
"webpack-cli": "^4.7.2", "webpack-cli": "^4.9.0",
"webpack-dev-server": "^3.11.2" "webpack-dev-server": "^3.11.2"
}, },
"dependencies": { "dependencies": {
"tsrpc-browser": "^3.0.4" "tsrpc-browser": "^3.0.7"
}, },
"browserslist": [ "browserslist": [
"defaults" "defaults"

View File

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

View File

@ -1,10 +0,0 @@
// This is a demo code file
// Feel free to delete it
export interface ReqAddData {
content: string;
}
export interface ResAddData {
time: Date
}

View File

@ -1,13 +0,0 @@
// This is a demo code file
// Feel free to delete it
export interface ReqGetData {
}
export interface ResGetData {
data: {
content: string,
time: Date
}[]
}

View File

@ -1,98 +0,0 @@
import { ServiceProto } from 'tsrpc-proto';
import { ReqAddData, ResAddData } from './PtlAddData';
import { ReqGetData, ResGetData } from './PtlGetData';
// This is a demo service proto file (auto generated)
// Feel free to delete it
export interface ServiceType {
api: {
"AddData": {
req: ReqAddData,
res: ResAddData
},
"GetData": {
req: ReqGetData,
res: ResGetData
}
},
msg: {
}
}
export const serviceProto: ServiceProto<ServiceType> = {
"version": 1,
"services": [
{
"id": 0,
"name": "AddData",
"type": "api"
},
{
"id": 1,
"name": "GetData",
"type": "api"
}
],
"types": {
"PtlAddData/ReqAddData": {
"type": "Interface",
"properties": [
{
"id": 0,
"name": "content",
"type": {
"type": "String"
}
}
]
},
"PtlAddData/ResAddData": {
"type": "Interface",
"properties": [
{
"id": 0,
"name": "time",
"type": {
"type": "Date"
}
}
]
},
"PtlGetData/ReqGetData": {
"type": "Interface"
},
"PtlGetData/ResGetData": {
"type": "Interface",
"properties": [
{
"id": 0,
"name": "data",
"type": {
"type": "Array",
"elementType": {
"type": "Interface",
"properties": [
{
"id": 0,
"name": "content",
"type": {
"type": "String"
}
},
{
"id": 1,
"name": "time",
"type": {
"type": "Date"
}
}
]
}
}
}
]
}
}
};

View File

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

View File

@ -2,7 +2,7 @@
"ver": "1.1.0", "ver": "1.1.0",
"importer": "directory", "importer": "directory",
"imported": true, "imported": true,
"uuid": "ba5a8782-c57d-437b-9371-4441de673229", "uuid": "40721700-8121-4c1e-be92-c90e93cdc4c8",
"files": [], "files": [],
"subMetas": {}, "subMetas": {},
"userData": { "userData": {

View File

@ -2,7 +2,7 @@
"ver": "4.0.22", "ver": "4.0.22",
"importer": "typescript", "importer": "typescript",
"imported": true, "imported": true,
"uuid": "bd48404c-a47a-4bbc-b177-19352bd570bf", "uuid": "43723a09-6be4-404b-a21a-22ad675d0e58",
"files": [], "files": [],
"subMetas": {}, "subMetas": {},
"userData": {} "userData": {}

View File

@ -2,7 +2,7 @@
"ver": "4.0.22", "ver": "4.0.22",
"importer": "typescript", "importer": "typescript",
"imported": true, "imported": true,
"uuid": "2a458e30-4c4a-4f1a-9aff-05ea0bdf0ab6", "uuid": "48e989fd-b5a5-4a5b-ac68-71d7f7f3d8ee",
"files": [], "files": [],
"subMetas": {}, "subMetas": {},
"userData": {} "userData": {}

View File

@ -2,7 +2,7 @@
"ver": "4.0.22", "ver": "4.0.22",
"importer": "typescript", "importer": "typescript",
"imported": true, "imported": true,
"uuid": "56f77762-3150-4a83-ab4a-15025fd73ade", "uuid": "aeef551d-77fd-412f-be4b-4ea615a24639",
"files": [], "files": [],
"subMetas": {}, "subMetas": {},
"userData": {} "userData": {}

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/assets/scripts/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

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

View File

@ -1,4 +0,0 @@
export interface MsgChat {
content: string,
time: Date
}

View File

@ -1,7 +0,0 @@
export interface ReqSend {
content: string
}
export interface ResSend {
time: Date
}

View File

@ -1,75 +0,0 @@
import { ServiceProto } from 'tsrpc-proto';
import { MsgChat } from './MsgChat';
import { ReqSend, ResSend } from './PtlSend';
export interface ServiceType {
api: {
"Send": {
req: ReqSend,
res: ResSend
}
},
msg: {
"Chat": MsgChat
}
}
export const serviceProto: ServiceProto<ServiceType> = {
"services": [
{
"id": 0,
"name": "Chat",
"type": "msg"
},
{
"id": 1,
"name": "Send",
"type": "api"
}
],
"types": {
"MsgChat/MsgChat": {
"type": "Interface",
"properties": [
{
"id": 0,
"name": "content",
"type": {
"type": "String"
}
},
{
"id": 1,
"name": "time",
"type": {
"type": "Date"
}
}
]
},
"PtlSend/ReqSend": {
"type": "Interface",
"properties": [
{
"id": 0,
"name": "content",
"type": {
"type": "String"
}
}
]
},
"PtlSend/ResSend": {
"type": "Interface",
"properties": [
{
"id": 0,
"name": "time",
"type": {
"type": "Date"
}
}
]
}
}
};

View File

@ -4,7 +4,7 @@
"uuid": "cd1e2fff-2ab5-4407-b917-e60b25207723", "uuid": "cd1e2fff-2ab5-4407-b917-e60b25207723",
"version": "3.3.0", "version": "3.3.0",
"dependencies": { "dependencies": {
"tsrpc-browser": "^3.0.6", "tsrpc-browser": "^3.0.7",
"tsrpc-miniapp": "^3.0.6" "tsrpc-miniapp": "^3.0.6"
} }
} }

View File

@ -48,8 +48,8 @@
}, },
"86c03e47-dc7e-4335-92f0-cf39fcd2462e": { "86c03e47-dc7e-4335-92f0-cf39fcd2462e": {
"position": { "position": {
"x": 807.1672726448774, "x": 1061.8571855907594,
"y": 621.6458364416596, "y": 54.22598603125158,
"z": 5000 "z": 5000
}, },
"rotation": { "rotation": {
@ -66,6 +66,6 @@
} }
}, },
"camera-uuids": [ "camera-uuids": [
"86c03e47-dc7e-4335-92f0-cf39fcd2462e" "f46876e4-e81b-4931-b493-6d367be385e7"
] ]
} }

View File

@ -1,6 +1,6 @@
{ {
"game": { "game": {
"name": "UNKNOW GAME", "name": "未知游戏",
"app_id": "UNKNOW", "app_id": "UNKNOW",
"c_id": "0" "c_id": "0"
}, },

View File

@ -4,23 +4,23 @@
"main": "index.js", "main": "index.js",
"private": true, "private": true,
"scripts": { "scripts": {
"proto": "tsrpc proto -i src/shared/protocols -o src/shared/protocols/serviceProto.ts", "proto": "tsrpc proto --config tsrpc.config.ts",
"sync": "tsrpc sync --from src/shared --to ../frontend/src/shared", "sync": "tsrpc sync --config tsrpc.config.ts",
"api": "tsrpc api -i src/shared/protocols/serviceProto.ts -o src/api", "api": "tsrpc api --config tsrpc.config.ts",
"dev": "onchange \"src/**/*.ts\" -i -k -- ts-node \"src/index.ts\"", "dev": "tsrpc dev --config tsrpc.config.ts",
"test": "mocha test/**/*.test.ts", "test": "mocha test/**/*.test.ts",
"build": "tsrpc build" "build": "tsrpc build --config tsrpc.config.ts"
}, },
"devDependencies": { "devDependencies": {
"@types/mocha": "^8.2.2", "@types/mocha": "^8.2.3",
"@types/node": "^15.12.5", "@types/node": "^15.14.9",
"mocha": "^9.0.1", "mocha": "^9.1.2",
"onchange": "^7.1.0", "onchange": "^7.1.0",
"ts-node": "^10.0.0", "ts-node": "^10.2.1",
"tsrpc-cli": "^2.0.3", "tsrpc-cli": "^2.0.8",
"typescript": "^4.3.4" "typescript": "^4.4.3"
}, },
"dependencies": { "dependencies": {
"tsrpc": "^3.0.4" "tsrpc": "^3.0.9"
} }
} }

View File

@ -14,13 +14,14 @@ server.flows.preRecvBufferFlow.push(async v => {
let conn = v.conn as HttpConnection; let conn = v.conn as HttpConnection;
if (conn.httpReq.method === 'GET') { if (conn.httpReq.method === 'GET') {
conn.logger.log('[GET]', conn.httpReq.url);
// Static File Service // Static File Service
if (conn.httpReq.url) { if (conn.httpReq.url) {
// Check whether the file is existed // Check if the file is existed
let resFilePath = path.join('res', conn.httpReq.url) let resFilePath = path.join('res', conn.httpReq.url)
let isExisted = await fs.access(resFilePath).then(() => true).catch(() => false); let isExisted = await fs.access(resFilePath).then(() => true).catch(() => false);
if (isExisted) { if (isExisted && (await fs.stat(resFilePath)).isFile()) {
// Response the file // Send the file
let content = await fs.readFile(resFilePath); let content = await fs.readFile(resFilePath);
conn.httpRes.end(content); conn.httpRes.end(content);
return undefined; return undefined;

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

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

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

View File

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

View File

@ -1,8 +0,0 @@
export interface ReqUpload {
fileName: string,
fileData: Uint8Array
}
export interface ResUpload {
url: string;
}

View File

@ -1,59 +0,0 @@
import { ServiceProto } from 'tsrpc-proto';
import { ReqUpload, ResUpload } from './PtlUpload';
export interface ServiceType {
api: {
"Upload": {
req: ReqUpload,
res: ResUpload
}
},
msg: {
}
}
export const serviceProto: ServiceProto<ServiceType> = {
"version": 2,
"services": [
{
"id": 0,
"name": "Upload",
"type": "api"
}
],
"types": {
"PtlUpload/ReqUpload": {
"type": "Interface",
"properties": [
{
"id": 0,
"name": "fileName",
"type": {
"type": "String"
}
},
{
"id": 1,
"name": "fileData",
"type": {
"type": "Buffer",
"arrayType": "Uint8Array"
}
}
]
},
"PtlUpload/ResUpload": {
"type": "Interface",
"properties": [
{
"id": 2,
"name": "url",
"type": {
"type": "String"
}
}
]
}
}
};

View File

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

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

View File

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

View File

@ -1,8 +0,0 @@
export interface ReqHello {
name: string
}
export interface ResHello {
reply: string,
time: Date
}

View File

@ -1,57 +0,0 @@
import { ServiceProto } from 'tsrpc-proto';
import { ReqHello, ResHello } from './PtlHello';
export interface ServiceType {
api: {
"Hello": {
req: ReqHello,
res: ResHello
}
},
msg: {
}
}
export const serviceProto: ServiceProto<ServiceType> = {
"services": [
{
"id": 0,
"name": "Hello",
"type": "api"
}
],
"types": {
"PtlHello/ReqHello": {
"type": "Interface",
"properties": [
{
"id": 0,
"name": "name",
"type": {
"type": "String"
}
}
]
},
"PtlHello/ResHello": {
"type": "Interface",
"properties": [
{
"id": 0,
"name": "reply",
"type": {
"type": "String"
}
},
{
"id": 1,
"name": "time",
"type": {
"type": "Date"
}
}
]
}
}
};

View File

@ -4,20 +4,20 @@
"main": "index.js", "main": "index.js",
"private": true, "private": true,
"scripts": { "scripts": {
"proto": "tsrpc proto -i src/shared/protocols -o src/shared/protocols/serviceProto.ts", "proto": "tsrpc proto --config tsrpc.config.ts",
"sync": "tsrpc sync --from src/shared --to ../frontend/src/shared", "sync": "tsrpc sync --config tsrpc.config.ts",
"api": "tsrpc api -i src/shared/protocols/serviceProto.ts -o src/api", "api": "tsrpc api --config tsrpc.config.ts",
"dev": "onchange \"src/**/*.ts\" -i -k -- ts-node \"src/index.ts\"", "dev": "tsrpc dev --config tsrpc.config.ts",
"build": "tsrpc build" "build": "tsrpc build --config tsrpc.config.ts"
}, },
"devDependencies": { "devDependencies": {
"@types/node": "^15.14.1", "@types/node": "^15.14.9",
"onchange": "^7.1.0", "onchange": "^7.1.0",
"ts-node": "^10.0.0", "ts-node": "^10.2.1",
"tsrpc-cli": "^2.0.3", "tsrpc-cli": "^2.0.8",
"typescript": "^4.3.5" "typescript": "^4.4.3"
}, },
"dependencies": { "dependencies": {
"tsrpc": "^3.0.4" "tsrpc": "^3.0.9"
} }
} }

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: 'copy' // 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,17 +7,17 @@
"tsrpc-laya": "index.js" "tsrpc-laya": "index.js"
}, },
"dependencies": { "dependencies": {
"core-js": "^3.15.2", "core-js": "^3.18.2",
"k8w-extend-native": "^1.4.5", "k8w-extend-native": "^1.4.5",
"k8w-linq-array": "^0.2.7", "k8w-linq-array": "^0.2.7",
"k8w-super-date": "^0.1.2", "k8w-super-date": "^0.1.3",
"k8w-super-object": "^0.3.0", "k8w-super-object": "^0.3.0",
"tsbuffer": "^2.0.4", "tsbuffer": "^2.0.4",
"tsbuffer-schema": "^2.0.2", "tsbuffer-schema": "^2.0.2",
"tsbuffer-validator": "^2.0.4", "tsbuffer-validator": "^2.0.4",
"tslib": "^2.3.0", "tslib": "^2.3.1",
"tsrpc-base-client": "^1.0.6", "tsrpc-base-client": "^1.0.11",
"tsrpc-browser": "^3.0.4", "tsrpc-browser": "^3.0.7",
"tsrpc-proto": "^1.3.4" "tsrpc-proto": "^1.3.4"
}, },
"scripts": { "scripts": {

View File

@ -4,27 +4,24 @@
"main": "index.js", "main": "index.js",
"private": true, "private": true,
"scripts": { "scripts": {
"watch:proto": "onchange \"src/shared/protocols/**/Ptl*.ts\" -- npm run protoAndSync", "proto": "tsrpc proto --config tsrpc.config.ts",
"protoAndSync": "npm run proto && npm run sync", "sync": "tsrpc sync --config tsrpc.config.ts",
"proto": "tsrpc proto -i src/shared/protocols -o src/shared/protocols/serviceProto.ts", "api": "tsrpc api --config tsrpc.config.ts",
"sync": "tsrpc sync --from src/shared --to ../frontend/src/shared", "dev": "tsrpc dev --config tsrpc.config.ts",
"api": "tsrpc api -i src/shared/protocols/serviceProto.ts -o src/api", "build": "tsrpc build --config tsrpc.config.ts"
"dev": "onchange \"src/**/*.ts\" -i -k -- ts-node \"src/index.ts\"",
"test": "mocha test/**/*.test.ts",
"build": "tsrpc build"
}, },
"devDependencies": { "devDependencies": {
"@types/mocha": "^8.2.2", "@types/mocha": "^8.2.3",
"@types/mongodb": "^3.6.18", "@types/mongodb": "^3.6.20",
"@types/node": "^15.12.4", "@types/node": "^15.14.9",
"mocha": "^9.0.1", "mocha": "^9.1.2",
"onchange": "^7.1.0", "onchange": "^7.1.0",
"ts-node": "^10.0.0", "ts-node": "^10.2.1",
"tsrpc-cli": "^2.0.3", "tsrpc-cli": "^2.0.8",
"typescript": "^4.3.4" "typescript": "^4.4.3"
}, },
"dependencies": { "dependencies": {
"mongodb": "^3.6.9", "mongodb": "^3.7.2",
"tsrpc": "^3.0.2" "tsrpc": "^3.0.9"
} }
} }

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

@ -9,14 +9,14 @@
"devDependencies": { "devDependencies": {
"copy-webpack-plugin": "^9.0.1", "copy-webpack-plugin": "^9.0.1",
"html-webpack-plugin": "^5.3.2", "html-webpack-plugin": "^5.3.2",
"ts-loader": "^9.2.3", "ts-loader": "^9.2.6",
"typescript": "^4.3.4", "typescript": "^4.4.3",
"webpack": "^5.40.0", "webpack": "^5.57.1",
"webpack-cli": "^4.7.2", "webpack-cli": "^4.9.0",
"webpack-dev-server": "^3.11.2" "webpack-dev-server": "^3.11.2"
}, },
"dependencies": { "dependencies": {
"tsrpc-browser": "^3.0.3" "tsrpc-browser": "^3.0.7"
}, },
"browserslist": [ "browserslist": [
"defaults" "defaults"

View File

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

View File

@ -1,9 +0,0 @@
import { Post } from "./models/Post";
export interface ReqAddPost {
newPost: Omit<Post, '_id' | 'create' | 'update' | 'visitedNum'>;
}
export interface ResAddPost {
insertedId: string;
}

View File

@ -1,7 +0,0 @@
export interface ReqDelPost {
_id: string;
}
export interface ResDelPost {
}

View File

@ -1,9 +0,0 @@
import { Post } from "./models/Post";
export interface ReqGetPost {
_id: string;
}
export interface ResGetPost {
post: Post;
}

View File

@ -1,9 +0,0 @@
import { Post } from "./models/Post";
export interface ReqUpdatePost {
update: { _id: string } & Partial<Pick<Post, 'title' | 'content'>>;
}
export interface ResUpdatePost {
matchedCount: number;
}

View File

@ -1,17 +0,0 @@
export interface Post {
_id: string;
author: string;
title: string;
content: string;
visitedNum: number;
create: {
uid: string;
time: Date;
}
update?: {
uid: string,
time: Date
}
}

View File

@ -1,276 +0,0 @@
import { ServiceProto } from 'tsrpc-proto';
import { ReqAddPost, ResAddPost } from './PtlAddPost';
import { ReqDelPost, ResDelPost } from './PtlDelPost';
import { ReqGetPost, ResGetPost } from './PtlGetPost';
import { ReqUpdatePost, ResUpdatePost } from './PtlUpdatePost';
export interface ServiceType {
api: {
"AddPost": {
req: ReqAddPost,
res: ResAddPost
},
"DelPost": {
req: ReqDelPost,
res: ResDelPost
},
"GetPost": {
req: ReqGetPost,
res: ResGetPost
},
"UpdatePost": {
req: ReqUpdatePost,
res: ResUpdatePost
}
},
msg: {
}
}
export const serviceProto: ServiceProto<ServiceType> = {
"version": 16,
"services": [
{
"id": 0,
"name": "AddPost",
"type": "api"
},
{
"id": 1,
"name": "DelPost",
"type": "api"
},
{
"id": 2,
"name": "GetPost",
"type": "api"
},
{
"id": 3,
"name": "UpdatePost",
"type": "api"
}
],
"types": {
"PtlAddPost/ReqAddPost": {
"type": "Interface",
"properties": [
{
"id": 0,
"name": "newPost",
"type": {
"target": {
"type": "Reference",
"target": "models/Post/Post"
},
"keys": [
"_id",
"create",
"update",
"visitedNum"
],
"type": "Omit"
}
}
]
},
"models/Post/Post": {
"type": "Interface",
"properties": [
{
"id": 0,
"name": "_id",
"type": {
"type": "String"
}
},
{
"id": 1,
"name": "author",
"type": {
"type": "String"
}
},
{
"id": 2,
"name": "title",
"type": {
"type": "String"
}
},
{
"id": 3,
"name": "content",
"type": {
"type": "String"
}
},
{
"id": 4,
"name": "visitedNum",
"type": {
"type": "Number"
}
},
{
"id": 5,
"name": "create",
"type": {
"type": "Interface",
"properties": [
{
"id": 0,
"name": "uid",
"type": {
"type": "String"
}
},
{
"id": 1,
"name": "time",
"type": {
"type": "Date"
}
}
]
}
},
{
"id": 6,
"name": "update",
"type": {
"type": "Interface",
"properties": [
{
"id": 0,
"name": "uid",
"type": {
"type": "String"
}
},
{
"id": 1,
"name": "time",
"type": {
"type": "Date"
}
}
]
},
"optional": true
}
]
},
"PtlAddPost/ResAddPost": {
"type": "Interface",
"properties": [
{
"id": 0,
"name": "insertedId",
"type": {
"type": "String"
}
}
]
},
"PtlDelPost/ReqDelPost": {
"type": "Interface",
"properties": [
{
"id": 0,
"name": "_id",
"type": {
"type": "String"
}
}
]
},
"PtlDelPost/ResDelPost": {
"type": "Interface"
},
"PtlGetPost/ReqGetPost": {
"type": "Interface",
"properties": [
{
"id": 0,
"name": "_id",
"type": {
"type": "String"
}
}
]
},
"PtlGetPost/ResGetPost": {
"type": "Interface",
"properties": [
{
"id": 0,
"name": "post",
"type": {
"type": "Reference",
"target": "models/Post/Post"
}
}
]
},
"PtlUpdatePost/ReqUpdatePost": {
"type": "Interface",
"properties": [
{
"id": 0,
"name": "update",
"type": {
"type": "Intersection",
"members": [
{
"id": 1,
"type": {
"type": "Interface",
"properties": [
{
"id": 0,
"name": "_id",
"type": {
"type": "String"
}
}
]
}
},
{
"id": 3,
"type": {
"type": "Partial",
"target": {
"target": {
"type": "Reference",
"target": "models/Post/Post"
},
"keys": [
"title",
"content"
],
"type": "Pick"
}
}
}
]
}
}
]
},
"PtlUpdatePost/ResUpdatePost": {
"type": "Interface",
"properties": [
{
"id": 0,
"name": "matchedCount",
"type": {
"type": "Number"
}
}
]
}
}
};

View File

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

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

View File

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

View File

@ -1,9 +0,0 @@
import { BaseRequest, BaseResponse } from './base';
export interface ReqClear extends BaseRequest {
}
export interface ResClear extends BaseResponse {
}

View File

@ -1,9 +0,0 @@
import { BaseRequest, BaseResponse } from './base'
export interface ReqSetCookie extends BaseRequest {
}
export interface ResSetCookie extends BaseResponse {
}

View File

@ -1,9 +0,0 @@
import { BaseRequest, BaseResponse } from './base'
export interface ReqSetSession extends BaseRequest {
}
export interface ResSetSession extends BaseResponse {
}

View File

@ -1,10 +0,0 @@
import { BaseRequest, BaseResponse } from './base';
export interface ReqTest extends BaseRequest {
}
export interface ResTest extends BaseResponse {
// From Session
testSession?: string
}

View File

@ -1,12 +0,0 @@
export interface BaseRequest {
__cookie?: Cookie;
}
export interface BaseResponse {
__cookie?: Cookie;
}
export interface Cookie {
sessionId?: string,
[key: string]: any
}

View File

@ -1,209 +0,0 @@
import { ServiceProto } from 'tsrpc-proto';
import { ReqClear, ResClear } from './PtlClear';
import { ReqSetCookie, ResSetCookie } from './PtlSetCookie';
import { ReqSetSession, ResSetSession } from './PtlSetSession';
import { ReqTest, ResTest } from './PtlTest';
export interface ServiceType {
api: {
"Clear": {
req: ReqClear,
res: ResClear
},
"SetCookie": {
req: ReqSetCookie,
res: ResSetCookie
},
"SetSession": {
req: ReqSetSession,
res: ResSetSession
},
"Test": {
req: ReqTest,
res: ResTest
}
},
msg: {
}
}
export const serviceProto: ServiceProto<ServiceType> = {
"services": [
{
"id": 0,
"name": "Clear",
"type": "api"
},
{
"id": 1,
"name": "SetCookie",
"type": "api"
},
{
"id": 2,
"name": "SetSession",
"type": "api"
},
{
"id": 3,
"name": "Test",
"type": "api"
}
],
"types": {
"PtlClear/ReqClear": {
"type": "Interface",
"extends": [
{
"id": 0,
"type": {
"type": "Reference",
"target": "base/BaseRequest"
}
}
]
},
"base/BaseRequest": {
"type": "Interface",
"properties": [
{
"id": 0,
"name": "__cookie",
"type": {
"type": "Reference",
"target": "base/Cookie"
},
"optional": true
}
]
},
"base/Cookie": {
"type": "Interface",
"properties": [
{
"id": 0,
"name": "sessionId",
"type": {
"type": "String"
},
"optional": true
}
],
"indexSignature": {
"keyType": "String",
"type": {
"type": "Any"
}
}
},
"PtlClear/ResClear": {
"type": "Interface",
"extends": [
{
"id": 0,
"type": {
"type": "Reference",
"target": "base/BaseResponse"
}
}
]
},
"base/BaseResponse": {
"type": "Interface",
"properties": [
{
"id": 0,
"name": "__cookie",
"type": {
"type": "Reference",
"target": "base/Cookie"
},
"optional": true
}
]
},
"PtlSetCookie/ReqSetCookie": {
"type": "Interface",
"extends": [
{
"id": 0,
"type": {
"type": "Reference",
"target": "base/BaseRequest"
}
}
]
},
"PtlSetCookie/ResSetCookie": {
"type": "Interface",
"extends": [
{
"id": 0,
"type": {
"type": "Reference",
"target": "base/BaseResponse"
}
}
]
},
"PtlSetSession/ReqSetSession": {
"type": "Interface",
"extends": [
{
"id": 0,
"type": {
"type": "Reference",
"target": "base/BaseRequest"
}
}
]
},
"PtlSetSession/ResSetSession": {
"type": "Interface",
"extends": [
{
"id": 0,
"type": {
"type": "Reference",
"target": "base/BaseResponse"
}
}
]
},
"PtlTest/ReqTest": {
"type": "Interface",
"extends": [
{
"id": 0,
"type": {
"type": "Reference",
"target": "base/BaseRequest"
}
}
]
},
"PtlTest/ResTest": {
"type": "Interface",
"extends": [
{
"id": 0,
"type": {
"type": "Reference",
"target": "base/BaseResponse"
}
}
],
"properties": [
{
"id": 0,
"name": "testSession",
"type": {
"type": "String"
},
"optional": true
}
]
}
}
};

View File

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

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

@ -9,14 +9,14 @@
"devDependencies": { "devDependencies": {
"copy-webpack-plugin": "^9.0.1", "copy-webpack-plugin": "^9.0.1",
"html-webpack-plugin": "^5.3.2", "html-webpack-plugin": "^5.3.2",
"ts-loader": "^9.2.3", "ts-loader": "^9.2.6",
"typescript": "^4.3.4", "typescript": "^4.4.3",
"webpack": "^5.41.0", "webpack": "^5.57.1",
"webpack-cli": "^4.7.2", "webpack-cli": "^4.9.0",
"webpack-dev-server": "^3.11.2" "webpack-dev-server": "^3.11.2"
}, },
"dependencies": { "dependencies": {
"tsrpc-browser": "^3.0.4" "tsrpc-browser": "^3.0.7"
}, },
"browserslist": [ "browserslist": [
"defaults" "defaults"

View File

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

View File

@ -1,17 +0,0 @@
export class EncryptUtil {
static encrypt(buf: Uint8Array): Uint8Array {
for (let i = 0; i < buf.length; ++i) {
buf[i] -= 1;
}
return buf;
}
static decrypt(buf: Uint8Array): Uint8Array {
for (let i = 0; i < buf.length; ++i) {
buf[i] += 1;
}
return buf;
}
}

View File

@ -1,10 +0,0 @@
// This is a demo code file
// Feel free to delete it
export interface ReqAddData {
content: string;
}
export interface ResAddData {
time: Date
}

View File

@ -1,13 +0,0 @@
// This is a demo code file
// Feel free to delete it
export interface ReqGetData {
}
export interface ResGetData {
data: {
content: string,
time: Date
}[]
}

View File

@ -1,98 +0,0 @@
import { ServiceProto } from 'tsrpc-proto';
import { ReqAddData, ResAddData } from './PtlAddData';
import { ReqGetData, ResGetData } from './PtlGetData';
// This is a demo service proto file (auto generated)
// Feel free to delete it
export interface ServiceType {
api: {
"AddData": {
req: ReqAddData,
res: ResAddData
},
"GetData": {
req: ReqGetData,
res: ResGetData
}
},
msg: {
}
}
export const serviceProto: ServiceProto<ServiceType> = {
"version": 1,
"services": [
{
"id": 0,
"name": "AddData",
"type": "api"
},
{
"id": 1,
"name": "GetData",
"type": "api"
}
],
"types": {
"PtlAddData/ReqAddData": {
"type": "Interface",
"properties": [
{
"id": 0,
"name": "content",
"type": {
"type": "String"
}
}
]
},
"PtlAddData/ResAddData": {
"type": "Interface",
"properties": [
{
"id": 0,
"name": "time",
"type": {
"type": "Date"
}
}
]
},
"PtlGetData/ReqGetData": {
"type": "Interface"
},
"PtlGetData/ResGetData": {
"type": "Interface",
"properties": [
{
"id": 0,
"name": "data",
"type": {
"type": "Array",
"elementType": {
"type": "Interface",
"properties": [
{
"id": 0,
"name": "content",
"type": {
"type": "String"
}
},
{
"id": 1,
"name": "time",
"type": {
"type": "Date"
}
}
]
}
}
}
]
}
}
};

View File

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

View File

@ -34,7 +34,7 @@ export interface ServiceType {
} }
export const serviceProto: ServiceProto<ServiceType> = { export const serviceProto: ServiceProto<ServiceType> = {
"version": 2, "version": 3,
"services": [ "services": [
{ {
"id": 0, "id": 0,
@ -248,6 +248,43 @@ export const serviceProto: ServiceProto<ServiceType> = {
"type": { "type": {
"type": "String" "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" "build": "webpack --mode=production"
}, },
"devDependencies": { "devDependencies": {
"copy-webpack-plugin": "^9.0.0", "copy-webpack-plugin": "^9.0.1",
"html-webpack-plugin": "^5.3.1", "html-webpack-plugin": "^5.3.2",
"ts-loader": "^9.2.3", "ts-loader": "^9.2.6",
"typescript": "^4.3.4", "typescript": "^4.4.3",
"webpack": "^5.39.1", "webpack": "^5.57.1",
"webpack-cli": "^4.7.2", "webpack-cli": "^4.9.0",
"webpack-dev-server": "^3.11.2" "webpack-dev-server": "^3.11.2"
}, },
"dependencies": { "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 = {
};

View File

@ -1,13 +1,35 @@
const fs = require('fs'); const fs = require('fs');
const path = require('path'); const path = require('path');
const { execSync } = require('child_process'); const { execSync, exec } = require('child_process');
let dirs = fs.readdirSync(path.resolve(__dirname, '../examples')); let dirs = fs.readdirSync(path.resolve(__dirname, '../examples'));
dirs.forEach(dir => { dirs.forEach(dir => {
console.log(dir); console.log(dir);
process.chdir(path.resolve(__dirname, '../examples', dir, 'backend'));
execSync('ncu -u -t minor', { stdio: 'inherit' }); if (!isDir(path.resolve(__dirname, '../examples', dir,))) {
process.chdir(path.resolve(__dirname, '../examples', dir, 'frontend')); return;
execSync('ncu -u -t minor', { stdio: 'inherit' }); }
})
let num = 0;
if (isDir(path.resolve(__dirname, '../examples', dir, 'backend'))) {
++num;
process.chdir(path.resolve(__dirname, '../examples', dir, 'backend'));
execSync('ncu -u -t minor && npm i --registry https://registry.npm.taobao.org', { stdio: 'inherit' });
}
if (isDir(path.resolve(__dirname, '../examples', dir, 'frontend'))) {
++num;
process.chdir(path.resolve(__dirname, '../examples', dir, 'frontend'));
execSync('ncu -u -t minor && npm i --registry https://registry.npm.taobao.org', { stdio: 'inherit' });
}
if (!num) {
process.chdir(path.resolve(__dirname, '../examples', dir));
execSync('ncu -u -t minor && npm i --registry https://registry.npm.taobao.org', { stdio: 'inherit' });
}
})
function isDir(dir) {
return fs.existsSync(dir) && fs.statSync(dir).isDirectory()
}